Media.cpp 5.93 KB
Newer Older
1
/*****************``************************************************************
2
 * Media.cpp: Represents a basic container for media informations.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *****************************************************************************
 * Copyright (C) 2008-2009 the VLMC team
 *
 * Authors: Hugo Beauzee-Luyssen <hugo@vlmc.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

23
/** \file
24
  * This file contains the Media class implementation.
25 26 27 28
  * It contains a VLCMedia and the meta-datas.
  * It's used by the Library
  */

29
#include <QtDebug>
30
#include "Media.h"
31

32 33 34 35
QPixmap*        Media::defaultSnapshot = NULL;
const QString   Media::VideoExtensions = "*.mov *.avi *.mkv *.mpg *.mpeg *.wmv *.mp4";
const QString   Media::ImageExtensions = "*.gif *.png *.jpg";
const QString   Media::AudioExtensions = "*.mp3 *.oga *.flac *.aac *.wav";
36
const QString   Media::streamPrefix = "stream://";
37

38
Media::Media( const QString& filePath )
39 40 41 42 43 44
    : m_vlcMedia( NULL ),
    m_snapshot( NULL ),
    m_fileInfo( NULL ),
    m_length( 0 ),
    m_width( 0 ),
    m_height( 0 )
45 46
{
    m_uuid = QUuid::createUuid();
47 48 49 50 51 52 53 54 55 56 57

    if ( filePath.startsWith( Media::streamPrefix ) == false )
    {
        m_inputType = Media::File;
        m_fileInfo = new QFileInfo( filePath );
        setFileType();
        if ( m_fileType == Media::Video || m_fileType == Media::Audio )
            m_mrl = "file:///" + m_fileInfo->absoluteFilePath();
        else
            m_mrl = "fake:///" + m_fileInfo->absoluteFilePath();
    }
58
    else
59 60 61
    {
        m_inputType = Media::Stream;
        m_mrl = filePath.right( filePath.length() - streamPrefix.length() );
62 63
        //FIXME:
        m_fileType = Media::Video;
64
    }
65
    m_vlcMedia = new LibVLCpp::Media( m_mrl );
66 67
}

68
Media::~Media()
69 70 71
{
    if ( m_vlcMedia )
        delete m_vlcMedia;
72 73
    if ( m_snapshot )
        delete m_snapshot;
74 75
    if ( m_fileInfo )
        delete m_fileInfo;
76 77
}

78 79 80 81 82 83 84 85 86 87 88 89 90
void        Media::setFileType()
{
    QString filter = "*." + m_fileInfo->suffix();
    if ( Media::VideoExtensions.contains( filter ) )
        m_fileType = Media::Video;
    else if ( Media::AudioExtensions.contains( filter ) )
        m_fileType = Media::Audio;
    else if ( Media::ImageExtensions.contains( filter ) )
        m_fileType = Media::Image;
    else
        qDebug() << "What the hell is this extension ? And how did you loaded it ?!";
}

91
void        Media::flushVolatileParameters()
92
{
93 94 95 96 97 98 99 100 101 102 103 104
    QString     defaultValue;
    foreach ( defaultValue, m_volatileParameters )
    {
        m_vlcMedia->addOption( defaultValue.toStdString().c_str() );
    }
    m_volatileParameters.clear();
}

void        Media::addVolatileParam( const QString& param, const QString& defaultValue )
{
    m_vlcMedia->addOption( param.toStdString().c_str() );
    m_volatileParameters.append( defaultValue );
105 106
}

107
void        Media::addConstantParam( const QString& param )
108
{
109
    m_vlcMedia->addOption( param.toStdString().c_str() );
110 111
}

112
void        Media::setSnapshot( QPixmap* snapshot )
113
{
114 115
    if ( m_snapshot != NULL )
        delete m_snapshot;
116 117 118
    m_snapshot = snapshot;
}

119
const QPixmap&    Media::getSnapshot() const
120
{
121
    if ( m_snapshot != NULL )
122
        return *m_snapshot;
123 124 125
    if ( Media::defaultSnapshot == NULL )
        Media::defaultSnapshot = new QPixmap( ":/images/images/vlmc.png" );
    return *Media::defaultSnapshot;
126
}
127

128
const QUuid&        Media::getUuid() const
129 130 131
{
    return m_uuid;
}
132

133
const QFileInfo*    Media::getFileInfo() const
134 135 136 137
{
    return m_fileInfo;
}

138
void                Media::setLength( qint64 length )
139 140 141 142
{
    m_length = length;
}

143
qint64              Media::getLength() const
144 145 146 147
{
    return m_length;
}

148
int                 Media::getWidth() const
149 150 151 152
{
    return m_width;
}

153
void                Media::setWidth( int width )
154 155 156 157
{
    m_width = width;
}

158
int                 Media::getHeight() const
159 160 161 162
{
    return m_height;
}

163
void                Media::setHeight( int height )
164 165 166 167
{
    m_height = height;
}

168 169 170 171 172 173 174 175 176 177
unsigned int        Media::getFps() const
{
    return m_fps;
}

void                Media::setFps( unsigned int fps )
{
    m_fps = fps;
}

178 179 180 181 182
Media::FileType     Media::getFileType() const
{
    return m_fileType;
}

183 184 185 186 187 188 189 190 191 192 193
void                Media::initAudioData( void* datas, unsigned int* freq, unsigned int* nbChannels, unsigned int* fourCCFormat, unsigned int* frameSize )
{
    m_audioData.freq = freq;
    m_audioData.nbChannels = nbChannels;
    m_audioData.frameSize = frameSize;
    m_audioData.fourCCFormat = fourCCFormat;
    m_audioData.datas = datas;
}

void                Media::addAudioFrame( void* datas, unsigned char* buffer, size_t buffSize, unsigned int nbSample )
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
194
    Q_UNUSED( datas );
195 196 197 198
    m_audioData.nbSample = nbSample;
    m_audioData.buffSize = buffSize;

    int* frame = new int[ m_audioData.buffSize ];
chouquette's avatar
chouquette committed
199
    for (unsigned int i = 0, u = 0; u < m_audioData.nbSample; i += 4, u++)
200 201
    {
        int value = buffer[i];
202
        value <<= 8;
203
        value += buffer[i + 1];
204
        value <<= 8;
205
        value += buffer[i + 2];
206
        value <<= 8;
207 208 209 210 211 212
        value += buffer[i + 3];
        frame[u] = value;
    }
    m_audioData.frameList.append( frame );
//    qDebug() << m_audioData.frameList.size();
}
213

214
void            Media::emitMetaDataComputed()
215 216 217 218
{
    emit metaDataComputed();
    emit metaDataComputed( this );
}
219 220 221 222 223

Media::InputType    Media::getInputType() const
{
    return m_inputType;
}