Media.cpp 7.78 KB
Newer Older
1
/*****************``************************************************************
2
 * Media.cpp: Represents a basic container for media informations.
3
 *****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
4
 * Copyright (C) 2008-2010 VideoLAN
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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 <QUrl>
31
#include "Media.h"
32
#include "MetaDataManager.h"
33 34
#include "VLCMedia.h"
#include "Clip.h"
35

36
QPixmap*        Media::defaultSnapshot = NULL;
Ludovic Fauvet's avatar
Ludovic Fauvet committed
37
const QString   Media::VideoExtensions = "*.mov *.avi *.mkv *.mpg *.mpeg *.wmv *.mp4 *.ogg *.ogv";
38
const QString   Media::ImageExtensions = "*.gif *.png *.jpg *.jpeg";
39
const QString   Media::AudioExtensions = "*.mp3 *.oga *.flac *.aac *.wav";
40
const QString   Media::streamPrefix = "stream://";
41

42
Media::Media( const QString& filePath, const QString& uuid /*= QString()*/ )
43 44 45
    : m_vlcMedia( NULL ),
    m_snapshot( NULL ),
    m_fileInfo( NULL ),
46
    m_lengthMS( 0 ),
47
    m_nbFrames( 0 ),
48
    m_width( 0 ),
49
    m_height( 0 ),
50
    m_fps( .0f ),
51
    m_nbAudioTracks( 0 ),
52 53
    m_nbVideoTracks( 0 ),
    m_metadataComputed( false )
54
{
55 56 57 58
    if ( filePath.startsWith( Media::streamPrefix ) == false )
    {
        m_inputType = Media::File;
        m_fileInfo = new QFileInfo( filePath );
59
        m_fileName = m_fileInfo->fileName();
60 61
        setFileType();
        if ( m_fileType == Media::Video || m_fileType == Media::Audio )
62 63
            m_mrl = "file:///" + QUrl::toPercentEncoding( m_fileInfo->absoluteFilePath(),
                                                          "/" );
64
        else
65 66
            m_mrl = "fake:///" + QUrl::toPercentEncoding( m_fileInfo->absoluteFilePath(),
                                                          "/" );
67
    }
68
    else
69 70 71
    {
        m_inputType = Media::Stream;
        m_mrl = filePath.right( filePath.length() - streamPrefix.length() );
72 73
        //FIXME:
        m_fileType = Media::Video;
74 75
        m_fileName = m_mrl;
        qDebug() << "Loading a stream";
76
    }
77
    m_baseClip = new Clip( this, uuid );
78
    m_audioValueList = new QList<int>();
79
    m_vlcMedia = new LibVLCpp::Media( m_mrl );
80 81
}

82
Media::~Media()
83 84 85
{
    if ( m_vlcMedia )
        delete m_vlcMedia;
86 87
    if ( m_snapshot )
        delete m_snapshot;
88 89
    if ( m_fileInfo )
        delete m_fileInfo;
90 91
}

92 93
void        Media::setFileType()
{
94
    QString filter = "*." + m_fileInfo->suffix().toLower();
95 96 97 98 99 100 101 102 103 104
    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 ?!";
}

105
void        Media::flushVolatileParameters()
106
{
107 108 109 110 111 112 113 114 115 116 117 118
    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 );
119 120
}

121
void        Media::addConstantParam( const QString& param )
122
{
123
    m_vlcMedia->addOption( param.toStdString().c_str() );
124 125
}

126
void        Media::setSnapshot( QPixmap* snapshot )
127
{
128 129
    if ( m_snapshot != NULL )
        delete m_snapshot;
130 131 132
    m_snapshot = snapshot;
}

133
const QPixmap&    Media::snapshot() const
134
{
135
    if ( m_snapshot != NULL )
136
        return *m_snapshot;
137 138 139
    if ( Media::defaultSnapshot == NULL )
        Media::defaultSnapshot = new QPixmap( ":/images/images/vlmc.png" );
    return *Media::defaultSnapshot;
140
}
141

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

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

152
qint64              Media::lengthMS() const
153
{
154
    return m_lengthMS;
155 156
}

157
int                 Media::width() const
158 159 160 161
{
    return m_width;
}

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

167
int                 Media::height() const
168 169 170 171
{
    return m_height;
}

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

177
float               Media::fps() const
178 179 180 181
{
    return m_fps;
}

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

187
Media::FileType     Media::fileType() const
188 189 190 191
{
    return m_fileType;
}

192
void            Media::emitMetaDataComputed()
193
{
194 195 196
    m_baseClip->setBegin( 0 );
    m_baseClip->setEnd( m_nbFrames );
    m_baseClip->computeLength();
197
    m_metadataComputed = true;
198 199
    emit metaDataComputed( this );
}
200

201 202 203 204 205
void            Media::emitSnapshotComputed()
{
    emit snapshotComputed( this );
}

206 207
void            Media::emitAudioSpectrumComuted()
{
208
    emit audioSpectrumComputed( baseClip()->uuid() );
209 210
}

211
Media::InputType    Media::inputType() const
212 213 214
{
    return m_inputType;
}
215

216 217 218 219 220
void                Media::setNbFrames( qint64 nbFrames )
{
    m_nbFrames = nbFrames;
}

221
qint64              Media::nbFrames() const
222 223 224
{
    return m_nbFrames;
}
225

226
const QString&      Media::mrl() const
227 228 229 230
{
    return m_mrl;
}

231
const QString&      Media::fileName() const
232 233 234
{
    return m_fileName;
}
235

236
const QStringList&  Media::metaTags() const
237 238 239 240
{
    return m_metaTags;
}

241
void                Media::setMetaTags( const QStringList& tags )
242 243 244 245
{
    m_metaTags = tags;
}

246
bool                Media::matchMetaTag( const QString& tag ) const
247 248 249 250 251 252 253 254 255 256 257
{
    if ( tag.length() == 0 )
        return true;
    QString metaTag;
    foreach ( metaTag, m_metaTags )
    {
        if ( metaTag.startsWith( tag, Qt::CaseInsensitive ) == true )
            return true;
    }
    return false;
}
258

259
bool            Media::addClip( Clip* clip )
260
{
261
    if ( clip->getParent() != this )
262
        return false;
263 264 265
    foreach ( Clip *c, m_clips.values() )
    {
        if ( clip->begin() == c->begin() && clip->end() == clip->end() )
266
            return false;
267
    }
268
    m_clips.insert( clip->uuid(), clip );
269 270
    emit clipAdded( clip );
    return true;
271 272
}

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
Clip*
Media::clip( const QUuid &uuid )
{
    QHash<QUuid, Clip*>::const_iterator it = m_clips.find( uuid );
    if ( it == m_clips.end() )
        return NULL;
    return *it;
}

quint32
Media::clipsCount() const
{
    return m_clips.size();
}

const QHash<QUuid, Clip*>&
Media::clips() const
{
    return m_clips;
}

294 295
Clip*
Media::removeClip( const QUuid& uuid )
296
{
297 298 299
    Clip*   clip = m_clips.take( uuid );
    emit clipRemoved( clip );
    return clip;
300
}
301 302

bool
303
Media::hasAudioTrack() const
304
{
305
    return ( m_nbAudioTracks > 0 );
306 307 308 309 310
}

bool
Media::hasVideoTrack() const
{
311
    return ( m_nbVideoTracks > 0 );
312 313 314
}

void
315
Media::setNbAudioTrack( int nbTrack )
316
{
317 318 319 320 321 322 323 324 325 326
    m_nbAudioTracks = nbTrack;
}

void
Media::setNbVideoTrack( int nbTracks )
{
    m_nbVideoTracks = nbTracks;
}

int
327
Media::nbAudioTracks() const
328 329 330 331 332
{
    return m_nbAudioTracks;
}

int
333
Media::nbVideoTracks() const
334 335
{
    return m_nbVideoTracks;
336
}
337 338 339 340 341 342

bool
Media::isMetadataComputed() const
{
    return m_metadataComputed;
}