MainWorkflow.cpp 15.1 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_currentFrame( 0 ),
33
        m_length( 0 ),
34 35
        m_trackCount( trackCount ),
        m_renderStarted( false )
36
{
37 38 39 40 41 42
    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 );
43 44

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

MainWorkflow::~MainWorkflow()
{
63
    //FIXME: this is probably useless, since already done by the renderer
64 65
    stop();

66
    delete m_nbTracksToRenderMutex;
67 68 69 70
    delete m_synchroneRenderWaitConditionMutex;
    delete m_synchroneRenderWaitCondition;
    delete m_highestTrackNumberMutex;
    delete m_renderMutex;
71 72 73 74 75
    delete m_renderStartedLock;
    for (unsigned int i = 0; i < m_trackCount; ++i)
        delete m_tracks[i];
    delete[] m_tracks;
    delete[] blackOutput;
76 77
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
78
void        MainWorkflow::addClip( Clip* clip, unsigned int trackId, qint64 start )
79
{
80
    Q_ASSERT_X( trackId < m_trackCount, "MainWorkflow::addClip",
81 82
                "The specified trackId isn't valid, for it's higher than the number of tracks");

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

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

92
    //Inform the GUI
93
    emit clipAdded( clip, trackId, start );
94
}
95

96
void            MainWorkflow::computeLength()
97
{
98 99 100 101 102 103 104 105
    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;
106 107
}

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

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

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

136
            ++m_nbTracksToRender;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
137
            m_tracks[i]->getOutput( m_currentFrame );
138
        }
139 140
        if ( m_paused == false )
            nextFrame();
141
    }
142
}
143

144 145
void        MainWorkflow::pause()
{
146 147
    QMutexLocker    lock( m_renderMutex );

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

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
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();
        }
    }
}

174 175 176 177 178 179 180 181 182 183 184 185 186 187
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 );
}

188 189
void        MainWorkflow::setPosition( float pos )
{
190 191 192 193 194 195 196
    if ( m_renderStarted == true )
    {
        //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)
            activateTrack( i );
    }
chouquette's avatar
chouquette committed
197
    qint64  frame = static_cast<qint64>( (float)m_length * pos );
198 199
    m_currentFrame = frame;
    emit frameChanged( frame );
200
    //Do not emit a signal for the RenderWidget, since it's the one that triggered that call...
201 202 203 204 205 206
}

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

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

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

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

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

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

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

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

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

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

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

    if ( oldTrack == newTrack )
    {
        //And now, just move the clip.
        m_tracks[newTrack]->moveClip( clipUuid, startingFrame );
271
        activateTrack( newTrack );
272 273 274
    }
    else
    {
275 276 277 278
        bool    needRepo;

        if ( m_tracks[oldTrack]->getClipPosition( clipUuid ) != startingFrame )
            needRepo = true;
279 280
        ClipWorkflow* cw = m_tracks[oldTrack]->removeClipWorkflow( clipUuid );
        m_tracks[newTrack]->addClip( cw, startingFrame );
281 282
        if ( needRepo == true )
            m_tracks[newTrack]->forceRepositionning();
283 284
        activateTrack( oldTrack );
        activateTrack( newTrack );
285
    }
286
    computeLength();
287 288 289 290
    if ( undoRedoCommand == true )
    {
        emit clipMoved( clipUuid, newTrack, startingFrame );
    }
291
}
292

293 294 295 296 297
Clip*       MainWorkflow::removeClip( const QUuid& uuid, unsigned int trackId )
{
    Q_ASSERT( trackId < m_trackCount );

    Clip* clip = m_tracks[trackId]->removeClip( uuid );
298
    computeLength();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
299
    activateTrack( trackId );
300 301
    emit clipRemoved( uuid, trackId );
    return clip;
302
}
303

304
void        MainWorkflow::trackPaused()
305
{
306
    m_nbTracksToPause.fetchAndAddAcquire( -1 );
307
    if ( m_nbTracksToPause <= 0 )
308
    {
309
        m_paused = true;
310
        emit mainWorkflowPaused();
311 312
    }
}
313

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

void        MainWorkflow::tracksRenderCompleted( unsigned int trackId )
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
326
//    qDebug() << "tracksRenderCompleted";
327 328
    QMutexLocker    lockNbTracks( m_nbTracksToRenderMutex );
    --m_nbTracksToRender;
329 330 331

    {
        QMutexLocker    lock( m_highestTrackNumberMutex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
332 333 334

        unsigned char* buff = m_tracks[trackId]->getSynchroneOutput();
        if ( m_highestTrackNumber <= trackId && buff != NULL )
335 336
        {
            m_highestTrackNumber = trackId;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
337
            m_synchroneRenderingBuffer = buff;;
338 339 340 341 342 343
        }
    }
    //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 )
    {
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
344
//        qDebug() << "main workflow render completed";
345 346 347 348 349 350 351 352 353 354 355 356
        //Just a synchronisation barriere
        {
            QMutexLocker    lock( m_synchroneRenderWaitConditionMutex );
        }
        m_synchroneRenderWaitCondition->wakeAll();
    }
}

unsigned char*  MainWorkflow::getSynchroneOutput()
{
    m_synchroneRenderWaitConditionMutex->lock();
    getOutput();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
357
//    qDebug() << "Waiting for sync output";
358
    m_synchroneRenderWaitCondition->wait( m_synchroneRenderWaitConditionMutex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
359
//    qDebug() << "Got it";
360 361 362 363 364
    m_synchroneRenderWaitConditionMutex->unlock();
    if ( m_synchroneRenderingBuffer == NULL )
        return MainWorkflow::blackOutput;
    return m_synchroneRenderingBuffer;
}
365 366 367 368 369 370 371 372

void        MainWorkflow::cancelSynchronisation()
{
    {
        QMutexLocker    lock( m_synchroneRenderWaitConditionMutex );
    }
    m_synchroneRenderWaitCondition->wakeAll();
}
373 374 375 376 377 378 379 380 381 382

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

void        MainWorkflow::unmuteTrack( unsigned int trackId )
{
    m_tracks[trackId].setHardDeactivation( false );
}
383 384 385 386 387 388

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

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
390 391 392 393 394 395 396
void        MainWorkflow::activateTrack( unsigned int trackId )
{
    if ( m_tracks[trackId]->getLength() > 0 )
        m_tracks[trackId].activate();
    else
        m_tracks[trackId].deactivate();
}
397 398 399 400 401 402 403

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

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

void        MainWorkflow::loadProject( const QDomElement& project )
{
407 408 409 410 411 412
    if ( project.isNull() == true || project.tagName() != "timeline" )
    {
        qWarning() << "Invalid timeline node (" << project.tagName() << ')';
        return ;
    }

413 414
    clear();

415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
    QDomElement elem = project.firstChild().toElement();

    while ( elem.isNull() == false )
    {
        bool    ok;

        Q_ASSERT( elem.tagName() == "track" );
        unsigned int trackId = elem.attribute( "id" ).toUInt( &ok );
        if ( ok == false )
        {
            qWarning() << "Invalid track number in project file";
            return ;
        }
        QDomElement clip = elem.firstChild().toElement();
        while ( clip.isNull() == false )
        {
            //Iterate over clip fields:
            QDomElement clipProperty = clip.firstChild().toElement();
            QUuid       parent;
434 435
            qint64      begin;
            qint64      end;
436 437 438 439 440 441 442 443 444 445 446
            qint64      startPos;

            while ( clipProperty.isNull() == false )
            {
                QString tagName = clipProperty.tagName();
                bool    ok;

                if ( tagName == "parent" )
                    parent = QUuid( clipProperty.text() );
                else if ( tagName == "begin" )
                {
447
                    begin = clipProperty.text().toLongLong( &ok );
448 449 450 451 452 453 454 455
                    if ( ok == false )
                    {
                        qWarning() << "Invalid clip begin";
                        return ;
                    }
                }
                else if ( tagName == "end" )
                {
456
                    end = clipProperty.text().toLongLong( &ok );
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
                    if ( ok == false )
                    {
                        qWarning() << "Invalid clip end";
                        return ;
                    }
                }
                else if ( tagName == "startFrame" )
                {
                    startPos = clipProperty.text().toLongLong( &ok );
                    if ( ok == false )
                    {
                        qWarning() << "Invalid clip starting frame";
                        return ;
                    }
                }
                else
                    qDebug() << "Unknown field" << clipProperty.tagName();

                clipProperty = clipProperty.nextSibling().toElement();
            }

            Clip*       c = new Clip( parent, begin, end );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
479
            addClip( c, trackId, startPos );
480 481 482 483 484

            clip = clip.nextSibling().toElement();
        }
        elem = elem.nextSibling().toElement();
    }
485 486
}

487
void        MainWorkflow::saveProject( QDomDocument& doc, QDomElement& rootNode )
488 489 490 491 492 493 494 495 496 497 498 499 500 501
{
    QDomElement project = doc.createElement( "timeline" );
    for ( unsigned int i = 0; i < m_trackCount; ++i )
    {
        if ( m_tracks[i]->getLength() > 0 )
        {
            QDomElement     trackNode = doc.createElement( "track" );

            trackNode.setAttribute( "id", i );

            m_tracks[i]->save( doc, trackNode );
            project.appendChild( trackNode );
        }
    }
502
    rootNode.appendChild( project );
503
}
504 505 506 507 508 509 510 511

void        MainWorkflow::clear()
{
    for ( unsigned int i = 0; i < m_trackCount; ++i )
    {
        m_tracks[i]->clear();
    }
    m_length = 0;
512
    emit cleared();
513
}
514 515 516 517 518 519

void        MainWorkflow::setFullSpeedRender( bool value )
{
    for ( unsigned int i = 0; i < m_trackCount; ++i )
        m_tracks[i]->setFullSpeedRender( value );
}