Media.cpp 6.82 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
  * It contains the Media meta-datas, and information to be used with a VLC Media.
26 27 28
  * It's used by the Library
  */

29
#include "Media.h"
30
#include "MetaDataManager.h"
31 32
#include "VLCMedia.h"
#include "Clip.h"
33

34 35 36 37
#include <QXmlStreamWriter>
#include <QtDebug>
#include <QUrl>

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

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

85
Media::~Media()
86 87 88
{
    if ( m_vlcMedia )
        delete m_vlcMedia;
89 90
    if ( m_snapshot )
        delete m_snapshot;
91 92
    if ( m_fileInfo )
        delete m_fileInfo;
93 94
}

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

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

124
void        Media::addConstantParam( const QString& param )
125
{
126
    m_vlcMedia->addOption( param.toStdString().c_str() );
127 128
}

129
void        Media::setSnapshot( QPixmap* snapshot )
130
{
131 132
    if ( m_snapshot != NULL )
        delete m_snapshot;
133 134 135
    m_snapshot = snapshot;
}

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

145
const QFileInfo*    Media::fileInfo() const
146 147 148 149
{
    return m_fileInfo;
}

150
void                Media::setLength( qint64 length )
151
{
152
    m_lengthMS = length;
153 154
}

155
qint64              Media::lengthMS() const
156
{
157
    return m_lengthMS;
158 159
}

160
int                 Media::width() const
161 162 163 164
{
    return m_width;
}

165
void                Media::setWidth( int width )
166 167 168 169
{
    m_width = width;
}

170
int                 Media::height() const
171 172 173 174
{
    return m_height;
}

175
void                Media::setHeight( int height )
176 177 178 179
{
    m_height = height;
}

180
float               Media::fps() const
181 182 183 184
{
    return m_fps;
}

185
void                Media::setFps( float fps )
186 187 188 189
{
    m_fps = fps;
}

190
Media::FileType     Media::fileType() const
191 192 193 194
{
    return m_fileType;
}

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

204 205 206 207 208
void            Media::emitSnapshotComputed()
{
    emit snapshotComputed( this );
}

209 210
void            Media::emitAudioSpectrumComuted()
{
211
    emit audioSpectrumComputed( baseClip()->uuid() );
212 213
}

214
Media::InputType    Media::inputType() const
215 216 217
{
    return m_inputType;
}
218

219 220 221 222 223
void                Media::setNbFrames( qint64 nbFrames )
{
    m_nbFrames = nbFrames;
}

224
qint64              Media::nbFrames() const
225 226 227
{
    return m_nbFrames;
}
228

229
const QString&      Media::mrl() const
230 231 232 233
{
    return m_mrl;
}

234
const QString&      Media::fileName() const
235 236 237
{
    return m_fileName;
}
238

239
bool
240
Media::hasAudioTrack() const
241
{
242
    return ( m_nbAudioTracks > 0 );
243 244 245 246 247
}

bool
Media::hasVideoTrack() const
{
248
    return ( m_nbVideoTracks > 0 );
249 250 251
}

void
252
Media::setNbAudioTrack( int nbTrack )
253
{
254 255 256 257 258 259 260 261 262 263
    m_nbAudioTracks = nbTrack;
}

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

int
264
Media::nbAudioTracks() const
265 266 267 268 269
{
    return m_nbAudioTracks;
}

int
270
Media::nbVideoTracks() const
271 272
{
    return m_nbVideoTracks;
273
}
274 275 276 277 278 279

bool
Media::isMetadataComputed() const
{
    return m_metadataComputed;
}
280 281 282 283 284 285 286 287

void
Media::save( QXmlStreamWriter& project )
{
    project.writeStartElement( "media" );
    project.writeAttribute( "mrl", m_fileInfo->absoluteFilePath() );
    project.writeEndElement();
}