Media.cpp 5.07 KB
Newer Older
1
/*****************************************************************************
2
 * Media.cpp: Represents a basic container for media informations.
3
 *****************************************************************************
4
 * Copyright (C) 2008-2014 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 30
#include <QUrl>

31
#include "Media.h"
32 33

#include "Clip.h"
34 35 36 37 38
#include "Metadata/MetaDataManager.h"
#include "Tools/VlmcDebug.h"
#include "Project/Workspace.h"
#include "Backend/ISource.h"
#include "Backend/IBackend.h"
39

40

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

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

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

56 57 58
QPixmap*        Media::defaultSnapshot = NULL;

Media::Media(const QString &path )
59 60
    : m_source( NULL )
    , m_fileInfo( NULL )
61 62
    , m_baseClip( NULL )
    , m_snapshotImage( NULL )
63
{
64
    setFilePath( path );
65 66
}

67
Media::~Media()
68
{
69 70
    delete m_source;
    delete m_fileInfo;
71 72
}

73 74
const QFileInfo*
Media::fileInfo() const
75 76 77 78
{
    return m_fileInfo;
}

79 80
Media::FileType
Media::fileType() const
81 82 83 84
{
    return m_fileType;
}

85 86 87 88 89
void Media::setFileType(Media::FileType type)
{
    m_fileType = type;
}

90 91
const QString&
Media::mrl() const
92 93 94 95
{
    return m_mrl;
}

96 97
const QString&
Media::fileName() const
98 99 100
{
    return m_fileName;
}
101

102 103 104 105 106 107
Backend::ISource*
Media::source()
{
    return m_source;
}

108 109
const Backend::ISource*
Media::source() const
110
{
111
    return m_source;
112
}
113

114 115 116 117 118 119
void
Media::setBaseClip( Clip *clip )
{
    Q_ASSERT( m_baseClip == NULL );
    m_baseClip = clip;
}
120

121 122 123 124
void
Media::onMetaDataComputed()
{
    emit metaDataComputed();
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    if ( m_source->hasVideo() == true )
    {
        const QString filter = "*." + m_fileInfo->suffix().toLower();
        if ( Media::ImageExtensions.contains( filter ) )
            m_fileType = Image;
        else
            m_fileType = Video;
        if ( m_source->snapshot() != NULL )
        {
            Q_ASSERT( m_snapshotImage == NULL );
            m_snapshotImage = new QImage( m_source->snapshot(), 320, 180, QImage::Format_RGB32 );
            emit snapshotAvailable();
        }
    }
    else if ( m_source->hasAudio() )
        m_fileType = Audio;
    else
142
    {
143 144 145
        // We expect this case to be handled by the metadata manager. It should
        // trigger an error for this kind of file since we can't use them.
        vlmcFatal("Got metadata for a file which has no video nor audio.");
146 147 148
    }
}

149 150 151
void
Media::setFilePath( const QString &filePath )
{
152 153
    if ( m_fileInfo )
        delete m_fileInfo;
154 155
    m_fileInfo = new QFileInfo( filePath );
    m_fileName = m_fileInfo->fileName();
156
    m_mrl = "file:///" + QUrl::toPercentEncoding( filePath, "/" );
157 158 159

    Backend::IBackend* backend = Backend::getBackend();
    delete m_source;
160
    m_source = backend->createSource( qPrintable( filePath ) );
161
    MetaDataManager::getInstance()->computeMediaMetadata( this );
162
}
163

164 165
QPixmap&
Media::snapshot()
166
{
167 168 169 170 171 172 173 174 175 176 177 178 179 180
    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;
181
}