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
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
    m_waitCond = new WaitCondition;
42
    m_initWaitCond = new WaitCondition;
43
44
    m_renderWaitCond = new WaitCondition;
    m_pausingStateWaitCond = new WaitCondition;
45
//    m_backBufferLock = new QReadWriteLock;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
46
47
48
49
}

ClipWorkflow::~ClipWorkflow()
{
50
51
//    delete[] m_backBuffer;
//    delete m_backBufferLock;
52
    delete m_pausingStateWaitCond;
53
    delete m_initWaitCond;
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
63
//    QReadLocker     lock( m_backBufferLock );
//    if ( m_usingBackBuffer == true )
64
        return m_buffer;
65
//    return m_backBuffer;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
66
67
}

68
69
70
void    ClipWorkflow::checkStateChange()
{
    QMutexLocker    lock( m_requiredStateLock );
71
    QWriteLocker    lock2( m_stateLock );
72
73
    if ( m_requiredState != ClipWorkflow::None )
    {
74
//        qDebug() << "Changed state from" << m_state << "to state" << m_requiredState;
75
76
        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
//    QReadLocker lock( cw->m_backBufferLock );
84

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

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

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

115
116
117
//            qDebug() << "Entering condwait";
        cw->m_waitCond->wait();
//            qDebug() << "Leaved condwait";
118
119
        cw->m_stateLock->lockForWrite();
        cw->m_state = Rendering;
120
121
122
123
//        {
//            QWriteLocker    lock2( cw->m_backBufferLock );
//            cw->m_usingBackBuffer = !cw->m_usingBackBuffer;
//        }
124
125
126
127
        cw->m_stateLock->unlock();
    }
    else if ( cw->m_state == Paused )
    {
128
//        qDebug() << "Forcing pause by pausing thread";
129
        cw->m_stateLock->unlock();
130
        cw->m_waitCond->wait();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
131
    }
132
133
    else
        cw->m_stateLock->unlock();
134
    cw->checkStateChange();
135
136
}

137
void    ClipWorkflow::setVmem()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
138
139
140
{
    char        buffer[32];

141
142
143
144
145
146
    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
147

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

151
    sprintf( buffer, ":vmem-height=%i", VIDEOHEIGHT );
152
    m_vlcMedia->addOption( buffer );
153
154

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

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

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

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

178
void    ClipWorkflow::pauseAfterPlaybackStarted()
179
{
180
181
    disconnect( m_mediaPlayer, SIGNAL( positionChanged() ), this, SLOT( pauseAfterPlaybackStarted() ) );
    disconnect( m_mediaPlayer, SIGNAL( playing() ), this, SLOT( pauseAfterPlaybackStarted() ) );
182

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

}

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

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

bool    ClipWorkflow::isEndReached() const
{
202
203
204
205
206
207
208
209
210
211
212
213
214
    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;
215
}
216
217
218

void    ClipWorkflow::startRender()
{
219
220
221
    if ( isReady() == false )
        m_initWaitCond->wait();

222
    m_mediaPlayer->play();
223
    setState( Rendering );
224
}
225

226
void    ClipWorkflow::clipEndReached()
227
{
228
    setState( EndReached );
229
}
230

231
Clip*     ClipWorkflow::getClip()
232
233
234
{
    return m_clip;
}
235
236
237

void            ClipWorkflow::stop()
{
238
239
240
    if ( m_mediaPlayer )
    {
        m_mediaPlayer->stop();
241
        Pool<LibVLCpp::MediaPlayer>::getInstance()->release( m_mediaPlayer );
242
243
244
245
246
        disconnect( m_mediaPlayer, SIGNAL( endReached() ), this, SLOT( clipEndReached() ) );
        m_mediaPlayer = NULL;
        setState( Stopped );
        QMutexLocker    lock( m_requiredStateLock );
        m_requiredState = ClipWorkflow::None;
247
        delete m_vlcMedia;
248
249
250
    }
    else
        qDebug() << "ClipWorkflow has already been stopped";
251
252
253
254
255
256
}

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

258
bool            ClipWorkflow::isRendering() const
259
{
260
261
    QReadLocker lock( m_stateLock );
    return m_state == ClipWorkflow::Rendering;
262
}
263

264
265
266
267
268
269
270
void            ClipWorkflow::checkSynchronisation( State newState )
{
    switch ( newState )
    {
        case Ready:
            m_initWaitCond->wake();
            break ;
271
272
273
        case Pausing:
            m_pausingStateWaitCond->wake();
            break ;
274
275
276
277
278
        default:
            break ;
    }
}

279
void            ClipWorkflow::setState( State state )
280
{
281
282
283
284
285
    {
        QWriteLocker    lock( m_stateLock );
        m_state = state;
    }
    checkSynchronisation( state );
286
}
287
288
289
290
291
292

void            ClipWorkflow::queryStateChange( State newState )
{
    QMutexLocker    lock( m_requiredStateLock );
    m_requiredState = newState;
}
293
294
295

void            ClipWorkflow::wake()
{
296
    m_waitCond->wake();
297
298
299
300
301
302
303
304
305
306
307
308
309
}

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

void            ClipWorkflow::reinitialize()
{
    QWriteLocker    lock( m_stateLock );
    m_state = Stopped;
    queryStateChange( None );
}
310
311
312
313

void            ClipWorkflow::pause()
{
    setState( Paused );
314
    m_mediaPlayer->pause();
315
316
    QMutexLocker    lock( m_requiredStateLock );
    m_requiredState = ClipWorkflow::None;
317
318
}

319
void            ClipWorkflow::unpause( bool wakeRenderThread /*= true*/ )
320
321
322
{
    //Since VLC will detect that the media player is paused and unpause it, we can do this safely
    setState( ClipWorkflow::Rendering );
323
324
325
326
327
    m_mediaPlayer->pause();
//    QMutexLocker    lock( m_requiredStateLock );
//    m_requiredState = ClipWorkflow::None;
    if ( wakeRenderThread == true )
        wake();
328
}
329
330
331
332
333
334

//void            ClipWorkflow::activateOneFrameOnly()
//{
//    qDebug() << "Activating one frame only";
//    m_oneFrameOnly = 1;
//}
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349

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

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

void        ClipWorkflow::waitForPausingState()
{
350
    if ( getState() != Pausing )
351
352
        m_pausingStateWaitCond->wait();
}