MainWorkflow.cpp 13.3 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
LightVideoFrame*    MainWorkflow::blackOutput = NULL;
35

36
MainWorkflow::MainWorkflow( int trackCount ) :
37
        m_lengthFrame( 0 ),
38
        m_renderStarted( false )
39
{
40
    m_currentFrameLock = new QReadWriteLock;
41
    m_renderStartedLock = new QReadWriteLock;
42
    m_renderMutex = new QMutex;
43

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

51 52 53
    m_effectEngine = new EffectsEngine;
    m_effectEngine->disable();

54
    m_tracks = new TrackHandler*[MainWorkflow::NbTrackType];
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
55
    m_currentFrame = new qint64[MainWorkflow::NbTrackType];
56
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
57
    {
58
        MainWorkflow::TrackType trackType = (i == 0 ? MainWorkflow::VideoTrack : MainWorkflow::AudioTrack );
59
        m_tracks[i] = new TrackHandler( trackCount, trackType, m_effectEngine );
60
        connect( m_tracks[i], SIGNAL( tracksEndReached() ), this, SLOT( tracksEndReached() ) );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
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 * Pixel::NbComposantes );
    memset( (*blackOutput)->frame.octets, 0, (*blackOutput)->nboctets );
67 68 69 70
}

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

74
    delete m_effectEngine;
75
    delete m_renderMutex;
76
    delete m_renderStartedLock;
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
}

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

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

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

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

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

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
119
MainWorkflow::OutputBuffers*    MainWorkflow::getOutput( TrackType trackType )
120
{
121 122
    QReadLocker         lock( m_renderStartedLock );
    QMutexLocker        lock2( m_renderMutex );
123

124
    if ( m_renderStarted == true )
125
    {
126
        QReadLocker         lock3( m_currentFrameLock );
127

128
        m_tracks[trackType]->getOutput( m_currentFrame[VideoTrack], m_currentFrame[trackType] );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
129
        if ( trackType == MainWorkflow::VideoTrack )
130
        {
131 132 133
            (*m_effectEngine)->render();
            LightVideoFrame const & tmp = (*((*m_effectEngine)->getInternalStaticVideoInput( 1 )) );
            if (tmp->nboctets == 0 )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
134 135
                m_outputBuffers->video = MainWorkflow::blackOutput;
            else
136
                m_outputBuffers->video = &tmp;
137
        }
138 139
        else
        {
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
140
            m_outputBuffers->audio = m_tracks[MainWorkflow::AudioTrack]->getTmpAudioBuffer();
141
        }
142
    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
143
    return m_outputBuffers;
144
}
145

146 147
void        MainWorkflow::pause()
{
148 149 150 151 152
    {
        //Just wait for the current render to finish
        //TODO:
        //FIXME: check if this is not alreay handled by the stacked actions system.
        QMutexLocker    lock( m_renderMutex );
153

154 155 156 157
        for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
            m_tracks[i]->pause();
    }
    emit mainWorkflowPaused();
158 159
}

160 161
void        MainWorkflow::unpause()
{
162 163
    {
        QMutexLocker    lock( m_renderMutex );
164

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

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
171
void        MainWorkflow::goToNextFrame( MainWorkflow::TrackType trackType )
172 173
{
    if ( m_paused == false )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
174
        nextFrame( trackType );
175 176
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
177
void        MainWorkflow::nextFrame( MainWorkflow::TrackType trackType )
178
{
179
    QWriteLocker    lock( m_currentFrameLock );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
180

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
181 182 183
    ++m_currentFrame[trackType];
    if ( trackType == MainWorkflow::VideoTrack )
        emit frameChanged( m_currentFrame[MainWorkflow::VideoTrack], Renderer );
184 185
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
186
void        MainWorkflow::previousFrame( MainWorkflow::TrackType trackType )
187
{
188
    QWriteLocker    lock( m_currentFrameLock );
189

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
190 191 192
    --m_currentFrame[trackType];
    if ( trackType == MainWorkflow::VideoTrack )
        emit frameChanged( m_currentFrame[MainWorkflow::VideoTrack], Renderer );
193 194
}

195
qint64      MainWorkflow::getLengthFrame() const
196
{
197
    return m_lengthFrame;
198
}
199

200
qint64      MainWorkflow::getClipPosition( const QUuid& uuid, unsigned int trackId, MainWorkflow::TrackType trackType ) const
201
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
202
    return m_tracks[trackType]->getClipPosition( uuid, trackId );
203
}
204 205 206 207

void            MainWorkflow::stop()
{
    QWriteLocker    lock( m_renderStartedLock );
208
    QWriteLocker    lock2( m_currentFrameLock );
209 210

    m_renderStarted = false;
211
    for (unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i)
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
212
    {
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
213
        m_tracks[i]->stop();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
214 215
        m_currentFrame[i] = 0;
    }
216
    emit frameChanged( 0, Renderer );
217
}
218

219
void           MainWorkflow::moveClip( const QUuid& clipUuid, unsigned int oldTrack,
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
220
                                       unsigned int newTrack, qint64 startingFrame,
221
                                       MainWorkflow::TrackType trackType, bool undoRedoCommand /*= false*/ )
222
{
223
    m_tracks[trackType]->moveClip( clipUuid, oldTrack, newTrack, startingFrame );
224
    computeLength();
225

226 227 228 229
    if ( undoRedoCommand == true )
    {
        emit clipMoved( clipUuid, newTrack, startingFrame, trackType );
    }
230
}
231

232
Clip*       MainWorkflow::removeClip( const QUuid& uuid, unsigned int trackId, MainWorkflow::TrackType trackType )
233
{
234 235
    Clip* clip = m_tracks[trackType]->removeClip( uuid, trackId );
    computeLength();
236
    emit clipRemoved( clip, trackId, trackType );
237
    return clip;
238 239
}

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

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

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

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

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

271 272 273
/**
 *  \warning    The mainworkflow is expected to be cleared already by the ProjectManager
 */
274 275
void        MainWorkflow::loadProject( const QDomElement& project )
{
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
    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;
304
            MainWorkflow::TrackType     trackType = MainWorkflow::VideoTrack;
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 339 340 341

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

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

355 356 357 358 359
            if ( Library::getInstance()->getMedia( parent ) != NULL )
            {
                Clip*       c = new Clip( parent, begin, end );
                addClip( c, trackId, startPos, trackType );
            }
360 361 362 363 364

            clip = clip.nextSibling().toElement();
        }
        elem = elem.nextSibling().toElement();
    }
365 366
}

367
void        MainWorkflow::saveProject( QDomDocument& doc, QDomElement& rootNode )
368
{
369
    QDomElement project = doc.createElement( "timeline" );
370
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
371 372 373 374
    {
        m_tracks[i]->save( doc, project );
    }
    rootNode.appendChild( project );
375
}
376 377 378

void        MainWorkflow::clear()
{
379
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
380
        m_tracks[i]->clear();
381
    emit cleared();
382
}
383

384 385 386 387 388 389 390 391
void        MainWorkflow::tracksEndReached()
{
    for ( unsigned int i = 0; i < MainWorkflow::NbTrackType; ++i )
        if ( m_tracks[i]->endIsReached() == false )
            return ;
    emit mainWorkflowEndReached();
}

392
int         MainWorkflow::getTrackCount( MainWorkflow::TrackType trackType ) const
393 394 395
{
    return m_tracks[trackType]->getTrackCount();
}
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
396

397 398
qint64      MainWorkflow::getCurrentFrame() const
{
399 400
    QReadLocker     lock( m_currentFrameLock );

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
401
    return m_currentFrame[MainWorkflow::VideoTrack];
402
}
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422

void        MainWorkflow::widthChanged( const QVariant& width )
{
    m_width = width.toUInt();
}

void        MainWorkflow::heightChanged( const QVariant& height )
{
    m_height = height.toUInt();
}

uint32_t    MainWorkflow::getWidth() const
{
    return m_width;
}

uint32_t    MainWorkflow::getHeight() const
{
    return m_height;
}