MainWorkflow.cpp 13.5 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
/*****************************************************************************
 * 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"
27 28
#include "TrackWorkflow.h"
#include "TrackHandler.h"
29

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
30 31 32 33 34
//JUST FOR THE DEFINES !
//TODO:
//FIXME: remove this !
#include "ClipWorkflow.h"

35 36
LightVideoFrame*     MainWorkflow::blackOutput = NULL;

37
MainWorkflow::MainWorkflow( int trackCount ) :
38
        m_currentFrame( 0 ),
39
        m_lengthFrame( 0 ),
40
        m_renderStarted( false )
41
{
42
    m_currentFrameLock = new QReadWriteLock;
43
    m_renderStartedLock = new QReadWriteLock;
44
    m_renderMutex = new QMutex;
45 46
    m_synchroneRenderWaitCondition = new QWaitCondition;
    m_synchroneRenderWaitConditionMutex = new QMutex;
47 48 49 50

    m_effectEngine = new EffectsEngine;
    m_effectEngine->disable();

51 52
    m_tracks = new TrackHandler*[MainWorkflow::NbTrackType];
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
53
    {
54
        MainWorkflow::TrackType trackType = (i == 0 ? MainWorkflow::VideoTrack : MainWorkflow::AudioTrack );
55
        m_tracks[i] = new TrackHandler( trackCount, trackType, m_effectEngine );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
56
        connect( m_tracks[i], SIGNAL( tracksPaused() ), this, SLOT( tracksPaused() ) );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
57
        connect( m_tracks[i], SIGNAL( tracksUnpaused() ), this, SLOT( tracksUnpaused() ) );
58
        connect( m_tracks[i], SIGNAL( allTracksRenderCompleted() ), this, SLOT( tracksRenderCompleted() ) );
59
        connect( m_tracks[i], SIGNAL( tracksEndReached() ), this, SLOT( tracksEndReached() ) );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
60 61
    }
    m_outputBuffers = new OutputBuffers;
62 63 64

    blackOutput = new LightVideoFrame( VIDEOHEIGHT * VIDEOWIDTH * Pixel::NbComposantes );
    memset( (*blackOutput)->frame.octets, 0, (*blackOutput)->nboctets );
65 66 67 68
}

MainWorkflow::~MainWorkflow()
{
69
    //FIXME: this is probably useless, since already done by the renderer
70 71
    stop();

72
    delete m_effectEngine;
73 74 75
    delete m_synchroneRenderWaitConditionMutex;
    delete m_synchroneRenderWaitCondition;
    delete m_renderMutex;
76
    delete m_renderStartedLock;
77
    delete m_currentFrameLock;
78
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
79
        delete m_tracks[i];
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
80
    delete[] m_tracks;
81 82
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
83
EffectsEngine*          MainWorkflow::getEffectsEngine()
84
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
85
    return m_effectEngine;
86
}
87

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
88
void            MainWorkflow::addClip( Clip* clip, unsigned int trackId,
89
                                        qint64 start, MainWorkflow::TrackType trackType )
90
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
91
    m_tracks[trackType]->addClip( clip, trackId, start );
92
    computeLength();
93
    //Inform the GUI
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
94
    emit clipAdded( clip, trackId, start, trackType );
95
}
96

97
void            MainWorkflow::computeLength()
98
{
99 100
    qint64      maxLength = 0;

101
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
102 103 104 105
    {
        if ( m_tracks[i]->getLength() > maxLength )
            maxLength = m_tracks[i]->getLength();
    }
106
    m_lengthFrame = maxLength;
107 108
}

109 110 111
void    MainWorkflow::startRender()
{
    m_renderStarted = true;
112
    m_paused = false;
113
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
114
        m_tracks[i]->startRender();
115 116 117
    computeLength();
}

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

123
    if ( m_renderStarted == true )
124
    {
125 126 127 128 129 130
        {
            QReadLocker         lock3( m_currentFrameLock );

            for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
                m_tracks[i]->getOutput( m_currentFrame );
        }
131 132
        if ( m_paused == false )
            nextFrame();
133
    }
134
}
135

136 137
void        MainWorkflow::pause()
{
138 139
    QMutexLocker    lock( m_renderMutex );

140
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
141
        m_tracks[i]->pause();
142 143
}

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

148
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
149
        m_tracks[i]->unpause();
150 151
}

152 153
void        MainWorkflow::nextFrame()
{
154
    QWriteLocker    lock( m_currentFrameLock );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
155

156
    ++m_currentFrame;
157
    emit frameChanged( m_currentFrame, Renderer );
158 159 160 161
}

void        MainWorkflow::previousFrame()
{
162 163
    QWriteLocker    lock( m_currentFrameLock );

164
    --m_currentFrame;
165
    emit frameChanged( m_currentFrame, Renderer );
166 167
}

168
qint64      MainWorkflow::getLengthFrame() const
169
{
170
    return m_lengthFrame;
171
}
172

173
qint64      MainWorkflow::getClipPosition( const QUuid& uuid, unsigned int trackId, MainWorkflow::TrackType trackType ) const
174
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
175
    return m_tracks[trackType]->getClipPosition( uuid, trackId );
176
}
177 178 179 180

void            MainWorkflow::stop()
{
    QWriteLocker    lock( m_renderStartedLock );
181
    QWriteLocker    lock2( m_currentFrameLock );
182 183

    m_renderStarted = false;
184
    for (unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i)
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
185
        m_tracks[i]->stop();
186
    m_currentFrame = 0;
187
    emit frameChanged( 0, Renderer );
188
}
189

190
void           MainWorkflow::moveClip( const QUuid& clipUuid, unsigned int oldTrack,
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
191
                                       unsigned int newTrack, qint64 startingFrame,
192
                                       MainWorkflow::TrackType trackType, bool undoRedoCommand /*= false*/ )
193
{
194
    m_tracks[trackType]->moveClip( clipUuid, oldTrack, newTrack, startingFrame );
195
    computeLength();
196

197 198 199 200
    if ( undoRedoCommand == true )
    {
        emit clipMoved( clipUuid, newTrack, startingFrame, trackType );
    }
201
}
202

203
Clip*       MainWorkflow::removeClip( const QUuid& uuid, unsigned int trackId, MainWorkflow::TrackType trackType )
204
{
205 206
    Clip* clip = m_tracks[trackType]->removeClip( uuid, trackId );
    computeLength();
207
    emit clipRemoved( clip, trackId, trackType );
208
    return clip;
209 210
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
211
MainWorkflow::OutputBuffers*  MainWorkflow::getSynchroneOutput()
212 213 214
{
    m_synchroneRenderWaitConditionMutex->lock();
    getOutput();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
215
//    qDebug() << "Waiting for sync output";
216
    m_synchroneRenderWaitCondition->wait( m_synchroneRenderWaitConditionMutex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
217
//    qDebug() << "Got it";
218
    m_effectEngine->render();
219 220 221 222 223
    if ( m_effectEngine->getOutputFrame( 0 )->nboctets == 0 )
        m_outputBuffers->video = MainWorkflow::blackOutput;
    else
        m_outputBuffers->video = &( m_effectEngine->getOutputFrame( 0 ) );

224
    m_synchroneRenderWaitConditionMutex->unlock();
225 226

    //    m_outputBuffers->video = reinterpret_cast<LightVideoFrame*>( m_tracks[TrackWorkflow::Video]->getSynchroneOutput() );
227
//    m_outputBuffers->audio = reinterpret_cast<unsigned char*>( m_tracks[TrackWorkflow::Audio]->getSynchroneOutput() );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
228
    return m_outputBuffers;
229
}
230 231 232 233 234 235 236 237

void        MainWorkflow::cancelSynchronisation()
{
    {
        QMutexLocker    lock( m_synchroneRenderWaitConditionMutex );
    }
    m_synchroneRenderWaitCondition->wakeAll();
}
238

239
void        MainWorkflow::muteTrack( unsigned int trackId, MainWorkflow::TrackType trackType )
240
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
241
    m_tracks[trackType]->muteTrack( trackId );
242 243
}

244
void        MainWorkflow::unmuteTrack( unsigned int trackId, MainWorkflow::TrackType trackType )
245
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
246
    m_tracks[trackType]->unmuteTrack( trackId );
247
}
248

249
void        MainWorkflow::setCurrentFrame( qint64 currentFrame, MainWorkflow::FrameChangedReason reason )
250
{
251 252
    QWriteLocker    lock( m_currentFrameLock );

253 254 255 256 257 258 259
    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 < MainWorkflow::NbTrackType; ++i)
            m_tracks[i]->activateAll();
    }
260
    m_currentFrame = currentFrame;
261
    emit frameChanged( m_currentFrame, reason );
262
}
263

264
Clip*       MainWorkflow::getClip( const QUuid& uuid, unsigned int trackId, MainWorkflow::TrackType trackType )
265
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
266
    return m_tracks[trackType]->getClip( uuid, trackId );
267
}
268 269 270

void        MainWorkflow::loadProject( const QDomElement& project )
{
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    if ( project.isNull() == true || project.tagName() != "timeline" )
    {
        qWarning() << "Invalid timeline node (" << project.tagName() << ')';
        return ;
    }

    clear();

    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;
            qint64                      begin;
            qint64                      end;
            qint64                      startPos;
301
            MainWorkflow::TrackType     trackType = MainWorkflow::VideoTrack;
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338

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

                if ( tagName == "parent" )
                    parent = QUuid( clipProperty.text() );
                else if ( tagName == "begin" )
                {
                    begin = clipProperty.text().toLongLong( &ok );
                    if ( ok == false )
                    {
                        qWarning() << "Invalid clip begin";
                        return ;
                    }
                }
                else if ( tagName == "end" )
                {
                    end = clipProperty.text().toLongLong( &ok );
                    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 if ( tagName == "trackType" )
                {
339
                    trackType = static_cast<MainWorkflow::TrackType>( clipProperty.text().toUInt( &ok ) );
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
                    if ( ok == false )
                    {
                        qWarning() << "Invalid track type starting frame";
                        return ;
                    }
                }
                else
                    qDebug() << "Unknown field" << clipProperty.tagName();

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

            Clip*       c = new Clip( parent, begin, end );
            addClip( c, trackId, startPos, trackType );

            clip = clip.nextSibling().toElement();
        }
        elem = elem.nextSibling().toElement();
    }
359 360
}

361
void        MainWorkflow::saveProject( QDomDocument& doc, QDomElement& rootNode )
362
{
363
    QDomElement project = doc.createElement( "timeline" );
364
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
365 366 367 368
    {
        m_tracks[i]->save( doc, project );
    }
    rootNode.appendChild( project );
369
}
370 371 372

void        MainWorkflow::clear()
{
373
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
374
        m_tracks[i]->clear();
375
    emit cleared();
376
}
377 378 379

void        MainWorkflow::setFullSpeedRender( bool value )
{
380
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
381 382
        m_tracks[i]->setFullSpeedRender( value );
}
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
383 384 385

void        MainWorkflow::tracksPaused()
{
386
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
387 388
        if ( m_tracks[i]->isPaused() == false )
            return ;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
389
    m_paused = true;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
390 391 392
    emit mainWorkflowPaused();
}

393 394 395 396 397 398 399 400
void        MainWorkflow::tracksEndReached()
{
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
        if ( m_tracks[i]->endIsReached() == false )
            return ;
    emit mainWorkflowEndReached();
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
401 402
void        MainWorkflow::tracksUnpaused()
{
403
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
404 405 406 407 408 409
        if ( m_tracks[i]->isPaused() == true )
            return ;
    m_paused = false;
    emit mainWorkflowUnpaused();
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
410 411
void        MainWorkflow::tracksRenderCompleted()
{
412
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
413 414 415 416 417 418 419
        if ( m_tracks[i]->allTracksRendered() == false )
            return ;
    {
        QMutexLocker    lock( m_synchroneRenderWaitConditionMutex );
    }
    m_synchroneRenderWaitCondition->wakeAll();
}
420

421
int         MainWorkflow::getTrackCount( MainWorkflow::TrackType trackType ) const
422 423 424
{
    return m_tracks[trackType]->getTrackCount();
}
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
425

426 427
qint64      MainWorkflow::getCurrentFrame() const
{
428 429
    QReadLocker     lock( m_currentFrameLock );

430 431
    return m_currentFrame;
}