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

28
unsigned char*  MainWorkflow::blackOutput = NULL;
29
MainWorkflow*   MainWorkflow::m_instance = NULL;
30

31 32
MainWorkflow::MainWorkflow( QObject* parent, int trackCount ) :
        QObject( parent ),
33
        m_length( 0 ),
34 35
        m_trackCount( trackCount ),
        m_renderStarted( false )
36
{
37 38 39 40 41 42
    Q_ASSERT_X( MainWorkflow::m_instance == NULL,
                "MainWorkflow constructor", "Can't have more than one MainWorkflow instance" );
    m_instance = this;

    MainWorkflow::blackOutput = new unsigned char[VIDEOHEIGHT * VIDEOWIDTH * 3];
    memset( MainWorkflow::blackOutput, 0, VIDEOHEIGHT * VIDEOWIDTH * 3 );
43 44 45 46 47 48

    m_tracks = new Toggleable<TrackWorkflow*>[trackCount];
    for (int i = 0; i < trackCount; ++i)
    {
        m_tracks[i].setPtr( new TrackWorkflow( i ) );
        connect( m_tracks[i], SIGNAL( trackEndReached( unsigned int ) ), this, SLOT( trackEndReached(unsigned int) ) );
49
    }
50
    m_renderStartedLock = new QReadWriteLock;
51
    m_lastRenderedFrame = MainWorkflow::blackOutput;
52 53 54 55 56 57 58 59 60
}

MainWorkflow::~MainWorkflow()
{
    delete m_renderStartedLock;
    for (unsigned int i = 0; i < m_trackCount; ++i)
        delete m_tracks[i];
    delete[] m_tracks;
    delete[] blackOutput;
61 62
}

63
void    MainWorkflow::addClip( Clip* clip, unsigned int trackId, qint64 start )
64
{
65
    Q_ASSERT_X( trackId < m_trackCount, "MainWorkflow::addClip",
66 67
                "The specified trackId isn't valid, for it's higher than the number of tracks");

68 69 70
    //if the track is deactivated, we need to reactivate it :
    if ( m_tracks[trackId].deactivated() == true )
        m_tracks[trackId].activate();
71
    m_tracks[trackId]->addClip( clip, start );
72 73
    if ( m_tracks[trackId]->getLength() > m_length )
        m_length = m_tracks[trackId]->getLength();
74
}
75 76 77

void    MainWorkflow::startRender()
{
78 79
    qint64      maxLength = 0;

80
    m_renderStarted = true;
81 82
    m_currentFrame = 0;
    emit frameChanged( 0 );
83 84
    for ( unsigned int i = 0; i < m_trackCount; ++i )
    {
85
        m_tracks[i].activate();
86 87 88 89
        if ( m_tracks[i]->getLength() > maxLength )
            maxLength = m_tracks[i]->getLength();
    }
    m_length = maxLength;
90 91 92 93
}

unsigned char*    MainWorkflow::getOutput()
{
94
    QReadLocker     lock( m_renderStartedLock );
95
    qDebug() << "New frame asked";
96

97 98 99 100 101
    if ( m_renderOnlyOneFrame == 1 && m_lastRenderedFrame != NULL )
    {
        qDebug() << "Returning last frame";
        return m_lastRenderedFrame;
    }
102
    qDebug() << "Computing frame";
103
    if ( m_renderStarted == true )
104
    {
105 106 107
        unsigned char* ret;

        for ( unsigned int i = 0; i < m_trackCount; ++i )
108
        {
109 110 111 112
            if ( m_tracks[i].activated() == false )
                continue ;
            if ( ( ret = m_tracks[i]->getOutput( m_currentFrame ) ) != NULL )
                break ;
113
        }
114 115
        if ( ret == NULL )
            ret = MainWorkflow::blackOutput;
116

117 118 119
        if ( m_renderOnlyOneFrame == 0 )
            nextFrame();
        m_lastRenderedFrame = ret;
120 121 122
        return ret;
    }
    else
123 124
    {
        m_lastRenderedFrame = MainWorkflow::blackOutput;
125
        return MainWorkflow::blackOutput;
126
    }
127
}
128

129 130 131 132 133 134 135 136 137
void        MainWorkflow::pause()
{
    for ( unsigned int i = 0; i < m_trackCount; ++i )
    {
        if ( m_tracks[i].activated() == true )
            m_tracks[i]->pause();
    }
}

138 139
void        MainWorkflow::nextFrame()
{
140
    qDebug() << "Going to next frame";
141
    ++m_currentFrame;
142
    //FIXME: This is probably a bit much...
143 144 145 146 147 148 149
    emit frameChanged( m_currentFrame );
    emit positionChanged( (float)m_currentFrame / (float)m_length );
}

void        MainWorkflow::previousFrame()
{
    --m_currentFrame;
150
    //FIXME: This is probably a bit much...
151 152 153 154
    emit frameChanged( m_currentFrame );
    emit positionChanged( (float)m_currentFrame / (float)m_length );
}

155 156
void        MainWorkflow::setPosition( float pos )
{
157 158 159 160 161
    //Since any track can be reactivated, we reactivate all of them, and let them
    //unable themself if required.
    for ( unsigned int i = 0; i < m_trackCount; ++i)
        m_tracks[i].activate();

162 163
    if ( m_renderStarted == false )
        return ;
chouquette's avatar
chouquette committed
164
    qint64  frame = static_cast<qint64>( (float)m_length * pos );
165 166
    m_currentFrame = frame;
    emit frameChanged( frame );
167
    //Do not emit a signal for the RenderWidget, since it's the one that triggered that call...
168 169 170 171 172 173
}

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

175
void        MainWorkflow::trackEndReached( unsigned int trackId )
176
{
177 178 179 180 181 182 183
    m_tracks[trackId].deactivate();

    for ( unsigned int i = 0; i < m_trackCount; ++i)
    {
        if ( m_tracks[i].activated() == true )
            return ;
    }
184
    emit mainWorkflowEndReached();
185 186 187
    m_renderStarted = false;
    m_currentFrame = 0;
    emit frameChanged( 0 );
188
}
189 190 191 192 193

unsigned int    MainWorkflow::getTrackCount() const
{
    return m_trackCount;
}
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

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

    m_renderStarted = false;
    for (unsigned int i = 0; i < m_trackCount; ++i)
    {
        //FIXME: After debugging period, this should'nt be necessary --
        m_tracks[i].activate();
        //--------
        m_tracks[i]->stop();
    }
    m_currentFrame = 0;
    emit frameChanged( 0 );
}
210 211 212 213 214 215

MainWorkflow*   MainWorkflow::getInstance()
{
    Q_ASSERT( m_instance != NULL );
    return m_instance;
}
216

217
void           MainWorkflow::clipMoved( QUuid clipUuid, int oldTrack, int newTrack, qint64 startingFrame )
218
{
219 220 221 222 223 224
    Q_ASSERT( newTrack < m_trackCount && oldTrack < m_trackCount && oldTrack >= 0 && newTrack >= 0 );

    if ( oldTrack == newTrack )
    {
        //And now, just move the clip.
        m_tracks[newTrack]->moveClip( clipUuid, startingFrame );
225
        m_tracks[newTrack].activate();
226 227 228 229 230
    }
    else
    {
        Clip* clip = m_tracks[oldTrack]->removeClip( clipUuid );
        m_tracks[newTrack]->addClip( clip, startingFrame );
231 232
        m_tracks[oldTrack].activate();
        m_tracks[newTrack].activate();
233
    }
234
}
235 236 237 238

void        MainWorkflow::activateOneFrameOnly()
{
    m_renderOnlyOneFrame = 1;
239
    m_lastRenderedFrame = NULL;
240
}