TrackHandler.cpp 6.88 KB
Newer Older
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
1 2 3
/*****************************************************************************
 * TrackHandler.cpp : Handle multiple track of a kind (audio or video)
 *****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
4
 * Copyright (C) 2008-2010 VideoLAN
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
5
 *
6
 * Authors: Hugo Beauzée-Luyssen <beauze.h@gmail.com>
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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"
25
#include "Workflow/Types.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
26

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

30
TrackHandler::TrackHandler( unsigned int nbTracks, Workflow::TrackType trackType ) :
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
31 32
        m_trackCount( nbTracks ),
        m_trackType( trackType ),
33
        m_length( 0 )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
34 35 36 37
{
    m_tracks = new Toggleable<TrackWorkflow*>[nbTracks];
    for ( unsigned int i = 0; i < nbTracks; ++i )
    {
38
        m_tracks[i].setPtr( new TrackWorkflow( trackType ) );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
39 40 41 42 43 44 45 46 47 48
    }
}

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

49 50
void
TrackHandler::addClip( ClipHelper* ch, unsigned int trackId, qint64 start )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
51 52 53 54
{
    Q_ASSERT_X( trackId < m_trackCount, "MainWorkflow::addClip",
                "The specified trackId isn't valid, for it's higher than the number of tracks");

55
    m_tracks[trackId]->addClip( ch, start );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
56 57 58 59 60 61

    //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();
}

62 63 64 65 66 67
void
TrackHandler::addEffect( Effect *effect, quint32 trackId, const QUuid &uuid )
{
    m_tracks[trackId]->addEffect( effect, uuid );
}

68
void
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
69
TrackHandler::startRender( quint32 width, quint32 height, double fps )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
70
{
71
    m_endReached = false;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
72
    computeLength();
73 74 75 76 77
    if ( m_length == 0 )
        m_endReached = true;
    else
    {
        for ( unsigned int i = 0; i < m_trackCount; ++i )
78
        {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
79
            m_tracks[i]->initRender( width, height, fps );
80
        }
81
    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
82 83
}

84 85
void
TrackHandler::computeLength()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
86 87 88 89 90 91 92 93 94 95 96
{
    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;
}

97 98
qint64
TrackHandler::getLength() const
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
99 100 101 102
{
    return m_length;
}

103
Workflow::OutputBuffer*
104
TrackHandler::getOutput( qint64 currentFrame, qint64 subFrame, bool paused )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
105
{
106 107
    bool        validTrack = false;

108
    for ( int i = m_trackCount - 1; i >= 0; --i )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
109
    {
110 111
        if ( m_tracks[i].activated() == false || m_tracks[i]->hasFrameToRender( currentFrame ) )
            continue ;
112
        validTrack = true;
113
        Workflow::OutputBuffer  *ret = m_tracks[i]->getOutput( currentFrame, subFrame, paused );
114 115
        if ( ret == NULL )
            continue ;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
116
        else
117
            return ret;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
118
    }
119 120
    if ( validTrack == false )
        allTracksEnded();
121
    return NULL;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
122 123
}

124 125
qint64
TrackHandler::getClipPosition( const QUuid &uuid, unsigned int trackId ) const
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
126 127 128 129 130 131
{
    Q_ASSERT( trackId < m_trackCount );

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

132 133
void
TrackHandler::stop()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
134 135
{
    for (unsigned int i = 0; i < m_trackCount; ++i)
136
        m_tracks[i]->stop();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
137 138
}

139 140
void
TrackHandler::moveClip(const QUuid &clipUuid, unsigned int oldTrack,
141
                                       unsigned int newTrack, qint64 startingFrame )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
{
     Q_ASSERT( newTrack < m_trackCount && oldTrack < m_trackCount );

    if ( oldTrack == newTrack )
    {
        //And now, just move the clip.
        m_tracks[newTrack]->moveClip( clipUuid, startingFrame );
    }
    else
    {
        ClipWorkflow* cw = m_tracks[oldTrack]->removeClipWorkflow( clipUuid );
        m_tracks[newTrack]->addClip( cw, startingFrame );
    }
    computeLength();
}

158 159
Clip*
TrackHandler::removeClip( const QUuid& uuid, unsigned int trackId )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
160 161 162 163 164 165 166 167
{
    Q_ASSERT( trackId < m_trackCount );

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

168 169
void
TrackHandler::muteTrack( unsigned int trackId )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
170
{
171
    m_tracks[trackId].deactivate();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
172 173
}

174 175
void
TrackHandler::unmuteTrack( unsigned int trackId )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
176
{
177
    m_tracks[trackId].activate();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
178 179
}

180 181
Clip*
TrackHandler::getClip( const QUuid& uuid, unsigned int trackId )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
182 183 184 185 186 187
{
    Q_ASSERT( trackId < m_trackCount );

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

188 189
void
TrackHandler::clear()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
190 191 192 193 194 195 196 197
{
    for ( unsigned int i = 0; i < m_trackCount; ++i )
    {
        m_tracks[i]->clear();
    }
    m_length = 0;
}

198 199
bool
TrackHandler::endIsReached() const
200 201 202 203
{
    return m_endReached;
}

204
void
205
TrackHandler::allTracksEnded()
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
206
{
207 208
    m_endReached = true;
    emit tracksEndReached();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
209 210
}

211 212
unsigned int
TrackHandler::getTrackCount() const
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
213 214 215
{
    return m_trackCount;
}
216

217
void
218
TrackHandler::save( QXmlStreamWriter& project ) const
219 220 221 222 223
{
    for ( unsigned int i = 0; i < m_trackCount; ++i)
    {
        if ( m_tracks[i]->getLength() > 0 )
        {
224 225 226 227 228
            project.writeStartElement( "track" );
            project.writeAttribute( "type", QString::number( (int)m_trackType ) );
            project.writeAttribute( "id", QString::number( i ) );
            m_tracks[i]->save( project );
            project.writeEndElement();
229 230 231
        }
    }
}
232 233 234 235 236 237 238 239 240 241

void
TrackHandler::renderOneFrame()
{
    for ( unsigned int i = 0; i < m_trackCount; ++i)
    {
        if ( m_tracks[i].activated() == true )
            m_tracks[i]->renderOneFrame();
    }
}
242 243 244 245 246 247 248

void
TrackHandler::setFullSpeedRender( bool val )
{
    for ( unsigned int i = 0; i < m_trackCount; ++i)
        m_tracks[i]->setFullSpeedRender( val );
}
249 250 251 252 253 254 255 256 257 258 259 260

void
TrackHandler::muteClip( const QUuid &uuid, quint32 trackId )
{
    m_tracks[trackId]->muteClip( uuid );
}

void
TrackHandler::unmuteClip( const QUuid &uuid, quint32 trackId )
{
    m_tracks[trackId]->unmuteClip( uuid );
}
261 262 263 264 265 266 267 268 269

bool
TrackHandler::contains( const QUuid &uuid ) const
{
    for ( unsigned int i = 0; i < m_trackCount; ++i )
        if ( m_tracks[i]->contains( uuid ) == true )
            return true;
    return false;
}
270 271 272 273 274 275 276 277 278 279 280 281

void
TrackHandler::stopFrameComputing()
{
    for ( unsigned int i = 0; i < m_trackCount; ++i )
    {
        //First stop the frame computing
        m_tracks[i]->stopFrameComputing();
        //then deactivate the track to avoid the generation to be resumed.
        m_tracks[i].deactivate();
    }
}