TrackWorkflow.cpp 15.4 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 "ImageClipWorkflow.h"
29
#include "AudioClipWorkflow.h"
30 31 32 33 34
#include "Clip.h"
#include "Media.h"
#include <QReadWriteLock>
#include <QDomDocument>
#include <QDomElement>
35

36
TrackWorkflow::TrackWorkflow( unsigned int trackId, MainWorkflow::TrackType type  ) :
37
        m_trackId( trackId ),
38
        m_length( 0 ),
39
        m_forceRepositionning( false ),
40
        m_paused( false ),
41
        m_trackType( type ),
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
42 43 44
        m_lastFrame( 0 ),
        m_videoStackedBuffer( NULL ),
        m_audioStackedBuffer( NULL )
45
{
46
    m_forceRepositionningMutex = new QMutex;
47
    m_clipsLock = new QReadWriteLock;
48 49
}

50 51
TrackWorkflow::~TrackWorkflow()
{
52 53
    QMap<qint64, ClipWorkflow*>::iterator       it = m_clips.begin();
    QMap<qint64, ClipWorkflow*>::iterator       end = m_clips.end();
54 55 56 57 58 59 60

    while ( it != end )
    {
        stopClipWorkflow( it.value() );
        delete it.value();
        it = m_clips.erase( it );
    }
61
    delete m_clipsLock;
62
    delete m_forceRepositionningMutex;
63 64
}

65
void    TrackWorkflow::addClip( Clip* clip, qint64 start )
66
{
67
    ClipWorkflow* cw;
68
    if ( m_trackType == MainWorkflow::VideoTrack )
69 70 71 72 73 74
    {
        if ( clip->getParent()->getFileType() == Media::Video )
            cw = new VideoClipWorkflow( clip );
        else
            cw = new ImageClipWorkflow( clip );
    }
75 76
    else
        cw = new AudioClipWorkflow( clip );
77
    addClip( cw, start );
78 79
}

80 81 82 83 84 85 86
void    TrackWorkflow::addClip( ClipWorkflow* cw, qint64 start )
{
    QWriteLocker    lock( m_clipsLock );
    m_clips.insert( start, cw );
    computeLength();
}

87
//Must be called from a thread safe method (m_clipsLock locked)
88 89 90
void                TrackWorkflow::computeLength()
{
    if ( m_clips.count() == 0 )
91
    {
92
        m_length = 0;
93 94
        return ;
    }
95
    QMap<qint64, ClipWorkflow*>::const_iterator it = m_clips.end() - 1;
96 97 98 99 100 101 102 103
    m_length = (it.key() + it.value()->getClip()->getLength() );
}

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

104
qint64              TrackWorkflow::getClipPosition( const QUuid& uuid ) const
105
{
106 107
    QMap<qint64, ClipWorkflow*>::const_iterator     it = m_clips.begin();
    QMap<qint64, ClipWorkflow*>::const_iterator     end = m_clips.end();
108 109 110

    while ( it != end )
    {
111
        if ( it.value()->getClip()->getUuid() == uuid )
112 113 114 115
            return it.key();
        ++it;
    }
    return -1;
116 117
}

118 119 120 121 122 123 124
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 )
    {
125
        if ( it.value()->getClip()->getUuid() == uuid )
126 127 128 129 130 131
            return it.value()->getClip();
        ++it;
    }
    return NULL;
}

132
void*       TrackWorkflow::renderClip( ClipWorkflow* cw, qint64 currentFrame,
133
                                        qint64 start , bool needRepositioning )
134
{
135
    cw->getStateLock()->lockForRead();
136

137
    qDebug() << "TrackWorkflow::renderClip. currentFrame:" << currentFrame << "trackType:" << m_trackType;
138
//    qDebug() << "Rendering clip" << cw << "state:" << cw->getState() << "Type:" << m_trackType;
139 140 141 142
    if ( cw->getState() == ClipWorkflow::Rendering ||
         cw->getState() == ClipWorkflow::Paused ||
         cw->getState() == ClipWorkflow::PauseRequired ||
         cw->getState() == ClipWorkflow::UnpauseRequired )
143
    {
144 145
        cw->getStateLock()->unlock();
        if ( needRepositioning == true )
146 147
        {
            qDebug() << "adjusting because of needRepositionning";
148
            adjustClipTime( currentFrame, start, cw );
149
        }
150
        return cw->getOutput( ClipWorkflow::Pop );
151
    }
152
    else if ( cw->getState() == ClipWorkflow::Stopped )
153
    {
154
        cw->getStateLock()->unlock();
155
        cw->initialize();
156
        if ( start != currentFrame || cw->getClip()->getBegin() != 0 ) //Clip was not started as its real begining
157
        {
158 159
            qDebug() << "Adjusting clip start. start:" << start << "currentframe:" << currentFrame
                    << "clip begin:" << cw->getClip()->getBegin();
160
            adjustClipTime( currentFrame, start, cw );
161
        }
162 163
        cw->waitForCompleteInit();
        return cw->getOutput( ClipWorkflow::Pop );
164
    }
165 166 167 168 169
    else if ( cw->getState() == ClipWorkflow::EndReached )
    {
        cw->getStateLock()->unlock();
        //The stopClipWorkflow() method will take care of that.
    }
170
    else
171
    {
172
        qCritical() << "Unexpected state:" << cw->getState();
173
        cw->getStateLock()->unlock();
174
    }
175
    return NULL;
176
}
177

178
void                TrackWorkflow::preloadClip( ClipWorkflow* cw )
179
{
180
    cw->getStateLock()->lockForRead();
181

182
    if ( cw->getState() == ClipWorkflow::Stopped )
183
    {
184
        cw->getStateLock()->unlock();
185
        cw->initialize();
186
        return ;
187
    }
188
    cw->getStateLock()->unlock();
189 190
}

191
void                TrackWorkflow::stopClipWorkflow( ClipWorkflow* cw )
192
{
193
//    qDebug() << "Stopping clip workflow";
194
    cw->getStateLock()->lockForRead();
195

196
    if ( cw->getState() == ClipWorkflow::Stopped )
197
    {
198 199
        cw->getStateLock()->unlock();
        return ;
200
    }
201 202
    if ( cw->getState() == ClipWorkflow::EndReached ||
         cw->getState() == ClipWorkflow::Rendering )
203
    {
204
        cw->getStateLock()->unlock();
205 206
        cw->stop();
    }
207 208
    else
    {
209
        qCritical() << "Unexpected ClipWorkflow::State when stopping :" << cw->getState();
210
        cw->getStateLock()->unlock();
211 212 213
    }
}

214 215 216 217 218 219 220 221 222 223
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 );
}

224 225
void                    TrackWorkflow::stop()
{
226 227
    QMap<qint64, ClipWorkflow*>::iterator       it = m_clips.begin();
    QMap<qint64, ClipWorkflow*>::iterator       end = m_clips.end();
228 229 230 231 232 233

    while ( it != end )
    {
        stopClipWorkflow( it.value() );
        ++it;
    }
234
    m_lastFrame = 0;
235 236
}

237 238 239 240 241 242 243 244
void                TrackWorkflow::releasePreviousRender()
{
    if ( m_audioStackedBuffer != NULL )
        m_audioStackedBuffer->release();
    if ( m_videoStackedBuffer != NULL )
        m_videoStackedBuffer->release();
}

245
void*               TrackWorkflow::getOutput( qint64 currentFrame )
246
{
247
    releasePreviousRender();
248 249
    QReadLocker     lock( m_clipsLock );

250 251 252
    QMap<qint64, ClipWorkflow*>::iterator       it = m_clips.begin();
    QMap<qint64, ClipWorkflow*>::iterator       end = m_clips.end();
    bool                                        needRepositioning;
253
    void*                                       ret = NULL;
254

255 256 257
    if ( checkEnd( currentFrame ) == true )
    {
        emit trackEndReached( m_trackId );
258
        //We continue, as there can be ClipWorkflow that requires to be stopped.
259
    }
260 261 262 263
    {
        QMutexLocker    lock( m_forceRepositionningMutex );
        if ( m_forceRepositionning == true )
        {
264
            qDebug() << "Force repositionning is required.";
265 266 267
            needRepositioning = true;
            m_forceRepositionning = false;
        }
268 269 270 271 272
        else if ( m_paused == true && currentFrame != m_lastFrame )
        {
            needRepositioning = true;
            qDebug() << "Paused is true, currentframe != lastframe -> repo";
        }
273
        else
274 275 276 277 278 279 280 281
        {
            needRepositioning = ( abs( currentFrame - m_lastFrame ) > 1 ) ? true : false;
            if ( needRepositioning == true )
            {
                qDebug() << "Need repo because more than 1 frame has passed. currentFrame:"
                        << currentFrame << "m_lastFrame:" << m_lastFrame;
            }
        }
282
    }
283

284
    while ( it != end )
285
    {
286 287 288
        qint64          start = it.key();
        ClipWorkflow*   cw = it.value();
        //Is the clip supposed to render now ?
289
//        qDebug() << "Start:" << start << "Current Frame:" << currentFrame;
290
        if ( start <= currentFrame && currentFrame <= start + cw->getClip()->getLength() )
291
        {
292 293 294
            if ( ret != NULL )
                qCritical() << "There's more than one clip to render here. Undefined behaviour !";
            ret = renderClip( cw, currentFrame, start, needRepositioning );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
295
            if ( m_trackType == MainWorkflow::VideoTrack )
296
                m_videoStackedBuffer = reinterpret_cast<StackedBuffer<LightVideoFrame*>*>( ret );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
297
            else
298
                m_audioStackedBuffer = reinterpret_cast<StackedBuffer<AudioClipWorkflow::AudioSample*>*>( ret );
299
        }
300 301 302 303 304 305
        //Is it about to be rendered ?
        else if ( start > currentFrame &&
                start - currentFrame < TrackWorkflow::nbFrameBeforePreload )
            preloadClip( cw );
        //Is it supposed to be stopped ?
        else
306 307
            stopClipWorkflow( cw );
        ++it;
308
    }
309
    m_lastFrame = currentFrame;
310
    return ret;
311 312
}

313
void                TrackWorkflow::pause()
314
{
315
    m_paused = true;
316 317
}

318
void            TrackWorkflow::moveClip( const QUuid& id, qint64 startingFrame )
319
{
320 321
    QWriteLocker    lock( m_clipsLock );

322 323
    QMap<qint64, ClipWorkflow*>::iterator       it = m_clips.begin();
    QMap<qint64, ClipWorkflow*>::iterator       end = m_clips.end();
324 325 326

    while ( it != end )
    {
327
        if ( it.value()->getClip()->getUuid() == id )
328 329 330 331 332 333
        {
            ClipWorkflow* cw = it.value();
            m_clips.erase( it );
            m_clips[startingFrame] = cw;
            QMutexLocker    lock( m_forceRepositionningMutex );
            m_forceRepositionning = true;
334
            computeLength();
335 336 337 338 339 340 341
            return ;
        }
        ++it;
    }
    qDebug() << "Track" << m_trackId << "was asked to move clip" << id << "to position" << startingFrame
            << "but this clip doesn't exist in this track";
}
342 343 344 345 346

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

347 348
    QMap<qint64, ClipWorkflow*>::iterator       it = m_clips.begin();
    QMap<qint64, ClipWorkflow*>::iterator       end = m_clips.end();
349 350 351

    while ( it != end )
    {
352
        if ( it.value()->getClip()->getUuid() == id )
353 354
        {
            ClipWorkflow*   cw = it.value();
355
            Clip*           clip = cw->getClip();
356
            m_clips.erase( it );
357
            stopClipWorkflow( cw );
358
            computeLength();
359
            disconnectClipWorkflow( cw );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
360
            delete cw;
361 362
            if ( m_length == 0 )
                emit trackEndReached( m_trackId );
363
            return clip;
364 365 366 367 368
        }
        ++it;
    }
    return NULL;
}
369

370 371 372 373 374 375 376 377 378 379 380 381
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();
382
            disconnectClipWorkflow( cw );
383 384 385 386 387 388 389 390 391 392
            m_clips.erase( it );
            computeLength();
            return cw;

        }
        ++it;
    }
    return NULL;
}

393 394
void    TrackWorkflow::unpause()
{
395
    m_paused = false;
396
}
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436

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 );
        }
437 438 439 440 441 442 443
        {
            QDomElement     trackType = doc.createElement( "trackType" );

            QDomCharacterData   text = doc.createTextNode( QString::number( m_trackType ) );
            trackType.appendChild( text );
            clipNode.appendChild( trackType );
        }
444 445 446 447
        trackNode.appendChild( clipNode );
    }
}

448 449 450 451 452 453 454 455
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 )
    {
456
        //The clip contained in the trackworkflow will be delete by the undo stack.
457
        ClipWorkflow*   cw = it.value();
458
        delete cw->getClip();
459 460 461 462 463
        delete cw;
    }
    m_clips.clear();
    m_length = 0;
}
464 465 466

void    TrackWorkflow::adjustClipTime( qint64 currentFrame, qint64 start, ClipWorkflow* cw )
{
467 468
    if ( m_trackType == MainWorkflow::AudioTrack )
        qDebug() << "Adjusting clip time for audio:" << currentFrame;
469
    qint64  nbMs = ( currentFrame - start ) / cw->getClip()->getParent()->getFps() * 1000;
470 471
    qint64  beginInMs = cw->getClip()->getBegin() / cw->getClip()->getParent()->getFps() * 1000;
    qint64  startFrame = beginInMs + nbMs;
472 473
    cw->setTime( startFrame );
}
474

475 476 477 478 479 480
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() ) );
}
481 482 483 484 485 486

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