Commit ad9a3174 authored by Hugo Beauzee-Luyssen's avatar Hugo Beauzee-Luyssen

Reverting one month of crap.

parent 1500023d
......@@ -34,8 +34,6 @@ ClipWorkflow::ClipWorkflow( Clip::Clip* clip ) :
m_rendering( false ),
m_initFlag( false )
{
for ( unsigned int i = 0; i < 5; ++i )
m_availableBuffers.enqueue( new unsigned char[VIDEOHEIGHT * VIDEOWIDTH * 4] );
m_stateLock = new QReadWriteLock;
m_requiredStateLock = new QMutex;
m_waitCond = new QWaitCondition;
......@@ -43,43 +41,27 @@ ClipWorkflow::ClipWorkflow( Clip::Clip* clip ) :
m_initWaitCond = new WaitCondition;
m_renderWaitCond = new WaitCondition;
m_pausingStateWaitCond = new WaitCondition;
m_pausedThreadCondWait = new WaitCondition;
m_buffersLock = new QMutex;
m_renderLock = new QMutex;
m_buffer = new unsigned char[VIDEOHEIGHT * VIDEOWIDTH * 4];
}
ClipWorkflow::~ClipWorkflow()
{
delete m_buffersLock;
delete m_pausedThreadCondWait;
delete m_renderLock;
delete m_pausingStateWaitCond;
delete m_initWaitCond;
delete m_condMutex;
delete m_waitCond;
delete m_requiredStateLock;
delete m_stateLock;
while ( m_buffers.empty() == false )
delete[] m_buffers.dequeue();
while ( m_availableBuffers.empty() == false )
delete[] m_availableBuffers.dequeue();
delete[] m_buffer;
}
unsigned char* ClipWorkflow::getOutput()
{
QMutexLocker lock( m_buffersLock );
QMutexLocker lock( m_renderLock );
if ( m_mediaPlayer->isPlaying() == false )
{
qDebug() << "Generating in same buffer as last time";
return m_buffers.head();
}
if ( m_buffers.isEmpty() == true )
return NULL;
unsigned char* buff = m_buffers.dequeue();
m_availableBuffers.enqueue( buff );
qDebug() << "Getting output. Buffers... Available:" << m_availableBuffers.size() << "Used:" << m_buffers.size();
return buff;
return m_buffer;
}
void ClipWorkflow::checkStateChange()
......@@ -96,30 +78,13 @@ void ClipWorkflow::checkStateChange()
void ClipWorkflow::lock( ClipWorkflow* cw, void** pp_ret )
{
qDebug() << "Buffers... Available:" << cw->m_availableBuffers.size() << "Used:" << cw->m_buffers.size();
cw->m_buffersLock->lock();
unsigned char* buff;
if ( cw->m_mediaPlayer->isPlaying() == false )
{
if ( cw->m_buffers.size() > 0 )
*pp_ret = cw->m_buffers.last();
else
*pp_ret = cw->m_availableBuffers.head();
qDebug() << "Using same buffer than last time";
return ;
}
qDebug() << "New frame";
if ( cw->m_availableBuffers.size() > 0 )
buff = cw->m_availableBuffers.dequeue();
else
buff = new unsigned char[VIDEOHEIGHT * VIDEOWIDTH * 4];
cw->m_buffers.enqueue( buff );
*pp_ret = buff;
cw->m_renderLock->lock();
*pp_ret = cw->m_buffer;
}
void ClipWorkflow::unlock( ClipWorkflow* cw )
{
cw->m_buffersLock->unlock();
cw->m_renderLock->unlock();
cw->m_stateLock->lockForWrite();
if ( cw->m_state == Rendering )
......@@ -137,16 +102,6 @@ void ClipWorkflow::unlock( ClipWorkflow* cw )
cw->m_state = Rendering;
cw->m_stateLock->unlock();
}
else if ( cw->m_state == Paused )
{
QMutexLocker lock( cw->m_condMutex );
cw->m_stateLock->unlock();
cw->setState( ClipWorkflow::ThreadPaused );
cw->m_pausedThreadCondWait->wake();
cw->m_waitCond->wait( cw->m_condMutex );
cw->setState( ClipWorkflow::Paused );
}
else
cw->m_stateLock->unlock();
cw->checkStateChange();
......@@ -183,6 +138,7 @@ void ClipWorkflow::initialize()
connect( m_mediaPlayer, SIGNAL( playing() ), this, SLOT( setPositionAfterPlayback() ), Qt::DirectConnection );
connect( m_mediaPlayer, SIGNAL( endReached() ), this, SLOT( clipEndReached() ), Qt::DirectConnection );
connect( m_mediaPlayer, SIGNAL( paused() ), this, SLOT( pausedMediaPlayer() ), Qt::DirectConnection );
m_mediaPlayer->play();
}
......@@ -215,18 +171,6 @@ bool ClipWorkflow::isReady() const
return m_state == ClipWorkflow::Ready;
}
bool ClipWorkflow::isPausing() const
{
QReadLocker lock( m_stateLock );
return m_state == ClipWorkflow::Pausing;
}
bool ClipWorkflow::isThreadPaused() const
{
QReadLocker lock( m_stateLock );
return m_state == ClipWorkflow::ThreadPaused;
}
bool ClipWorkflow::isEndReached() const
{
QReadLocker lock( m_stateLock );
......@@ -300,9 +244,6 @@ void ClipWorkflow::checkSynchronisation( State newState )
case Ready:
m_initWaitCond->wake();
break ;
case Pausing:
m_pausingStateWaitCond->wake();
break ;
default:
break ;
}
......@@ -344,20 +285,22 @@ void ClipWorkflow::reinitialize()
void ClipWorkflow::pause()
{
setState( Paused );
setState( Pausing );
m_mediaPlayer->pause();
QMutexLocker lock( m_requiredStateLock );
m_requiredState = ClipWorkflow::None;
QReadLocker lock2( m_stateLock );
if ( m_state == Sleeping )
{
QMutexLocker sync( m_condMutex );
wake();
}
}
void ClipWorkflow::unpause( bool wakeRenderThread /*= true*/ )
void ClipWorkflow::unpause()
{
queryStateChange( ClipWorkflow::Rendering );
m_mediaPlayer->pause();
if ( wakeRenderThread == true )
{
wake();
}
}
void ClipWorkflow::waitForCompleteRender( bool dontCheckRenderStarted /*= false*/ )
......@@ -372,18 +315,6 @@ void ClipWorkflow::waitForCompleteInit()
m_initWaitCond->wait();
}
void ClipWorkflow::waitForPausingState()
{
if ( isPausing() == false )
m_pausingStateWaitCond->wait();
}
void ClipWorkflow::waitForPausedThread()
{
if ( isThreadPaused() == false )
m_pausedThreadCondWait->wait();
}
QMutex* ClipWorkflow::getSleepMutex()
{
return m_condMutex;
......@@ -393,3 +324,8 @@ LibVLCpp::MediaPlayer* ClipWorkflow::getMediaPlayer()
{
return m_mediaPlayer;
}
void ClipWorkflow::pausedMediaPlayer()
{
setState( Paused );
}
......@@ -53,9 +53,8 @@ class ClipWorkflow : public QObject
Sleeping, //4
Pausing, //5
Paused, //6
ThreadPaused, //7
Stopping, //8
EndReached, //9
Stopping, //7
EndReached, //8
};
int debugId;
......@@ -151,12 +150,10 @@ class ClipWorkflow : public QObject
*/
void reinitialize();
void unpause( bool wakeRenderThread = true );
void unpause();
void waitForCompleteInit();
void waitForCompleteRender( bool dontCheckRenderStarted = false );
void waitForPausingState();
void waitForPausedThread();
QMutex* getSleepMutex();
LibVLCpp::MediaPlayer* getMediaPlayer();
......@@ -180,9 +177,8 @@ class ClipWorkflow : public QObject
*/
LibVLCpp::Media* m_vlcMedia;
QQueue<unsigned char*> m_availableBuffers;
QQueue<unsigned char*> m_buffers;
QMutex* m_buffersLock;
unsigned char* m_buffer;
QMutex* m_renderLock;
LibVLCpp::MediaPlayer* m_mediaPlayer;
......@@ -194,12 +190,9 @@ class ClipWorkflow : public QObject
State m_requiredState;
QMutex* m_requiredStateLock;
QAtomicInt m_oneFrameOnly;
WaitCondition* m_initWaitCond;
WaitCondition* m_renderWaitCond;
WaitCondition* m_pausingStateWaitCond;
WaitCondition* m_pausedThreadCondWait;
/**
* While this flag is set to false, we will use the same buffer, to prevent
......@@ -217,6 +210,7 @@ class ClipWorkflow : public QObject
void pauseAfterPlaybackStarted();
void initializedMediaPlayer();
void setPositionAfterPlayback();
void pausedMediaPlayer();
public slots:
void clipEndReached();
......
......@@ -256,16 +256,6 @@ void MainWorkflow::clipMoved( QUuid clipUuid, int oldTrack, int newTra
computeLength();
}
void MainWorkflow::activateOneFrameOnly()
{
for (unsigned int i = 0; i < m_trackCount; ++i)
{
//FIXME: After debugging period, this should'nt be necessary --
if ( m_tracks[i].activated() == true )
m_tracks[i]->activateOneFrameOnly();
}
}
void MainWorkflow::trackPaused()
{
// qDebug() << "Track pausing finished...";
......
......@@ -76,8 +76,6 @@ class MainWorkflow : public QObject, public Singleton<MainWorkflow>
void nextFrame();
void previousFrame();
void activateOneFrameOnly();
static MainWorkflow* getInstance();
static void deleteInstance();
......
......@@ -83,20 +83,10 @@ qint64 TrackWorkflow::getLength() const
}
void TrackWorkflow::renderClip( ClipWorkflow* cw, qint64 currentFrame,
qint64 start , bool needRepositioning,
bool pauseAfterRender )
qint64 start , bool needRepositioning )
{
cw->getStateLock()->lockForRead();
if ( cw->getState() == ClipWorkflow::ThreadPaused && pauseAfterRender == false )
{
cw->getStateLock()->unlock();
//If we must pause after render, we must NOT wake the renderer thread, or it could render more than one frame
// (since this is for the next/previous frame)
//However, if this is just for a classic unpause, with just don't give a shit :)
cw->unpause( true );
cw->getStateLock()->lockForRead();
}
if ( cw->getState() == ClipWorkflow::Rendering )
{
//The rendering state meens... whell it means that the frame is
......@@ -110,7 +100,7 @@ void TrackWorkflow::renderClip( ClipWorkflow* cw, qint64 currentFrame,
}
//If frame has been rendered :
if ( cw->getState() == ClipWorkflow::Sleeping || pauseAfterRender == true )
if ( cw->getState() == ClipWorkflow::Sleeping )
{
cw->getStateLock()->unlock();
......@@ -119,23 +109,8 @@ void TrackWorkflow::renderClip( ClipWorkflow* cw, qint64 currentFrame,
float pos = ( (float)( currentFrame - start ) / (float)(cw->getClip()->getLength()) );
cw->setPosition( pos );
}
if ( pauseAfterRender == false )
{
QMutexLocker lock( cw->getSleepMutex() );
cw->wake();
}
else
{
cw->queryStateChange( ClipWorkflow::Rendering );
cw->getMediaPlayer()->pause();
cw->wake();
cw->waitForCompleteRender( true );
{
QMutexLocker lock( cw->getSleepMutex() );
}
cw->queryStateChange( ClipWorkflow::Paused );
cw->wake();
}
QMutexLocker lock( cw->getSleepMutex() );
cw->wake();
}
else if ( cw->getState() == ClipWorkflow::Stopped )
{
......@@ -228,17 +203,6 @@ void TrackWorkflow::stopClipWorkflow( ClipWorkflow* cw )
cw->wake();
cw->stop();
}
else if ( cw->getState() == ClipWorkflow::ThreadPaused )
{
cw->getStateLock()->unlock();
//Ensure the thread is really paused...
{
QMutexLocker lock( cw->getSleepMutex() );
}
cw->queryStateChange( ClipWorkflow::Stopped );
cw->wake();
cw->stop();
}
else
{
qDebug() << "Unexpected ClipWorkflow::State when stopping :" << cw->getState();
......@@ -266,7 +230,6 @@ void TrackWorkflow::stop()
stopClipWorkflow( it.value() );
++it;
}
m_oneFrameOnly = 0;
}
bool TrackWorkflow::getOutput( qint64 currentFrame )
......@@ -277,11 +240,8 @@ bool TrackWorkflow::getOutput( qint64 currentFrame )
QMap<qint64, ClipWorkflow*>::iterator end = m_clips.end();
static qint64 lastFrame = 0;
bool needRepositioning;
bool oneFrameOnlyFlag = false;
bool hasRendered = false;
if ( m_oneFrameOnly == 1 )
oneFrameOnlyFlag = true;
if ( checkEnd( currentFrame ) == true )
{
emit trackEndReached( m_trackId );
......@@ -309,7 +269,7 @@ bool TrackWorkflow::getOutput( qint64 currentFrame )
if ( start <= currentFrame && currentFrame <= start + cw->getClip()->getLength() )
{
m_nbClipToRender.fetchAndAddAcquire( 1 );
renderClip( cw, currentFrame, start, needRepositioning, oneFrameOnlyFlag );
renderClip( cw, currentFrame, start, needRepositioning );
lastFrame = currentFrame;
hasRendered = true;
}
......@@ -327,10 +287,6 @@ bool TrackWorkflow::getOutput( qint64 currentFrame )
++it;
}
if ( oneFrameOnlyFlag == true )
{
m_oneFrameOnly = 0;
}
if ( hasRendered == false )
clipWorkflowRenderCompleted( NULL );
return hasRendered;
......@@ -347,47 +303,11 @@ void TrackWorkflow::pauseClipWorkflow( ClipWorkflow* cw )
}
if ( cw->getState() != ClipWorkflow::Paused )
{
if ( cw->getState() == ClipWorkflow::Sleeping ||
cw->getState() == ClipWorkflow::Ready ||
cw->getState() == ClipWorkflow::EndReached )
{
cw->getStateLock()->unlock();
//Locking this mutex ensure that the thread is really asleep, and hasn't
//just changed the state, and then get pulled back by the scheduler...
{
QMutexLocker lock( cw->getSleepMutex() );
cw->queryStateChange( ClipWorkflow::Pausing );
}
cw->wake();
}
else if ( cw->getState() == ClipWorkflow::Rendering )
{
cw->getStateLock()->unlock();
cw->waitForCompleteRender();
{
QMutexLocker lock( cw->getSleepMutex() );
cw->queryStateChange( ClipWorkflow::Pausing );
}
cw->wake();
}
else if ( cw->getState() == ClipWorkflow::Initializing )
{
cw->getStateLock()->unlock();
//TODO: since a Initializing clipworkflow will pause itself at the end, shouldn't we do nothing ?
cw->waitForCompleteInit();
}
else
{
// qDebug() << "Unexpected ClipWorkflow::State when pausing:" << cw->getState();
cw->getStateLock()->unlock();
}
cw->waitForPausingState();
cw->pause();
//TODO (redo actually...) :)
Q_ASSERT( false );
}
else
cw->getStateLock()->unlock();
cw->waitForPausedThread();
}
void TrackWorkflow::pause()
......@@ -409,7 +329,7 @@ void TrackWorkflow::pause()
cw->getStateLock()->unlock();
continue ;
}
else if ( cw->getState() != ClipWorkflow::ThreadPaused )
else if ( cw->getState() != ClipWorkflow::Paused )
{
cw->getStateLock()->unlock();
m_nbClipToPause.fetchAndAddAcquire( 1 );
......@@ -474,11 +394,6 @@ Clip* TrackWorkflow::removeClip( const QUuid& id )
return NULL;
}
void TrackWorkflow::activateOneFrameOnly()
{
m_oneFrameOnly = 1;
}
void TrackWorkflow::clipWorkflowRenderCompleted( ClipWorkflow* cw )
{
if ( cw != NULL )
......
......@@ -61,7 +61,6 @@ class TrackWorkflow : public QObject
Clip* removeClip( const QUuid& id );
void addClip( Clip*, qint64 start );
void addClip( ClipWorkflow*, qint64 start );
void activateOneFrameOnly();
/**
* Returns the output that has been computed in synchrone mode.
*/
......@@ -73,8 +72,7 @@ class TrackWorkflow : public QObject
private:
void computeLength();
void renderClip( ClipWorkflow* cw, qint64 currentFrame,
qint64 start, bool needRepositioning,
bool pauseAfterRender );
qint64 start, bool needRepositioning );
void preloadClip( ClipWorkflow* cw );
void stopClipWorkflow( ClipWorkflow* cw );
void pauseClipWorkflow( ClipWorkflow* cw );
......@@ -101,7 +99,6 @@ class TrackWorkflow : public QObject
bool m_paused;
QAtomicInt m_oneFrameOnly;
QAtomicInt m_nbClipToPause;
QAtomicInt m_nbClipToRender;
......
......@@ -29,10 +29,8 @@
WorkflowRenderer::WorkflowRenderer( MainWorkflow* mainWorkflow ) :
m_mainWorkflow( mainWorkflow ),
m_framePlayed( false ),
m_pauseAsked( false ),
m_pausedMediaPlayer( false ),
m_frameByFrameMode( 0 )
m_pausedMediaPlayer( false )
{
char buffer[64];
......@@ -86,42 +84,17 @@ void* WorkflowRenderer::lock( void* datas )
//If renderer is stopping, don't ask for another frame:
if ( self->m_isRendering == false )
return self->m_lastFrame;
//If a pause was asked, don't try to start a new render... it could (and would) dead lock...
if ( self->m_pauseAsked == true )
return self->m_lastFrame;
//If we're not playing, then where in a paused media player.
if ( self->m_pausedMediaPlayer == true )
return self->m_lastFrame;
if ( self->m_oneFrameOnly < 2 )
{
void* ret;
// if ( self->m_oneFrameOnly == 0 )
// ret = self->m_mainWorkflow->getOutput();
// else
// {
// qDebug() << "Asking synchrone frame";
ret = self->m_mainWorkflow->getSynchroneOutput();
// qDebug() << "Got it";
// }
self->m_lastFrame = static_cast<unsigned char*>( ret );
return ret;
}
else
return self->m_lastFrame;
void* ret = self->m_mainWorkflow->getSynchroneOutput();
self->m_lastFrame = static_cast<unsigned char*>( ret );
return ret;
}
void WorkflowRenderer::unlock( void* datas )
{
WorkflowRenderer* self = reinterpret_cast<WorkflowRenderer*>( datas );
if ( self->m_oneFrameOnly == 1 )
{
self->internalPlayPause( true );
self->m_oneFrameOnly = 2;
}
self->m_framePlayed = true;
self->internalPlayPause( true );
self->checkActions();
}
......@@ -178,43 +151,8 @@ void WorkflowRenderer::setPosition( float newPos )
m_mainWorkflow->setPosition( newPos );
}
void WorkflowRenderer::frameByFrameAfterPaused()
{
m_oneFrameOnly = 1;
m_mainWorkflow->activateOneFrameOnly();
internalPlayPause( false );
}
void WorkflowRenderer::frameByFramePausingProxy()
{
static unsigned int nbPaused = 0;
++nbPaused;
if ( nbPaused == 2 )
{
nbPaused = 0;
disconnect( m_mediaPlayer, SIGNAL( paused() ), this, SLOT( frameByFramePausingProxy() ) );
disconnect( m_mainWorkflow, SIGNAL( mainWorkflowPaused() ), this, SLOT( frameByFramePausingProxy() ) );
frameByFrameAfterPaused();
}
}
void WorkflowRenderer::nextFrame()
{
if ( m_frameByFrameMode == 0 )
{
m_frameByFrameMode = 1;
}
if ( m_pausedMediaPlayer == true )
{
frameByFrameAfterPaused();
}
else
{
connect( m_mediaPlayer, SIGNAL( paused() ), this, SLOT( frameByFramePausingProxy() ), Qt::QueuedConnection );
connect( m_mainWorkflow, SIGNAL( mainWorkflowPaused() ), this, SLOT( frameByFramePausingProxy() ), Qt::QueuedConnection );
internalPlayPause( true );
}
}
void WorkflowRenderer::previousFrame()
......@@ -234,21 +172,11 @@ void WorkflowRenderer::mainWorkflowPaused()
{
m_paused = true;
m_pauseAsked = false;
if ( m_frameByFrameMode < 2 )
{
emit paused();
//On the first iteration, when inform the PreviewWidget that we are in some paused mode
if ( m_frameByFrameMode == 1 )
m_frameByFrameMode = 2;
}
emit paused();
}
void WorkflowRenderer::togglePlayPause( bool forcePause )
{
if ( m_frameByFrameMode != 0 )
{
m_frameByFrameMode = 0;
}
if ( m_isRendering == false && forcePause == false )
startPreview();
else
......@@ -284,7 +212,6 @@ void WorkflowRenderer::stop()
m_mainWorkflow->cancelSynchronisation();
m_mediaPlayer->stop();
m_mainWorkflow->stop();
m_oneFrameOnly = 0;
}
/////////////////////////////////////////////////////////////////////
......@@ -309,17 +236,12 @@ void WorkflowRenderer::__positionChanged( float pos )
void WorkflowRenderer::__videoPaused()
{
if ( m_oneFrameOnly != 0 )
{
m_oneFrameOnly = 0;
}
pauseMainWorkflow();
}
void WorkflowRenderer::__videoPlaying()
{
if ( m_frameByFrameMode == 0 )
emit playing();
emit playing();
m_pausedMediaPlayer = false;
m_paused = false;
}
......
......@@ -67,14 +67,11 @@ class WorkflowRenderer : public GenericRenderer
void pauseMainWorkflow();
virtual void startPreview();
void checkActions();
void frameByFrameAfterPaused();
private:
MainWorkflow* m_mainWorkflow;
LibVLCpp::Media* m_media;