MainWorkflow.cpp 11.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*****************************************************************************
 * MainWorkflow.cpp : Will query all of the track workflows to render the final
 *                    image
 *****************************************************************************
 * 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.
 *****************************************************************************/

#include <QtDebug>

#include "MainWorkflow.h"

28
unsigned char*  MainWorkflow::blackOutput = NULL;
29
MainWorkflow*   MainWorkflow::m_instance = NULL;
30

31
MainWorkflow::MainWorkflow( int trackCount ) :
32
        m_length( 0 ),
33 34
        m_trackCount( trackCount ),
        m_renderStarted( false )
35
{
36 37 38 39 40 41
    Q_ASSERT_X( MainWorkflow::m_instance == NULL,
                "MainWorkflow constructor", "Can't have more than one MainWorkflow instance" );
    m_instance = this;

    MainWorkflow::blackOutput = new unsigned char[VIDEOHEIGHT * VIDEOWIDTH * 3];
    memset( MainWorkflow::blackOutput, 0, VIDEOHEIGHT * VIDEOWIDTH * 3 );
42 43

    m_tracks = new Toggleable<TrackWorkflow*>[trackCount];
44
    for ( int i = 0; i < trackCount; ++i )
45 46 47
    {
        m_tracks[i].setPtr( new TrackWorkflow( i ) );
        connect( m_tracks[i], SIGNAL( trackEndReached( unsigned int ) ), this, SLOT( trackEndReached(unsigned int) ) );
48
        connect( m_tracks[i], SIGNAL( trackPaused() ), this, SLOT( trackPaused() ) );
49
        connect( m_tracks[i], SIGNAL( trackUnpaused() ), this, SLOT( trackUnpaused() ) );
50
        connect( m_tracks[i], SIGNAL( renderCompleted( unsigned int ) ), this,  SLOT( tracksRenderCompleted( unsigned int ) ), Qt::QueuedConnection );
51
    }
52
    m_renderStartedLock = new QReadWriteLock;
53
    m_renderMutex = new QMutex;
54 55 56
    m_highestTrackNumberMutex = new QMutex;
    m_synchroneRenderWaitCondition = new QWaitCondition;
    m_synchroneRenderWaitConditionMutex = new QMutex;
57 58 59 60
}

MainWorkflow::~MainWorkflow()
{
61 62
    stop();

63 64 65 66
    delete m_synchroneRenderWaitConditionMutex;
    delete m_synchroneRenderWaitCondition;
    delete m_highestTrackNumberMutex;
    delete m_renderMutex;
67 68 69 70 71
    delete m_renderStartedLock;
    for (unsigned int i = 0; i < m_trackCount; ++i)
        delete m_tracks[i];
    delete[] m_tracks;
    delete[] blackOutput;
72 73
}

74
void    MainWorkflow::addClip( Clip* clip, unsigned int trackId, qint64 start )
75
{
76
    Q_ASSERT_X( trackId < m_trackCount, "MainWorkflow::addClip",
77 78
                "The specified trackId isn't valid, for it's higher than the number of tracks");

79 80 81
    
    m_tracks[trackId]->addClip( clip, start );
    //if the track is deactivated, we need to reactivate it.
82
    if ( m_tracks[trackId].deactivated() == true )
83
        activateTrack( trackId );
84 85

    //Now check if this clip addition has changed something about the workflow's length
86 87
    if ( m_tracks[trackId]->getLength() > m_length )
        m_length = m_tracks[trackId]->getLength();
88

89
    //Inform the GUI
90
    emit clipAdded( clip, trackId, start );
91
}
92

93
void            MainWorkflow::computeLength()
94
{
95 96 97 98 99 100 101 102
    qint64      maxLength = 0;

    for ( unsigned int i = 0; i < m_trackCount; ++i )
    {
        if ( m_tracks[i]->getLength() > maxLength )
            maxLength = m_tracks[i]->getLength();
    }
    m_length = maxLength;
103 104
}

105 106 107
void    MainWorkflow::startRender()
{
    m_renderStarted = true;
108
    m_paused = false;
109 110 111
    m_currentFrame = 0;
    emit frameChanged( 0 );
    for ( unsigned int i = 0; i < m_trackCount; ++i )
112
        activateTrack( i );
113 114 115
    computeLength();
}

116
void                MainWorkflow::getOutput()
117
{
118
    QReadLocker     lock( m_renderStartedLock );
119
    QMutexLocker    lock2( m_renderMutex );
120

121 122 123 124 125 126
    {
        QMutexLocker    lockHighestTrackNumber( m_highestTrackNumberMutex );
        m_highestTrackNumber = 0;
    }
    m_nbTracksToRender = 0;
    m_synchroneRenderingBuffer = NULL;
127
    if ( m_renderStarted == true )
128
    {
129
        for ( unsigned int i = 0; i < m_trackCount; ++i )
130
        {
131 132
            if ( m_tracks[i].activated() == false )
                continue ;
133

134
            m_nbTracksToRender.fetchAndAddAcquire( 1 );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
135
            m_tracks[i]->getOutput( m_currentFrame );
136
        }
137 138
        if ( m_paused == false )
            nextFrame();
139
    }
140
}
141

142 143
void        MainWorkflow::pause()
{
144 145
    QMutexLocker    lock( m_renderMutex );

146
    m_nbTracksToPause = 0;
147 148 149
    for ( unsigned int i = 0; i < m_trackCount; ++i )
    {
        if ( m_tracks[i].activated() == true )
150 151
        {
            m_nbTracksToPause.fetchAndAddAcquire( 1 );
152
            m_tracks[i]->pause();
153
        }
154 155 156
    }
}

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
void        MainWorkflow::unpause()
{
    QMutexLocker    lock( m_renderMutex );

    m_nbTracksToUnpause = 0;
    for ( unsigned int i = 0; i < m_trackCount; ++i )
    {
        if ( m_tracks[i].activated() == true )
        {
            m_nbTracksToUnpause.fetchAndAddAcquire( 1 );
            m_tracks[i]->unpause();
        }
    }
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185
void        MainWorkflow::nextFrame()
{
    ++m_currentFrame;
    emit frameChanged( m_currentFrame );
    emit positionChanged( (float)m_currentFrame / (float)m_length );
}

void        MainWorkflow::previousFrame()
{
    --m_currentFrame;
    emit frameChanged( m_currentFrame );
    emit positionChanged( (float)m_currentFrame / (float)m_length );
}

186 187
void        MainWorkflow::setPosition( float pos )
{
188 189
    if ( m_renderStarted == false )
        return ;
190 191 192
    //Since any track can be reactivated, we reactivate all of them, and let them
    //unable themself if required.
    for ( unsigned int i = 0; i < m_trackCount; ++i)
193
        activateTrack( i );
194

chouquette's avatar
chouquette committed
195
    qint64  frame = static_cast<qint64>( (float)m_length * pos );
196 197
    m_currentFrame = frame;
    emit frameChanged( frame );
198
//    cancelSynchronisation();
199
    //Do not emit a signal for the RenderWidget, since it's the one that triggered that call...
200 201 202 203 204 205
}

qint64      MainWorkflow::getLength() const
{
    return m_length;
}
206

207 208 209 210 211 212 213
qint64      MainWorkflow::getClipPosition( const QUuid& uuid, unsigned int trackId ) const
{
    Q_ASSERT( trackId < m_trackCount );

    return m_tracks[trackId]->getClipPosition( uuid );
}

214
void        MainWorkflow::trackEndReached( unsigned int trackId )
215
{
216 217 218 219 220 221 222
    m_tracks[trackId].deactivate();

    for ( unsigned int i = 0; i < m_trackCount; ++i)
    {
        if ( m_tracks[i].activated() == true )
            return ;
    }
223
    emit mainWorkflowEndReached();
224 225 226
    m_renderStarted = false;
    m_currentFrame = 0;
    emit frameChanged( 0 );
227
}
228 229 230 231 232

unsigned int    MainWorkflow::getTrackCount() const
{
    return m_trackCount;
}
233 234 235 236 237 238 239 240

void            MainWorkflow::stop()
{
    QWriteLocker    lock( m_renderStartedLock );

    m_renderStarted = false;
    for (unsigned int i = 0; i < m_trackCount; ++i)
    {
241 242
        if ( m_tracks[i].activated() == true )
            m_tracks[i]->stop();
243 244 245 246
    }
    m_currentFrame = 0;
    emit frameChanged( 0 );
}
247 248 249 250 251 252

MainWorkflow*   MainWorkflow::getInstance()
{
    Q_ASSERT( m_instance != NULL );
    return m_instance;
}
253

254 255 256 257 258 259 260 261 262
void            MainWorkflow::deleteInstance()
{
    if ( m_instance != NULL )
    {
        delete m_instance;
        m_instance = NULL;
    }
}

263 264
void           MainWorkflow::moveClip( const QUuid& clipUuid, unsigned int oldTrack,
                                       unsigned int newTrack, qint64 startingFrame, bool undoRedoCommand /*= false*/ )
265
{
266
    Q_ASSERT( newTrack < m_trackCount && oldTrack < m_trackCount );
267 268 269 270 271

    if ( oldTrack == newTrack )
    {
        //And now, just move the clip.
        m_tracks[newTrack]->moveClip( clipUuid, startingFrame );
272
        activateTrack( newTrack );
273 274 275 276 277
    }
    else
    {
        Clip* clip = m_tracks[oldTrack]->removeClip( clipUuid );
        m_tracks[newTrack]->addClip( clip, startingFrame );
278 279
        activateTrack( oldTrack );
        activateTrack( newTrack );
280
    }
281
    computeLength();
282 283 284 285
    if ( undoRedoCommand == true )
    {
        emit clipMoved( clipUuid, newTrack, startingFrame );
    }
286
}
287

288 289 290 291 292
Clip*       MainWorkflow::removeClip( const QUuid& uuid, unsigned int trackId )
{
    Q_ASSERT( trackId < m_trackCount );

    Clip* clip = m_tracks[trackId]->removeClip( uuid );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
293
    activateTrack( trackId );
294 295
    emit clipRemoved( uuid, trackId );
    return clip;
296
}
297

298
void        MainWorkflow::trackPaused()
299
{
300
    m_nbTracksToPause.fetchAndAddAcquire( -1 );
301
    if ( m_nbTracksToPause <= 0 )
302
    {
303
        m_paused = true;
304
        emit mainWorkflowPaused();
305 306
    }
}
307

308
void        MainWorkflow::trackUnpaused()
309
{
310 311
    m_nbTracksToUnpause.fetchAndAddAcquire( -1 );
    if ( m_nbTracksToUnpause <= 0 )
312
    {
313
        m_paused = false;
314
        emit mainWorkflowUnpaused();
315 316
    }
}
317 318 319 320 321 322 323

void        MainWorkflow::tracksRenderCompleted( unsigned int trackId )
{
    m_nbTracksToRender.fetchAndAddAcquire( -1 );

    {
        QMutexLocker    lock( m_highestTrackNumberMutex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
324 325 326

        unsigned char* buff = m_tracks[trackId]->getSynchroneOutput();
        if ( m_highestTrackNumber <= trackId && buff != NULL )
327 328
        {
            m_highestTrackNumber = trackId;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
329
            m_synchroneRenderingBuffer = buff;;
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
        }
    }
    //We check for minus or equal, since we can have 0 frame to compute,
    //therefore, m_nbTracksToRender will be equal to -1
    if ( m_nbTracksToRender <= 0 )
    {
        //Just a synchronisation barriere
        {
            QMutexLocker    lock( m_synchroneRenderWaitConditionMutex );
        }
        m_synchroneRenderWaitCondition->wakeAll();
    }
}

unsigned char*  MainWorkflow::getSynchroneOutput()
{
    m_synchroneRenderWaitConditionMutex->lock();
    getOutput();
348
//    qDebug() << "Waiting for sync output";
349
    m_synchroneRenderWaitCondition->wait( m_synchroneRenderWaitConditionMutex );
350
//    qDebug() << "Got it";
351 352 353 354 355
    m_synchroneRenderWaitConditionMutex->unlock();
    if ( m_synchroneRenderingBuffer == NULL )
        return MainWorkflow::blackOutput;
    return m_synchroneRenderingBuffer;
}
356 357 358 359 360 361 362 363

void        MainWorkflow::cancelSynchronisation()
{
    {
        QMutexLocker    lock( m_synchroneRenderWaitConditionMutex );
    }
    m_synchroneRenderWaitCondition->wakeAll();
}
364 365 366 367 368 369 370 371 372 373

void        MainWorkflow::muteTrack( unsigned int trackId )
{
    m_tracks[trackId].setHardDeactivation( true );
}

void        MainWorkflow::unmuteTrack( unsigned int trackId )
{
    m_tracks[trackId].setHardDeactivation( false );
}
374 375 376 377 378 379

void        MainWorkflow::setCurrentFrame( qint64 currentFrame )
{
    m_currentFrame = currentFrame;
    emit positionChanged( (float)m_currentFrame / (float)m_length );
}
380

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
381 382 383 384 385 386 387
void        MainWorkflow::activateTrack( unsigned int trackId )
{
    if ( m_tracks[trackId]->getLength() > 0 )
        m_tracks[trackId].activate();
    else
        m_tracks[trackId].deactivate();
}
388 389 390 391 392 393 394

Clip*       MainWorkflow::getClip( const QUuid& uuid, unsigned int trackId )
{
    Q_ASSERT( trackId < m_trackCount );

    return m_tracks[trackId]->getClip( uuid );
}