VideoClipWorkflow.cpp 6.47 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
/*****************************************************************************
 * 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.
 *****************************************************************************/

Ludovic Fauvet's avatar
Ludovic Fauvet committed
23
#include "vlmc.h"
24
#include "VideoClipWorkflow.h"
25
#include "MainWorkflow.h"
26
#include "StackedBuffer.hpp"
27 28 29

VideoClipWorkflow::VideoClipWorkflow( Clip* clip ) : ClipWorkflow( clip )
{
30 31 32 33
    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()
34
                                    * MainWorkflow::getInstance()->getHeight()
35 36
                                    * Pixel::NbComposantes ) );
    }
37
    debugType = 2;
38 39 40 41
}

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

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

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

62
    m_vlcMedia->addOption( ":no-sout-smem-time-sync" );
63

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

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

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

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

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

85
void*       VideoClipWorkflow::getOutput( ClipWorkflow::GetMode mode )
86
{
87
    QMutexLocker    lock( m_renderLock );
88
    QMutexLocker    lock2( m_computedBuffersMutex );
89

90
    preGetOutput();
91
//    qWarning() << "Video::getOutput(). Available:" << m_availableBuffers.count() << "Computed:" << m_computedBuffers.count();
92 93
    if ( isEndReached() == true )
        return NULL;
94
    StackedBuffer<LightVideoFrame*, VideoClipWorkflow>* buff;
95
    if ( mode == ClipWorkflow::Pop )
96 97 98
    {
        buff = new StackedBuffer<LightVideoFrame*, VideoClipWorkflow>( m_computedBuffers.dequeue(), this, true );
    }
99
    else if ( mode == ClipWorkflow::Get )
100
        buff = new StackedBuffer<LightVideoFrame*, VideoClipWorkflow>( m_computedBuffers.head(), NULL, false );
101
    postGetOutput();
102
    return buff;
103 104
}

105 106 107
void    VideoClipWorkflow::lock( VideoClipWorkflow* cw, void** pp_ret, int size )
{
    Q_UNUSED( size );
108
    QMutexLocker        lock( cw->m_availableBuffersMutex );
109
    cw->m_renderLock->lock();
110 111
    cw->m_computedBuffersMutex->lock();

112 113 114
    LightVideoFrame*    lvf = NULL;
    if ( cw->m_availableBuffers.isEmpty() == true )
    {
115
//        qCritical() << "Late buffer generation. Spawning new video buffer";
116 117 118 119 120
        lvf = new LightVideoFrame( MainWorkflow::getInstance()->getWidth()
                                   * MainWorkflow::getInstance()->getHeight()
                                   * Pixel::NbComposantes );
    }
    else
121 122 123
        lvf = cw->m_availableBuffers.dequeue();
    cw->m_computedBuffers.enqueue( lvf );
//    qWarning() << ">>>VideoGeneration. Available:" << cw->m_availableBuffers.count() << "Computed:" << cw->m_computedBuffers.count();
124
//    qWarning() << "feeding video buffer";
125
    *pp_ret = (*(lvf))->frame.octets;
126 127
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
128
void    VideoClipWorkflow::unlock( VideoClipWorkflow* cw, void* buffer, int width, int height, int bpp, int size, qint64 pts )
129 130 131 132 133 134
{
    Q_UNUSED( buffer );
    Q_UNUSED( width );
    Q_UNUSED( height );
    Q_UNUSED( bpp );
    Q_UNUSED( size );
135

136
    cw->computePtsDiff( pts );
137
    LightVideoFrame*    lvf = cw->m_computedBuffers.last();
138
    (*(lvf))->ptsDiff = cw->m_currentPts - cw->m_previousPts;
139 140 141 142 143 144
//    qWarning() << "Computed ptsDiff:" << (*(lvf))->ptsDiff;
//    if ( (*(lvf))->ptsDiff > 100000 )
//    {
//        qWarning() << "Probably invalid pts diff. pts:" << pts << "m_currentPts:" << cw->m_currentPts << "m_previousPts:" << cw->m_previousPts
//                << "state:" << cw->m_state;
//    }
145
    //If this is the first buffer that has been rendered, there may be a waiting TrackWorkflow.
146
    cw->commonUnlock();
147
    cw->m_renderLock->unlock();
148
    cw->m_computedBuffersMutex->unlock();
149 150
}

151
uint32_t    VideoClipWorkflow::getNbComputedBuffers() const
152 153 154
{
    return m_computedBuffers.count();
}
155 156 157 158 159

uint32_t    VideoClipWorkflow::getMaxComputedBuffers() const
{
    return VideoClipWorkflow::nbBuffers;
}
160 161 162 163 164 165

void        VideoClipWorkflow::releaseBuffer( LightVideoFrame *lvf )
{
    QMutexLocker    lock( m_availableBuffersMutex );
    m_availableBuffers.enqueue( lvf );
}
166 167 168 169 170 171 172 173 174 175 176

void        VideoClipWorkflow::flushComputedBuffers()
{
    QMutexLocker    lock( m_computedBuffersMutex );
    QMutexLocker    lock2( m_availableBuffersMutex );

    while ( m_computedBuffers.isEmpty() == false )
    {
        m_availableBuffers.enqueue( m_computedBuffers.dequeue() );
    }
}