WorkflowRenderer.cpp 8.56 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 42 43 44 45
    m_media = new LibVLCpp::Media( "fake://" );

    sprintf( buffer, ":invmem-width=%i", VIDEOWIDTH );
    m_media->addOption( ":codec=invmem" );
    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
    m_media->addOption( ":no-audio" );
57

58 59
    m_condMutex = new QMutex;
    m_waitCond = new QWaitCondition;
60 61 62 63 64 65 66 67 68

        //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() ) );

69 70
}

71

72
WorkflowRenderer::~WorkflowRenderer()
73
{
74
    stop();
75

76
    //FIXME this is probably useless...
77 78 79 80 81 82
    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 ) ) );

83
    delete m_actionsLock;
84
    delete m_media;
85 86
    delete m_condMutex;
    delete m_waitCond;
87 88
}

89
void*   WorkflowRenderer::lock( void* datas )
90
{
91
    WorkflowRenderer* self = reinterpret_cast<WorkflowRenderer*>( datas );
92

93 94 95
    if ( self->m_stopping == false )
    {
        void* ret = self->m_mainWorkflow->getSynchroneOutput();
96
        self->m_lastFrame = static_cast<MainWorkflow::OutputBuffers*>( ret );
97
    }
98
    return self->m_lastFrame->video;
99 100
}

101
void    WorkflowRenderer::unlock( void* datas )
102
{
103
    WorkflowRenderer* self = reinterpret_cast<WorkflowRenderer*>( datas );
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    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:
120 121 122
                if ( m_pauseAsked == true )
                    continue ;
                m_pauseAsked = true;
123 124
//                m_mediaPlayer->pause();
                pauseMainWorkflow();
125 126 127 128 129 130 131
                //This will also pause the MainWorkflow via a signal/slot
                break ;
            default:
                qDebug() << "Unhandled action:" << act;
                break ;
        }
    }
132 133
}

134
void        WorkflowRenderer::startPreview()
135
{
136 137
    if ( m_mainWorkflow->getLength() <= 0 )
        return ;
138 139 140 141 142 143 144 145 146
    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 ) ) );

147
    m_mainWorkflow->setFullSpeedRender( false );
148 149
    m_mainWorkflow->startRender();
    m_mediaPlayer->play();
150
    m_isRendering = true;
151
    m_paused = false;
152 153
    m_stopping = false;
    m_lastFrame = NULL;
154 155
}

156
void        WorkflowRenderer::setPosition( float newPos )
157
{
158
    m_mainWorkflow->setPosition( newPos );
159 160
}

161 162
void        WorkflowRenderer::nextFrame()
{
163
    m_mainWorkflow->nextFrame();
164 165
}

166
void        WorkflowRenderer::previousFrame()
167
{
168
    m_mainWorkflow->previousFrame();
169 170
}

171 172
void        WorkflowRenderer::pauseMainWorkflow()
{
173 174
    if ( m_paused == true )
        return ;
175 176

    QMutexLocker    lock( m_condMutex );
177
    m_mainWorkflow->pause();
178
    m_waitCond->wait( m_condMutex );
179 180
}

181 182 183 184 185 186 187
void        WorkflowRenderer::unpauseMainWorkflow()
{
    if ( m_paused == false )
        return ;
    m_mainWorkflow->unpause();
}

188 189
void        WorkflowRenderer::mainWorkflowPaused()
{
190
    m_paused = true;
191
    m_pauseAsked = false;
192 193 194 195
    {
        QMutexLocker    lock( m_condMutex );
    }
    m_waitCond->wakeAll();
196
    emit paused();
197 198
}

199 200 201 202 203 204 205
void        WorkflowRenderer::mainWorkflowUnpaused()
{
    m_paused = false;
    m_unpauseAsked = false;
    emit playing();
}

206
void        WorkflowRenderer::togglePlayPause( bool forcePause )
207
{
208
    if ( m_isRendering == false && forcePause == false )
209
        startPreview();
210 211 212 213 214 215 216 217
    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 )
218
    {
219 220
        if ( m_paused == true && forcePause == false )
        {
221 222 223
            if ( m_paused == true )
            {
                m_unpauseAsked = true;
224
                unpauseMainWorkflow();
225
            }
226
        }
227
        else
228
        {
229 230
            if ( m_paused == false )
            {
231 232
                QWriteLocker        lock( m_actionsLock );
                m_actions.push( Pause );
233
            }
234
        }
235
    }
236
}
237

238
void        WorkflowRenderer::stop()
239 240
{
    m_isRendering = false;
241
    m_paused = false;
242
    m_pauseAsked = false;
243
    m_unpauseAsked = false;
244
    m_stopping = true;
245
    m_mainWorkflow->cancelSynchronisation();
246
    m_mediaPlayer->stop();
247
    m_mainWorkflow->stop();
248 249
}

250 251 252 253
/////////////////////////////////////////////////////////////////////
/////SLOTS :
/////////////////////////////////////////////////////////////////////

254
void        WorkflowRenderer::__endReached()
255
{
256
    stop();
257
    emit endReached();
258 259
}

260
void        WorkflowRenderer::__positionChanged()
261
{
262 263 264
    qFatal("This should never be used ! Get out of here !");
}

265
void        WorkflowRenderer::__positionChanged( float pos )
266 267
{
    emit positionChanged( pos );
268 269
}

270
void        WorkflowRenderer::__videoPaused()
271
{
272 273
    if ( m_pauseAsked == true )
        pauseMainWorkflow();
274 275
}

276
void        WorkflowRenderer::__videoPlaying()
277
{
278 279 280 281 282 283 284
    if ( m_unpauseAsked == true )
        unpauseMainWorkflow();
    else
    {
        m_paused = false;
        emit playing();
    }
285
}
286

287
void        WorkflowRenderer::__videoStopped()
288 289 290
{
    emit endReached();
}
291 292 293 294 295

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