VideoClipWorkflow.cpp 5 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
/*****************************************************************************
 * VideoClipWorkflow.cpp : Clip workflow. Will extract a single frame from a VLCMedia
 *****************************************************************************
 * 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.
 *****************************************************************************/

#include "VideoClipWorkflow.h"
24
#include "MainWorkflow.h"
25
#include "StackedBuffer.hpp"
26 27 28

VideoClipWorkflow::VideoClipWorkflow( Clip* clip ) : ClipWorkflow( clip )
{
29 30 31 32
    for ( unsigned int i = 0; i < VideoClipWorkflow::nbBuffers; ++i )
    {
        //Actually we don't release, we extend pool, but the release method do exactly what we want.
        m_availableBuffers.push_back( new LightVideoFrame( MainWorkflow::getInstance()->getWidth()
33
                                    * MainWorkflow::getInstance()->getHeight()
34 35 36 37
                                    * Pixel::NbComposantes ) );
    }
//    m_availableBuffersLock = new QReadWriteLock;
//    m_computedBuffersLock = new QReadWriteLock;
38 39 40 41
}

VideoClipWorkflow::~VideoClipWorkflow()
{
42 43 44 45 46 47
    while ( m_availableBuffers.isEmpty() == false )
        delete m_availableBuffers.pop();
    while ( m_computedBuffers.isEmpty() == false )
        delete m_computedBuffers.pop();
//    delete m_availableBuffersLock;
//    delete m_computedBuffersLock;
48 49 50 51 52 53 54 55 56
}

void            VideoClipWorkflow::initVlcOutput()
{
    char        buffer[32];

    m_vlcMedia->addOption( ":no-audio" );
    m_vlcMedia->addOption( ":no-sout-audio" );
    m_vlcMedia->addOption( ":sout=#transcode{}:smem" );
57 58 59
    m_vlcMedia->setVideoDataCtx( this );
    m_vlcMedia->setVideoLockCallback( reinterpret_cast<void*>( getLockCallback() ) );
    m_vlcMedia->setVideoUnlockCallback( reinterpret_cast<void*>( getUnlockCallback() ) );
60 61 62 63
    m_vlcMedia->addOption( ":sout-transcode-vcodec=RV24" );
    m_vlcMedia->addOption( ":sout-transcode-acodec=s16l" );
//    m_vlcMedia->addOption( ":no-sout-keep" );

64
    m_vlcMedia->addOption( ":no-sout-smem-time-sync" );
65

66
    sprintf( buffer, ":sout-transcode-width=%i", MainWorkflow::getInstance()->getWidth() );
67 68
    m_vlcMedia->addOption( buffer );

69
    sprintf( buffer, ":sout-transcode-height=%i", MainWorkflow::getInstance()->getHeight() );
70 71
    m_vlcMedia->addOption( buffer );

72
    //Forced output fps
73
    sprintf( buffer, ":sout-transcode-fps=%f", (float)Clip::DefaultFPS );
74
    m_vlcMedia->addOption( buffer );
75 76 77 78 79 80 81 82 83 84 85 86
}

void*       VideoClipWorkflow::getLockCallback()
{
    return reinterpret_cast<void*>(&VideoClipWorkflow::lock);
}

void*       VideoClipWorkflow::getUnlockCallback()
{
    return reinterpret_cast<void*>( &VideoClipWorkflow::unlock );
}

87
void*       VideoClipWorkflow::getOutput( ClipWorkflow::GetMode mode )
88 89 90
{
    QMutexLocker    lock( m_renderLock );

91 92
    preGetOutput();

93
    qWarning() << "poping buffer";
94 95
    if ( isEndReached() == true )
        return NULL;
96 97 98 99 100
    StackedBuffer<LightVideoFrame*>* buff;
    if ( mode == ClipWorkflow::Pop )
        buff = new StackedBuffer<LightVideoFrame*>( m_computedBuffers.pop(), &m_availableBuffers, true );
    else if ( mode == ClipWorkflow::Get )
        buff = new StackedBuffer<LightVideoFrame*>( m_computedBuffers.head(), NULL, false );
101
    ClipWorkflow::postGetOutput();
102
    return buff;
103 104
}

105 106 107 108
void    VideoClipWorkflow::lock( VideoClipWorkflow* cw, void** pp_ret, int size )
{
    Q_UNUSED( size );
    cw->m_renderLock->lock();
109 110
    LightVideoFrame*    lvf = cw->m_availableBuffers.pop();
    cw->m_computedBuffers.push_back( lvf );
111
    qWarning() << "feeding video buffer";
112
    *pp_ret = (*(lvf))->frame.octets;
113 114
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
115
void    VideoClipWorkflow::unlock( VideoClipWorkflow* cw, void* buffer, int width, int height, int bpp, int size, qint64 pts )
116 117 118 119 120 121
{
    Q_UNUSED( buffer );
    Q_UNUSED( width );
    Q_UNUSED( height );
    Q_UNUSED( bpp );
    Q_UNUSED( size );
122

123
    cw->computePtsDiff( pts );
124 125 126
    LightVideoFrame*    lvf = cw->m_computedBuffers.head();
    (*(lvf))->ptsDiff = cw->m_currentPts - cw->m_previousPts;
    cw->m_renderLock->unlock();
127
    //If this is the first buffer that has been rendered, there may be a waiting TrackWorkflow.
128
    cw->commonUnlock();
129
}
130 131 132 133 134 135 136 137 138 139

uint32_t    VideoClipWorkflow::getAvailableBuffers() const
{
    return m_availableBuffers.count();
}

uint32_t    VideoClipWorkflow::getComputedBuffers() const
{
    return m_computedBuffers.count();
}