ClipWorkflow.h 8.35 KB
Newer Older
1
 /*****************************************************************************
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
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
 * ClipWorkflow.h : Clip workflow. Will extract a single frame from a VLCMedia
 *****************************************************************************
 * 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.
 *****************************************************************************/

#ifndef CLIPWORKFLOW_H
#define CLIPWORKFLOW_H

#include <QReadWriteLock>
#include <QMutex>
#include <QObject>
29
#include <QQueue>
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
30 31

#include "Clip.h"
32
#include "WaitCondition.hpp"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
33
#include "VLCMediaPlayer.h"
34
#include "LightVideoFrame.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
35 36 37 38 39 40

class   ClipWorkflow : public QObject
{
    Q_OBJECT

    public:
41 42
        enum        State
        {
43
            None = -1,
44 45 46 47 48 49 50
            Stopped,        //0
            Initializing,   //1
            Ready,          //2
            Rendering,      //3
            Sleeping,       //4
            Pausing,        //5
            Paused,         //6
51 52
            Stopping,       //7
            EndReached,     //8
53 54
        };

55
        ClipWorkflow( Clip* clip );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
56 57
        virtual ~ClipWorkflow();

58
        /**
59 60 61
         *  This method returns the current frame. It locks the renderMutex,
         *  therefore, you can call this method blindly, without taking care
         *  of the rendering process advancement.
62
         */
63 64
        virtual void*           getOutput() = 0;
        virtual void            initVlcOutput() = 0;
65
        void                    initialize( bool preloading = false );
66

67 68 69 70 71
        /**
         *  Return true ONLY if the state is equal to Ready.
         *  If the state is Rendering, EndReached or anything else, this will
         *  return false.
         */
72
        bool                    isReady() const;
73 74 75 76
        /**
         *  Return true ONLY if the state is equal to EndReached.
         *  In any other cases, this will return false.
         */
77
        bool                    isEndReached() const;
78 79 80 81 82 83 84 85

        /**
         *  Return true ONLY if the state is equal to Stopped.
         *  In any other cases, this will return false.
         */
        bool                    isStopped() const;

        /**
86
         *  Return true ONLY if the state is equal to Rendering.
87 88 89 90
         *  In any other cases, this will return false.
         */
        bool                    isRendering() const;

91
        bool                    isSleeping() const;
92

93 94
        /**
         *  Returns the current workflow state.
95 96 97
         *  Be carrefull, as this function is NOT thread safe, and return the
         *  state without locking the state.
         *  It's your job to do it, by calling the getStateLock() method.
98 99 100 101 102 103 104 105
         */
        State                   getState() const;

        /**
         *  This method start the effective render, ie calling the play() method
         *  on the media player.
         *  If the media player isn't ready, this method waits.
         */
106
        void                    startRender( bool startInPausedMode );
107

108 109 110 111 112
        /**
            \brief              Returns the Clip this workflow instance is based
                                uppon, so that you can query information on it.
            \return             A pointer to a constant clip instance.
        */
113
        Clip*                   getClip();
114 115 116 117
        /**
            \brief  Stop this workflow.
        */
        void                    stop();
118
        void                    pause();
119
        void                    setTime( qint64 time );
120

121 122 123 124 125 126 127 128
        /**
         *  This method must be used to change the state of the ClipWorkflow
         *  from outside its render loop, otherwise, it may lead to deadlocks.
         *  No additional operations will be executed (for example, if setting
         *  the new state to stop, the media player won't be stopped.)
         *  This is mainly to change the behaviour of the render loop.
         */
        void                    queryStateChange( State newState );
129

130 131 132 133 134 135 136 137 138 139 140 141
        /**
         *  This method will wake the renderer thread for one iteration.
         */
        void                    wake();

        /**
         *  This returns the QReadWriteLock that protects the ClipWorkflow's state.
         *  It should be use to lock the value when checking states from outside this
         *  class.
         */
        QReadWriteLock*         getStateLock();

142
        void                    unpause();
143

144
        void                    waitForCompleteInit();
145 146 147 148 149 150

        /**
         *  \brief      Return the wait condition associated to the render.
         *              It's used when waiting for a render to terminate.
         */
        WaitCondition*          getRenderCondWait();
151
        void                    waitForCompleteRender( bool dontCheckRenderStarted = false );
152
        QMutex*                 getSleepMutex();
153

154 155 156
        virtual void*           getLockCallback() = 0;
        virtual void*           getUnlockCallback() = 0;

157 158
        LibVLCpp::MediaPlayer*  getMediaPlayer();

159 160
        void                    setFullSpeedRender( bool value );

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
161
    private:
162
        void                    setState( State state );
163
        void                    checkSynchronisation( State newState );
164
        void                    adjustBegin();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
165

166
    protected:
167
        void                    computePtsDiff( qint64 pts );
168 169
        void                    commonUnlock();

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
170
    private:
171
        LibVLCpp::MediaPlayer*  m_mediaPlayer;
172

173 174
        State                   m_requiredState;
        QMutex*                 m_requiredStateLock;
175

176
        WaitCondition*          m_initWaitCond;
177
        WaitCondition*          m_pausingStateWaitCond;
178

179 180 181 182
        /**
         *  While this flag is set to false, we will use the same buffer, to prevent
         *  having X buffers with the same picture (when media player is paused mainly)
         */
183
        bool                    m_rendering;
184 185 186 187 188
        /**
         *  This flag is here to avoid multiple connection to the mediaPlayer* slots.
         *  It's essentially a nasty hack due to the multiples calls to lock/unlock when
         *  the render is started, and that cannot really be avoided...
         */
189 190
        bool                    m_initFlag;

191
    protected:
192
        Clip*                   m_clip;
193 194 195 196 197 198
        QMutex*                 m_renderLock;
        QReadWriteLock*         m_stateLock;
        QMutex*                 m_condMutex;
        State                   m_state;
        WaitCondition*          m_renderWaitCond;
        QWaitCondition*         m_waitCond;
199
        bool                    m_fullSpeedRender;
200 201
        qint64                  m_previousPts;
        qint64                  m_currentPts;
202 203 204 205 206 207 208
        /**
         *  \brief  The VLC media used to render
         */
        LibVLCpp::Media*        m_vlcMedia;

    protected:
        /**
209
         *  \warning    Don't ever call this method from anywhere else than the unlock() method
210 211
         */
        void                    checkStateChange();
212

213
    private slots:
214 215 216
        /**
         *  \brief  This slot is used when preloading, to pause the mediaplayer once fully loaded.
         */
217
        void                    pauseAfterPlaybackStarted();
218 219 220
        /**
         *  \brief  When preloading, this slot is used to mark that the media player has been paused again.
         */
221
        void                    initializedMediaPlayer();
222
        void                    loadingComplete();
223
        void                    pausedMediaPlayer();
224
        void                    unpausedMediaPlayer();
225 226 227

    public slots:
        void                    clipEndReached();
228 229 230

    signals:
        void                    renderComplete( ClipWorkflow* );
231 232
        void                    paused();
        void                    unpaused();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
233 234 235
};

#endif // CLIPWORKFLOW_H