ClipWorkflow.cpp 8.92 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
    delete m_waitCond;
    delete m_requiredStateLock;
    delete m_stateLock;
57
    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
    return m_buffer;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
65
66
}

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

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

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

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

94
95
        cw->m_state = Sleeping;
        cw->m_stateLock->unlock();
96

97
        cw->m_renderWaitCond->wake();
98
        cw->emit renderComplete( cw );
99

100
        cw->m_waitCond->wait( cw->m_condMutex );
101
102
        cw->m_stateLock->lockForWrite();
        cw->m_state = Rendering;
103
104
105
106
        cw->m_stateLock->unlock();
    }
    else
        cw->m_stateLock->unlock();
107
    cw->checkStateChange();
108
109
}

110
void    ClipWorkflow::setVmem()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
111
112
113
{
    char        buffer[32];

114
115
116
117
118
119
    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
120

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

124
    sprintf( buffer, ":vmem-height=%i", VIDEOHEIGHT );
125
    m_vlcMedia->addOption( buffer );
126
127

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

131
void    ClipWorkflow::initialize()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
132
{
133
    setState( Initializing );
134
    m_vlcMedia = new LibVLCpp::Media( m_clip->getParent()->getFileInfo()->absoluteFilePath() );
135
    setVmem();
136
137
    m_mediaPlayer = Pool<LibVLCpp::MediaPlayer>::getInstance()->get();
    m_mediaPlayer->setMedia( m_vlcMedia );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
138

139
    connect( m_mediaPlayer, SIGNAL( playing() ), this, SLOT( setPositionAfterPlayback() ), Qt::DirectConnection );
140
    connect( m_mediaPlayer, SIGNAL( endReached() ), this, SLOT( clipEndReached() ), Qt::DirectConnection );
141
    connect( m_mediaPlayer, SIGNAL( paused() ), this, SLOT( pausedMediaPlayer() ), Qt::DirectConnection );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
142
143
144
    m_mediaPlayer->play();
}

145
void    ClipWorkflow::setPositionAfterPlayback()
146
{
147
    disconnect( m_mediaPlayer, SIGNAL( playing() ), this, SLOT( setPositionAfterPlayback() ) );
148
    connect( m_mediaPlayer, SIGNAL( positionChanged() ), this, SLOT( pauseAfterPlaybackStarted() ), Qt::DirectConnection );
149
150
151
    m_mediaPlayer->setPosition( m_clip->getBegin() );
}

152
void    ClipWorkflow::pauseAfterPlaybackStarted()
153
{
154
155
    disconnect( m_mediaPlayer, SIGNAL( positionChanged() ), this, SLOT( pauseAfterPlaybackStarted() ) );
    disconnect( m_mediaPlayer, SIGNAL( playing() ), this, SLOT( pauseAfterPlaybackStarted() ) );
156

157
    connect( m_mediaPlayer, SIGNAL( paused() ), this, SLOT( initializedMediaPlayer() ), Qt::DirectConnection );
158
159
160
161
    m_mediaPlayer->pause();

}

162
void    ClipWorkflow::initializedMediaPlayer()
163
{
164
    disconnect( m_mediaPlayer, SIGNAL( paused() ), this, SLOT( initializedMediaPlayer() ) );
165
    setState( Ready );
166
167
}

168
bool    ClipWorkflow::isReady() const
169
{
170
171
    QReadLocker lock( m_stateLock );
    return m_state == ClipWorkflow::Ready;
172
}
173
174
175

bool    ClipWorkflow::isEndReached() const
{
176
177
178
179
180
181
182
183
184
185
186
187
188
    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;
189
}
190
191
192

void    ClipWorkflow::startRender()
{
193
194
195
    if ( isReady() == false )
        m_initWaitCond->wait();

196
    m_mediaPlayer->play();
197
    setState( Rendering );
198
}
199

200
void    ClipWorkflow::clipEndReached()
201
{
202
    setState( EndReached );
203
}
204

205
Clip*     ClipWorkflow::getClip()
206
207
208
{
    return m_clip;
}
209
210
211

void            ClipWorkflow::stop()
{
212
213
214
    if ( m_mediaPlayer )
    {
        m_mediaPlayer->stop();
215
        Pool<LibVLCpp::MediaPlayer>::getInstance()->release( m_mediaPlayer );
216
217
218
219
220
        disconnect( m_mediaPlayer, SIGNAL( endReached() ), this, SLOT( clipEndReached() ) );
        m_mediaPlayer = NULL;
        setState( Stopped );
        QMutexLocker    lock( m_requiredStateLock );
        m_requiredState = ClipWorkflow::None;
221
        delete m_vlcMedia;
222
223
        m_initFlag = false;
        m_rendering = false;
224
225
226
    }
    else
        qDebug() << "ClipWorkflow has already been stopped";
227
228
229
230
231
232
}

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

234
bool            ClipWorkflow::isRendering() const
235
{
236
237
    QReadLocker lock( m_stateLock );
    return m_state == ClipWorkflow::Rendering;
238
}
239

240
241
242
243
244
245
246
247
248
249
250
251
void            ClipWorkflow::checkSynchronisation( State newState )
{
    switch ( newState )
    {
        case Ready:
            m_initWaitCond->wake();
            break ;
        default:
            break ;
    }
}

252
void            ClipWorkflow::setState( State state )
253
{
254
255
    {
        QWriteLocker    lock( m_stateLock );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
256
//        qDebug() << "Changing from state" << m_state << "to state" << state;
257
258
259
        m_state = state;
    }
    checkSynchronisation( state );
260
}
261
262
263

void            ClipWorkflow::queryStateChange( State newState )
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
264
//    qDebug() << "Querying state change to" << newState;
265
266
267
    QMutexLocker    lock( m_requiredStateLock );
    m_requiredState = newState;
}
268
269
270

void            ClipWorkflow::wake()
{
271
    m_waitCond->wakeAll();
272
273
274
275
276
277
278
279
280
281
282
283
284
}

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

void            ClipWorkflow::reinitialize()
{
    QWriteLocker    lock( m_stateLock );
    m_state = Stopped;
    queryStateChange( None );
}
285
286
287

void            ClipWorkflow::pause()
{
288
    setState( Pausing );
289
    m_mediaPlayer->pause();
290
291
    QMutexLocker    lock( m_requiredStateLock );
    m_requiredState = ClipWorkflow::None;
292
293
294
295
296
297
    QReadLocker    lock2( m_stateLock );
    if ( m_state == Sleeping )
    {
        QMutexLocker    sync( m_condMutex );
        wake();
    }
298
299
}

300
void            ClipWorkflow::unpause()
301
{
302
    queryStateChange( ClipWorkflow::Rendering );
303
    m_mediaPlayer->pause();
304
}
305

306
void        ClipWorkflow::waitForCompleteRender( bool dontCheckRenderStarted /*= false*/ )
307
{
308
    if ( isRendering() == true || dontCheckRenderStarted == true )
309
310
311
312
313
314
315
316
317
        m_renderWaitCond->wait();
}

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

318
319
320
321
QMutex*     ClipWorkflow::getSleepMutex()
{
    return m_condMutex;
}
322
323
324
325
326

LibVLCpp::MediaPlayer*       ClipWorkflow::getMediaPlayer()
{
    return m_mediaPlayer;
}
327
328
329
330
331

void        ClipWorkflow::pausedMediaPlayer()
{
    setState( Paused );
}