MainWorkflow.cpp 12.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"

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

33
MainWorkflow::MainWorkflow( int trackCount ) :
34
        m_currentFrame( 0 ),
35
        m_length( 0 ),
36
        m_renderStarted( false )
37
{
38
    m_renderStartedLock = new QReadWriteLock;
39
    m_renderMutex = new QMutex;
40 41
    m_synchroneRenderWaitCondition = new QWaitCondition;
    m_synchroneRenderWaitConditionMutex = new QMutex;
42 43 44 45 46

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

    m_tracks = new TrackHandler*[TrackWorkflow::NbType];
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
47 48
    for ( unsigned int i = 0; i < TrackWorkflow::NbType; ++i )
    {
49
        TrackWorkflow::TrackType trackType = (i == 0 ? TrackWorkflow::Video : TrackWorkflow::Audio );
50
        m_tracks[i] = new TrackHandler( trackCount, trackType, m_effectEngine );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
51
        connect( m_tracks[i], SIGNAL( tracksPaused() ), this, SLOT( tracksPaused() ) );
52
        connect( m_tracks[i], SIGNAL( allTracksRenderCompleted() ), this, SLOT( tracksRenderCompleted() ) );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
53 54
    }
    m_outputBuffers = new OutputBuffers;
55 56 57 58
}

MainWorkflow::~MainWorkflow()
{
59
    //FIXME: this is probably useless, since already done by the renderer
60 61
    stop();

62
    delete m_effectEngine;
63 64 65
    delete m_synchroneRenderWaitConditionMutex;
    delete m_synchroneRenderWaitCondition;
    delete m_renderMutex;
66
    delete m_renderStartedLock;
67 68
    for ( unsigned int i = 0; i < TrackWorkflow::NbType; ++i )
        delete m_tracks[i];
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
69
    delete[] m_tracks;
70 71
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
72
EffectsEngine*          MainWorkflow::getEffectsEngine()
73
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
74
    return m_effectEngine;
75
}
76

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
77 78
void            MainWorkflow::addClip( Clip* clip, unsigned int trackId,
                                        qint64 start, TrackWorkflow::TrackType trackType )
79
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
80
    m_tracks[trackType]->addClip( clip, trackId, start );
81

82
    //Inform the GUI
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
83
    emit clipAdded( clip, trackId, start, trackType );
84
}
85

86
void            MainWorkflow::computeLength()
87
{
88 89
    qint64      maxLength = 0;

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
90
    for ( unsigned int i = 0; i < TrackWorkflow::NbType; ++i )
91 92 93 94 95
    {
        if ( m_tracks[i]->getLength() > maxLength )
            maxLength = m_tracks[i]->getLength();
    }
    m_length = maxLength;
96 97
}

98 99 100
void    MainWorkflow::startRender()
{
    m_renderStarted = true;
101
    m_paused = false;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
102 103
    for ( unsigned int i = 0; i < TrackWorkflow::NbType; ++i )
        m_tracks[i]->startRender();
104 105 106
    computeLength();
}

107
void                    MainWorkflow::getOutput()
108
{
109 110
    QReadLocker         lock( m_renderStartedLock );
    QMutexLocker        lock2( m_renderMutex );
111

112
    if ( m_renderStarted == true )
113
    {
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
114
        for ( unsigned int i = 0; i < TrackWorkflow::NbType; ++i )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
115
            m_tracks[i]->getOutput( m_currentFrame );
116 117
        if ( m_paused == false )
            nextFrame();
118
    }
119
}
120

121 122
void        MainWorkflow::pause()
{
123 124
    QMutexLocker    lock( m_renderMutex );

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
125 126
    for ( unsigned int i = 0; i < TrackWorkflow::NbType; ++i )
        m_tracks[i]->pause();
127 128
}

129 130 131 132
void        MainWorkflow::unpause()
{
    QMutexLocker    lock( m_renderMutex );

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
133 134
    for ( unsigned int i = 0; i < TrackWorkflow::NbType; ++i )
        m_tracks[i]->unpause();
135 136
}

137 138 139 140 141 142 143 144 145 146 147 148 149 150
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 );
}

151 152
void        MainWorkflow::setPosition( float pos )
{
153 154 155 156
    if ( m_renderStarted == true )
    {
        //Since any track can be reactivated, we reactivate all of them, and let them
        //unable themself if required.
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
157 158
        for ( unsigned int i = 0; i < TrackWorkflow::NbType; ++i)
            m_tracks[i]->activateAll();
159
    }
chouquette's avatar
chouquette committed
160
    qint64  frame = static_cast<qint64>( (float)m_length * pos );
161 162
    m_currentFrame = frame;
    emit frameChanged( frame );
163
    //Do not emit a signal for the RenderWidget, since it's the one that triggered that call...
164 165 166 167 168 169
}

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

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

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

    m_renderStarted = false;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
181 182
    for (unsigned int i = 0; i < TrackWorkflow::NbType; ++i)
        m_tracks[i]->stop();
183 184
    m_currentFrame = 0;
    emit frameChanged( 0 );
185
    emit positionChanged( 0 );
186
}
187

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

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
200
Clip*       MainWorkflow::removeClip( const QUuid& uuid, unsigned int trackId, TrackWorkflow::TrackType trackType )
201
{
202
    emit clipRemoved( uuid, trackId, trackType );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
203
    return m_tracks[trackType]->removeClip( uuid, trackId );
204 205
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
206
MainWorkflow::OutputBuffers*  MainWorkflow::getSynchroneOutput()
207 208 209
{
    m_synchroneRenderWaitConditionMutex->lock();
    getOutput();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
210
//    qDebug() << "Waiting for sync output";
211
    m_synchroneRenderWaitCondition->wait( m_synchroneRenderWaitConditionMutex );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
212
//    qDebug() << "Got it";
213
    m_effectEngine->render();
214
    m_synchroneRenderWaitConditionMutex->unlock();
215 216
    m_outputBuffers->video = reinterpret_cast<LightVideoFrame*>( m_tracks[TrackWorkflow::Video]->getSynchroneOutput() );
    m_outputBuffers->audio = reinterpret_cast<unsigned char*>( m_tracks[TrackWorkflow::Audio]->getSynchroneOutput() );
217
    qDebug() << "video ptr:" << (void*) m_outputBuffers->video;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
218
    return m_outputBuffers;
219
}
220 221 222 223 224 225 226 227

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

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
229
void        MainWorkflow::muteTrack( unsigned int trackId, TrackWorkflow::TrackType trackType )
230
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
231
    m_tracks[trackType]->muteTrack( trackId );
232 233
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
234
void        MainWorkflow::unmuteTrack( unsigned int trackId, TrackWorkflow::TrackType trackType )
235
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
236
    m_tracks[trackType]->unmuteTrack( trackId );
237
}
238 239 240 241 242 243

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

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
245
Clip*       MainWorkflow::getClip( const QUuid& uuid, unsigned int trackId, TrackWorkflow::TrackType trackType )
246
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
247
    return m_tracks[trackType]->getClip( uuid, trackId );
248
}
249 250 251

void        MainWorkflow::loadProject( const QDomElement& project )
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 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 301 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
//    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;
//
//            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
//                    qDebug() << "Unknown field" << clipProperty.tagName();
//
//                clipProperty = clipProperty.nextSibling().toElement();
//            }
//
//            Clip*       c = new Clip( parent, begin, end );
//            addClip( c, trackId, startPos, TrackWorkflow::Video );
//
//            clip = clip.nextSibling().toElement();
//        }
//        elem = elem.nextSibling().toElement();
//    }
330 331
}

332
void        MainWorkflow::saveProject( QDomDocument& doc, QDomElement& rootNode )
333
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347
//    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 );
//        }
//    }
//    rootNode.appendChild( project );
348
}
349 350 351

void        MainWorkflow::clear()
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
352
    for ( unsigned int i = 0; i < TrackWorkflow::NbType; ++i )
353
        m_tracks[i]->clear();
354
    emit cleared();
355
}
356 357 358

void        MainWorkflow::setFullSpeedRender( bool value )
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
359
    for ( unsigned int i = 0; i < TrackWorkflow::NbType; ++i )
360 361
        m_tracks[i]->setFullSpeedRender( value );
}
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380

void        MainWorkflow::tracksPaused()
{
    for ( unsigned int i = 0; i < TrackWorkflow::NbType; ++i )
        if ( m_tracks[i]->isPaused() == false )
            return ;
    emit mainWorkflowPaused();
}

void        MainWorkflow::tracksRenderCompleted()
{
    for ( unsigned int i = 0; i < TrackWorkflow::NbType; ++i )
        if ( m_tracks[i]->allTracksRendered() == false )
            return ;
    {
        QMutexLocker    lock( m_synchroneRenderWaitConditionMutex );
    }
    m_synchroneRenderWaitCondition->wakeAll();
}
381 382 383 384 385

int         MainWorkflow::getTrackCount( TrackWorkflow::TrackType trackType ) const
{
    return m_tracks[trackType]->getTrackCount();
}
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
386