ClipWorkflow.cpp 11.3 KB
Newer Older
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*****************************************************************************
 * ClipWorkflow.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.
 *****************************************************************************/

23
#include <QtDebug>
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
24

25
#include "vlmc.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
26
#include "ClipWorkflow.h"
27
#include "Pool.hpp"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
28

29
ClipWorkflow::ClipWorkflow( Clip::Clip* clip ) :
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
30
31
                m_clip( clip ),
                m_buffer( NULL ),
32
                //m_usingBackBuffer( false ),
33
                m_mediaPlayer(NULL),
34
35
                m_state( ClipWorkflow::Stopped ),
                m_requiredState( ClipWorkflow::None )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
36
{
37
    m_buffer = new unsigned char[VIDEOHEIGHT * VIDEOWIDTH * 4];
38
//    m_backBuffer = new unsigned char[VIDEOHEIGHT * VIDEOWIDTH * 4];
39
40
    m_stateLock = new QReadWriteLock;
    m_requiredStateLock = new QMutex;
41
42
    m_waitCond = new QWaitCondition;
    m_condMutex = new QMutex;
43
    m_initWaitCond = new WaitCondition;
44
45
    m_renderWaitCond = new WaitCondition;
    m_pausingStateWaitCond = new WaitCondition;
46
    m_pausedThreadCondWait = new WaitCondition;
47
//    m_backBufferLock = new QReadWriteLock;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
48
49
50
51
}

ClipWorkflow::~ClipWorkflow()
{
52
53
//    delete[] m_backBuffer;
//    delete m_backBufferLock;
54
    delete m_pausedThreadCondWait;
55
    delete m_pausingStateWaitCond;
56
    delete m_initWaitCond;
57
    delete m_condMutex;
58
59
60
61
    delete m_waitCond;
    delete m_requiredStateLock;
    delete m_stateLock;
    delete[] m_buffer;
62
}
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
63

64
unsigned char*    ClipWorkflow::getOutput()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
65
{
66
67
//    QReadLocker     lock( m_backBufferLock );
//    if ( m_usingBackBuffer == true )
68
        return m_buffer;
69
//    return m_backBuffer;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
70
71
}

72
73
74
void    ClipWorkflow::checkStateChange()
{
    QMutexLocker    lock( m_requiredStateLock );
75
    QWriteLocker    lock2( m_stateLock );
76
77
    if ( m_requiredState != ClipWorkflow::None )
    {
78
        qDebug() << "Changed state from" << m_state << "to state" << m_requiredState;
79
80
        m_state = m_requiredState;
        m_requiredState = ClipWorkflow::None;
81
        checkSynchronisation( m_state );
82
83
84
    }
}

85
void    ClipWorkflow::lock( ClipWorkflow* cw, void** pp_ret )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
86
{
87
//    QReadLocker lock( cw->m_backBufferLock );
88

89
90
91
92
93
94
//    if ( cw->m_usingBackBuffer )
//    {
//        *pp_ret = cw->m_backBuffer;
//    }
//    else
//    {
95
        *pp_ret = cw->m_buffer;
96
        qDebug() << "Clip workflow locking <<<<<<<<<<<<<<<<<<<<<<<<<<";
97
//    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
98
99
}

100
void    ClipWorkflow::unlock( ClipWorkflow* cw )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
101
{
102
    qDebug() << "ClipWorkflow::unlock()";
103
    cw->m_stateLock->lockForWrite();
104

105
106
107
108
109
110
111
112
113
//    if ( cw->m_oneFrameOnly )
//    {
//        qDebug() << "One frame only mode is ON :)";
//        //Forcing pause after rendering a frame
//        cw->m_oneFrameOnly = 0;
//        cw->m_state = Paused;
//    }
//    else
//        qDebug() << "One frame only mode is OFF :(";
114
    if ( cw->m_state == Rendering )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
115
    {
116
117
        QMutexLocker    lock( cw->m_condMutex );

118
119
        cw->m_state = Sleeping;
        cw->m_stateLock->unlock();
120
        //Signal that render has been completed.
121
        cw->m_renderWaitCond->wake();
122
        qDebug() << "Clip render completed";
123
        cw->emit renderComplete( cw );
124

125
//        qDebug() << "Entering condwait";
126
        cw->m_waitCond->wait( cw->m_condMutex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
127
//        qDebug() << "Leaved condwait";
128
129
        cw->m_stateLock->lockForWrite();
        cw->m_state = Rendering;
130
131
132
133
//        {
//            QWriteLocker    lock2( cw->m_backBufferLock );
//            cw->m_usingBackBuffer = !cw->m_usingBackBuffer;
//        }
134
135
136
137
        cw->m_stateLock->unlock();
    }
    else if ( cw->m_state == Paused )
    {
138
139
        QMutexLocker    lock( cw->m_condMutex );

140
        cw->m_stateLock->unlock();
141
142
143
        qDebug() << "Entering forced pause condwait";
        cw->setState( ClipWorkflow::ThreadPaused );
        cw->m_pausedThreadCondWait->wake();
144
        cw->m_waitCond->wait( cw->m_condMutex );
145
146
        qDebug() << "Leaving forced pause condwait";
        cw->setState( ClipWorkflow::Paused );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
147
    }
148
149
    else
        cw->m_stateLock->unlock();
150
    qDebug() << "End of the ClipWorkflow::unlock()";
151
    cw->checkStateChange();
152
153
}

154
void    ClipWorkflow::setVmem()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
155
156
157
{
    char        buffer[32];

158
159
160
161
162
163
    m_vlcMedia->addOption( ":no-audio" );
    m_vlcMedia->addOption( ":vout=vmem" );
    m_vlcMedia->setDataCtx( this );
    m_vlcMedia->setLockCallback( reinterpret_cast<LibVLCpp::Media::lockCallback>( &ClipWorkflow::lock ) );
    m_vlcMedia->setUnlockCallback( reinterpret_cast<LibVLCpp::Media::unlockCallback>( &ClipWorkflow::unlock ) );
    m_vlcMedia->addOption( ":vmem-chroma=RV24" );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
164

165
    sprintf( buffer, ":vmem-width=%i", VIDEOWIDTH );
166
    m_vlcMedia->addOption( buffer );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
167

168
    sprintf( buffer, ":vmem-height=%i", VIDEOHEIGHT );
169
    m_vlcMedia->addOption( buffer );
170
171

    sprintf( buffer, "vmem-pitch=%i", VIDEOWIDTH * 3 );
172
    m_vlcMedia->addOption( buffer );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
173
174
}

175
void    ClipWorkflow::initialize()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
176
{
177
    setState( Initializing );
178
    m_vlcMedia = new LibVLCpp::Media( m_clip->getParent()->getFileInfo()->absoluteFilePath() );
179
    setVmem();
180
181
    m_mediaPlayer = Pool<LibVLCpp::MediaPlayer>::getInstance()->get();
    m_mediaPlayer->setMedia( m_vlcMedia );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
182

183
    connect( m_mediaPlayer, SIGNAL( playing() ), this, SLOT( setPositionAfterPlayback() ), Qt::DirectConnection );
184
    connect( m_mediaPlayer, SIGNAL( endReached() ), this, SLOT( clipEndReached() ), Qt::DirectConnection );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
185
186
187
    m_mediaPlayer->play();
}

188
void    ClipWorkflow::setPositionAfterPlayback()
189
{
190
    disconnect( m_mediaPlayer, SIGNAL( playing() ), this, SLOT( setPositionAfterPlayback() ) );
191
    connect( m_mediaPlayer, SIGNAL( positionChanged() ), this, SLOT( pauseAfterPlaybackStarted() ), Qt::DirectConnection );
192
193
194
    m_mediaPlayer->setPosition( m_clip->getBegin() );
}

195
void    ClipWorkflow::pauseAfterPlaybackStarted()
196
{
197
198
    disconnect( m_mediaPlayer, SIGNAL( positionChanged() ), this, SLOT( pauseAfterPlaybackStarted() ) );
    disconnect( m_mediaPlayer, SIGNAL( playing() ), this, SLOT( pauseAfterPlaybackStarted() ) );
199

200
    connect( m_mediaPlayer, SIGNAL( paused() ), this, SLOT( initializedMediaPlayer() ), Qt::DirectConnection );
201
202
203
204
    m_mediaPlayer->pause();

}

205
void    ClipWorkflow::initializedMediaPlayer()
206
{
207
    disconnect( m_mediaPlayer, SIGNAL( paused() ), this, SLOT( initializedMediaPlayer() ) );
208
    setState( Ready );
209
210
}

211
bool    ClipWorkflow::isReady() const
212
{
213
214
    QReadLocker lock( m_stateLock );
    return m_state == ClipWorkflow::Ready;
215
}
216

217
218
219
220
221
222
bool    ClipWorkflow::isPausing() const
{
    QReadLocker lock( m_stateLock );
    return m_state == ClipWorkflow::Pausing;
}

223
224
225
226
227
228
bool    ClipWorkflow::isThreadPaused() const
{
    QReadLocker lock( m_stateLock );
    return m_state == ClipWorkflow::ThreadPaused;
}

229
230
bool    ClipWorkflow::isEndReached() const
{
231
232
233
234
235
236
237
238
239
240
241
242
243
    QReadLocker lock( m_stateLock );
    return m_state == ClipWorkflow::EndReached;
}

bool    ClipWorkflow::isStopped() const
{
    QReadLocker lock( m_stateLock );
    return m_state == ClipWorkflow::Stopped;
}

ClipWorkflow::State     ClipWorkflow::getState() const
{
    return m_state;
244
}
245
246
247

void    ClipWorkflow::startRender()
{
248
249
250
    if ( isReady() == false )
        m_initWaitCond->wait();

251
    m_mediaPlayer->play();
252
    setState( Rendering );
253
}
254

255
void    ClipWorkflow::clipEndReached()
256
{
257
    setState( EndReached );
258
}
259

260
Clip*     ClipWorkflow::getClip()
261
262
263
{
    return m_clip;
}
264
265
266

void            ClipWorkflow::stop()
{
267
268
269
    if ( m_mediaPlayer )
    {
        m_mediaPlayer->stop();
270
        Pool<LibVLCpp::MediaPlayer>::getInstance()->release( m_mediaPlayer );
271
272
273
274
275
        disconnect( m_mediaPlayer, SIGNAL( endReached() ), this, SLOT( clipEndReached() ) );
        m_mediaPlayer = NULL;
        setState( Stopped );
        QMutexLocker    lock( m_requiredStateLock );
        m_requiredState = ClipWorkflow::None;
276
        delete m_vlcMedia;
277
278
279
    }
    else
        qDebug() << "ClipWorkflow has already been stopped";
280
281
282
283
284
285
}

void            ClipWorkflow::setPosition( float pos )
{
    m_mediaPlayer->setPosition( pos );
}
286

287
bool            ClipWorkflow::isRendering() const
288
{
289
290
    QReadLocker lock( m_stateLock );
    return m_state == ClipWorkflow::Rendering;
291
}
292

293
294
295
296
297
298
299
void            ClipWorkflow::checkSynchronisation( State newState )
{
    switch ( newState )
    {
        case Ready:
            m_initWaitCond->wake();
            break ;
300
301
302
        case Pausing:
            m_pausingStateWaitCond->wake();
            break ;
303
304
305
306
307
        default:
            break ;
    }
}

308
void            ClipWorkflow::setState( State state )
309
{
310
311
312
313
314
    {
        QWriteLocker    lock( m_stateLock );
        m_state = state;
    }
    checkSynchronisation( state );
315
}
316
317
318

void            ClipWorkflow::queryStateChange( State newState )
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
319
//    qDebug() << "Querying state change to" << newState;
320
321
322
    QMutexLocker    lock( m_requiredStateLock );
    m_requiredState = newState;
}
323
324
325

void            ClipWorkflow::wake()
{
326
    m_waitCond->wakeAll();
327
328
329
330
331
332
333
334
335
336
337
338
339
}

QReadWriteLock* ClipWorkflow::getStateLock()
{
    return m_stateLock;
}

void            ClipWorkflow::reinitialize()
{
    QWriteLocker    lock( m_stateLock );
    m_state = Stopped;
    queryStateChange( None );
}
340
341
342
343

void            ClipWorkflow::pause()
{
    setState( Paused );
344
    m_mediaPlayer->pause();
345
346
    QMutexLocker    lock( m_requiredStateLock );
    m_requiredState = ClipWorkflow::None;
347
348
}

349
void            ClipWorkflow::unpause( bool wakeRenderThread /*= true*/ )
350
351
352
{
    //Since VLC will detect that the media player is paused and unpause it, we can do this safely
    setState( ClipWorkflow::Rendering );
353
354
355
356
    m_mediaPlayer->pause();
//    QMutexLocker    lock( m_requiredStateLock );
//    m_requiredState = ClipWorkflow::None;
    if ( wakeRenderThread == true )
357
358
    {
        qDebug() << "Unpausing and waking thread";
359
        wake();
360
    }
361
}
362
363
364
365
366
367

//void            ClipWorkflow::activateOneFrameOnly()
//{
//    qDebug() << "Activating one frame only";
//    m_oneFrameOnly = 1;
//}
368

369
void        ClipWorkflow::waitForCompleteRender( bool dontCheckRenderStarted /*= false*/ )
370
{
371
    if ( isRendering() == true || dontCheckRenderStarted == true )
372
        m_renderWaitCond->wait();
373
374
    else
        qDebug() << "waitForCompleteRender(), not rendering. State == " << getState();
375
376
377
378
379
380
381
382
383
384
}

void        ClipWorkflow::waitForCompleteInit()
{
    if ( isReady() == false )
        m_initWaitCond->wait();
}

void        ClipWorkflow::waitForPausingState()
{
385
    if ( isPausing() == false )
386
387
        m_pausingStateWaitCond->wait();
}
388

389
390
391
392
393
394
void        ClipWorkflow::waitForPausedThread()
{
    if ( isThreadPaused() == false )
        m_pausedThreadCondWait->wait();
}

395
396
397
398
QMutex*     ClipWorkflow::getSleepMutex()
{
    return m_condMutex;
}
399
400
401
402
403

LibVLCpp::MediaPlayer*       ClipWorkflow::getMediaPlayer()
{
    return m_mediaPlayer;
}