VLCSource.cpp 4.6 KB
Newer Older
1 2 3
/*****************************************************************************
 * VLCSource.cpp: Implementation of ISource based on a libvlc_media_t
 *****************************************************************************
4
 * Copyright (C) 2008-2016 VideoLAN
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
 *
 * 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.
 *****************************************************************************/

#include "VLCBackend.h"
#include "VLCSource.h"
#include "VLCVmemRenderer.h"
26
#include "Tools/VlmcDebug.h"
27 28 29 30 31 32

using namespace Backend;
using namespace Backend::VLC;

VLCSource::VLCSource( VLCBackend* backend, const QString& path )
    : m_backend( backend )
33 34 35 36 37 38
    , m_width( 0 )
    , m_height( 0 )
    , m_fps( .0f )
    , m_nbVideoTracks( 0 )
    , m_nbAudioTracks( 0 )
    , m_length( 0 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
39
    , m_snapshot( nullptr )
40
    , m_isParsed( false )
41
    , m_nbFrames( 0 )
42
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
43
    m_media = ::VLC::Media( backend->vlcInstance(), path.toStdString(), ::VLC::Media::FromPath );
44 45
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
46
::VLC::Media&
47 48 49 50 51 52 53 54 55 56 57 58 59 60
VLCSource::media()
{
    return m_media;
}

ISourceRenderer*
VLCSource::createRenderer( ISourceRendererEventCb *callback )
{
    return new VLCSourceRenderer( m_backend, this, callback );
}

bool
VLCSource::preparse()
{
61 62
    // This assume we won't try to parse the same media twice ast the same time
    m_isParsed = true;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
63 64 65
    Q_ASSERT( m_nbAudioTracks == 0 );
    Q_ASSERT( m_nbVideoTracks == 0 );

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
66
    VmemRenderer renderer( m_backend, this, nullptr );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
67 68 69 70
    m_media.parse();
    m_length = m_media.duration();
    auto tracks = m_media.tracks();
    for ( const auto& t : tracks )
71
    {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
72
        if ( t.type() == ::VLC::MediaTrack::Type::Video )
73
        {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
            ++m_nbVideoTracks;
            //FIXME: This doesn't handle media with multiple video tracks
            //We assume the first track to be the most valuable one for now.
            if ( m_nbVideoTracks == 1 )
            {
                m_fps = (float)t.fpsNum() / (float)t.fpsDen();
                if ( m_fps < 0.1f )
                {
                    vlmcWarning() << "Invalid FPS for source" << m_media.mrl();
                    return false;
                }
                m_width = t.width();
                m_height = t.height();
                m_nbFrames = (int64_t)( (float)( m_length / 1000 ) * m_fps );
                computeSnapshot( renderer );
            }
90
        }
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
91 92
        else if ( t.type() == ::VLC::MediaTrack::Type::Audio )
            ++m_nbAudioTracks;
93
    }
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
94
    //FIXME: handle images with something like m_length = 10000;
95 96 97
    return true;
}

98 99 100 101 102 103
bool
VLCSource::isParsed() const
{
    return m_isParsed;
}

104
bool
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
105
VLCSource::computeSnapshot( VmemRenderer& renderer )
106
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
107
    Q_ASSERT( m_snapshot == nullptr );
108
    renderer.start();
109
    {
110 111 112 113 114 115 116 117 118 119 120 121
        QMutex mutex;
        QWaitCondition cond;
        auto em = renderer.mediaPlayer().eventManager();
        em.onPositionChanged([&mutex, &cond](float pos) {
            QMutexLocker lock( &mutex );
            if ( pos > 0.2 )
                cond.wakeAll();
        });
        QMutexLocker lock( &mutex );
        renderer.setPosition( 0.3 );
        if ( cond.wait( &mutex, 2000 ) == false )
            return false;
122
    }
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
123
    m_snapshot = renderer.waitSnapshot();
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
124
    return m_snapshot != nullptr;
125 126 127 128 129 130 131 132 133 134 135 136 137 138
}

unsigned int
VLCSource::width() const
{
    return m_width;
}

unsigned int
VLCSource::height() const
{
    return m_height;
}

139 140 141 142 143 144
int64_t
VLCSource::length() const
{
    return m_length;
}

145 146 147 148 149 150 151 152 153 154 155 156
float
VLCSource::fps() const
{
    return m_fps;
}

bool
VLCSource::hasVideo() const
{
    return m_nbVideoTracks > 0;
}

157 158 159 160 161 162
unsigned int
VLCSource::nbVideoTracks() const
{
    return m_nbVideoTracks;
}

163 164 165 166
bool VLCSource::hasAudio() const
{
    return m_nbAudioTracks > 0;
}
167 168 169 170 171 172

unsigned int
VLCSource::nbAudioTracks() const
{
    return m_nbAudioTracks;
}
173 174 175 176

const uint8_t*
VLCSource::snapshot() const
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
177 178
    if ( hasVideo() == false || m_snapshot == nullptr )
        return nullptr;
179 180 181
    return m_snapshot->bits();
}

182 183 184 185 186 187
int64_t
VLCSource::nbFrames() const
{
    return m_nbFrames;
}