VideoClipWorkflow.cpp 7.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
#include "LightVideoFrame.h"
28
#include "Clip.h"
29

30 31
VideoClipWorkflow::VideoClipWorkflow( Clip *clip ) :
        ClipWorkflow( clip )
32
{
33 34
    for ( unsigned int i = 0; i < VideoClipWorkflow::nbBuffers; ++i )
    {
35
        m_availableBuffers.enqueue( new LightVideoFrame( MainWorkflow::getInstance()->getWidth()
36
                                    * MainWorkflow::getInstance()->getHeight()
37 38
                                    * Pixel::NbComposantes ) );
    }
39
    debugType = 2;
40 41 42 43
}

VideoClipWorkflow::~VideoClipWorkflow()
{
44
    while ( m_availableBuffers.isEmpty() == false )
45
        delete m_availableBuffers.dequeue();
46
    while ( m_computedBuffers.isEmpty() == false )
47
        delete m_computedBuffers.dequeue();
48 49
}

50 51
void
VideoClipWorkflow::initVlcOutput()
52 53 54 55 56 57
{
    char        buffer[32];

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

65 66
    m_vlcMedia->addOption( ":sout-smem-time-sync" );
//    m_vlcMedia->addOption( ":no-sout-smem-time-sync" );
67

68 69
    sprintf( buffer, ":sout-transcode-width=%i",
             MainWorkflow::getInstance()->getWidth() );
70
    m_vlcMedia->addOption( buffer );
71 72
    sprintf( buffer, ":sout-transcode-height=%i",
             MainWorkflow::getInstance()->getHeight() );
73
    m_vlcMedia->addOption( buffer );
74
    sprintf( buffer, ":sout-transcode-fps=%f", (float)Clip::DefaultFPS );
75
    m_vlcMedia->addOption( buffer );
76 77
}

78 79
void*
VideoClipWorkflow::getLockCallback()
80 81 82 83
{
    return reinterpret_cast<void*>(&VideoClipWorkflow::lock);
}

84 85
void*
VideoClipWorkflow::getUnlockCallback()
86 87 88 89
{
    return reinterpret_cast<void*>( &VideoClipWorkflow::unlock );
}

90 91
void*
VideoClipWorkflow::getOutput( ClipWorkflow::GetMode mode )
92
{
93
//    qDebug() << "entering video get output";
94
    QMutexLocker    lock( m_renderLock );
95
//    qDebug() << "got video render lock";
96
    QMutexLocker    lock2( m_computedBuffersMutex );
97
//    qDebug() << "got video computer buffers lock";
98

99
    if ( preGetOutput() == false )
100 101
    {
//        qDebug() << "video preGetOutput() returned false";
102
        return NULL;
103
    }
104 105
//    qWarning() << "Video::getOutput(). Available:" << m_availableBuffers.count() <<
//    "Computed:" << m_computedBuffers.count();
106
    if ( isEndReached() == true )
107 108
    {
//        qDebug() << "video end reached was true";
109
        return NULL;
110
    }
111
    ::StackedBuffer<LightVideoFrame*>* buff;
112
    if ( mode == ClipWorkflow::Pop )
113
    {
114
        buff = new StackedBuffer( m_computedBuffers.dequeue(), this, true );
115
    }
116
    else if ( mode == ClipWorkflow::Get )
117
        buff = new StackedBuffer( m_computedBuffers.head(), NULL, false );
118
//    qDebug() << "calling video postGetOutput();";
119
    postGetOutput();
120
//    qDebug() << "returning videobuff";
121
    return buff;
122 123
}

124 125
void
VideoClipWorkflow::lock( VideoClipWorkflow *cw, void **pp_ret, int size )
126
{
127
//    qDebug() << "Entering VideoClipWorkflow::lock";
128
    Q_UNUSED( size );
129
    QMutexLocker        lock( cw->m_availableBuffersMutex );
130
    cw->m_renderLock->lock();
131
    cw->m_computedBuffersMutex->lock();
132
//    qDebug() << "Got all videoclipworkflow::lock mutexs acquired.";
133

134 135 136
    LightVideoFrame*    lvf = NULL;
    if ( cw->m_availableBuffers.isEmpty() == true )
    {
137
//        qCritical() << "Late buffer generation. Spawning new video buffer";
138 139 140 141 142
        lvf = new LightVideoFrame( MainWorkflow::getInstance()->getWidth()
                                   * MainWorkflow::getInstance()->getHeight()
                                   * Pixel::NbComposantes );
    }
    else
143 144
        lvf = cw->m_availableBuffers.dequeue();
    cw->m_computedBuffers.enqueue( lvf );
145 146
//    qWarning() << ">>>VideoGeneration. Available:" <<
//        cw->m_availableBuffers.count() << "Computed:" << cw->m_computedBuffers.count() << "position" << cw->m_mediaPlayer->getPosition();
147
//    qWarning() << "feeding video buffer";
148
    *pp_ret = (*(lvf))->frame.octets;
149 150
}

151 152 153
void
VideoClipWorkflow::unlock( VideoClipWorkflow *cw, void *buffer, int width,
                           int height, int bpp, int size, qint64 pts )
154 155 156 157 158 159
{
    Q_UNUSED( buffer );
    Q_UNUSED( width );
    Q_UNUSED( height );
    Q_UNUSED( bpp );
    Q_UNUSED( size );
160

161
    cw->computePtsDiff( pts );
162
    LightVideoFrame     *lvf = cw->m_computedBuffers.last();
163
    (*(lvf))->ptsDiff = cw->m_currentPts - cw->m_previousPts;
164 165 166
//    qWarning() << "Computed ptsDiff:" << (*(lvf))->ptsDiff;
//    if ( (*(lvf))->ptsDiff > 100000 )
//    {
167 168
//        qWarning() << "Probably invalid pts diff. pts:" << pts << "m_currentPts:" <<
//    cw->m_currentPts << "m_previousPts:" << cw->m_previousPts
169 170
//                << "state:" << cw->m_state;
//    }
171 172
    // If this is the first buffer that has been rendered,
    // there may be a waiting TrackWorkflow.
173
    cw->commonUnlock();
174
    cw->m_renderLock->unlock();
175
    cw->m_computedBuffersMutex->unlock();
176 177
}

178 179
uint32_t
VideoClipWorkflow::getNbComputedBuffers() const
180 181 182
{
    return m_computedBuffers.count();
}
183

184 185
uint32_t
VideoClipWorkflow::getMaxComputedBuffers() const
186 187 188
{
    return VideoClipWorkflow::nbBuffers;
}
189

190 191
void
VideoClipWorkflow::releaseBuffer( LightVideoFrame *lvf )
192 193 194 195
{
    QMutexLocker    lock( m_availableBuffersMutex );
    m_availableBuffers.enqueue( lvf );
}
196

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

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

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

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