Media.cpp 5.99 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 <hugo@beauzee.fr>
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 "VlmcDebug.h"
34
#include "Workspace.h"
35 36
#include "ISource.h"
#include "IBackend.h"
37

38 39
#include <QUrl>

40
const QString   Media::VideoExtensions = "*.avi *.3gp *.amv *.asf *.divx *.dv *.flv *.gxf "
41 42 43 44 45
                                         "*.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";

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

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";
53
const QString   Media::streamPrefix = "stream://";
54

55 56 57 58 59 60 61 62
QPixmap*        Media::defaultSnapshot = NULL;

Media::Media(const QString &path )
    : m_fileInfo( NULL )
    , m_nbFrames( 0 )
    , m_baseClip( NULL )
    , m_inWorkspace( false )
    , m_snapshotImage( NULL )
63
{
64
    setFilePath( path );
65 66
    Backend::IBackend* backend = Backend::getBackend();
    m_source = backend->createSource( qPrintable( path ) );
67
    MetaDataManager::getInstance()->computeMediaMetadata( this );
68 69
}

70
Media::~Media()
71
{
72 73
    if ( m_source )
        delete m_source;
74 75
    if ( m_fileInfo )
        delete m_fileInfo;
76 77
}

78
void
79
Media::computeFileType()
80
{
81
    const QString filter = "*." + m_fileInfo->suffix().toLower();
82 83 84 85 86 87 88
    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
89
        vlmcDebug() << "What the hell is this extension? And how did you loaded it?!";
90 91
}

92 93
const QFileInfo*
Media::fileInfo() const
94 95 96 97
{
    return m_fileInfo;
}

98 99
Media::FileType
Media::fileType() const
100 101 102 103
{
    return m_fileType;
}

104 105 106 107 108
void Media::setFileType(Media::FileType type)
{
    m_fileType = type;
}

109 110
void
Media::setNbFrames( qint64 nbFrames )
111 112 113 114
{
    m_nbFrames = nbFrames;
}

115 116
qint64
Media::nbFrames() const
117 118 119
{
    return m_nbFrames;
}
120

121 122
const QString&
Media::mrl() const
123 124 125 126
{
    return m_mrl;
}

127 128
const QString&
Media::fileName() const
129 130 131
{
    return m_fileName;
}
132

133 134 135 136 137 138
Backend::ISource*
Media::source()
{
    return m_source;
}

139 140
const Backend::ISource*
Media::source() const
141
{
142
    return m_source;
143
}
144

145 146 147 148
void
Media::save( QXmlStreamWriter& project )
{
    project.writeStartElement( "media" );
149 150 151 152
    if ( m_inWorkspace == true )
        project.writeAttribute( "mrl", Workspace::workspacePrefix + m_workspacePath );
    else
        project.writeAttribute( "mrl", m_fileInfo->absoluteFilePath() );
153 154
    project.writeEndElement();
}
155 156 157 158 159 160 161

void
Media::setBaseClip( Clip *clip )
{
    Q_ASSERT( m_baseClip == NULL );
    m_baseClip = clip;
}
162 163 164 165 166 167 168

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

169 170 171 172 173 174 175
void
Media::onMetaDataComputed()
{
    emit metaDataComputed();
    if ( m_source->hasVideo() && m_source->snapshot() != NULL )
    {
        Q_ASSERT( m_snapshotImage == NULL );
176
        m_snapshotImage = new QImage( m_source->snapshot(), 320, 180, QImage::Format_RGB32 );
177 178 179 180
        emit snapshotAvailable();
    }
}

181 182 183
void
Media::setFilePath( const QString &filePath )
{
184 185
    if ( m_fileInfo )
        delete m_fileInfo;
186 187
    m_fileInfo = new QFileInfo( filePath );
    m_fileName = m_fileInfo->fileName();
188
    computeFileType();
189
    m_mrl = "file:///" + QUrl::toPercentEncoding( filePath, "/" );
190 191 192
//    if ( m_vlcMedia )
//        delete m_vlcMedia;
//    m_vlcMedia = new LibVLCpp::Media( m_mrl );
193 194 195
    //Don't call this before setting all the internals, as it relies on Media::fileInfo.
    if ( Workspace::isInProjectDir( this ) == true )
    {
196
        m_inWorkspace = true;
197 198
        m_workspacePath = Workspace::pathInProjectDir( this );
    }
199 200 201 202 203
    else
    {
        m_inWorkspace = false;
        m_workspacePath = "";
    }
204
    emit workspaceStateChanged( m_inWorkspace );
205
}
206

207 208
QPixmap&
Media::snapshot()
209
{
210 211 212 213 214 215 216 217 218 219 220 221 222 223
    if ( m_snapshot.isNull() == false )
        return m_snapshot;

    if ( m_snapshotImage != NULL )
    {
        m_snapshot = QPixmap::fromImage( *m_snapshotImage );
        delete m_snapshotImage;

        if ( m_snapshot.isNull() == false )
            return m_snapshot;
    }
    if ( Media::defaultSnapshot == NULL )
        Media::defaultSnapshot = new QPixmap( ":/images/vlmc" );
    return *Media::defaultSnapshot;
224
}