WorkflowRenderer.cpp 9.09 KB
Newer Older
1
/*****************************************************************************
2
 * WorkflowRenderer.cpp: Allow a current workflow preview
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *****************************************************************************
 * 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.
 *****************************************************************************/

23
#include <QtDebug>
24
#include <QThread>
25

26
#include "vlmc.h"
27
#include "WorkflowRenderer.h"
28
#include "Timeline.h"
29

30 31
WorkflowRenderer::WorkflowRenderer() :
            m_mainWorkflow( MainWorkflow::getInstance() ),
32
            m_stopping( false ),
33
            m_pauseAsked( false ),
34
            m_unpauseAsked( false )
35
{
36 37
    char        buffer[64];

38
    m_actionsLock = new QReadWriteLock;
39 40 41
    m_media = new LibVLCpp::Media( "fake://" );

    sprintf( buffer, ":invmem-width=%i", VIDEOWIDTH );
42
    m_media->addOption( ":codec=invmem" );
43 44 45
    m_media->addOption( buffer );
    sprintf( buffer, ":invmem-height=%i", VIDEOHEIGHT );
    m_media->addOption( buffer );
46
    sprintf( buffer, ":invmem-lock=%lld", (qint64)WorkflowRenderer::lock );
47
    m_media->addOption( buffer );
48
    sprintf( buffer, ":invmem-unlock=%lld", (qint64)WorkflowRenderer::unlock );
49 50 51
    m_media->addOption( buffer );
    sprintf( buffer, ":invmem-data=%lld", (qint64)this );
    m_media->addOption( buffer );
52 53 54 55
    sprintf( buffer, ":width=%i", VIDEOWIDTH );
    m_media->addOption( buffer );
    sprintf( buffer, ":height=%i", VIDEOHEIGHT );
    m_media->addOption( buffer );
56 57 58 59 60

    sprintf( buffer, ":inamem-data=%lld", (qint64)this );
    m_media->addOption( buffer );
    sprintf( buffer, ":inamem-callback=%lld", (qint64)WorkflowRenderer::lock );
    m_media->addOption( buffer );
61

62 63
    m_condMutex = new QMutex;
    m_waitCond = new QWaitCondition;
64

65
    m_renderVideoFrame = new unsigned char[VIDEOHEIGHT * VIDEOWIDTH * Pixel::NbComposantes];
66
   
67 68 69 70 71 72 73 74
        //Workflow part
    connect( m_mainWorkflow, SIGNAL( frameChanged(qint64) ),
            Timeline::getInstance()->tracksView()->tracksCursor(), SLOT( setCursorPos( qint64 ) ), Qt::QueuedConnection );
    connect( Timeline::getInstance()->tracksView()->tracksCursor(), SIGNAL( cursorPositionChanged( qint64 ) ),
             this, SLOT( timelineCursorChanged(qint64) ) );
    connect( m_mainWorkflow, SIGNAL( mainWorkflowPaused() ), this, SLOT( mainWorkflowPaused() ) );
    connect( m_mainWorkflow, SIGNAL( mainWorkflowUnpaused() ), this, SLOT( mainWorkflowUnpaused() ) );

75 76
}

77

78
WorkflowRenderer::~WorkflowRenderer()
79
{
80
    stop();
81

82
    //FIXME this is probably useless...
83 84 85 86 87 88
    disconnect( m_mediaPlayer, SIGNAL( playing() ),    this,   SLOT( __videoPlaying() ) );
    disconnect( m_mediaPlayer, SIGNAL( paused() ),     this,   SLOT( __videoPaused() ) );
    disconnect( m_mediaPlayer, SIGNAL( stopped() ),    this,   SLOT( __videoStopped() ) );
    disconnect( m_mainWorkflow, SIGNAL( mainWorkflowEndReached() ), this, SLOT( __endReached() ) );
    disconnect( m_mainWorkflow, SIGNAL( positionChanged( float ) ), this, SLOT( __positionChanged( float ) ) );

89
    delete m_actionsLock;
90
    delete m_media;
91 92
    delete m_condMutex;
    delete m_waitCond;
93 94
}

95 96 97 98 99
void*   WorkflowRenderer::lockAudio( void* datas )
{
    WorkflowRenderer* self = reinterpret_cast<WorkflowRenderer*>( datas );

    qDebug() << "Injecting audio data";
100
    return self->m_renderAudioSample;
101 102
}

103
void*   WorkflowRenderer::lock( void* datas )
104
{
105
    WorkflowRenderer* self = reinterpret_cast<WorkflowRenderer*>( datas );
106

107 108
    if ( self->m_stopping == false )
    {
109 110 111
        MainWorkflow::OutputBuffers* ret = self->m_mainWorkflow->getSynchroneOutput();
        memcpy( self->m_renderVideoFrame, (*(ret->video))->frame.octets, (*(ret->video))->nboctets );
        self->m_renderAudioSample = ret->audio;
112
    }
113
    return self->m_renderVideoFrame;
114 115
}

116
void    WorkflowRenderer::unlock( void* datas )
117
{
118
    WorkflowRenderer* self = reinterpret_cast<WorkflowRenderer*>( datas );
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    self->checkActions();
}

void        WorkflowRenderer::checkActions()
{
    QReadLocker     lock( m_actionsLock );

    if ( m_actions.size() == 0 )
        return ;
    while ( m_actions.empty() == false )
    {
        Actions act = m_actions.top();
        m_actions.pop();
        switch ( act )
        {
            case    Pause:
135 136 137
                if ( m_pauseAsked == true )
                    continue ;
                m_pauseAsked = true;
138 139
//                m_mediaPlayer->pause();
                pauseMainWorkflow();
140 141 142 143 144 145 146
                //This will also pause the MainWorkflow via a signal/slot
                break ;
            default:
                qDebug() << "Unhandled action:" << act;
                break ;
        }
    }
147 148
}

149
void        WorkflowRenderer::startPreview()
150
{
151 152
    if ( m_mainWorkflow->getLength() <= 0 )
        return ;
153 154 155 156 157 158 159 160 161
    m_mediaPlayer->setMedia( m_media );

    //Media player part: to update PreviewWidget
    connect( m_mediaPlayer, SIGNAL( playing() ),    this,   SLOT( __videoPlaying() ), Qt::DirectConnection );
    connect( m_mediaPlayer, SIGNAL( paused() ),     this,   SLOT( __videoPaused() ), Qt::DirectConnection );
    connect( m_mediaPlayer, SIGNAL( stopped() ),    this,   SLOT( __videoStopped() ) );
    connect( m_mainWorkflow, SIGNAL( mainWorkflowEndReached() ), this, SLOT( __endReached() ) );
    connect( m_mainWorkflow, SIGNAL( positionChanged( float ) ), this, SLOT( __positionChanged( float ) ) );

162
    m_mainWorkflow->setFullSpeedRender( false );
163 164
    m_mainWorkflow->startRender();
    m_mediaPlayer->play();
165
    m_isRendering = true;
166
    m_paused = false;
167
    m_stopping = false;
168 169
}

170
void        WorkflowRenderer::setPosition( float newPos )
171
{
172
    m_mainWorkflow->setPosition( newPos );
173 174
}

175 176
void        WorkflowRenderer::nextFrame()
{
177
    m_mainWorkflow->nextFrame();
178 179
}

180
void        WorkflowRenderer::previousFrame()
181
{
182
    m_mainWorkflow->previousFrame();
183 184
}

185 186
void        WorkflowRenderer::pauseMainWorkflow()
{
187 188
    if ( m_paused == true )
        return ;
189 190

    QMutexLocker    lock( m_condMutex );
191
    m_mainWorkflow->pause();
192
    m_waitCond->wait( m_condMutex );
193 194
}

195 196 197 198 199 200 201
void        WorkflowRenderer::unpauseMainWorkflow()
{
    if ( m_paused == false )
        return ;
    m_mainWorkflow->unpause();
}

202 203
void        WorkflowRenderer::mainWorkflowPaused()
{
204
    m_paused = true;
205
    m_pauseAsked = false;
206 207 208 209
    {
        QMutexLocker    lock( m_condMutex );
    }
    m_waitCond->wakeAll();
210
    emit paused();
211 212
}

213 214 215 216 217 218 219
void        WorkflowRenderer::mainWorkflowUnpaused()
{
    m_paused = false;
    m_unpauseAsked = false;
    emit playing();
}

220
void        WorkflowRenderer::togglePlayPause( bool forcePause )
221
{
222
    if ( m_isRendering == false && forcePause == false )
223
        startPreview();
224 225 226 227 228 229 230 231
    else
        internalPlayPause( forcePause );
}

void        WorkflowRenderer::internalPlayPause( bool forcePause )
{
    //If force pause is true, we just ensure that this render is paused... no need to start it.
    if ( m_isRendering == true )
232
    {
233 234
        if ( m_paused == true && forcePause == false )
        {
235 236 237
            if ( m_paused == true )
            {
                m_unpauseAsked = true;
238
                unpauseMainWorkflow();
239
            }
240
        }
241
        else
242
        {
243 244
            if ( m_paused == false )
            {
245 246
                QWriteLocker        lock( m_actionsLock );
                m_actions.push( Pause );
247
            }
248
        }
249
    }
250
}
251

252
void        WorkflowRenderer::stop()
253 254
{
    m_isRendering = false;
255
    m_paused = false;
256
    m_pauseAsked = false;
257
    m_unpauseAsked = false;
258
    m_stopping = true;
259
    m_mainWorkflow->cancelSynchronisation();
260
    m_mediaPlayer->stop();
261
    m_mainWorkflow->stop();
262 263
}

264 265 266 267
/////////////////////////////////////////////////////////////////////
/////SLOTS :
/////////////////////////////////////////////////////////////////////

268
void        WorkflowRenderer::__endReached()
269
{
270
    stop();
271
    emit endReached();
272 273
}

274
void        WorkflowRenderer::__positionChanged()
275
{
276 277 278
    qFatal("This should never be used ! Get out of here !");
}

279
void        WorkflowRenderer::__positionChanged( float pos )
280 281
{
    emit positionChanged( pos );
282 283
}

284
void        WorkflowRenderer::__videoPaused()
285
{
286 287
    if ( m_pauseAsked == true )
        pauseMainWorkflow();
288 289
}

290
void        WorkflowRenderer::__videoPlaying()
291
{
292 293 294 295 296 297 298
    if ( m_unpauseAsked == true )
        unpauseMainWorkflow();
    else
    {
        m_paused = false;
        emit playing();
    }
299
}
300

301
void        WorkflowRenderer::__videoStopped()
302 303 304
{
    emit endReached();
}
305 306 307 308 309

void        WorkflowRenderer::timelineCursorChanged( qint64 newFrame )
{
    m_mainWorkflow->setCurrentFrame( newFrame );
}