Media.cpp 7.28 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, const QString& uuid )
39 40 41
    : m_vlcMedia( NULL ),
    m_snapshot( NULL ),
    m_fileInfo( NULL ),
42
    m_lengthMS( 0 ),
43
    m_nbFrames( 0 ),
44
    m_width( 0 ),
45 46
    m_height( 0 ),
    m_metadataParsed( false )
47
{
48 49 50 51
    if ( uuid.length() == 0 )
        m_uuid = QUuid::createUuid();
    else
        m_uuid = QUuid( uuid );
52 53 54 55 56

    if ( filePath.startsWith( Media::streamPrefix ) == false )
    {
        m_inputType = Media::File;
        m_fileInfo = new QFileInfo( filePath );
57
        m_fileName = m_fileInfo->fileName();
58 59 60 61 62 63
        setFileType();
        if ( m_fileType == Media::Video || m_fileType == Media::Audio )
            m_mrl = "file:///" + m_fileInfo->absoluteFilePath();
        else
            m_mrl = "fake:///" + m_fileInfo->absoluteFilePath();
    }
64
    else
65 66 67
    {
        m_inputType = Media::Stream;
        m_mrl = filePath.right( filePath.length() - streamPrefix.length() );
68 69
        //FIXME:
        m_fileType = Media::Video;
70 71
        m_fileName = m_mrl;
        qDebug() << "Loading a stream";
72
    }
73
    m_vlcMedia = new LibVLCpp::Media( m_mrl );
74 75
}

76
Media::~Media()
77 78 79
{
    if ( m_vlcMedia )
        delete m_vlcMedia;
80 81
    if ( m_snapshot )
        delete m_snapshot;
82 83
    if ( m_fileInfo )
        delete m_fileInfo;
84 85
}

86 87 88 89 90 91 92 93 94 95 96 97 98
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 ?!";
}

99
void        Media::flushVolatileParameters()
100
{
101 102 103 104 105 106 107 108 109 110 111 112
    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 );
113 114
}

115
void        Media::addConstantParam( const QString& param )
116
{
117
    m_vlcMedia->addOption( param.toStdString().c_str() );
118 119
}

120
void        Media::setSnapshot( QPixmap* snapshot )
121
{
122 123
    if ( m_snapshot != NULL )
        delete m_snapshot;
124 125 126
    m_snapshot = snapshot;
}

127
const QPixmap&    Media::getSnapshot() const
128
{
129
    if ( m_snapshot != NULL )
130
        return *m_snapshot;
131 132 133
    if ( Media::defaultSnapshot == NULL )
        Media::defaultSnapshot = new QPixmap( ":/images/images/vlmc.png" );
    return *Media::defaultSnapshot;
134
}
135

136
const QUuid&        Media::getUuid() const
137 138 139
{
    return m_uuid;
}
140

141
const QFileInfo*    Media::getFileInfo() const
142 143 144 145
{
    return m_fileInfo;
}

146
void                Media::setLength( qint64 length )
147
{
148
    m_lengthMS = length;
149 150
}

151
qint64              Media::getLengthMS() const
152
{
153
    return m_lengthMS;
154 155
}

156
int                 Media::getWidth() const
157 158 159 160
{
    return m_width;
}

161
void                Media::setWidth( int width )
162 163 164 165
{
    m_width = width;
}

166
int                 Media::getHeight() const
167 168 169 170
{
    return m_height;
}

171
void                Media::setHeight( int height )
172 173 174 175
{
    m_height = height;
}

176
float               Media::getFps() const
177 178 179 180
{
    return m_fps;
}

181
void                Media::setFps( float fps )
182 183 184 185
{
    m_fps = fps;
}

186 187 188 189 190
Media::FileType     Media::getFileType() const
{
    return m_fileType;
}

191 192
void                Media::initAudioData( void* datas, unsigned int* freq, unsigned int* nbChannels, unsigned int* fourCCFormat, unsigned int* frameSize )
{
193 194 195 196
    m_audioData.freq = freq;
    m_audioData.nbChannels = nbChannels;
    m_audioData.frameSize = frameSize;
    m_audioData.fourCCFormat = fourCCFormat;
197 198 199 200 201
    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
202
    Q_UNUSED( datas );
203 204 205
    m_audioData.nbSample = nbSample;
    m_audioData.buffSize = buffSize;

206 207
    int* frame = new int[ m_audioData.buffSize ];
    for (unsigned int i = 0, u = 0; u < m_audioData.nbSample; i += 4, u++)
208
    {
209 210 211 212 213 214 215 216
        int value = buffer[i];
        value <<= 8;
        value += buffer[i + 1];
        value <<= 8;
        value += buffer[i + 2];
        value <<= 8;
        value += buffer[i + 3];
        frame[u] = value;
217
    }
218 219
    m_audioData.frameList.append( frame );
//    qDebug() << m_audioData.frameList.size();
220
}
221

222
void            Media::emitMetaDataComputed()
223
{
224
    m_metadataParsed = true;
225 226
    emit metaDataComputed( this );
}
227 228 229 230 231

Media::InputType    Media::getInputType() const
{
    return m_inputType;
}
232 233 234 235 236

void                Media::setUuid( const QUuid& uuid )
{
    m_uuid = uuid;
}
237 238 239 240 241

bool                Media::hasMetadata() const
{
    return m_metadataParsed;
}
242

243 244 245 246 247
void                Media::setNbFrames( qint64 nbFrames )
{
    m_nbFrames = nbFrames;
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
248
qint64              Media::getNbFrames() const
249 250 251
{
    return m_nbFrames;
}
252 253 254 255 256 257 258 259 260 261

const QString&      Media::getMrl() const
{
    return m_mrl;
}

const QString&      Media::getFileName() const
{
    return m_fileName;
}
262

263
const QStringList&  Media::getMetaTags() const
264 265 266 267
{
    return m_metaTags;
}

268
void                Media::setMetaTags( const QStringList& tags )
269 270 271 272
{
    m_metaTags = tags;
}

273
bool                Media::matchMetaTag( const QString& tag ) const
274 275 276 277 278 279 280 281 282 283 284
{
    if ( tag.length() == 0 )
        return true;
    QString metaTag;
    foreach ( metaTag, m_metaTags )
    {
        if ( metaTag.startsWith( tag, Qt::CaseInsensitive ) == true )
            return true;
    }
    return false;
}
285 286 287 288 289 290 291 292 293 294

void            Media::addClip( Clip* clip )
{
    m_clips.insert( clip->getUuid(), clip );
}

void            Media::removeClip( const QUuid& uuid )
{
    m_clips.remove( uuid );
}