ClipWorkflow.cpp 10.2 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_backBufferLock = new QReadWriteLock;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
47
48
49
50
}

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

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

70
71
72
void    ClipWorkflow::checkStateChange()
{
    QMutexLocker    lock( m_requiredStateLock );
73
    QWriteLocker    lock2( m_stateLock );
74
75
    if ( m_requiredState != ClipWorkflow::None )
    {
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
76
//        qDebug() << "Changed state from" << m_state << "to state" << m_requiredState;
77
78
        m_state = m_requiredState;
        m_requiredState = ClipWorkflow::None;
79
        checkSynchronisation( m_state );
80
81
82
    }
}

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

87
88
89
90
91
92
//    if ( cw->m_usingBackBuffer )
//    {
//        *pp_ret = cw->m_backBuffer;
//    }
//    else
//    {
93
        *pp_ret = cw->m_buffer;
94
//    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
95
96
}

97
void    ClipWorkflow::unlock( ClipWorkflow* cw )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
98
{
99
    cw->m_stateLock->lockForWrite();
100

101
102
103
104
105
106
107
108
109
//    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 :(";
110
    if ( cw->m_state == Rendering )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
111
    {
112
113
        QMutexLocker    lock( cw->m_condMutex );

114
115
        cw->m_state = Sleeping;
        cw->m_stateLock->unlock();
116
        //Signal that render has been completed.
117
        cw->m_renderWaitCond->wake();
118
        cw->emit renderComplete( cw );
119

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

140
void    ClipWorkflow::setVmem()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
141
142
143
{
    char        buffer[32];

144
145
146
147
148
149
    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
150

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

154
    sprintf( buffer, ":vmem-height=%i", VIDEOHEIGHT );
155
    m_vlcMedia->addOption( buffer );
156
157

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

161
void    ClipWorkflow::initialize()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
162
{
163
    setState( Initializing );
164
    m_vlcMedia = new LibVLCpp::Media( "file://" + m_clip->getParent()->getFileInfo()->absoluteFilePath() );
165
    setVmem();
166
167
    m_mediaPlayer = Pool<LibVLCpp::MediaPlayer>::getInstance()->get();
    m_mediaPlayer->setMedia( m_vlcMedia );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
168

169
    connect( m_mediaPlayer, SIGNAL( playing() ), this, SLOT( setPositionAfterPlayback() ), Qt::DirectConnection );
170
    connect( m_mediaPlayer, SIGNAL( endReached() ), this, SLOT( clipEndReached() ), Qt::DirectConnection );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
171
172
173
    m_mediaPlayer->play();
}

174
void    ClipWorkflow::setPositionAfterPlayback()
175
{
176
    disconnect( m_mediaPlayer, SIGNAL( playing() ), this, SLOT( setPositionAfterPlayback() ) );
177
    connect( m_mediaPlayer, SIGNAL( positionChanged() ), this, SLOT( pauseAfterPlaybackStarted() ), Qt::DirectConnection );
178
179
180
    m_mediaPlayer->setPosition( m_clip->getBegin() );
}

181
void    ClipWorkflow::pauseAfterPlaybackStarted()
182
{
183
184
    disconnect( m_mediaPlayer, SIGNAL( positionChanged() ), this, SLOT( pauseAfterPlaybackStarted() ) );
    disconnect( m_mediaPlayer, SIGNAL( playing() ), this, SLOT( pauseAfterPlaybackStarted() ) );
185

186
    connect( m_mediaPlayer, SIGNAL( paused() ), this, SLOT( initializedMediaPlayer() ), Qt::DirectConnection );
187
188
189
190
    m_mediaPlayer->pause();

}

191
void    ClipWorkflow::initializedMediaPlayer()
192
{
193
    disconnect( m_mediaPlayer, SIGNAL( paused() ), this, SLOT( initializedMediaPlayer() ) );
194
    setState( Ready );
195
196
}

197
bool    ClipWorkflow::isReady() const
198
{
199
200
    QReadLocker lock( m_stateLock );
    return m_state == ClipWorkflow::Ready;
201
}
202

203
204
205
206
207
208
bool    ClipWorkflow::isPausing() const
{
    QReadLocker lock( m_stateLock );
    return m_state == ClipWorkflow::Pausing;
}

209
210
bool    ClipWorkflow::isEndReached() const
{
211
212
213
214
215
216
217
218
219
220
221
222
223
    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;
224
}
225
226
227

void    ClipWorkflow::startRender()
{
228
229
230
    if ( isReady() == false )
        m_initWaitCond->wait();

231
    m_mediaPlayer->play();
232
    setState( Rendering );
233
}
234

235
void    ClipWorkflow::clipEndReached()
236
{
237
    setState( EndReached );
238
}
239

240
Clip*     ClipWorkflow::getClip()
241
242
243
{
    return m_clip;
}
244
245
246

void            ClipWorkflow::stop()
{
247
248
249
    if ( m_mediaPlayer )
    {
        m_mediaPlayer->stop();
250
        Pool<LibVLCpp::MediaPlayer>::getInstance()->release( m_mediaPlayer );
251
252
253
254
255
        disconnect( m_mediaPlayer, SIGNAL( endReached() ), this, SLOT( clipEndReached() ) );
        m_mediaPlayer = NULL;
        setState( Stopped );
        QMutexLocker    lock( m_requiredStateLock );
        m_requiredState = ClipWorkflow::None;
256
        delete m_vlcMedia;
257
258
259
    }
    else
        qDebug() << "ClipWorkflow has already been stopped";
260
261
262
263
264
265
}

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

267
bool            ClipWorkflow::isRendering() const
268
{
269
270
    QReadLocker lock( m_stateLock );
    return m_state == ClipWorkflow::Rendering;
271
}
272

273
274
275
276
277
278
279
void            ClipWorkflow::checkSynchronisation( State newState )
{
    switch ( newState )
    {
        case Ready:
            m_initWaitCond->wake();
            break ;
280
281
282
        case Pausing:
            m_pausingStateWaitCond->wake();
            break ;
283
284
285
286
287
        default:
            break ;
    }
}

288
void            ClipWorkflow::setState( State state )
289
{
290
291
292
293
294
    {
        QWriteLocker    lock( m_stateLock );
        m_state = state;
    }
    checkSynchronisation( state );
295
}
296
297
298

void            ClipWorkflow::queryStateChange( State newState )
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
299
//    qDebug() << "Querying state change to" << newState;
300
301
302
    QMutexLocker    lock( m_requiredStateLock );
    m_requiredState = newState;
}
303
304
305

void            ClipWorkflow::wake()
{
306
    m_waitCond->wakeAll();
307
308
309
310
311
312
313
314
315
316
317
318
319
}

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

void            ClipWorkflow::reinitialize()
{
    QWriteLocker    lock( m_stateLock );
    m_state = Stopped;
    queryStateChange( None );
}
320
321
322
323

void            ClipWorkflow::pause()
{
    setState( Paused );
324
    m_mediaPlayer->pause();
325
326
    QMutexLocker    lock( m_requiredStateLock );
    m_requiredState = ClipWorkflow::None;
327
328
}

329
void            ClipWorkflow::unpause( bool wakeRenderThread /*= true*/ )
330
331
332
{
    //Since VLC will detect that the media player is paused and unpause it, we can do this safely
    setState( ClipWorkflow::Rendering );
333
334
335
336
337
    m_mediaPlayer->pause();
//    QMutexLocker    lock( m_requiredStateLock );
//    m_requiredState = ClipWorkflow::None;
    if ( wakeRenderThread == true )
        wake();
338
}
339
340
341
342
343
344

//void            ClipWorkflow::activateOneFrameOnly()
//{
//    qDebug() << "Activating one frame only";
//    m_oneFrameOnly = 1;
//}
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359

void        ClipWorkflow::waitForCompleteRender()
{
    if ( isRendering() == true )
        m_renderWaitCond->wait();
}

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

void        ClipWorkflow::waitForPausingState()
{
360
    if ( isPausing() == false )
361
362
        m_pausingStateWaitCond->wait();
}
363
364
365
366
367

QMutex*     ClipWorkflow::getSleepMutex()
{
    return m_condMutex;
}
368
369
370
371
372

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