VideoClipWorkflow.cpp 6.75 KB
Newer Older
1 2 3
/*****************************************************************************
 * VideoClipWorkflow.cpp : Clip workflow. Will extract a single frame from a VLCMedia
 *****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
4
 * Copyright (C) 2008-2010 VideoLAN
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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.
 *****************************************************************************/

23 24
#include "Clip.h"
#include "LightVideoFrame.h"
25
#include "MainWorkflow.h"
26
#include "StackedBuffer.hpp"
27
#include "VideoClipWorkflow.h"
28
#include "VLCMedia.h"
29
#include "WaitCondition.hpp"
30

31
#include <QReadWriteLock>
32

33
VideoClipWorkflow::VideoClipWorkflow( Clip *clip ) :
34
        ClipWorkflow( clip ),
35 36
        m_width( 0 ),
        m_height( 0 )
37
{
38 39 40
}

VideoClipWorkflow::~VideoClipWorkflow()
41 42 43 44 45 46
{
    releasePrealocated();
}

void
VideoClipWorkflow::releasePrealocated()
47
{
48
    while ( m_availableBuffers.isEmpty() == false )
49
        delete m_availableBuffers.dequeue();
50
    while ( m_computedBuffers.isEmpty() == false )
51
        delete m_computedBuffers.dequeue();
52 53
}

54 55 56
void
VideoClipWorkflow::preallocate()
{
57 58 59
    quint32     newWidth = MainWorkflow::getInstance()->getWidth();
    quint32     newHeight = MainWorkflow::getInstance()->getHeight();
    if ( newWidth != m_width || newHeight != m_height )
60
    {
61 62 63 64 65 66 67 68
        m_width = newWidth;
        m_height = newHeight;
        while ( m_availableBuffers.isEmpty() == false )
            delete m_availableBuffers.dequeue();
        for ( unsigned int i = 0; i < VideoClipWorkflow::nbBuffers; ++i )
        {
            m_availableBuffers.enqueue( new LightVideoFrame( newWidth, newHeight ) );
        }
69 70 71
    }
}

72 73
void
VideoClipWorkflow::initVlcOutput()
74 75 76
{
    char        buffer[32];

77
    preallocate();
78 79 80
    m_vlcMedia->addOption( ":no-audio" );
    m_vlcMedia->addOption( ":no-sout-audio" );
    m_vlcMedia->addOption( ":sout=#transcode{}:smem" );
81 82 83
    m_vlcMedia->setVideoDataCtx( this );
    m_vlcMedia->setVideoLockCallback( reinterpret_cast<void*>( getLockCallback() ) );
    m_vlcMedia->setVideoUnlockCallback( reinterpret_cast<void*>( getUnlockCallback() ) );
84
    m_vlcMedia->addOption( ":sout-transcode-vcodec=RV24" );
85 86 87 88
    if ( m_fullSpeedRender == false )
        m_vlcMedia->addOption( ":sout-smem-time-sync" );
    else
        m_vlcMedia->addOption( ":no-sout-smem-time-sync" );
89

90
    sprintf( buffer, ":sout-transcode-width=%i", m_width );
91
    m_vlcMedia->addOption( buffer );
92
    sprintf( buffer, ":sout-transcode-height=%i", m_height );
93
    m_vlcMedia->addOption( buffer );
94
    sprintf( buffer, ":sout-transcode-fps=%f", (float)Clip::DefaultFPS );
95
    m_vlcMedia->addOption( buffer );
96 97
}

98
void*
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
99
VideoClipWorkflow::getLockCallback() const
100 101 102 103
{
    return reinterpret_cast<void*>(&VideoClipWorkflow::lock);
}

104
void*
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
105
VideoClipWorkflow::getUnlockCallback() const
106 107 108 109
{
    return reinterpret_cast<void*>( &VideoClipWorkflow::unlock );
}

110 111
void*
VideoClipWorkflow::getOutput( ClipWorkflow::GetMode mode )
112
{
113
    QMutexLocker    lock( m_renderLock );
114
    QMutexLocker    lock2( m_computedBuffersMutex );
115

116 117
    if ( isEndReached() == true )
        return NULL;
118 119 120 121 122 123 124 125 126 127 128
    if ( preGetOutput() == false )
    {
        QMutexLocker    waitLock( m_renderWaitCond->getMutex() );
        m_computedBuffersMutex->unlock();
        m_renderLock->unlock();

        m_renderWaitCond->waitLocked();

        m_renderLock->lock();
        m_computedBuffersMutex->lock();
    }
129 130 131 132
    //FIXME
    //TODO: This is a nasty work arround. The race condition needs to be fixed !
    if ( m_computedBuffers.isEmpty() == true )
        return NULL;
133
    ::StackedBuffer<LightVideoFrame*>* buff;
134
    if ( mode == ClipWorkflow::Pop )
135
        buff = new StackedBuffer( m_computedBuffers.dequeue(), this, true );
136
    else if ( mode == ClipWorkflow::Get )
137
        buff = new StackedBuffer( m_computedBuffers.head(), NULL, false );
138
    postGetOutput();
139
    return buff;
140 141
}

142 143
void
VideoClipWorkflow::lock( VideoClipWorkflow *cw, void **pp_ret, int size )
144 145
{
    Q_UNUSED( size );
146
    QMutexLocker        lock( cw->m_availableBuffersMutex );
147 148
    LightVideoFrame*    lvf = NULL;

149
    cw->m_renderLock->lock();
150
    cw->m_computedBuffersMutex->lock();
151 152
    if ( cw->m_availableBuffers.isEmpty() == true )
    {
153
        lvf = new LightVideoFrame( cw->m_width, cw->m_height );
154 155
    }
    else
156 157
        lvf = cw->m_availableBuffers.dequeue();
    cw->m_computedBuffers.enqueue( lvf );
158
    *pp_ret = (*(lvf))->frame.octets;
159 160
}

161 162 163
void
VideoClipWorkflow::unlock( VideoClipWorkflow *cw, void *buffer, int width,
                           int height, int bpp, int size, qint64 pts )
164 165 166 167 168 169
{
    Q_UNUSED( buffer );
    Q_UNUSED( width );
    Q_UNUSED( height );
    Q_UNUSED( bpp );
    Q_UNUSED( size );
170

171
    cw->computePtsDiff( pts );
172
    LightVideoFrame     *lvf = cw->m_computedBuffers.last();
173
    (*(lvf))->ptsDiff = cw->m_currentPts - cw->m_previousPts;
174
    cw->commonUnlock();
175
    cw->m_renderLock->unlock();
176
    cw->m_computedBuffersMutex->unlock();
177 178
    QMutexLocker    lock( cw->m_renderWaitCond->getMutex() );
    cw->m_renderWaitCond->wake();
179 180
}

181 182
uint32_t
VideoClipWorkflow::getNbComputedBuffers() const
183 184 185
{
    return m_computedBuffers.count();
}
186

187 188
uint32_t
VideoClipWorkflow::getMaxComputedBuffers() const
189 190 191
{
    return VideoClipWorkflow::nbBuffers;
}
192

193 194
void
VideoClipWorkflow::releaseBuffer( LightVideoFrame *lvf )
195 196 197 198
{
    QMutexLocker    lock( m_availableBuffersMutex );
    m_availableBuffers.enqueue( lvf );
}
199

200 201
void
VideoClipWorkflow::flushComputedBuffers()
202 203 204 205 206 207 208
{
    QMutexLocker    lock( m_computedBuffersMutex );
    QMutexLocker    lock2( m_availableBuffersMutex );

    while ( m_computedBuffers.isEmpty() == false )
        m_availableBuffers.enqueue( m_computedBuffers.dequeue() );
}
209 210 211 212 213 214 215 216 217

VideoClipWorkflow::StackedBuffer::StackedBuffer( LightVideoFrame *lvf,
                                                    VideoClipWorkflow *poolHandler,
                                                    bool mustBeReleased) :
    ::StackedBuffer<LightVideoFrame*>( lvf, mustBeReleased ),
    m_poolHandler( poolHandler )
{
}

218 219
void
VideoClipWorkflow::StackedBuffer::release()
220
{
221
    if ( m_mustRelease == true && m_poolHandler.isNull() == false )
222 223 224
        m_poolHandler->releaseBuffer( m_buff );
    delete this;
}