TrackWorkflow.cpp 18.7 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
/*****************************************************************************
 * TrackWorkflow.cpp : Will query the Clip workflow for each successive clip in the track
 *****************************************************************************
 * 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>

25
#include "vlmc.h"
26
#include "TrackWorkflow.h"
27
#include "VideoClipWorkflow.h"
28
#include "AudioClipWorkflow.h"
29

30
TrackWorkflow::TrackWorkflow( unsigned int trackId, TrackWorkflow::TrackType type  ) :
31
        m_trackId( trackId ),
32
        m_length( 0 ),
33
        m_forceRepositionning( false ),
34
        m_paused( false ),
35 36
        m_synchroneRenderBuffer( NULL ),
        m_trackType( type )
37
{
38
    m_forceRepositionningMutex = new QMutex;
39
    m_clipsLock = new QReadWriteLock;
40 41
}

42 43
TrackWorkflow::~TrackWorkflow()
{
44 45
    QMap<qint64, ClipWorkflow*>::iterator       it = m_clips.begin();
    QMap<qint64, ClipWorkflow*>::iterator       end = m_clips.end();
46 47 48 49 50 51 52

    while ( it != end )
    {
        stopClipWorkflow( it.value() );
        delete it.value();
        it = m_clips.erase( it );
    }
53
    delete m_clipsLock;
54
    delete m_forceRepositionningMutex;
55 56
}

57
void    TrackWorkflow::addClip( Clip* clip, qint64 start )
58
{
59 60
    if ( m_trackType == Audio )
        start = 0;
61 62 63
    ClipWorkflow* cw;
    if ( m_trackType == TrackWorkflow::Video )
        cw = new VideoClipWorkflow( clip );
64 65
    else
        cw = new AudioClipWorkflow( clip );
66
    addClip( cw, start );
67 68
}

69 70 71
void    TrackWorkflow::addClip( ClipWorkflow* cw, qint64 start )
{
    QWriteLocker    lock( m_clipsLock );
72
    connect( cw, SIGNAL( renderComplete( ClipWorkflow* ) ), this, SLOT( clipWorkflowRenderCompleted( ClipWorkflow* ) ), Qt::DirectConnection );
73 74
    connect( cw, SIGNAL( paused() ), this, SLOT( clipWorkflowPaused() ) );
    connect( cw, SIGNAL( unpaused() ), this, SLOT( clipWorkflowUnpaused() ) );
75 76 77 78
    m_clips.insert( start, cw );
    computeLength();
}

79
//Must be called from a thread safe method (m_clipsLock locked)
80 81 82
void                TrackWorkflow::computeLength()
{
    if ( m_clips.count() == 0 )
83
    {
84
        m_length = 0;
85 86
        return ;
    }
87
    QMap<qint64, ClipWorkflow*>::const_iterator it = m_clips.end() - 1;
88 89 90 91 92 93 94 95
    m_length = (it.key() + it.value()->getClip()->getLength() );
}

qint64              TrackWorkflow::getLength() const
{
    return m_length;
}

96
qint64              TrackWorkflow::getClipPosition( const QUuid& uuid ) const
97
{
98 99
    QMap<qint64, ClipWorkflow*>::const_iterator     it = m_clips.begin();
    QMap<qint64, ClipWorkflow*>::const_iterator     end = m_clips.end();
100 101 102

    while ( it != end )
    {
103
        if ( it.value()->getClip()->getUuid() == uuid )
104 105 106 107
            return it.key();
        ++it;
    }
    return -1;
108 109
}

110 111 112 113 114 115 116
Clip*               TrackWorkflow::getClip( const QUuid& uuid )
{
    QMap<qint64, ClipWorkflow*>::const_iterator     it = m_clips.begin();
    QMap<qint64, ClipWorkflow*>::const_iterator     end = m_clips.end();

    while ( it != end )
    {
117
        if ( it.value()->getClip()->getUuid() == uuid )
118 119 120 121 122 123
            return it.value()->getClip();
        ++it;
    }
    return NULL;
}

124
void        TrackWorkflow::renderClip( ClipWorkflow* cw, qint64 currentFrame,
125
                                        qint64 start , bool needRepositioning )
126
{
127
    cw->getStateLock()->lockForRead();
128

129
//    qDebug() << "Rendering clip" << cw << "state:" << cw->getState() << "Type:" << m_trackType;
130
    if ( cw->getState() == ClipWorkflow::Rendering )
131
    {
132 133 134
        //The rendering state meens... whell it means that the frame is
        //beeing rendered, so we wait.
        cw->getStateLock()->unlock();
135
        cw->waitForCompleteRender();
136
        //This way we can trigger the appropriate if just below.
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
137 138 139
        //by restoring the initial state of the function, and just pretend that
        //nothing happened.
        cw->getStateLock()->lockForRead();
140
    }
141

142
    //If frame has been rendered :
143
    if ( cw->getState() == ClipWorkflow::Sleeping )
144
    {
145
        cw->getStateLock()->unlock();
146

147
        if ( needRepositioning == true )
148
            adjustClipTime( currentFrame, start, cw );
149
        QMutexLocker    lock( cw->getSleepMutex() );
150
        cw->wake();
151
    }
152
    else if ( cw->getState() == ClipWorkflow::Stopped )
153
    {
154
        cw->getStateLock()->unlock();
155 156
        cw->initialize();
        cw->startRender( m_paused );
157
        if ( start != currentFrame || cw->getClip()->getBegin() != 0 ) //Clip was not started as its real begining
158
        {
159
            adjustClipTime( currentFrame, start, cw );
160
        }
161 162
        if ( m_paused == true )
            clipWorkflowRenderCompleted( cw );
163
    }
164 165
    else if ( cw->getState() == ClipWorkflow::Ready ||
              cw->getState() == ClipWorkflow::Initializing )
166
    {
167 168 169
        //If the state is Initializing, then the workflow will wait.
        //Otherwise, it will start directly.
        cw->getStateLock()->unlock();
170
        cw->startRender( false );
171

172 173
        if ( needRepositioning == true )
        {
174
            adjustClipTime( currentFrame, start, cw );
175
        }
176
    }
177 178 179
    else if ( cw->getState() == ClipWorkflow::EndReached )
    {
        cw->getStateLock()->unlock();
180
        clipWorkflowRenderCompleted( cw );
181 182
        //The stopClipWorkflow() method will take care of that.
    }
183 184 185 186 187
    else if ( cw->getState() == ClipWorkflow::Paused )
    {
        cw->getStateLock()->unlock();
        if ( needRepositioning == true )
        {
188
            adjustClipTime( currentFrame, start, cw );
189 190 191
        }
        clipWorkflowRenderCompleted( cw );
    }
192
    else
193
    {
194
        cw->getStateLock()->unlock();
195
    }
196
}
197

198
void                TrackWorkflow::preloadClip( ClipWorkflow* cw )
199
{
200
    cw->getStateLock()->lockForRead();
201

202
    if ( cw->getState() == ClipWorkflow::Stopped )
203
    {
204
        cw->getStateLock()->unlock();
205
        cw->initialize( true );
206
        return ;
207
    }
208
    cw->getStateLock()->unlock();
209 210
}

211
void                TrackWorkflow::stopClipWorkflow( ClipWorkflow* cw )
212
{
213
    cw->getStateLock()->lockForRead();
214

215
    if ( cw->getState() == ClipWorkflow::Stopped )
216
    {
217 218
        cw->getStateLock()->unlock();
        return ;
219
    }
220 221 222
    if ( cw->getState() == ClipWorkflow::Sleeping ||
         cw->getState() == ClipWorkflow::Ready ||
         cw->getState() == ClipWorkflow::EndReached )
223
    {
224
        cw->getStateLock()->unlock();
225 226 227 228
        {
            QMutexLocker    lock( cw->getSleepMutex() );
            cw->queryStateChange( ClipWorkflow::Stopping );
        }
229 230
        cw->wake();
        cw->stop();
231
    }
232
    else if ( cw->getState() == ClipWorkflow::Rendering )
233
    {
234
        cw->getStateLock()->unlock();
235
        cw->waitForCompleteRender();
236 237 238 239
        {
            QMutexLocker    lock( cw->getSleepMutex() );
            cw->queryStateChange( ClipWorkflow::Stopping );
        }
240 241
        cw->wake();
        cw->stop();
242
    }
243
    else if ( cw->getState() == ClipWorkflow::Initializing )
244
    {
245
        cw->getStateLock()->unlock();
246
        cw->waitForCompleteInit();
247
        cw->stop();
248
    }
249 250 251 252
    else if ( cw->getState() == ClipWorkflow::Paused )
    {
        cw->getStateLock()->unlock();
        cw->queryStateChange( ClipWorkflow::Stopping );
253
        cw->wake();
254 255
        cw->stop();
    }
256 257
    else
    {
258
        qDebug() << "Unexpected ClipWorkflow::State when stopping :" << cw->getState();
259
        cw->getStateLock()->unlock();
260 261 262
    }
}

263 264 265 266 267 268 269 270 271 272
bool                TrackWorkflow::checkEnd( qint64 currentFrame ) const
{
    if ( m_clips.size() == 0 )
        return true;
    //This is the last video by chronological order :
    QMap<qint64, ClipWorkflow*>::const_iterator   it = m_clips.end() - 1;
    //If it ends before the current frame, we reached end.
    return ( it.value()->getClip()->getLength() + it.key() < currentFrame );
}

273 274
void                    TrackWorkflow::stop()
{
275 276
    QMap<qint64, ClipWorkflow*>::iterator       it = m_clips.begin();
    QMap<qint64, ClipWorkflow*>::iterator       end = m_clips.end();
277 278 279 280 281 282 283 284

    while ( it != end )
    {
        stopClipWorkflow( it.value() );
        ++it;
    }
}

285
bool                TrackWorkflow::getOutput( qint64 currentFrame )
286
{
287 288
    QReadLocker     lock( m_clipsLock );

289 290 291 292 293
    QMap<qint64, ClipWorkflow*>::iterator       it = m_clips.begin();
    QMap<qint64, ClipWorkflow*>::iterator       end = m_clips.end();
    static  qint64                              lastFrame = 0;
    bool                                        needRepositioning;
    bool                                        hasRendered = false;
294

295 296 297
    if ( checkEnd( currentFrame ) == true )
    {
        emit trackEndReached( m_trackId );
298
        //We continue, as there can be ClipWorkflow that requires to be stopped.
299
    }
300 301 302 303 304 305 306
    {
        QMutexLocker    lock( m_forceRepositionningMutex );
        if ( m_forceRepositionning == true )
        {
            needRepositioning = true;
            m_forceRepositionning = false;
        }
307
        else if ( m_paused == true && currentFrame != lastFrame )
308
            needRepositioning = true;
309
        else
310
            needRepositioning = ( abs( currentFrame - lastFrame ) > 1 ) ? true : false;
311
    }
312
    m_nbClipToRender = 0;
313

314
    while ( it != end )
315
    {
316 317 318
        qint64          start = it.key();
        ClipWorkflow*   cw = it.value();
        //Is the clip supposed to render now ?
319
//        qDebug() << "Start:" << start << "Current Frame:" << currentFrame;
320
        if ( start <= currentFrame && currentFrame <= start + cw->getClip()->getLength() )
321
        {
322
            m_nbClipToRender.fetchAndAddAcquire( 1 );
323
            renderClip( cw, currentFrame, start, needRepositioning );
324
            hasRendered = true;
325
        }
326 327 328
        //Is it about to be rendered ?
        else if ( start > currentFrame &&
                start - currentFrame < TrackWorkflow::nbFrameBeforePreload )
329
        {
330
            preloadClip( cw );
331
        }
332 333
        //Is it supposed to be stopped ?
        else
334
        {
335
            stopClipWorkflow( cw );
336
        }
337 338

        ++it;
339
    }
340
    if ( hasRendered == false )
341 342 343
    {
        clipWorkflowRenderCompleted( NULL );
    }
344
    lastFrame = currentFrame;
345
    return hasRendered;
346 347
}

348
void                TrackWorkflow::pause()
349 350 351
{
    QReadLocker     lock( m_clipsLock );

352 353 354
    QMap<qint64, ClipWorkflow*>::iterator       it = m_clips.begin();
    QMap<qint64, ClipWorkflow*>::iterator       end = m_clips.end();
    bool                                        pauseRequired = false;
355

356
    m_nbClipToPause = 0;
357
    for ( ; it != end; ++it )
358 359 360
    {
        ClipWorkflow*   cw = it.value();

361
        cw->getStateLock()->lockForRead();
362 363 364 365 366
        if ( cw->getState() == ClipWorkflow::Stopped )
        {
            cw->getStateLock()->unlock();
            continue ;
        }
367
        else if ( cw->getState() != ClipWorkflow::Paused )
368
        {
369
            cw->getStateLock()->unlock();
370
            m_nbClipToPause.fetchAndAddAcquire( 1 );
371
            cw->pause();
372
            pauseRequired = true;
373 374 375
        }
        else
        {
376
            //This should never be used.
377
            qDebug() << "Asking to pause in an already paused state";
378
            cw->getStateLock()->unlock();
379 380
        }
    }
381 382 383 384
    if ( pauseRequired == false )
    {
        clipWorkflowPaused();
    }
385 386
}

387
void            TrackWorkflow::moveClip( const QUuid& id, qint64 startingFrame )
388
{
389 390
    QWriteLocker    lock( m_clipsLock );

391 392
    QMap<qint64, ClipWorkflow*>::iterator       it = m_clips.begin();
    QMap<qint64, ClipWorkflow*>::iterator       end = m_clips.end();
393 394 395

    while ( it != end )
    {
396
        if ( it.value()->getClip()->getUuid() == id )
397 398 399 400 401 402
        {
            ClipWorkflow* cw = it.value();
            m_clips.erase( it );
            m_clips[startingFrame] = cw;
            QMutexLocker    lock( m_forceRepositionningMutex );
            m_forceRepositionning = true;
403
            computeLength();
404 405 406 407 408 409 410
            return ;
        }
        ++it;
    }
    qDebug() << "Track" << m_trackId << "was asked to move clip" << id << "to position" << startingFrame
            << "but this clip doesn't exist in this track";
}
411 412 413 414 415

Clip*       TrackWorkflow::removeClip( const QUuid& id )
{
    QWriteLocker    lock( m_clipsLock );

416 417
    QMap<qint64, ClipWorkflow*>::iterator       it = m_clips.begin();
    QMap<qint64, ClipWorkflow*>::iterator       end = m_clips.end();
418 419 420

    while ( it != end )
    {
421
        if ( it.value()->getClip()->getUuid() == id )
422 423
        {
            ClipWorkflow*   cw = it.value();
424
            Clip*           clip = cw->getClip();
425
            m_clips.erase( it );
426
            computeLength();
427
            disconnectClipWorkflow( cw );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
428
            delete cw;
429
            return clip;
430 431 432 433 434
        }
        ++it;
    }
    return NULL;
}
435

436 437 438 439 440 441 442 443 444 445 446 447
ClipWorkflow*       TrackWorkflow::removeClipWorkflow( const QUuid& id )
{
    QWriteLocker    lock( m_clipsLock );

    QMap<qint64, ClipWorkflow*>::iterator       it = m_clips.begin();
    QMap<qint64, ClipWorkflow*>::iterator       end = m_clips.end();

    while ( it != end )
    {
        if ( it.value()->getClip()->getUuid() == id )
        {
            ClipWorkflow*   cw = it.value();
448
            disconnectClipWorkflow( cw );
449 450 451 452 453 454 455 456 457 458
            m_clips.erase( it );
            computeLength();
            return cw;

        }
        ++it;
    }
    return NULL;
}

459 460
void        TrackWorkflow::clipWorkflowRenderCompleted( ClipWorkflow* cw )
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
461
//    qDebug() << "Clip [" << QObject::sender() << "] render is completed on track" << m_trackId;
462
    if ( cw != NULL )
463
    {
464
        m_synchroneRenderBuffer = cw->getOutput();
465
    }
466
    else
467
    {
468
        m_synchroneRenderBuffer = NULL;
469
    }
470
    m_nbClipToRender.fetchAndAddAcquire( -1 );
471 472 473
    //When there is nothing to render, m_nbClipToRender will be equal to one here, so we check for minus
    //or equal to 0
    if ( m_nbClipToRender <= 0 )
474
    {
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
475
//        qDebug() << "Track render completed";
476
        emit renderCompleted( m_trackId );
477
    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
478
//    else
479
//        qDebug() << "Track render not completed yet";
480
}
481

482
void*       TrackWorkflow::getSynchroneOutput()
483 484 485
{
    return m_synchroneRenderBuffer;
}
486 487 488 489 490 491

void    TrackWorkflow::clipWorkflowPaused()
{
    m_nbClipToPause.fetchAndAddAcquire( -1 );
    if ( m_nbClipToPause <= 0 )
    {
492
        m_paused = true;
493 494 495 496 497 498 499 500
        emit trackPaused();
    }
}

void    TrackWorkflow::unpause()
{
    QReadLocker     lock( m_clipsLock );

501 502
    QMap<qint64, ClipWorkflow*>::iterator       it = m_clips.begin();
    QMap<qint64, ClipWorkflow*>::iterator       end = m_clips.end();
503
    bool                                        unpauseRequired = false;
504 505 506 507 508 509 510 511 512 513 514 515

    m_nbClipToUnpause = 0;
    for ( ; it != end; ++it )
    {
        ClipWorkflow*   cw = it.value();

        cw->getStateLock()->lockForRead();
        if ( cw->getState() == ClipWorkflow::Paused )
        {
            cw->getStateLock()->unlock();
            m_nbClipToUnpause.fetchAndAddAcquire( 1 );
            cw->unpause();
516
            unpauseRequired = true;
517 518 519 520 521 522
        }
        else
        {
            cw->getStateLock()->unlock();
        }
    }
523 524
    if ( unpauseRequired == false )
        clipWorkflowUnpaused();
525 526 527 528 529 530 531
}

void    TrackWorkflow::clipWorkflowUnpaused()
{
    m_nbClipToUnpause.fetchAndAddAcquire( -1 );
    if ( m_nbClipToUnpause <= 0 )
    {
532
        m_paused = false;
533 534 535
        emit trackUnpaused();
    }
}
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579

void    TrackWorkflow::save( QDomDocument& doc, QDomElement& trackNode ) const
{
    QReadLocker     lock( m_clipsLock );

    QMap<qint64, ClipWorkflow*>::const_iterator     it = m_clips.begin();
    QMap<qint64, ClipWorkflow*>::const_iterator     end = m_clips.end();

    for ( ; it != end ; ++it )
    {
        QDomElement     clipNode = doc.createElement( "clip" );

        {
            QDomElement     parent = doc.createElement( "parent" );

            QDomCharacterData   text = doc.createTextNode( it.value()->getClip()->getParent()->getUuid().toString() );
            parent.appendChild( text );
            clipNode.appendChild( parent );
        }
        {
            QDomElement     startFrame = doc.createElement( "startFrame" );

            QDomCharacterData   text = doc.createTextNode( QString::number( it.key() ) );
            startFrame.appendChild( text );
            clipNode.appendChild( startFrame );
        }
        {
            QDomElement     begin = doc.createElement( "begin" );

            QDomCharacterData   text = doc.createTextNode( QString::number( it.value()->getClip()->getBegin() ) );
            begin.appendChild( text );
            clipNode.appendChild( begin );
        }
        {
            QDomElement     end = doc.createElement( "end" );

            QDomCharacterData   text = doc.createTextNode( QString::number( it.value()->getClip()->getEnd() ) );
            end.appendChild( text );
            clipNode.appendChild( end );
        }
        trackNode.appendChild( clipNode );
    }
}

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
void    TrackWorkflow::clear()
{
    QWriteLocker    lock( m_clipsLock );
    QMap<qint64, ClipWorkflow*>::iterator       it = m_clips.begin();
    QMap<qint64, ClipWorkflow*>::iterator       end = m_clips.end();

    for ( ; it != end; ++it )
    {
        ClipWorkflow*   cw = it.value();
        delete cw->getClip();
        delete cw;
    }
    m_clips.clear();
    m_length = 0;
}
595 596 597 598

void    TrackWorkflow::adjustClipTime( qint64 currentFrame, qint64 start, ClipWorkflow* cw )
{
    qint64  nbMs = ( currentFrame - start ) / cw->getClip()->getParent()->getFps() * 1000;
599 600
    qint64  beginInMs = cw->getClip()->getBegin() / cw->getClip()->getParent()->getFps() * 1000;
    qint64  startFrame = beginInMs + nbMs;
601 602
    cw->setTime( startFrame );
}
603 604 605 606 607 608 609 610 611 612 613 614

void        TrackWorkflow::setFullSpeedRender( bool value )
{
    QMap<qint64, ClipWorkflow*>::iterator       it = m_clips.begin();
    QMap<qint64, ClipWorkflow*>::iterator       end = m_clips.end();

    while ( it != end )
    {
        it.value()->setFullSpeedRender( value );
        ++it;
    }
}
615 616 617 618 619 620 621

void    TrackWorkflow::disconnectClipWorkflow( ClipWorkflow* cw )
{
    disconnect( cw, SIGNAL( renderComplete( ClipWorkflow* ) ), this, SLOT( clipWorkflowRenderCompleted( ClipWorkflow* ) ) );
    disconnect( cw, SIGNAL( paused() ), this, SLOT( clipWorkflowPaused() ) );
    disconnect( cw, SIGNAL( unpaused() ), this, SLOT( clipWorkflowUnpaused() ) );
}
622 623 624 625 626 627

void    TrackWorkflow::forceRepositionning()
{
    QMutexLocker    lock( m_forceRepositionningMutex );
    m_forceRepositionning = true;
}