TrackHandler.cpp 8.47 KB
Newer Older
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*****************************************************************************
 * TrackHandler.cpp : Handle multiple track of a kind (audio or video)
 *****************************************************************************
 * 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.
 *****************************************************************************/

23
#include "EffectsEngine.h"
24
#include "LightVideoFrame.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
25
#include "TrackHandler.h"
26
#include "TrackWorkflow.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
27

28 29 30
#include <QDomDocument>
#include <QDomElement>

31 32
LightVideoFrame* TrackHandler::nullOutput = NULL;

33 34
TrackHandler::TrackHandler( unsigned int nbTracks, MainWorkflow::TrackType trackType,
                            EffectsEngine* effectsEngine ) :
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
35 36
        m_trackCount( nbTracks ),
        m_trackType( trackType ),
37 38
        m_length( 0 ),
        m_effectEngine( effectsEngine )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
39
{
40 41
    TrackHandler::nullOutput = new LightVideoFrame();

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
42 43 44
    m_tracks = new Toggleable<TrackWorkflow*>[nbTracks];
    for ( unsigned int i = 0; i < nbTracks; ++i )
    {
45
        m_tracks[i].setPtr( new TrackWorkflow( i, trackType ) );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
46 47 48 49 50 51
        connect( m_tracks[i], SIGNAL( trackEndReached( unsigned int ) ), this, SLOT( trackEndReached(unsigned int) ) );
    }
}

TrackHandler::~TrackHandler()
{
52 53 54 55 56
    if ( nullOutput != NULL )
    {
        delete nullOutput;
        nullOutput = NULL;
    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
57 58 59 60 61
    for (unsigned int i = 0; i < m_trackCount; ++i)
        delete m_tracks[i];
    delete[] m_tracks;
}

62 63
void
TrackHandler::addClip( Clip* clip, unsigned int trackId, qint64 start )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77
{
    Q_ASSERT_X( trackId < m_trackCount, "MainWorkflow::addClip",
                "The specified trackId isn't valid, for it's higher than the number of tracks");

    m_tracks[trackId]->addClip( clip, start );
    //if the track is deactivated, we need to reactivate it.
    if ( m_tracks[trackId].deactivated() == true )
        activateTrack( trackId );

    //Now check if this clip addition has changed something about the workflow's length
    if ( m_tracks[trackId]->getLength() > m_length )
        m_length = m_tracks[trackId]->getLength();
}

78 79
void
TrackHandler::startRender()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
80 81
{
    m_paused = false;
82
    m_endReached = false;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
83
    computeLength();
84 85 86 87 88 89 90
    if ( m_length == 0 )
        m_endReached = true;
    else
    {
        for ( unsigned int i = 0; i < m_trackCount; ++i )
            activateTrack( i );
    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
91 92
}

93 94
void
TrackHandler::computeLength()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
95 96 97 98 99 100 101 102 103 104 105
{
    qint64      maxLength = 0;

    for ( unsigned int i = 0; i < m_trackCount; ++i )
    {
        if ( m_tracks[i]->getLength() > maxLength )
            maxLength = m_tracks[i]->getLength();
    }
    m_length = maxLength;
}

106 107
qint64
TrackHandler::getLength() const
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
108 109 110 111
{
    return m_length;
}

112
void
113
TrackHandler::getOutput( qint64 currentFrame, qint64 subFrame )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
114
{
115
    m_tmpAudioBuffer = NULL;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
116 117
    for ( unsigned int i = 0; i < m_trackCount; ++i )
    {
118
        if ( m_trackType == MainWorkflow::VideoTrack )
119
        {
120
            if ( m_tracks[i].activated() == false )
121
                m_effectEngine->setVideoInput( i + 1, *TrackHandler::nullOutput );
122
            else
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
123
            {
124
                void*   ret = m_tracks[i]->getOutput( currentFrame, subFrame );
125 126 127 128 129 130 131 132
                if ( ret == NULL )
                    m_effectEngine->setVideoInput( i + 1, *TrackHandler::nullOutput );
                else
                {
                    StackedBuffer<LightVideoFrame*>* stackedBuffer =
                        reinterpret_cast<StackedBuffer<LightVideoFrame*>*>( ret );
                    m_effectEngine->setVideoInput( i + 1, *( stackedBuffer->get() ) );
                }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
133 134 135 136
            }
        }
        else
        {
137 138 139 140 141 142 143 144 145 146 147
            if ( m_tracks[i].activated() == true )
            {
                void*   ret = m_tracks[i]->getOutput( currentFrame, subFrame );
                //m_tmpAudioBuffer is NULl by default, so it will remain NULL if we continue;
                if ( ret == NULL )
                    continue ;
                StackedBuffer<AudioClipWorkflow::AudioSample*>* stackedBuffer =
                    reinterpret_cast<StackedBuffer<AudioClipWorkflow::AudioSample*>*> ( ret );
                if ( stackedBuffer != NULL )
                    m_tmpAudioBuffer = stackedBuffer->get();
            }
148
        }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
149 150 151
    }
}

152 153
void
TrackHandler::pause()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
154 155 156 157 158 159 160 161
{
    for ( unsigned int i = 0; i < m_trackCount; ++i )
    {
        if ( m_tracks[i].activated() == true )
            m_tracks[i]->pause();
    }
}

162 163
void
TrackHandler::unpause()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
164 165 166
{
    for ( unsigned int i = 0; i < m_trackCount; ++i )
    {
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
167 168
        //Don't check for track activation, as it could have change from the time we paused.
        m_tracks[i]->unpause();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
169 170 171
    }
}

172 173
void
TrackHandler::activateAll()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
174 175 176 177 178
{
    for ( unsigned int i = 0; i < m_trackCount; ++i )
        activateTrack( i );
}

179 180
void
TrackHandler::activateTrack( unsigned int trackId )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
181 182 183 184 185 186 187
{
    if ( m_tracks[trackId]->getLength() > 0 )
        m_tracks[trackId].activate();
    else
        m_tracks[trackId].deactivate();
}

188 189
qint64
TrackHandler::getClipPosition( const QUuid &uuid, unsigned int trackId ) const
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
190 191 192 193 194 195
{
    Q_ASSERT( trackId < m_trackCount );

    return m_tracks[trackId]->getClipPosition( uuid );
}

196 197
void
TrackHandler::stop()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
198 199 200 201 202 203 204 205
{
    for (unsigned int i = 0; i < m_trackCount; ++i)
    {
        if ( m_tracks[i].activated() == true )
            m_tracks[i]->stop();
    }
}

206 207
void
TrackHandler::moveClip(const QUuid &clipUuid, unsigned int oldTrack,
208
                                       unsigned int newTrack, qint64 startingFrame )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
{
     Q_ASSERT( newTrack < m_trackCount && oldTrack < m_trackCount );

    if ( oldTrack == newTrack )
    {
        //And now, just move the clip.
        m_tracks[newTrack]->moveClip( clipUuid, startingFrame );
        activateTrack( newTrack );
    }
    else
    {
        bool    needRepo;

        if ( m_tracks[oldTrack]->getClipPosition( clipUuid ) != startingFrame )
            needRepo = true;
        ClipWorkflow* cw = m_tracks[oldTrack]->removeClipWorkflow( clipUuid );
        m_tracks[newTrack]->addClip( cw, startingFrame );
        if ( needRepo == true )
            m_tracks[newTrack]->forceRepositionning();
        activateTrack( oldTrack );
        activateTrack( newTrack );
    }
    computeLength();
}

234 235
Clip*
TrackHandler::removeClip( const QUuid& uuid, unsigned int trackId )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
236 237 238 239 240 241 242 243 244
{
    Q_ASSERT( trackId < m_trackCount );

    Clip* clip = m_tracks[trackId]->removeClip( uuid );
    computeLength();
    activateTrack( trackId );
    return clip;
}

245 246
void
TrackHandler::muteTrack( unsigned int trackId )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
247 248 249 250
{
    m_tracks[trackId].setHardDeactivation( true );
}

251 252
void
TrackHandler::unmuteTrack( unsigned int trackId )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
253 254 255 256
{
    m_tracks[trackId].setHardDeactivation( false );
}

257 258
Clip*
TrackHandler::getClip( const QUuid& uuid, unsigned int trackId )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
259 260 261 262 263 264
{
    Q_ASSERT( trackId < m_trackCount );

    return m_tracks[trackId]->getClip( uuid );
}

265 266
void
TrackHandler::clear()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
267 268 269 270 271 272 273 274
{
    for ( unsigned int i = 0; i < m_trackCount; ++i )
    {
        m_tracks[i]->clear();
    }
    m_length = 0;
}

275 276
bool
TrackHandler::isPaused() const
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
277 278 279 280
{
    return m_paused;
}

281 282
bool
TrackHandler::endIsReached() const
283 284 285 286
{
    return m_endReached;
}

287 288
void
TrackHandler::trackEndReached( unsigned int trackId )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
289 290 291 292 293 294 295 296
{
    m_tracks[trackId].deactivate();

    for ( unsigned int i = 0; i < m_trackCount; ++i)
    {
        if ( m_tracks[i].activated() == true )
            return ;
    }
297 298
    m_endReached = true;
    emit tracksEndReached();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
299 300
}

301 302
unsigned int
TrackHandler::getTrackCount() const
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
303 304 305
{
    return m_trackCount;
}
306

307 308
void
TrackHandler::save( QDomDocument& doc, QDomElement& timelineNode ) const
309 310 311 312 313 314 315 316 317 318 319 320 321 322
{
    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 );
            timelineNode.appendChild( trackNode );
        }
    }
}