SequenceWorkflow.cpp 9.91 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 28 29 30 31 32 33 34 35
/*****************************************************************************
 * SequenceWorkflow.cpp : Manages tracks in a single sequence
 *****************************************************************************
 * Copyright (C) 2008-2016 VideoLAN
 *
 * Authors: Yikei Lu    <luyikei.qmltu@gmail.com>
 *
 * 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.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "SequenceWorkflow.h"

#include "Backend/MLT/MLTTrack.h"
#include "Backend/MLT/MLTMultiTrack.h"
#include "EffectsEngine/EffectHelper.h"
#include "Workflow/MainWorkflow.h"
#include "Main/Core.h"
#include "Library/Library.h"
#include "Tools/VlmcDebug.h"
36
#include "Media/Media.h"
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

SequenceWorkflow::SequenceWorkflow( size_t trackCount )
    : m_multitrack( new Backend::MLT::MLTMultiTrack )
    , m_trackCount( trackCount )
{
    for ( int i = 0; i < trackCount; ++i )
    {
        auto audioTrack = std::shared_ptr<Backend::ITrack>( new Backend::MLT::MLTTrack );
        audioTrack->setVideoEnabled( false );
        m_tracks[Workflow::AudioTrack] << audioTrack;

        auto videoTrack = std::shared_ptr<Backend::ITrack>( new Backend::MLT::MLTTrack );
        videoTrack->setMute( true );
        m_tracks[Workflow::VideoTrack] << videoTrack;

        auto multitrack = std::shared_ptr<Backend::IMultiTrack>( new Backend::MLT::MLTMultiTrack );
        multitrack->setTrack( *videoTrack, 0 );
        multitrack->setTrack( *audioTrack, 1 );
        m_multiTracks << multitrack;

        m_multitrack->setTrack( *multitrack, i );
    }
}

SequenceWorkflow::~SequenceWorkflow()
{
    delete m_multitrack;
    clear();
}

bool
68
SequenceWorkflow::addClip( QSharedPointer<Clip> const& clip, quint32 trackId, qint32 pos )
69 70 71 72 73 74 75 76 77 78 79
{
    auto ret = trackFromFormats( trackId, clip->formats() )->insertAt( *clip->input(), pos );
    if ( ret == false )
        return false;
    m_clips.insert( clip->uuid(), std::make_tuple( clip, trackId, pos ) );
    return true;
}

QString
SequenceWorkflow::addClip( const QUuid& uuid, quint32 trackId, qint32 pos, bool isAudioClip )
{
80 81
    auto parentClip = Core::instance()->library()->clip( uuid );
    if ( parentClip == nullptr )
82 83 84 85
    {
        vlmcCritical() << "Couldn't find an acceptable parent to be added.";
        return QUuid().toString();
    }
86 87 88 89 90 91
    auto newClip = parentClip->media()->cut( parentClip->begin(), parentClip->end() );
    if ( newClip == nullptr )
    {
        vlmcCritical() << "Couldn't duplicate the parent clip.";
        return QUuid().toString();
    }
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

    if ( isAudioClip == true )
        newClip->setFormats( Clip::Audio );
    else
        newClip->setFormats( Clip::Video );

    bool ret = trackFromFormats( trackId, newClip->formats() )->insertAt( *newClip->input(), pos );
    if ( ret == false )
        return QUuid().toString();
    m_clips.insert( newClip->uuid(), std::make_tuple( newClip, trackId, pos ) );
    return newClip->uuid().toString();
}

bool
SequenceWorkflow::moveClip( const QUuid& uuid, quint32 trackId, qint64 pos )
{
    auto it = m_clips.find( uuid );
    if ( it == m_clips.end() )
    {
        vlmcCritical() << "Couldn't find a clip " << uuid;
        return false;
    }
    auto clip = std::get<ClipTupleIndex::Clip>( it.value() );
    auto oldTrackId = std::get<ClipTupleIndex::TrackId>( it.value() );
116 117 118
    auto oldPosition = std::get<ClipTupleIndex::Position>( it.value() );
    if ( oldPosition == pos )
        return true;
119 120 121 122 123 124 125 126 127 128
    auto track = trackFromFormats( oldTrackId, clip->formats() );
    bool ret = true;
    if ( trackId != oldTrackId )
    {
        removeClip( uuid );
        ret = addClip( clip, trackId, pos );
    }
    else
    {
        ret = track->move( std::get<ClipTupleIndex::Position>( it.value() ), pos );
129 130 131 132 133
        if ( ret == true )
        {
            m_clips.erase( it );
            m_clips.insert( uuid, std::make_tuple( clip, trackId, pos ) );
        }
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
    }
    // TODO: If we detect collision too strictly, there will be a problem if we want to move multiple
    //       clips at the same time.
    return ret;
}

bool
SequenceWorkflow::resizeClip( const QUuid& uuid, qint64 newBegin, qint64 newEnd, qint64 newPos )
{
    auto it = m_clips.find( uuid );
    if ( it == m_clips.end() )
    {
        vlmcCritical() << "Couldn't find a clip " << uuid;
        return false;
    }
    auto clip = std::get<ClipTupleIndex::Clip>( it.value() );
    auto trackId = std::get<ClipTupleIndex::TrackId>( it.value() );
    auto position = std::get<ClipTupleIndex::Position>( it.value() );
    auto track = trackFromFormats( trackId, clip->formats() );
    auto ret = track->resizeClip( track->clipIndexAt( position ), newBegin, newEnd );
    if ( ret == false )
        return false;
    ret = moveClip( uuid, trackId, newPos );
    return ret;
}

160
QSharedPointer<Clip>
161 162 163 164 165 166
SequenceWorkflow::removeClip( const QUuid& uuid )
{
    auto it = m_clips.find( uuid );
    if ( it == m_clips.end() )
    {
        vlmcCritical() << "Couldn't find a clip " << uuid;
167
        return {};
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    }
    auto clip = std::get<ClipTupleIndex::Clip>( it.value() );
    auto trackId = std::get<ClipTupleIndex::TrackId>( it.value() );
    auto position = std::get<ClipTupleIndex::Position>( it.value() );
    auto track = trackFromFormats( trackId, clip->formats() );
    track->remove( track->clipIndexAt( position ) );
    m_clips.erase( it );
    clip->disconnect( this );
    return clip;

}

bool
SequenceWorkflow::linkClips( const QUuid& uuidA, const QUuid& uuidB )
{
    auto clipA = clip( uuidA );
    auto clipB = clip( uuidB );
    if ( !clipA || !clipB )
    {
187
        vlmcCritical() << "Couldn't find clips: " << uuidA << " and " << uuidB;
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
        return false;
    }
    clipA->setLinkedClipUuid( clipB->uuid() );
    clipB->setLinkedClipUuid( clipA->uuid() );
    clipA->setLinked( true );
    clipB->setLinked( true );
    return true;
}

bool
SequenceWorkflow::unlinkClips( const QUuid& uuidA, const QUuid& uuidB )
{
    auto clipA = clip( uuidA );
    auto clipB = clip( uuidB );
    if ( !clipA || !clipB )
    {
204
        vlmcCritical() << "Couldn't find clips: " << uuidA << " and " << uuidB;
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
        return false;
    }
    clipA->setLinked( false );
    clipB->setLinked( false );
    return true;
}

QVariant
SequenceWorkflow::toVariant() const
{
    QVariantList l;
    for ( auto it = m_clips.begin(); it != m_clips.end(); ++it )
    {
        auto clip = std::get<ClipTupleIndex::Clip>( it.value() );
        auto trackId = std::get<ClipTupleIndex::TrackId>( it.value() );
        auto position = std::get<ClipTupleIndex::Position>( it.value() );
221 222 223 224
        QVariantHash h {
            { "uuid", clip->uuid() },
            { "position", position },
            { "trackId", trackId },
225 226
            { "linked", clip->isLinked() },
            { "linkedClip", clip->linkedClipUuid() },
227 228
            { "filters", EffectHelper::toVariant( clip->input() ) }
        };
229 230 231 232 233 234 235 236 237 238 239
        l << h;
    }
    QVariantHash h{ { "clips", l }, { "filters", EffectHelper::toVariant( m_multitrack ) } };
    return h;
}

void
SequenceWorkflow::loadFromVariant( const QVariant& variant )
{
    for ( auto& var : variant.toMap()["clips"].toList() )
    {
240 241
        auto m = var.toMap();
        auto clip = Core::instance()->library()->clip( m["uuid"].toUuid() );
242

243 244 245 246 247
        if ( clip == nullptr )
        {
            vlmcCritical() << "Couldn't find an acceptable parent to be added.";
            continue;
        }
248

249
        addClip( clip, m["trackId"].toUInt(), m["position"].toLongLong() );
250

251 252 253 254
        auto isLinked = m["linked"].toBool();
        clip->setLinked( isLinked );
        if ( isLinked == true )
            clip->setLinkedClipUuid( m["linkedClip"].toString() );
255

256
        EffectHelper::loadFromVariant( m["filters"], clip->input() );
257

258
        emit Core::instance()->workflow()->clipAdded( clip->uuid().toString() );
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    }
    EffectHelper::loadFromVariant( variant.toMap()["filters"], m_multitrack );
}

void
SequenceWorkflow::clear()
{
    auto it = m_clips.begin();
    while ( it != m_clips.end() )
    {
        removeClip( it.key() );
        // m_clips.begin() can be changed
        it = m_clips.begin();
    }
}

275
QSharedPointer<Clip>
276 277 278 279
SequenceWorkflow::clip( const QUuid& uuid )
{
    auto it = m_clips.find( uuid );
    if ( it == m_clips.end() )
280
        return {};
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    return std::get<ClipTupleIndex::Clip>( it.value() );
}

quint32
SequenceWorkflow::trackId( const QUuid& uuid )
{
    auto it = m_clips.find( uuid );
    if ( it == m_clips.end() )
        return 0;
    return std::get<ClipTupleIndex::TrackId>( it.value() );
}

qint32
SequenceWorkflow::position( const QUuid& uuid )
{
    auto it = m_clips.find( uuid );
    if ( it == m_clips.end() )
        return 0;
    return std::get<ClipTupleIndex::Position>( it.value() );
}

Backend::IInput*
SequenceWorkflow::input()
{
    return m_multitrack;
}

Backend::IInput*
SequenceWorkflow::trackInput( quint32 trackId )
{
    Q_ASSERT( trackId < m_multiTracks.size() );
    return m_multiTracks[trackId].get();
}

std::shared_ptr<Backend::ITrack>
SequenceWorkflow::trackFromFormats( quint32 trackId, Clip::Formats formats )
{
    if ( trackId >= (quint32)m_trackCount )
        return nullptr;
    if ( formats.testFlag( Clip::Audio ) )
        return m_tracks[Workflow::AudioTrack][trackId];
    else if ( formats.testFlag( Clip::Video ) )
        return m_tracks[Workflow::VideoTrack][trackId];
    return nullptr;
}