ClipWorkflow.cpp 9.75 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
                m_clip( clip ),
31
                m_mediaPlayer(NULL),
32
                m_state( ClipWorkflow::Stopped ),
33
34
35
                m_requiredState( ClipWorkflow::None ),
                m_rendering( false ),
                m_initFlag( false )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
36
{
37
38
    m_stateLock = new QReadWriteLock;
    m_requiredStateLock = new QMutex;
39
40
    m_waitCond = new QWaitCondition;
    m_condMutex = new QMutex;
41
    m_initWaitCond = new WaitCondition;
42
43
    m_renderWaitCond = new WaitCondition;
    m_pausingStateWaitCond = new WaitCondition;
44
45
    m_renderLock = new QMutex;
    m_buffer = new unsigned char[VIDEOHEIGHT * VIDEOWIDTH * 4];
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
46
47
48
49
}

ClipWorkflow::~ClipWorkflow()
{
50
    delete m_renderLock;
51
    delete m_pausingStateWaitCond;
52
    delete m_initWaitCond;
53
    delete m_condMutex;
54
55
56
57
    delete m_waitCond;
    delete m_requiredStateLock;
    delete m_stateLock;
    delete[] m_buffer;
58
}
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
59

60
unsigned char*    ClipWorkflow::getOutput()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
61
{
62
    QMutexLocker    lock( m_renderLock );
63

64
65
    if ( isEndReached() == true )
        return NULL;
66
    return m_buffer;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
67
68
}

69
70
71
void    ClipWorkflow::checkStateChange()
{
    QMutexLocker    lock( m_requiredStateLock );
72
    QWriteLocker    lock2( m_stateLock );
73
74
75
76
    if ( m_requiredState != ClipWorkflow::None )
    {
        m_state = m_requiredState;
        m_requiredState = ClipWorkflow::None;
77
        checkSynchronisation( m_state );
78
79
80
    }
}

81
void    ClipWorkflow::lock( ClipWorkflow* cw, void** pp_ret )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
82
{
83
84
    cw->m_renderLock->lock();
    *pp_ret = cw->m_buffer;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
85
86
}

87
void    ClipWorkflow::unlock( ClipWorkflow* cw )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
88
{
89
    cw->m_renderLock->unlock();
90
    cw->m_stateLock->lockForWrite();
91

92
    if ( cw->m_state == Rendering )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
93
    {
94
95
        QMutexLocker    lock( cw->m_condMutex );

96
97
        cw->m_state = Sleeping;
        cw->m_stateLock->unlock();
98

99
        cw->m_renderWaitCond->wake();
100
        cw->emit renderComplete( cw );
101

102
        qDebug() << "Entering cond wait";
103
        cw->m_waitCond->wait( cw->m_condMutex );
104
        qDebug() << "Leaving condwait";
105
        cw->m_stateLock->lockForWrite();
106
107
        if ( cw->m_state == Sleeping )
            cw->m_state = Rendering;
108
        cw->m_stateLock->unlock();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
109
    }
110
111
    else
        cw->m_stateLock->unlock();
112
    cw->checkStateChange();
113
114
}

115
void    ClipWorkflow::setVmem()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
116
117
118
{
    char        buffer[32];

119
120
121
122
123
124
    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
125

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

129
    sprintf( buffer, ":vmem-height=%i", VIDEOHEIGHT );
130
    m_vlcMedia->addOption( buffer );
131
132

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

136
void    ClipWorkflow::initialize()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
137
{
138
    setState( Initializing );
139
    m_vlcMedia = new LibVLCpp::Media( "file://" + m_clip->getParent()->getFileInfo()->absoluteFilePath() );
140
    setVmem();
141
142
    m_mediaPlayer = Pool<LibVLCpp::MediaPlayer>::getInstance()->get();
    m_mediaPlayer->setMedia( m_vlcMedia );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
143

144
    connect( m_mediaPlayer, SIGNAL( playing() ), this, SLOT( setPositionAfterPlayback() ), Qt::DirectConnection );
145
    connect( m_mediaPlayer, SIGNAL( endReached() ), this, SLOT( clipEndReached() ), Qt::DirectConnection );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
146
147
148
    m_mediaPlayer->play();
}

149
//FIXME: this step is probably useless, due to modification in the TrackWorkflow
150
void    ClipWorkflow::setPositionAfterPlayback()
151
{
152
    disconnect( m_mediaPlayer, SIGNAL( playing() ), this, SLOT( setPositionAfterPlayback() ) );
153
    connect( m_mediaPlayer, SIGNAL( positionChanged() ), this, SLOT( pauseAfterPlaybackStarted() ), Qt::DirectConnection );
154

155
156
157
    m_mediaPlayer->setPosition( m_clip->getBegin() );
}

158
void    ClipWorkflow::pauseAfterPlaybackStarted()
159
{
160
    disconnect( m_mediaPlayer, SIGNAL( positionChanged() ), this, SLOT( pauseAfterPlaybackStarted() ) );
161
    //FIXME: it seems that this signal is never connected :o
162
    disconnect( m_mediaPlayer, SIGNAL( playing() ), this, SLOT( pauseAfterPlaybackStarted() ) );
163
    connect( m_mediaPlayer, SIGNAL( paused() ), this, SLOT( initializedMediaPlayer() ), Qt::DirectConnection );
164

165
    m_mediaPlayer->pause();
166
167
}

168
void    ClipWorkflow::initializedMediaPlayer()
169
{
170
    disconnect( m_mediaPlayer, SIGNAL( paused() ), this, SLOT( initializedMediaPlayer() ) );
171
    setState( Ready );
172
173
}

174
bool    ClipWorkflow::isReady() const
175
{
176
177
    QReadLocker lock( m_stateLock );
    return m_state == ClipWorkflow::Ready;
178
}
179
180
181

bool    ClipWorkflow::isEndReached() const
{
182
183
184
185
186
187
188
189
190
191
192
193
194
    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;
195
}
196

197
void    ClipWorkflow::startRender( bool startInPausedMode )
198
{
199
200
201
    if ( isReady() == false )
        m_initWaitCond->wait();

202
203
204
205
206
207
208
209
210
    if ( startInPausedMode == false )
    {
        m_mediaPlayer->play();
        setState( Rendering );
    }
    else
    {
        setState( Paused );
    }
211
}
212

213
void    ClipWorkflow::clipEndReached()
214
{
215
    setState( EndReached );
216
    emit renderComplete( this );
217
}
218

219
Clip*     ClipWorkflow::getClip()
220
221
222
{
    return m_clip;
}
223
224
225

void            ClipWorkflow::stop()
{
226
227
228
    if ( m_mediaPlayer )
    {
        m_mediaPlayer->stop();
229
        Pool<LibVLCpp::MediaPlayer>::getInstance()->release( m_mediaPlayer );
230
231
232
233
234
        disconnect( m_mediaPlayer, SIGNAL( endReached() ), this, SLOT( clipEndReached() ) );
        m_mediaPlayer = NULL;
        setState( Stopped );
        QMutexLocker    lock( m_requiredStateLock );
        m_requiredState = ClipWorkflow::None;
235
        delete m_vlcMedia;
236
237
        m_initFlag = false;
        m_rendering = false;
238
        qDebug() << "Clip workflow stopped";
239
240
241
    }
    else
        qDebug() << "ClipWorkflow has already been stopped";
242
243
244
245
246
247
}

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

249
bool            ClipWorkflow::isRendering() const
250
{
251
252
    QReadLocker lock( m_stateLock );
    return m_state == ClipWorkflow::Rendering;
253
}
254

255
256
257
258
259
260
bool            ClipWorkflow::isSleeping() const
{
    QReadLocker lock( m_stateLock );
    return m_state == ClipWorkflow::Sleeping;
}

261
262
263
264
265
266
267
268
269
270
271
272
void            ClipWorkflow::checkSynchronisation( State newState )
{
    switch ( newState )
    {
        case Ready:
            m_initWaitCond->wake();
            break ;
        default:
            break ;
    }
}

273
void            ClipWorkflow::setState( State state )
274
{
275
276
277
278
279
    {
        QWriteLocker    lock( m_stateLock );
        m_state = state;
    }
    checkSynchronisation( state );
280
}
281
282
283
284
285
286

void            ClipWorkflow::queryStateChange( State newState )
{
    QMutexLocker    lock( m_requiredStateLock );
    m_requiredState = newState;
}
287
288
289

void            ClipWorkflow::wake()
{
290
    m_waitCond->wakeAll();
291
292
293
294
295
296
297
298
299
300
301
302
303
}

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

void            ClipWorkflow::reinitialize()
{
    QWriteLocker    lock( m_stateLock );
    m_state = Stopped;
    queryStateChange( None );
}
304
305
306

void            ClipWorkflow::pause()
{
307
    connect( m_mediaPlayer, SIGNAL( paused() ), this, SLOT( pausedMediaPlayer() ), Qt::DirectConnection );
308
    setState( Pausing );
309
    m_mediaPlayer->pause();
310
311
    QMutexLocker    lock( m_requiredStateLock );
    m_requiredState = ClipWorkflow::None;
312

313
314
315
316
    {
        QMutexLocker    sync( m_condMutex );
        wake();
    }
317
318
}

319
void            ClipWorkflow::unpause()
320
{
321
    queryStateChange( ClipWorkflow::Rendering );
322
    connect( m_mediaPlayer, SIGNAL( playing() ), this, SLOT( unpausedMediaPlayer() ), Qt::DirectConnection );
323
    m_mediaPlayer->pause();
324
}
325

326
void        ClipWorkflow::waitForCompleteRender( bool dontCheckRenderStarted /*= false*/ )
327
{
328
    if ( isRendering() == true || dontCheckRenderStarted == true )
329
330
331
332
333
334
335
336
337
        m_renderWaitCond->wait();
}

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

338
339
340
341
QMutex*     ClipWorkflow::getSleepMutex()
{
    return m_condMutex;
}
342
343
344
345
346

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

void        ClipWorkflow::pausedMediaPlayer()
{
350
    disconnect( m_mediaPlayer, SIGNAL( paused() ), this, SLOT( pausedMediaPlayer() ) );
351
    setState( Paused );
352
353
354
355
356
357
358
    emit paused();
}

void        ClipWorkflow::unpausedMediaPlayer()
{
    disconnect( m_mediaPlayer, SIGNAL( playing() ), this, SLOT( unpausedMediaPlayer() ) );
    emit unpaused();
359
}