TrackHandler.cpp 9.55 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 23
/*****************************************************************************
 * 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.
 *****************************************************************************/

#include "TrackHandler.h"
24
#include "TrackWorkflow.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
25

26 27
LightVideoFrame* TrackHandler::nullOutput = NULL;

28
TrackHandler::TrackHandler( unsigned int nbTracks, MainWorkflow::TrackType trackType, EffectsEngine* effectsEngine ) :
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
29 30
        m_trackCount( nbTracks ),
        m_trackType( trackType ),
31 32
        m_length( 0 ),
        m_effectEngine( effectsEngine )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
33
{
34 35
    TrackHandler::nullOutput = new LightVideoFrame();

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
36 37 38
    m_tracks = new Toggleable<TrackWorkflow*>[nbTracks];
    for ( unsigned int i = 0; i < nbTracks; ++i )
    {
39
        m_tracks[i].setPtr( new TrackWorkflow( i, trackType ) );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
40 41 42 43 44 45 46 47 48 49
        connect( m_tracks[i], SIGNAL( trackEndReached( unsigned int ) ), this, SLOT( trackEndReached(unsigned int) ) );
        connect( m_tracks[i], SIGNAL( trackPaused() ), this, SLOT( trackPaused() ) );
        connect( m_tracks[i], SIGNAL( trackUnpaused() ), this, SLOT( trackUnpaused() ) );
        connect( m_tracks[i], SIGNAL( renderCompleted( unsigned int ) ), this,  SLOT( tracksRenderCompleted( unsigned int ) ), Qt::QueuedConnection );
    }
    m_nbTracksToRenderMutex = new QMutex;
}

TrackHandler::~TrackHandler()
{
50 51 52 53 54
    if ( nullOutput != NULL )
    {
        delete nullOutput;
        nullOutput = NULL;
    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    delete m_nbTracksToRenderMutex;

    for (unsigned int i = 0; i < m_trackCount; ++i)
        delete m_tracks[i];
    delete[] m_tracks;
}

void        TrackHandler::addClip( Clip* clip, unsigned int trackId, qint64 start )
{
    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();
}

void        TrackHandler::startRender()
{
    m_paused = false;
80
    m_endReached = false;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    for ( unsigned int i = 0; i < m_trackCount; ++i )
        activateTrack( i );
    computeLength();
}

void        TrackHandler::computeLength()
{
    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;
}

qint64      TrackHandler::getLength() const
{
    return m_length;
}

void        TrackHandler::getOutput( qint64 currentFrame )
{
105 106
    QMutexLocker    lockNbTracks( m_nbTracksToRenderMutex );

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
107 108
    m_renderCompleted = false;
    m_nbTracksToRender = 0;
109
    m_tmpAudioBuffer = NULL;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
110 111
    for ( unsigned int i = 0; i < m_trackCount; ++i )
    {
112
        if ( m_trackType == MainWorkflow::VideoTrack )
113
        {
114
            if ( m_tracks[i].activated() == false )
115
                m_effectEngine->setInputFrame( *TrackHandler::nullOutput, i );
116 117
            else
                m_effectEngine->setInputFrame( m_tracks[i]->getOutput( currentFrame ), i );
118
        }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    }
}

void        TrackHandler::pause()
{
    m_nbTracksToPause = 0;
    for ( unsigned int i = 0; i < m_trackCount; ++i )
    {
        if ( m_tracks[i].activated() == true )
        {
            m_nbTracksToPause.fetchAndAddAcquire( 1 );
            m_tracks[i]->pause();
        }
    }
}

void        TrackHandler::unpause()
{
    m_nbTracksToUnpause = 0;
    for ( unsigned int i = 0; i < m_trackCount; ++i )
    {
        if ( m_tracks[i].activated() == true )
        {
            m_nbTracksToUnpause.fetchAndAddAcquire( 1 );
            m_tracks[i]->unpause();
        }
    }
}

void        TrackHandler::activateAll()
{
    for ( unsigned int i = 0; i < m_trackCount; ++i )
        activateTrack( i );
}

void        TrackHandler::activateTrack( unsigned int trackId )
{
    if ( m_tracks[trackId]->getLength() > 0 )
        m_tracks[trackId].activate();
    else
        m_tracks[trackId].deactivate();
}

qint64      TrackHandler::getClipPosition( const QUuid& uuid, unsigned int trackId ) const
{
    Q_ASSERT( trackId < m_trackCount );

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

void        TrackHandler::stop()
{
    for (unsigned int i = 0; i < m_trackCount; ++i)
    {
        if ( m_tracks[i].activated() == true )
            m_tracks[i]->stop();
    }
}

void        TrackHandler::moveClip(const QUuid& clipUuid, unsigned int oldTrack,
179
                                       unsigned int newTrack, qint64 startingFrame )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 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 234 235 236 237 238 239 240 241 242 243 244 245
{
     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();
}

Clip*       TrackHandler::removeClip( const QUuid& uuid, unsigned int trackId )
{
    Q_ASSERT( trackId < m_trackCount );

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

void        TrackHandler::muteTrack( unsigned int trackId )
{
    m_tracks[trackId].setHardDeactivation( true );
}

void        TrackHandler::unmuteTrack( unsigned int trackId )
{
    m_tracks[trackId].setHardDeactivation( false );
}

Clip*       TrackHandler::getClip( const QUuid& uuid, unsigned int trackId )
{
    Q_ASSERT( trackId < m_trackCount );

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

void        TrackHandler::clear()
{
    for ( unsigned int i = 0; i < m_trackCount; ++i )
    {
        m_tracks[i]->clear();
    }
    m_length = 0;
}

bool        TrackHandler::isPaused() const
{
    return m_paused;
}

246 247 248 249 250
bool        TrackHandler::endIsReached() const
{
    return m_endReached;
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264
bool        TrackHandler::allTracksRendered() const
{
    return m_renderCompleted;
}

void        TrackHandler::trackEndReached( unsigned int trackId )
{
    m_tracks[trackId].deactivate();

    for ( unsigned int i = 0; i < m_trackCount; ++i)
    {
        if ( m_tracks[i].activated() == true )
            return ;
    }
265 266
    m_endReached = true;
    emit tracksEndReached();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
}

void        TrackHandler::trackPaused()
{
    m_nbTracksToPause.fetchAndAddAcquire( -1 );
    if ( m_nbTracksToPause <= 0 )
    {
        m_paused = true;
        emit tracksPaused();
    }
}

void        TrackHandler::trackUnpaused()
{
    m_nbTracksToUnpause.fetchAndAddAcquire( -1 );
    if ( m_nbTracksToUnpause <= 0 )
    {
        m_paused = false;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
285
        emit tracksUnpaused();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
286 287 288 289 290 291 292 293 294
    }
}

void        TrackHandler::tracksRenderCompleted( unsigned int trackId )
{
    QMutexLocker    lockNbTracks( m_nbTracksToRenderMutex );
    --m_nbTracksToRender;

    {
295
        if ( m_trackType == MainWorkflow::VideoTrack )
296 297 298
        {
            LightVideoFrame* buff = reinterpret_cast<LightVideoFrame*>( m_tracks[trackId]->getSynchroneOutput() );
            if ( buff == NULL )
299
            {
300
                m_effectEngine->setInputFrame( *TrackHandler::nullOutput, trackId );
301
            }
302 303 304 305
            else
                m_effectEngine->setInputFrame( *buff, trackId );
        }
        else
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
306
        {
307
           AudioClipWorkflow::AudioSample* buff = reinterpret_cast<AudioClipWorkflow::AudioSample*>( m_tracks[trackId]->getOutput() );
308
           m_tmpAudioBuffer = buff;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
        }
    }
    //We check for minus or equal, since we can have 0 frame to compute,
    //therefore, m_nbTracksToRender will be equal to -1
    if ( m_nbTracksToRender <= 0 )
    {
        //Just a synchronisation barriere
        m_renderCompleted = true;
        emit allTracksRenderCompleted();
    }
}

unsigned int    TrackHandler::getTrackCount() const
{
    return m_trackCount;
}
325

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
void            TrackHandler::save( QDomDocument& doc, QDomElement& timelineNode ) const
{
    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 );
        }
    }
}