VLCSource.cpp 4.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*****************************************************************************
 * VLCSource.cpp: Implementation of ISource based on a libvlc_media_t
 *****************************************************************************
 * Copyright (C) 2008-2010 VideoLAN
 *
 * 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 "EventWaiter.h"
#include "VLCBackend.h"
#include "VLCSource.h"
#include "VLCVmemRenderer.h"
#include "VlmcDebug.h"

using namespace Backend;
using namespace Backend::VLC;

VLCSource::VLCSource( VLCBackend* backend, const QString& path )
    : m_backend( backend )
34
    , m_snapshot( NULL )
35
    , m_isParsed( false )
36
{
37
    m_media = new LibVLCpp::Media( backend->vlcInstance(), "file://" + path );
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
}

VLCSource::~VLCSource()
{
    delete m_media;
}

LibVLCpp::Media*
VLCSource::media()
{
    return m_media;
}

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

bool
VLCSource::preparse()
{
60 61 62
    // This assume we won't try to parse the same media twice ast the same time
    m_isParsed = true;

63 64 65 66
    VmemRenderer*           renderer = new VmemRenderer( m_backend, this, NULL );
    LibVLCpp::MediaPlayer*  mediaPlayer = renderer->mediaPlayer();
    {
        EventWaiter ew( mediaPlayer );
67
        ew.add( libvlc_MediaPlayerLengthChanged );
68 69 70 71 72 73 74 75 76 77 78 79
        renderer->start();
        if ( ew.wait( 3000 ) != EventWaiter::Success )
        {
            delete renderer;
            return false;
        }
    }
    m_media->parse();
    m_nbVideoTracks = mediaPlayer->getNbVideoTrack();
    m_nbAudioTracks = mediaPlayer->getNbAudioTrack();
    //FIXME: handle images with something like m_length = 10000;
    m_length = mediaPlayer->getLength();
80
    if ( hasVideo() == true )
81 82 83 84 85 86 87 88 89 90 91
    {
        mediaPlayer->getSize( &m_width, &m_height );
        m_fps = mediaPlayer->getFps();
        if ( m_fps < 0.1f )
            vlmcWarning() << "Invalid FPS for source" << m_media->getFileName();
        return computeSnapshot( renderer );
    }
    delete renderer;
    return true;
}

92 93 94 95 96 97
bool
VLCSource::isParsed() const
{
    return m_isParsed;
}

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
static bool
checkTimeChanged( const libvlc_event_t* event )
{
    Q_ASSERT( event->type == libvlc_MediaPlayerPositionChanged );
    return ( event->u.media_player_position_changed.new_position > 0.2f );
}

bool
VLCSource::computeSnapshot( VmemRenderer* renderer )
{
    Q_ASSERT( m_snapshot == NULL );
    LibVLCpp::MediaPlayer*  mediaPlayer = renderer->mediaPlayer();
    {
        EventWaiter ew( mediaPlayer );
        ew.add( libvlc_MediaPlayerPositionChanged );
        ew.setValidationCallback( &checkTimeChanged );
        renderer->setTime( m_length / 3 );
        if ( ew.wait( 3000 ) != EventWaiter::Success )
        {
            delete renderer;
            return false;
        }
    }
    m_snapshot = renderer->waitSnapshot();
    delete renderer;
    return true;
}

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

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

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

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

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

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

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

unsigned int
VLCSource::nbAudioTracks() const
{
    return m_nbAudioTracks;
}
172 173 174 175 176 177 178 179 180

const uint8_t*
VLCSource::snapshot() const
{
    if ( hasVideo() == false || m_snapshot == NULL )
        return NULL;
    return m_snapshot->bits();
}