Media.cpp 7.18 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
 * Authors: Hugo Beauzée-Luyssen <beauze.h@gmail.com>
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * 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 = "*.avi *.3gp *.amv *.asf *.divx *.dv *.flv *.gxf "
40
41
42
43
44
                                         "*.iso *.m1v *.m2v *.m2t *.m2ts *.m4v *.mkv *.mov "
                                         "*.mp2 *.mp4 *.mpeg *.mpeg1 *.mpeg2 *.mpeg4 *.mpg "
                                         "*.mts *.mxf *.nsv *.nuv *.ogg *.ogm *.ogv *.ogx *.ps "
                                         "*.rec *.rm *.rmvb *.tod *.ts *.vob *.vro *.webm *.wmv";

45
const QString   Media::ImageExtensions = "*.gif *.png *.jpg *.jpeg";
46
47
48
49
50
51

const QString   Media::AudioExtensions = "*.a52 *.aac *.ac3 *.aiff *.amr *.aob *.ape "
                                         "*.dts *.flac *.it *.m4a *.m4p *.mid *.mka *.mlp "
                                         "*.mod *.mp1 *.mp2 *.mp3 *.mpc *.oga *.ogg *.oma "
                                         "*.rmi *.s3m *.spx *.tta *.voc *.vqf *.w64 *.wav "
                                         "*.wma *.wv *.xa *.xm";
52
const QString   Media::streamPrefix = "stream://";
53

54
Media::Media( const QString& filePath )
55
56
    : m_vlcMedia( NULL ),
    m_fileInfo( NULL ),
57
    m_lengthMS( 0 ),
58
    m_nbFrames( 0 ),
59
    m_width( 0 ),
60
    m_height( 0 ),
61
    m_fps( .0f ),
62
    m_baseClip( NULL ),
63
    m_nbAudioTracks( 0 ),
64
    m_nbVideoTracks( 0 ),
65
66
    m_metadataComputed( false ),
    m_inWorkspace( false )
67
{
68
69
    if ( filePath.startsWith( Media::streamPrefix ) == false )
    {
70
        setFilePath( filePath );
71
    }
72
    else
73
74
75
    {
        m_inputType = Media::Stream;
        m_mrl = filePath.right( filePath.length() - streamPrefix.length() );
76
77
        //FIXME:
        m_fileType = Media::Video;
78
        m_fileName = m_mrl;
79
        m_vlcMedia = new LibVLCpp::Media( m_mrl );
80
    }
81
82
}

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

91
92
void
Media::setFileType()
93
{
94
    const 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
106
void
Media::flushVolatileParameters()
107
{
108
    foreach ( const QString &defaultValue, m_volatileParameters )
109
        m_vlcMedia->addOption( defaultValue.toUtf8().constData() );
110
111
112
    m_volatileParameters.clear();
}

113
114
void
Media::addVolatileParam( const QString& param, const QString& defaultValue )
115
{
116
    m_vlcMedia->addOption( param.toUtf8().constData() );
117
    m_volatileParameters.append( defaultValue );
118
119
}

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

126
127
const QFileInfo*
Media::fileInfo() const
128
129
130
131
{
    return m_fileInfo;
}

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

138
139
qint64
Media::lengthMS() const
140
{
141
    return m_lengthMS;
142
143
}

144
145
int
Media::width() const
146
147
148
149
{
    return m_width;
}

150
151
void
Media::setWidth( int width )
152
153
154
155
{
    m_width = width;
}

156
157
int
Media::height() const
158
159
160
161
{
    return m_height;
}

162
163
void
Media::setHeight( int height )
164
165
166
167
{
    m_height = height;
}

168
169
float
Media::fps() const
170
171
172
173
{
    return m_fps;
}

174
175
void
Media::setFps( float fps )
176
177
178
179
{
    m_fps = fps;
}

180
181
Media::FileType
Media::fileType() const
182
183
184
185
{
    return m_fileType;
}

186
187
void
Media::emitMetaDataComputed()
188
{
189
    m_metadataComputed = true;
190
191
    emit metaDataComputed( this );
}
192

193
194
Media::InputType
Media::inputType() const
195
196
197
{
    return m_inputType;
}
198

199
200
void
Media::setNbFrames( qint64 nbFrames )
201
202
203
204
{
    m_nbFrames = nbFrames;
}

205
206
qint64
Media::nbFrames() const
207
208
209
{
    return m_nbFrames;
}
210

211
212
const QString&
Media::mrl() const
213
214
215
216
{
    return m_mrl;
}

217
218
const QString&
Media::fileName() const
219
220
221
{
    return m_fileName;
}
222

223
bool
224
Media::hasAudioTrack() const
225
{
226
    return ( m_nbAudioTracks > 0 );
227
228
229
230
231
}

bool
Media::hasVideoTrack() const
{
232
    return ( m_nbVideoTracks > 0 );
233
234
235
}

void
236
Media::setNbAudioTrack( int nbTrack )
237
{
238
239
240
241
242
243
244
245
246
247
    m_nbAudioTracks = nbTrack;
}

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

int
248
Media::nbAudioTracks() const
249
250
251
252
253
{
    return m_nbAudioTracks;
}

int
254
Media::nbVideoTracks() const
255
256
{
    return m_nbVideoTracks;
257
}
258
259
260
261
262
263

bool
Media::isMetadataComputed() const
{
    return m_metadataComputed;
}
264
265
266
267
268

void
Media::save( QXmlStreamWriter& project )
{
    project.writeStartElement( "media" );
269
270
271
272
    if ( m_inWorkspace == true )
        project.writeAttribute( "mrl", Workspace::workspacePrefix + m_workspacePath );
    else
        project.writeAttribute( "mrl", m_fileInfo->absoluteFilePath() );
273
274
    project.writeEndElement();
}
275
276
277
278
279
280
281

void
Media::setBaseClip( Clip *clip )
{
    Q_ASSERT( m_baseClip == NULL );
    m_baseClip = clip;
}
282
283
284
285
286
287
288
289
290
291

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

void
Media::setFilePath( const QString &filePath )
{
292
    m_inputType = Media::File;
293
294
    if ( m_fileInfo )
        delete m_fileInfo;
295
296
    m_fileInfo = new QFileInfo( filePath );
    m_fileName = m_fileInfo->fileName();
297
    setFileType();
298
    if ( m_fileType != Media::Image )
299
        m_mrl = "file:///" + QUrl::toPercentEncoding( filePath, "/" );
300
301
    else
        m_mrl = "fake:///" + filePath; //It seems the url encoding part is handled internally by VLC.
302
303
    if ( m_vlcMedia )
        delete m_vlcMedia;
304
    m_vlcMedia = new LibVLCpp::Media( m_mrl );
305
306
307
    //Don't call this before setting all the internals, as it relies on Media::fileInfo.
    if ( Workspace::isInProjectDir( this ) == true )
    {
308
        m_inWorkspace = true;
309
310
        m_workspacePath = Workspace::pathInProjectDir( this );
    }
311
312
313
314
315
    else
    {
        m_inWorkspace = false;
        m_workspacePath = "";
    }
316
    emit workspaceStateChanged( m_inWorkspace );
317
}
318
319
320
321
322
323

void
Media::computeMetadata()
{
    MetaDataManager::getInstance()->computeMediaMetadata( this );
}