MainWorkflow.cpp 14.1 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * MainWorkflow.cpp : Will query all of the track workflows to render the final
 *                    image
 *****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
5
 * Copyright (C) 2008-2010 VideoLAN
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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>

Ludovic Fauvet's avatar
Ludovic Fauvet committed
26
#include "vlmc.h"
27 28 29 30
#include "Clip.h"
#include "EffectsEngine.h"
#include "Library.h"
#include "LightVideoFrame.h"
31
#include "MainWorkflow.h"
32 33
#include "TrackWorkflow.h"
#include "TrackHandler.h"
34
#include "SettingsManager.h"
35 36

#include <QDomElement>
37

38
LightVideoFrame     *MainWorkflow::blackOutput = NULL;
39

40
MainWorkflow::MainWorkflow( int trackCount ) :
41
        m_lengthFrame( 0 ),
42 43 44
        m_renderStarted( false ),
        m_width( 0 ),
        m_height( 0 )
45
{
46
    m_currentFrameLock = new QReadWriteLock;
47
    m_renderStartedMutex = new QMutex;
48 49 50 51

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

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

65 66
    blackOutput = new LightVideoFrame( m_width, m_height);
    // FIX ME vvvvvv , It doesn't update meta info (nbpixels, nboctets, etc.
67
    memset( (*blackOutput)->frame.octets, 0, (*blackOutput)->nboctets );
68 69 70 71
}

MainWorkflow::~MainWorkflow()
{
72
    //FIXME: this is probably useless, since already done by the renderer
73 74
    stop();

75
    delete m_effectEngine;
76
    delete m_renderStartedMutex;
77
    delete m_currentFrameLock;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
78
    delete m_currentFrame;
79
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
80
        delete m_tracks[i];
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
81
    delete[] m_tracks;
82 83
}

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

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

100 101
void
MainWorkflow::computeLength()
102
{
103 104
    qint64      maxLength = 0;

105
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
106 107 108 109
    {
        if ( m_tracks[i]->getLength() > maxLength )
            maxLength = m_tracks[i]->getLength();
    }
110 111 112 113 114 115
    if ( m_lengthFrame != maxLength )
    {
        m_lengthFrame = maxLength;
        emit lengthChanged( m_lengthFrame );
    }

116 117
}

118
void
119
MainWorkflow::startRender( quint32 width, quint32 height )
120 121
{
    m_renderStarted = true;
122 123
    m_width = width;
    m_height = height;
124
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
125
        m_tracks[i]->startRender();
126 127 128
    computeLength();
}

129
MainWorkflow::OutputBuffers*
130
MainWorkflow::getOutput( TrackType trackType, bool paused )
131
{
132
    QMutexLocker        lock( m_renderStartedMutex );
133

134
    if ( m_renderStarted == true )
135
    {
136
        QReadLocker         lock2( m_currentFrameLock );
137

138
        m_tracks[trackType]->getOutput( m_currentFrame[VideoTrack],
139
                                        m_currentFrame[trackType], paused );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
140
        if ( trackType == MainWorkflow::VideoTrack )
141
        {
142 143
            m_effectEngine->render();
            const LightVideoFrame &tmp = m_effectEngine->getVideoOutput( 1 );
144
            m_outputBuffers->video = &tmp;
145
        }
146 147
        else
        {
148 149
            m_outputBuffers->audio =
                    m_tracks[MainWorkflow::AudioTrack]->getTmpAudioBuffer();
150
        }
151
    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
152
    return m_outputBuffers;
153
}
154

155 156
void
MainWorkflow::nextFrame( MainWorkflow::TrackType trackType )
157
{
158
    QWriteLocker    lock( m_currentFrameLock );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
159

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
160 161 162
    ++m_currentFrame[trackType];
    if ( trackType == MainWorkflow::VideoTrack )
        emit frameChanged( m_currentFrame[MainWorkflow::VideoTrack], Renderer );
163 164
}

165 166
void
MainWorkflow::previousFrame( MainWorkflow::TrackType trackType )
167
{
168
    QWriteLocker    lock( m_currentFrameLock );
169

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
170 171 172
    --m_currentFrame[trackType];
    if ( trackType == MainWorkflow::VideoTrack )
        emit frameChanged( m_currentFrame[MainWorkflow::VideoTrack], Renderer );
173 174
}

175 176
qint64
MainWorkflow::getLengthFrame() const
177
{
178
    return m_lengthFrame;
179
}
180

181 182 183
qint64
MainWorkflow::getClipPosition( const QUuid& uuid, unsigned int trackId,
                               MainWorkflow::TrackType trackType ) const
184
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
185
    return m_tracks[trackType]->getClipPosition( uuid, trackId );
186
}
187

188 189
void
MainWorkflow::stop()
190
{
191
    QMutexLocker    lock( m_renderStartedMutex );
192
    QWriteLocker    lock2( m_currentFrameLock );
193 194

    m_renderStarted = false;
195
    for (unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i)
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
196
    {
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
197
        m_tracks[i]->stop();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
198 199
        m_currentFrame[i] = 0;
    }
200
    emit frameChanged( 0, Renderer );
201
}
202

203 204 205 206 207
void
MainWorkflow::moveClip( const QUuid &clipUuid, unsigned int oldTrack,
                        unsigned int newTrack, qint64 startingFrame,
                        MainWorkflow::TrackType trackType,
                        bool undoRedoCommand /*= false*/ )
208
{
209
    m_tracks[trackType]->moveClip( clipUuid, oldTrack, newTrack, startingFrame );
210
    computeLength();
211

212 213 214 215
    if ( undoRedoCommand == true )
    {
        emit clipMoved( clipUuid, newTrack, startingFrame, trackType );
    }
216
}
217

218 219 220
Clip*
MainWorkflow::removeClip( const QUuid &uuid, unsigned int trackId,
                          MainWorkflow::TrackType trackType )
221
{
222
    Clip *clip = m_tracks[trackType]->removeClip( uuid, trackId );
223 224 225 226 227
    if ( clip != NULL )
    {
        computeLength();
        emit clipRemoved( clip, trackId, trackType );
    }
228
    return clip;
229 230
}

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

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

243 244 245 246
void
MainWorkflow::muteClip( const QUuid& uuid, unsigned int trackId,
                        MainWorkflow::TrackType trackType )
{
247
    m_tracks[trackType]->muteClip( uuid, trackId );
248 249 250 251 252 253
}

void
MainWorkflow::unmuteClip( const QUuid& uuid, unsigned int trackId,
                          MainWorkflow::TrackType trackType )
{
254
    m_tracks[trackType]->unmuteClip( uuid, trackId );
255 256
}

257 258 259 260
void toggleBreakPoint()
{
}

261 262
void
MainWorkflow::setCurrentFrame( qint64 currentFrame, MainWorkflow::FrameChangedReason reason )
263
{
264 265
    QWriteLocker    lock( m_currentFrameLock );

266
    toggleBreakPoint();
267 268 269
    if ( m_renderStarted == true )
    {
        //Since any track can be reactivated, we reactivate all of them, and let them
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
270
        //disable themself if required.
271 272 273
        for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i)
            m_tracks[i]->activateAll();
    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
274 275 276
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i)
        m_currentFrame[i] = currentFrame;
    emit frameChanged( currentFrame, reason );
277
}
278

279 280 281
Clip*
MainWorkflow::getClip( const QUuid &uuid, unsigned int trackId,
                       MainWorkflow::TrackType trackType )
282
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
283
    return m_tracks[trackType]->getClip( uuid, trackId );
284
}
285

286
/**
287
 *  \warning    The mainworkflow is expected to be already cleared by the ProjectManager
288
 */
289 290
void
MainWorkflow::loadProject( const QDomElement &project )
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
    if ( project.isNull() == true || project.tagName() != "timeline" )
    {
        qWarning() << "Invalid timeline node (" << project.tagName() << ')';
        return ;
    }

    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;
320
            MainWorkflow::TrackType     trackType = MainWorkflow::VideoTrack;
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

            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" )
                {
358 359
                    trackType = static_cast<MainWorkflow::TrackType>(
                                                    clipProperty.text().toUInt( &ok ) );
360 361 362 363 364 365 366 367 368 369 370 371
                    if ( ok == false )
                    {
                        qWarning() << "Invalid track type starting frame";
                        return ;
                    }
                }
                else
                    qDebug() << "Unknown field" << clipProperty.tagName();

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

372
            if ( Library::getInstance()->media( parent ) != NULL )
373
            {
374
                Clip        *c = new Clip( parent, begin, end );
375 376
                addClip( c, trackId, startPos, trackType );
            }
377 378 379 380 381

            clip = clip.nextSibling().toElement();
        }
        elem = elem.nextSibling().toElement();
    }
382 383
}

384 385
void
MainWorkflow::saveProject( QDomDocument& doc, QDomElement& rootNode )
386
{
387
    QDomElement project = doc.createElement( "timeline" );
388
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
389 390 391 392
    {
        m_tracks[i]->save( doc, project );
    }
    rootNode.appendChild( project );
393
}
394

395 396
void
MainWorkflow::clear()
397
{
398
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
399
        m_tracks[i]->clear();
400
    emit cleared();
401
}
402

403 404
void
MainWorkflow::tracksEndReached()
405 406
{
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
407
    {
408 409
        if ( m_tracks[i]->endIsReached() == false )
            return ;
410
    }
411 412 413
    emit mainWorkflowEndReached();
}

414 415
int
MainWorkflow::getTrackCount( MainWorkflow::TrackType trackType ) const
416 417 418
{
    return m_tracks[trackType]->getTrackCount();
}
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
419

420 421
qint64
MainWorkflow::getCurrentFrame() const
422
{
423 424
    QReadLocker     lock( m_currentFrameLock );

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
425
    return m_currentFrame[MainWorkflow::VideoTrack];
426
}
427

428
quint32
429
MainWorkflow::getWidth() const
430
{
431
    Q_ASSERT( m_width != 0 );
432 433 434
    return m_width;
}

435
quint32
436
MainWorkflow::getHeight() const
437
{
438
    Q_ASSERT( m_height != 0 );
439 440
    return m_height;
}
441 442 443 444 445 446 447 448 449

void
MainWorkflow::renderOneFrame()
{
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
        m_tracks[i]->renderOneFrame();
    nextFrame( VideoTrack );
    nextFrame( AudioTrack );
}
450 451 452 453 454 455 456

void
MainWorkflow::setFullSpeedRender( bool val )
{
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
        m_tracks[i]->setFullSpeedRender( val );
}
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493

Clip*
MainWorkflow::split( Clip* toSplit, Clip* newClip, quint32 trackId, qint64 newClipPos, qint64 newClipBegin, MainWorkflow::TrackType trackType )
{
    QMutexLocker    lock( m_renderStartedMutex );

    if ( newClip == NULL )
        newClip = new Clip( toSplit, newClipBegin, toSplit->getEnd() );

    toSplit->setEnd( newClipBegin, true );
    addClip( newClip, trackId, newClipPos, trackType );
    return newClip;
}

void
MainWorkflow::resizeClip( Clip* clip, qint64 newBegin, qint64 newEnd, qint64 newPos,
                          quint32 trackId, MainWorkflow::TrackType trackType,
                                      bool undoRedoAction /*= false*/ )
{
    QMutexLocker    lock( m_renderStartedMutex );

    if ( newBegin != clip->getBegin() )
    {
        moveClip( clip->getUuid(), trackId, trackId, newPos, trackType, undoRedoAction );
    }
    clip->setBoundaries( newBegin, newEnd );
}

void
MainWorkflow::unsplit( Clip* origin, Clip* splitted, quint32 trackId,
                       MainWorkflow::TrackType trackType )
{
    QMutexLocker    lock( m_renderStartedMutex );

    removeClip( splitted->getUuid(), trackId, trackType );
    origin->setEnd( splitted->getEnd(), true );
}