ClipRenderer.cpp 6.73 KB
Newer Older
Christophe Courtaut's avatar
Christophe Courtaut committed
1
/*****************************************************************************
2
 * ClipRenderer.cpp: Render from a Clip (mainly for previewing purpose)
Christophe Courtaut's avatar
Christophe Courtaut committed
3
 *****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
4
 * Copyright (C) 2008-2010 VideoLAN
Christophe Courtaut's avatar
Christophe Courtaut committed
5
 *
6
 * Authors: Geoffroy Lacarrière <geoffroylaca@gmail.com>
7
 *          Hugo Beauzee-Luyssen <hugo@vlmc.org>
Christophe Courtaut's avatar
Christophe Courtaut committed
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.
 *****************************************************************************/

24
#include <QtDebug>
25
#include <QtGlobal>
26
#include <QLabel>
27

28
#include "Clip.h"
29
#include "ClipRenderer.h"
30
#include "Library.h"
31
#include "Media.h"
32
#include "MainWorkflow.h"
33
#include "VLCMediaPlayer.h"
34
#include "VLCMedia.h"
geoff's avatar
geoff committed
35

36 37 38
ClipRenderer::ClipRenderer() :
    GenericRenderer(),
    m_clipLoaded( false ),
39
    m_vlcMedia( NULL ),
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
40
    m_selectedClip( NULL ),
41 42
    m_begin( 0 ),
    m_end( -1 ),
43
    m_mediaChanged( false )
geoff's avatar
geoff committed
44
{
45
    connect( m_mediaPlayer,     SIGNAL( stopped() ),            this,   SLOT( __videoStopped() ) );
46 47
    connect( m_mediaPlayer,     SIGNAL( paused() ),             this,   SIGNAL( paused() ) );
    connect( m_mediaPlayer,     SIGNAL( playing() ),            this,   SIGNAL( playing() ) );
48
    connect( m_mediaPlayer,     SIGNAL( timeChanged( qint64 ) ),        this,   SLOT( __timeChanged( qint64 ) ) );
49
    connect( m_mediaPlayer,     SIGNAL( endReached() ),         this,   SLOT( __endReached() ) );
geoff's avatar
geoff committed
50 51
}

52
ClipRenderer::~ClipRenderer()
geoff's avatar
geoff committed
53
{
54 55
    if ( m_vlcMedia )
        delete m_vlcMedia;
56
    stop();
geoff's avatar
geoff committed
57
}
58

59
void
60
ClipRenderer::setClip( Clip* clip )
61
{
62 63 64 65

    if ( m_selectedClip != NULL && clip != NULL &&
         clip->uuid() != m_selectedClip->uuid() )
        clipUnloaded( m_selectedClip->uuid() );
66
    if ( clip == NULL )
67
    {
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
68
        m_selectedClip = NULL;
69
        m_clipLoaded = false;
70 71
        return ;
    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
72
    m_selectedClip = clip;
73 74 75 76 77 78 79 80
    if ( clip->length() == 0 )
        return ;
    updateInfos( clip );
}

void
ClipRenderer::updateInfos( Clip* clip )
{
81 82
    m_begin = clip->begin();
    m_end = clip->end();
83 84 85 86
    if ( m_isRendering == true )
        m_mediaChanged = true;
    else
        m_clipLoaded = false;
87 88
}

89 90
void
ClipRenderer::startPreview()
91
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
92
    if ( m_selectedClip == NULL )
93
        return ;
94
    updateInfos( m_selectedClip );
95

96
    //If an old media is found, we delete it, and disconnect
97 98
    if ( m_vlcMedia != NULL )
        delete m_vlcMedia;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
99
    m_vlcMedia = new LibVLCpp::Media( m_selectedClip->getMedia()->fileInfo()->absoluteFilePath() );
100 101

    m_mediaPlayer->setMedia( m_vlcMedia );
102

103
    m_mediaPlayer->play();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
104
    m_mediaPlayer->setPosition( (double)m_begin / (double)m_selectedClip->getMedia()->nbFrames() );
105
    m_clipLoaded = true;
106
    m_isRendering = true;
107
    m_paused = false;
108
    m_mediaChanged = false;
109 110
}

111 112
void
ClipRenderer::stop()
113
{
114
    if ( m_clipLoaded == true && m_isRendering == true )
115
    {
116
        m_isRendering = false;
117
        m_mediaPlayer->stop();
118
        m_paused = false;
119 120
        if ( m_mediaChanged == true )
            m_clipLoaded = false;
121 122 123
    }
}

124 125
void
ClipRenderer::togglePlayPause( bool forcePause )
126
{
127
    if ( m_clipLoaded == false )
128
    {
129
        emit frameChanged( 0, MainWorkflow::Renderer );
130
        startPreview();
131 132
        return ;
    }
133
    if ( m_paused == false && m_isRendering == true )
134
    {
135
        m_mediaPlayer->pause();
136 137
        m_paused = true;
    }
138
    else if ( forcePause == false )
139
    {
140
        if ( m_isRendering == false )
141 142
        {
            m_mediaPlayer->play();
143
            m_mediaPlayer->setPosition( m_begin / ( m_end - m_begin ) );
144
            m_isRendering = true;
145 146 147
        }
        else
            m_mediaPlayer->play();
148 149
        m_paused = false;
    }
150
}
151

152 153
void
ClipRenderer::nextFrame()
154
{
155
    if ( m_isRendering == true && m_paused == true )
156
    {
157
        m_mediaPlayer->nextFrame();
158 159 160
    }
}

161 162
void
ClipRenderer::previousFrame()
163
{
164
    if ( m_isRendering == true && m_paused == true )
165
    {
chouquette's avatar
chouquette committed
166
        qint64   interval =  static_cast<qint64>( (1.0f / m_mediaPlayer->getFps()) * 1000.0f );
167 168 169 170
        m_mediaPlayer->setTime( m_mediaPlayer->getTime() - interval );
    }
}

171 172
qint64
ClipRenderer::getLength() const
173 174 175 176
{
    return m_end - m_begin;
}

177 178
qint64
ClipRenderer::getLengthMs() const
179
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
180 181
    if ( m_selectedClip )
        return ( qRound64( (qreal)( m_end - m_begin ) / m_selectedClip->getMedia()->fps() * 1000.0 ) );
182 183 184
    return 0;
}

185
void
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
186
ClipRenderer::clipUnloaded( const QUuid& uuid )
187
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
188
    if ( m_selectedClip != NULL && m_selectedClip->uuid() == uuid )
189 190 191
    {
        m_mediaPlayer->stop();
        m_clipLoaded = false;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
192
        m_selectedClip = NULL;
193 194 195 196 197
        m_isRendering = false;
        m_paused = false;
    }
}

198 199
qint64
ClipRenderer::getCurrentFrame() const
200
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
201
    if ( m_clipLoaded == false || m_isRendering == false || m_selectedClip == NULL )
202
        return 0;
203
    return qRound64( (qreal)m_mediaPlayer->getTime() / 1000 *
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
204
                     (qreal)m_selectedClip->getMedia()->fps() );
205 206
}

207 208
float
ClipRenderer::getFps() const
209
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
210 211
    if ( m_selectedClip != NULL )
        return m_selectedClip->getMedia()->fps();
212 213 214
    return 0.0f;
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
215 216
Clip*
ClipRenderer::getClip()
217
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
218
    return m_selectedClip;
219 220
}

221 222
void
ClipRenderer::previewWidgetCursorChanged( qint64 newFrame )
223 224 225
{
    if ( m_isRendering == true )
    {
226
        newFrame += m_begin;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
227
        qint64 nbSeconds = qRound64( (qreal)newFrame / m_selectedClip->getMedia()->fps() );
228
        m_mediaPlayer->setTime( nbSeconds * 1000 );
229 230 231
    }
}

232 233 234 235
/////////////////////////////////////////////////////////////////////
/////SLOTS :
/////////////////////////////////////////////////////////////////////

236 237
void
ClipRenderer::__videoStopped()
238
{
239
    emit frameChanged( 0, MainWorkflow::Renderer );
240 241 242
    emit stopped();
}

243 244
void
ClipRenderer::__timeChanged( qint64 time )
245
{
246 247
    float   fps = (qreal)m_mediaPlayer->getFps();
    if ( fps < 0.1f )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
248
        fps = m_selectedClip->getMedia()->fps();
249
    qint64 f = qRound64( (qreal)time / 1000.0 * fps );
250 251 252 253 254 255
    if ( f >= m_end )
    {
        __endReached();
        return ;
    }
    f = f - m_begin;
256
    emit frameChanged( f, MainWorkflow::Renderer );
257 258
}

259 260
void
ClipRenderer::__endReached()
261 262
{
    m_mediaPlayer->stop();
263
    m_isRendering = false;
264 265
    if ( m_mediaChanged == true )
        m_clipLoaded = false;
266
    emit endReached();
267
}