Media.cpp 7.07 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 31

#include "Clip.h"
32
#include "MetaDataManager.h"
33
#include "VLCMedia.h"
34
#include "Workspace.h"
35

36 37 38
#include <QtDebug>
#include <QUrl>

39
const QString   Media::VideoExtensions = "*.mov *.avi *.mkv *.mpg *.mpeg *.wmv *.mp4 "
40
                                         "*.ogg *.ogv *.dv *.ts";
41
const QString   Media::ImageExtensions = "*.gif *.png *.jpg *.jpeg";
42
const QString   Media::AudioExtensions = "*.mp3 *.oga *.flac *.aac *.wav";
43
const QString   Media::streamPrefix = "stream://";
44

45
Media::Media( const QString& filePath )
46 47
    : m_vlcMedia( 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
    }
80
    m_audioValueList = new QList<int>();
81
    m_vlcMedia = new LibVLCpp::Media( m_mrl );
82 83
}

84
Media::~Media()
85 86 87
{
    if ( m_vlcMedia )
        delete m_vlcMedia;
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
    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
Nikoli's avatar
Nikoli committed
102
        qDebug() << "What the hell is this extension? And how did you loaded it?!";
103 104
}

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
const QFileInfo*    Media::fileInfo() const
127 128 129 130
{
    return m_fileInfo;
}

131
void                Media::setLength( qint64 length )
132
{
133
    m_lengthMS = length;
134 135
}

136
qint64              Media::lengthMS() const
137
{
138
    return m_lengthMS;
139 140
}

141
int                 Media::width() const
142 143 144 145
{
    return m_width;
}

146
void                Media::setWidth( int width )
147 148 149 150
{
    m_width = width;
}

151
int                 Media::height() const
152 153 154 155
{
    return m_height;
}

156
void                Media::setHeight( int height )
157 158 159 160
{
    m_height = height;
}

161
float               Media::fps() const
162 163 164 165
{
    return m_fps;
}

166
void                Media::setFps( float fps )
167 168 169 170
{
    m_fps = fps;
}

171
Media::FileType     Media::fileType() const
172 173 174 175
{
    return m_fileType;
}

176
void            Media::emitMetaDataComputed()
177
{
178
    m_metadataComputed = true;
179 180
    emit metaDataComputed( this );
}
181

182 183
void            Media::emitAudioSpectrumComuted()
{
184
    emit audioSpectrumComputed( baseClip()->uuid() );
185 186
}

187
Media::InputType    Media::inputType() const
188 189 190
{
    return m_inputType;
}
191

192 193 194 195 196
void                Media::setNbFrames( qint64 nbFrames )
{
    m_nbFrames = nbFrames;
}

197
qint64              Media::nbFrames() const
198 199 200
{
    return m_nbFrames;
}
201

202
const QString&      Media::mrl() const
203 204 205 206
{
    return m_mrl;
}

207
const QString&      Media::fileName() const
208 209 210
{
    return m_fileName;
}
211

212
bool
213
Media::hasAudioTrack() const
214
{
215
    return ( m_nbAudioTracks > 0 );
216 217 218 219 220
}

bool
Media::hasVideoTrack() const
{
221
    return ( m_nbVideoTracks > 0 );
222 223 224
}

void
225
Media::setNbAudioTrack( int nbTrack )
226
{
227 228 229 230 231 232 233 234 235 236
    m_nbAudioTracks = nbTrack;
}

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

int
237
Media::nbAudioTracks() const
238 239 240 241 242
{
    return m_nbAudioTracks;
}

int
243
Media::nbVideoTracks() const
244 245
{
    return m_nbVideoTracks;
246
}
247 248 249 250 251 252

bool
Media::isMetadataComputed() const
{
    return m_metadataComputed;
}
253 254 255 256 257

void
Media::save( QXmlStreamWriter& project )
{
    project.writeStartElement( "media" );
258 259 260 261
    if ( m_inWorkspace == true )
        project.writeAttribute( "mrl", Workspace::workspacePrefix + m_workspacePath );
    else
        project.writeAttribute( "mrl", m_fileInfo->absoluteFilePath() );
262 263
    project.writeEndElement();
}
264 265 266 267 268 269 270

void
Media::setBaseClip( Clip *clip )
{
    Q_ASSERT( m_baseClip == NULL );
    m_baseClip = clip;
}
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287

bool
Media::isInWorkspace() const
{
    return m_inWorkspace;
}

void
Media::setFilePath( const QString &filePath )
{
    delete m_fileInfo;
    m_fileInfo = new QFileInfo( filePath );
    m_fileName = m_fileInfo->fileName();
    if ( m_fileType == Media::Video || m_fileType == Media::Audio )
        m_mrl = "file:///" + QUrl::toPercentEncoding( filePath, "/" );
    else
        m_mrl = "fake:///" + QUrl::toPercentEncoding( filePath, "/" );
288 289
    delete m_vlcMedia;
    m_vlcMedia = new LibVLCpp::Media( m_mrl );
290 291 292
    //Don't call this before setting all the internals, as it relies on Media::fileInfo.
    if ( Workspace::isInProjectDir( this ) == true )
    {
293
        m_inWorkspace = true;
294 295
        m_workspacePath = Workspace::pathInProjectDir( this );
    }
296
}