ClipRenderer.cpp 7.91 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 4 5
 *****************************************************************************
 * Copyright (C) 2008-2009 the VLMC team
 *
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

27
#include "ClipRenderer.h"
28
#include "MainWorkflow.h"
geoff's avatar
geoff committed
29

30 31 32
ClipRenderer::ClipRenderer() :
    GenericRenderer(),
    m_clipLoaded( false ),
33
    m_vlcMedia( NULL ),
34 35 36
    m_selectedMedia( NULL ),
    m_begin( 0 ),
    m_end( -1 ),
37
    m_mediaChanged( false )
geoff's avatar
geoff committed
38
{
39 40 41
    connect( m_mediaPlayer,     SIGNAL( stopped() ),            this,   SLOT( __videoStopped() ) );
    connect( m_mediaPlayer,     SIGNAL( paused() ),             this,   SLOT( __videoPaused() ) );
    connect( m_mediaPlayer,     SIGNAL( playing() ),            this,   SLOT( __videoPlaying() ) );
42
    connect( m_mediaPlayer,     SIGNAL( timeChanged() ),        this,   SLOT( __timeChanged() ) );
43
    connect( m_mediaPlayer,     SIGNAL( endReached() ),         this,   SLOT( __endReached() ) );
geoff's avatar
geoff committed
44 45
}

46
ClipRenderer::~ClipRenderer()
geoff's avatar
geoff committed
47
{
48 49
    if ( m_vlcMedia )
        delete m_vlcMedia;
50
    stop();
geoff's avatar
geoff committed
51
}
52

53 54 55
void        ClipRenderer::setMedia( Media* media )
{
    m_selectedMedia = media;
56 57 58 59 60
    if ( media == NULL )
    {
        m_previewLabel->clear();
        return ;
    }
61 62 63 64 65
    m_begin = 0;
    m_end = media->getNbFrames();
    if ( m_isRendering == true )
        m_mediaChanged = true;
    else
66
    {
67 68 69
        //setSnapshotVisibility( true );
        //m_previewLabel->setPixmap( media->getSnapshot().scaled( m_previewLabel->size(),
        //                                                                    Qt::KeepAspectRatio ) );
70
        m_clipLoaded = false;
71
    }
72 73
}

74
void        ClipRenderer::setClip( Clip* clip )
75
{
76 77 78 79 80 81
    if ( clip == NULL )
    {
        m_selectedMedia = NULL;
        m_previewLabel->clear();
        return ;
    }
82 83 84
    m_selectedMedia = clip->getParent();
    m_begin = clip->getBegin();
    m_end = clip->getEnd();
85 86 87
    if ( m_isRendering == true )
        m_mediaChanged = true;
    else
88
    {
89
        setSnapshotVisibility( true );
90 91
        m_previewLabel->setPixmap( clip->getParent()->getSnapshot().scaled( m_previewLabel->size(),
                                                                            Qt::KeepAspectRatio ) );
92
        m_clipLoaded = false;
93
    }
94 95 96 97
}

void        ClipRenderer::startPreview()
{
98
    if ( m_selectedMedia == NULL )
99
        return ;
100
    setSnapshotVisibility( false );
101

102
    //If an old media is found, we delete it, and disconnect
103 104
    if ( m_vlcMedia != NULL )
        delete m_vlcMedia;
105
    m_vlcMedia = new LibVLCpp::Media( m_selectedMedia->getFileInfo()->absoluteFilePath() );
106 107

    m_mediaPlayer->setMedia( m_vlcMedia );
108

109
    m_mediaPlayer->play();
110
    m_mediaPlayer->setPosition( m_begin / m_end );
111
    m_clipLoaded = true;
112
    m_isRendering = true;
113
    m_paused = false;
114
    m_mediaChanged = false;
115 116
}

117
void        ClipRenderer::stop()
118
{
119
    if ( m_clipLoaded == true && m_isRendering == true )
120
    {
121
        m_isRendering = false;
122
        m_mediaPlayer->stop();
123
        m_paused = false;
124 125
        if ( m_mediaChanged == true )
            m_clipLoaded = false;
126 127 128
    }
}

129
void        ClipRenderer::togglePlayPause( bool forcePause )
130
{
131
    if ( m_clipLoaded == false )
132
    {
133
        emit frameChanged( 0, MainWorkflow::Renderer );
134
        startPreview();
135 136
        return ;
    }
137 138 139
    //If for some reason, nothing was loaded in startPreview(), we just return.
    if ( m_clipLoaded == false )
        return ;
140

141
    if ( m_paused == false && m_isRendering == true )
142
    {
143
        m_mediaPlayer->pause();
144 145
        m_paused = true;
    }
146
    else if ( forcePause == false )
147
    {
148
        if ( m_isRendering == false )
149 150
        {
            m_mediaPlayer->play();
151
            m_mediaPlayer->setPosition( m_begin / ( m_end - m_begin ) );
152
            m_isRendering = true;
153 154 155
        }
        else
            m_mediaPlayer->play();
156 157
        m_paused = false;
    }
158
}
159

160
void        ClipRenderer::nextFrame()
161
{
162
    if ( m_isRendering == true && m_paused == true )
163
    {
164
        m_mediaPlayer->nextFrame();
165 166 167
    }
}

168
void        ClipRenderer::previousFrame()
169
{
170
    if ( m_isRendering == true && m_paused == true )
171
    {
chouquette's avatar
chouquette committed
172
        qint64   interval =  static_cast<qint64>( (1.0f / m_mediaPlayer->getFps()) * 1000.0f );
173 174 175 176
        m_mediaPlayer->setTime( m_mediaPlayer->getTime() - interval );
    }
}

177
qint64      ClipRenderer::getLengthMs() const
178
{
179 180
    if ( m_selectedMedia )
        return ( qRound64( (qreal)( m_end - m_begin ) / m_selectedMedia->getFps() * 1000.0 ) );
181 182 183
    return 0;
}

184
//FIXME: this won't work with clips !
185 186
void        ClipRenderer::mediaUnloaded( const QUuid& uuid )
{
187
    if ( m_selectedMedia != NULL && m_selectedMedia->getUuid() == uuid )
188 189 190
    {
        m_mediaPlayer->stop();
        m_clipLoaded = false;
191
        m_selectedMedia = NULL;
192 193 194 195 196
        m_isRendering = false;
        m_paused = false;
    }
}

197 198 199 200 201 202
void        ClipRenderer::setSnapshotVisibility( bool val )
{
   m_previewLabel->setVisible( val );
   m_renderWidget->setVisible( !val );
}

203 204 205 206
qint64      ClipRenderer::getCurrentFrame() const
{
    if ( m_clipLoaded == false || m_isRendering == false || m_selectedMedia == NULL )
        return 0;
207
    return qRound64( (qreal)m_mediaPlayer->getTime() / 1000 * (qreal)m_selectedMedia->getFps() );
208 209
}

210 211 212 213 214 215 216
float       ClipRenderer::getFps() const
{
    if ( m_selectedMedia != NULL )
        return m_selectedMedia->getFps();
    return 0.0f;
}

217 218 219 220 221
Media*      ClipRenderer::getMedia() const
{
    return m_selectedMedia;
}

222 223 224 225 226
void        ClipRenderer::previewWidgetCursorChanged( qint64 newFrame )
{
    if ( m_isRendering == true )
    {
        qint64 nbSeconds = qRound64( (qreal)newFrame / m_selectedMedia->getFps() );
227
        m_mediaPlayer->setTime( nbSeconds * 1000 );
228 229 230
    }
}

231 232 233
/////////////////////////////////////////////////////////////////////
/////SLOTS :
/////////////////////////////////////////////////////////////////////
234
void        ClipRenderer::__videoPaused()
235 236 237 238
{
    emit paused();
}

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

245
void        ClipRenderer::__videoPlaying()
246
{
247
    emit playing();
248 249
}

250 251 252 253 254 255 256 257 258 259 260 261 262 263
//void        ClipRenderer::__positionChanged()
//{
//    if ( m_clipLoaded == false)
//        return ;
//
//    qDebug() << "begin:" << m_begin << "end:" << m_end;
//    qDebug() << "position:" << m_mediaPlayer->getPosition();
//    float   begin = m_begin / ( m_end - m_begin );
//    float   end = m_end / ( m_end - m_begin );
//    float pos = ( m_mediaPlayer->getPosition() - begin ) /
//                ( end - begin );
//    qDebug() << pos;
//    emit frameChanged( pos, MainWorkflow::Renderer );
//}
264

265 266
void        ClipRenderer::__timeChanged()
{
267 268 269 270
    float   fps = (qreal)m_mediaPlayer->getFps();
    if ( fps < 0.1f )
        fps = m_selectedMedia->getFps();
    qint64 f = qRound64( (qreal)m_mediaPlayer->getTime() / 1000.0 * fps );
271
    emit frameChanged( f, MainWorkflow::Renderer );
272 273
}

274
void        ClipRenderer::__endReached()
275 276
{
    m_mediaPlayer->stop();
277
    m_isRendering = false;
278 279
    if ( m_mediaChanged == true )
        m_clipLoaded = false;
280
    emit endReached();
281
}