MainWorkflow.cpp 13.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
/*****************************************************************************
 * 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>

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

#include <QDomElement>
36

37
LightVideoFrame     *MainWorkflow::blackOutput = NULL;
38

39
MainWorkflow::MainWorkflow( int trackCount ) :
40
        m_lengthFrame( 0 ),
41
        m_renderStarted( false )
42
{
43
    m_currentFrameLock = new QReadWriteLock;
44
    m_renderStartedLock = new QReadWriteLock;
45

46 47 48 49
    const SettingValue  *width = SettingsManager::getInstance()->getValue( "project",
                                                                    "VideoProjectWidth" );
    connect( width,     SIGNAL( changed( QVariant ) ),
             this,      SLOT( widthChanged( QVariant ) ) );
50
    m_width = width->get().toUInt();
51 52 53 54
    const SettingValue  *height = SettingsManager::getInstance()->getValue( "project",
                                                                "VideoProjectHeight" );
    connect( height, SIGNAL( changed( QVariant ) ),
             this, SLOT( widthChanged( QVariant ) ) );
55 56
    m_height = height->get().toUInt();

57 58 59
    m_effectEngine = new EffectsEngine;
    m_effectEngine->disable();

60
    m_tracks = new TrackHandler*[MainWorkflow::NbTrackType];
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
61
    m_currentFrame = new qint64[MainWorkflow::NbTrackType];
62
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
63
    {
64 65
        MainWorkflow::TrackType trackType =
                (i == 0 ? MainWorkflow::VideoTrack : MainWorkflow::AudioTrack );
66
        m_tracks[i] = new TrackHandler( trackCount, trackType, m_effectEngine );
67 68
        connect( m_tracks[i], SIGNAL( tracksEndReached() ),
                 this, SLOT( tracksEndReached() ) );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
69
	m_currentFrame[i] = 0;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
70 71
    }
    m_outputBuffers = new OutputBuffers;
72 73 74

    blackOutput = new LightVideoFrame( m_width * m_height * Pixel::NbComposantes );
    memset( (*blackOutput)->frame.octets, 0, (*blackOutput)->nboctets );
75 76 77 78
}

MainWorkflow::~MainWorkflow()
{
79
    //FIXME: this is probably useless, since already done by the renderer
80 81
    stop();

82
    delete m_effectEngine;
83
    delete m_renderStartedLock;
84
    delete m_currentFrameLock;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
85
    delete m_currentFrame;
86
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
87
        delete m_tracks[i];
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
88
    delete[] m_tracks;
89 90
}

91 92
EffectsEngine*
MainWorkflow::getEffectsEngine()
93
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
94
    return m_effectEngine;
95
}
96

97 98
void
MainWorkflow::addClip( Clip *clip, unsigned int trackId,
99
                                        qint64 start, MainWorkflow::TrackType trackType )
100
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
101
    m_tracks[trackType]->addClip( clip, trackId, start );
102
    computeLength();
103
    //Inform the GUI
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
104
    emit clipAdded( clip, trackId, start, trackType );
105
}
106

107 108
void
MainWorkflow::computeLength()
109
{
110 111
    qint64      maxLength = 0;

112
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
113 114 115 116
    {
        if ( m_tracks[i]->getLength() > maxLength )
            maxLength = m_tracks[i]->getLength();
    }
117
    m_lengthFrame = maxLength;
118 119
}

120 121
void
MainWorkflow::startRender()
122 123
{
    m_renderStarted = true;
124
    m_paused = false;
125
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
126
        m_tracks[i]->startRender();
127 128 129
    computeLength();
}

130 131
MainWorkflow::OutputBuffers*
MainWorkflow::getOutput( TrackType trackType )
132
{
133
    QReadLocker         lock( m_renderStartedLock );
134

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

139
        m_tracks[trackType]->getOutput( m_currentFrame[trackType] );
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
            if (tmp->nboctets == 0 )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
145 146
                m_outputBuffers->video = MainWorkflow::blackOutput;
            else
147
                m_outputBuffers->video = &tmp;
148
        }
149 150
        else
        {
151 152
            m_outputBuffers->audio =
                    m_tracks[MainWorkflow::AudioTrack]->getTmpAudioBuffer();
153
        }
154
    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
155
    return m_outputBuffers;
156
}
157

158 159
void
MainWorkflow::pause()
160
{
161 162
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
        m_tracks[i]->pause();
163
    emit mainWorkflowPaused();
164 165
}

166 167
void
MainWorkflow::unpause()
168
{
169 170
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
        m_tracks[i]->unpause();
171
    emit mainWorkflowUnpaused();
172 173
}

174 175
void
MainWorkflow::goToNextFrame( MainWorkflow::TrackType trackType )
176 177
{
    if ( m_paused == false )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
178
        nextFrame( trackType );
179 180
}

181 182
void
MainWorkflow::nextFrame( MainWorkflow::TrackType trackType )
183
{
184
    QWriteLocker    lock( m_currentFrameLock );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
185

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
186 187 188
    ++m_currentFrame[trackType];
    if ( trackType == MainWorkflow::VideoTrack )
        emit frameChanged( m_currentFrame[MainWorkflow::VideoTrack], Renderer );
189 190
}

191 192
void
MainWorkflow::previousFrame( MainWorkflow::TrackType trackType )
193
{
194
    QWriteLocker    lock( m_currentFrameLock );
195

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
196 197 198
    --m_currentFrame[trackType];
    if ( trackType == MainWorkflow::VideoTrack )
        emit frameChanged( m_currentFrame[MainWorkflow::VideoTrack], Renderer );
199 200
}

201 202
qint64
MainWorkflow::getLengthFrame() const
203
{
204
    return m_lengthFrame;
205
}
206

207 208 209
qint64
MainWorkflow::getClipPosition( const QUuid& uuid, unsigned int trackId,
                               MainWorkflow::TrackType trackType ) const
210
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
211
    return m_tracks[trackType]->getClipPosition( uuid, trackId );
212
}
213

214 215
void
MainWorkflow::stop()
216 217
{
    QWriteLocker    lock( m_renderStartedLock );
218
    QWriteLocker    lock2( m_currentFrameLock );
219 220

    m_renderStarted = false;
221
    for (unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i)
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
222
    {
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
223
        m_tracks[i]->stop();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
224 225
        m_currentFrame[i] = 0;
    }
226
    emit frameChanged( 0, Renderer );
227
}
228

229 230 231 232 233
void
MainWorkflow::moveClip( const QUuid &clipUuid, unsigned int oldTrack,
                        unsigned int newTrack, qint64 startingFrame,
                        MainWorkflow::TrackType trackType,
                        bool undoRedoCommand /*= false*/ )
234
{
235
    m_tracks[trackType]->moveClip( clipUuid, oldTrack, newTrack, startingFrame );
236
    computeLength();
237

238 239 240 241
    if ( undoRedoCommand == true )
    {
        emit clipMoved( clipUuid, newTrack, startingFrame, trackType );
    }
242
}
243

244 245 246
Clip*
MainWorkflow::removeClip( const QUuid &uuid, unsigned int trackId,
                          MainWorkflow::TrackType trackType )
247
{
248
    Clip *clip = m_tracks[trackType]->removeClip( uuid, trackId );
249
    computeLength();
250
    emit clipRemoved( clip, trackId, trackType );
251
    return clip;
252 253
}

254 255
void
MainWorkflow::muteTrack( unsigned int trackId, MainWorkflow::TrackType trackType )
256
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
257
    m_tracks[trackType]->muteTrack( trackId );
258 259
}

260 261
void
MainWorkflow::unmuteTrack( unsigned int trackId, MainWorkflow::TrackType trackType )
262
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
263
    m_tracks[trackType]->unmuteTrack( trackId );
264
}
265

266 267 268 269
void toggleBreakPoint()
{
}

270 271
void
MainWorkflow::setCurrentFrame( qint64 currentFrame, MainWorkflow::FrameChangedReason reason )
272
{
273 274
    QWriteLocker    lock( m_currentFrameLock );

275
    toggleBreakPoint();
276 277 278 279 280 281 282
    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();
    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
283 284 285
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i)
        m_currentFrame[i] = currentFrame;
    emit frameChanged( currentFrame, reason );
286
}
287

288 289 290
Clip*
MainWorkflow::getClip( const QUuid &uuid, unsigned int trackId,
                       MainWorkflow::TrackType trackType )
291
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
292
    return m_tracks[trackType]->getClip( uuid, trackId );
293
}
294

295
/**
296
 *  \warning    The mainworkflow is expected to be already cleared by the ProjectManager
297
 */
298 299
void
MainWorkflow::loadProject( const QDomElement &project )
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
    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;
329
            MainWorkflow::TrackType     trackType = MainWorkflow::VideoTrack;
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 358 359 360 361 362 363 364 365 366

            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" )
                {
367 368
                    trackType = static_cast<MainWorkflow::TrackType>(
                                                    clipProperty.text().toUInt( &ok ) );
369 370 371 372 373 374 375 376 377 378 379 380
                    if ( ok == false )
                    {
                        qWarning() << "Invalid track type starting frame";
                        return ;
                    }
                }
                else
                    qDebug() << "Unknown field" << clipProperty.tagName();

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

381 382
            if ( Library::getInstance()->getMedia( parent ) != NULL )
            {
383
                Clip        *c = new Clip( parent, begin, end );
384 385
                addClip( c, trackId, startPos, trackType );
            }
386 387 388 389 390

            clip = clip.nextSibling().toElement();
        }
        elem = elem.nextSibling().toElement();
    }
391 392
}

393 394
void
MainWorkflow::saveProject( QDomDocument& doc, QDomElement& rootNode )
395
{
396
    QDomElement project = doc.createElement( "timeline" );
397
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
398 399 400 401
    {
        m_tracks[i]->save( doc, project );
    }
    rootNode.appendChild( project );
402
}
403

404 405
void
MainWorkflow::clear()
406
{
407
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
408
        m_tracks[i]->clear();
409
    emit cleared();
410
}
411

412 413
void
MainWorkflow::tracksEndReached()
414 415 416 417 418 419 420
{
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
        if ( m_tracks[i]->endIsReached() == false )
            return ;
    emit mainWorkflowEndReached();
}

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

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

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
432
    return m_currentFrame[MainWorkflow::VideoTrack];
433
}
434

435 436
void
MainWorkflow::widthChanged( const QVariant &width )
437 438 439 440
{
    m_width = width.toUInt();
}

441 442
void
MainWorkflow::heightChanged( const QVariant &height )
443 444 445 446
{
    m_height = height.toUInt();
}

447 448
uint32_t
MainWorkflow::getWidth() const
449 450 451 452
{
    return m_width;
}

453 454
uint32_t
MainWorkflow::getHeight() const
455 456 457
{
    return m_height;
}