Media.cpp 6.92 KB
Newer Older
1
/*****************``************************************************************
2
 * Media.cpp: Represents a basic container for media informations.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *****************************************************************************
 * Copyright (C) 2008-2009 the VLMC team
 *
 * 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
QPixmap*        Media::defaultSnapshot = NULL;
Ludovic Fauvet's avatar
Ludovic Fauvet committed
35
const QString   Media::VideoExtensions = "*.mov *.avi *.mkv *.mpg *.mpeg *.wmv *.mp4 *.ogg *.ogv";
36
const QString   Media::ImageExtensions = "*.gif *.png *.jpg *.jpeg";
37
const QString   Media::AudioExtensions = "*.mp3 *.oga *.flac *.aac *.wav";
38
const QString   Media::streamPrefix = "stream://";
39

40
Media::Media( const QString& filePath, const QString& uuid /*= QString()*/ )
41 42 43
    : m_vlcMedia( NULL ),
    m_snapshot( NULL ),
    m_fileInfo( NULL ),
44
    m_lengthMS( 0 ),
45
    m_nbFrames( 0 ),
46
    m_width( 0 ),
47
    m_height( 0 ),
48
    m_metadataState( None )
49
{
50 51 52 53
    if ( uuid.length() == 0 )
        m_uuid = QUuid::createUuid();
    else
        m_uuid = QUuid( uuid );
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
            m_mrl = "file:///" + QUrl::toPercentEncoding( m_fileInfo->absoluteFilePath() );
63
        else
64
            m_mrl = "fake:///" + QUrl::toPercentEncoding( m_fileInfo->absoluteFilePath() );
65
    }
66
    else
67 68 69
    {
        m_inputType = Media::Stream;
        m_mrl = filePath.right( filePath.length() - streamPrefix.length() );
70 71
        //FIXME:
        m_fileType = Media::Video;
72 73
        m_fileName = m_mrl;
        qDebug() << "Loading a stream";
74
    }
75 76

    m_audioValueList = new QList<int>();
77
    m_vlcMedia = new LibVLCpp::Media( m_mrl );
78 79
}

80
Media::~Media()
81 82 83
{
    if ( m_vlcMedia )
        delete m_vlcMedia;
84 85
    if ( m_snapshot )
        delete m_snapshot;
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
void        Media::setSnapshot( QPixmap* snapshot )
125
{
126 127
    if ( m_snapshot != NULL )
        delete m_snapshot;
128 129 130
    m_snapshot = snapshot;
}

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

140
const QUuid&        Media::getUuid() const
141 142 143
{
    return m_uuid;
}
144

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

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

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

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

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

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

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

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

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

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

195
void            Media::emitMetaDataComputed( bool hasMetadata )
196
{
197
    if ( hasMetadata == true )
198 199
    {
        m_baseClip = new Clip( this );
200
        m_metadataState = ParsedWithoutSnapshot;
201
    }
202 203
    emit metaDataComputed( this );
}
204

205 206
void            Media::emitSnapshotComputed()
{
207 208
    if ( m_metadataState == ParsedWithoutSnapshot )
        m_metadataState = ParsedWithSnapshot;
209 210 211
    emit snapshotComputed( this );
}

212 213 214
void            Media::emitAudioSpectrumComuted()
{
    m_metadataState = ParsedWithAudioSpectrum;
215
    emit audioSpectrumComputed( this->getUuid() );
216 217
}

218 219 220 221
Media::InputType    Media::getInputType() const
{
    return m_inputType;
}
222 223 224 225 226

void                Media::setUuid( const QUuid& uuid )
{
    m_uuid = uuid;
}
227

228 229 230 231 232
void                Media::setNbFrames( qint64 nbFrames )
{
    m_nbFrames = nbFrames;
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
233
qint64              Media::getNbFrames() const
234 235 236
{
    return m_nbFrames;
}
237 238 239 240 241 242 243 244 245 246

const QString&      Media::getMrl() const
{
    return m_mrl;
}

const QString&      Media::getFileName() const
{
    return m_fileName;
}
247

248
const QStringList&  Media::getMetaTags() const
249 250 251 252
{
    return m_metaTags;
}

253
void                Media::setMetaTags( const QStringList& tags )
254 255 256 257
{
    m_metaTags = tags;
}

258
bool                Media::matchMetaTag( const QString& tag ) const
259 260 261 262 263 264 265 266 267 268 269
{
    if ( tag.length() == 0 )
        return true;
    QString metaTag;
    foreach ( metaTag, m_metaTags )
    {
        if ( metaTag.startsWith( tag, Qt::CaseInsensitive ) == true )
            return true;
    }
    return false;
}
270 271 272 273 274 275 276 277 278 279

void            Media::addClip( Clip* clip )
{
    m_clips.insert( clip->getUuid(), clip );
}

void            Media::removeClip( const QUuid& uuid )
{
    m_clips.remove( uuid );
}
280 281 282 283 284

Media::MetadataState   Media::getMetadata() const
{
    return m_metadataState;
}