Media.cpp 6.17 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
#include <QtDebug>
#include <QUrl>

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

43
Media::Media( const QString& filePath )
44 45
    : m_vlcMedia( 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_baseClip( NULL ),
52
    m_nbAudioTracks( 0 ),
53 54
    m_nbVideoTracks( 0 ),
    m_metadataComputed( false )
55
{
56 57 58 59
    if ( filePath.startsWith( Media::streamPrefix ) == false )
    {
        m_inputType = Media::File;
        m_fileInfo = new QFileInfo( filePath );
60
        m_fileName = m_fileInfo->fileName();
61 62
        setFileType();
        if ( m_fileType == Media::Video || m_fileType == Media::Audio )
63 64
            m_mrl = "file:///" + QUrl::toPercentEncoding( m_fileInfo->absoluteFilePath(),
                                                          "/" );
65
        else
66 67
            m_mrl = "fake:///" + QUrl::toPercentEncoding( m_fileInfo->absoluteFilePath(),
                                                          "/" );
68
    }
69
    else
70 71 72
    {
        m_inputType = Media::Stream;
        m_mrl = filePath.right( filePath.length() - streamPrefix.length() );
73 74
        //FIXME:
        m_fileType = Media::Video;
75 76
        m_fileName = m_mrl;
        qDebug() << "Loading a stream";
77
    }
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_fileInfo )
        delete m_fileInfo;
88 89
}

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

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

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

124
const QFileInfo*    Media::fileInfo() const
125 126 127 128
{
    return m_fileInfo;
}

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

134
qint64              Media::lengthMS() const
135
{
136
    return m_lengthMS;
137 138
}

139
int                 Media::width() const
140 141 142 143
{
    return m_width;
}

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

149
int                 Media::height() const
150 151 152 153
{
    return m_height;
}

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

159
float               Media::fps() const
160 161 162 163
{
    return m_fps;
}

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

169
Media::FileType     Media::fileType() const
170 171 172 173
{
    return m_fileType;
}

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

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

185
Media::InputType    Media::inputType() const
186 187 188
{
    return m_inputType;
}
189

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

195
qint64              Media::nbFrames() const
196 197 198
{
    return m_nbFrames;
}
199

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

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

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

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

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

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

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

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

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

void
Media::save( QXmlStreamWriter& project )
{
    project.writeStartElement( "media" );
    project.writeAttribute( "mrl", m_fileInfo->absoluteFilePath() );
    project.writeEndElement();
}
259 260 261 262 263 264 265

void
Media::setBaseClip( Clip *clip )
{
    Q_ASSERT( m_baseClip == NULL );
    m_baseClip = clip;
}