ClipWorkflow.h 8.3 KB
Newer Older
1
 /*****************************************************************************
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
2 3
 * ClipWorkflow.h : Clip workflow. Will extract a single frame from a VLCMedia
 *****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
4
 * Copyright (C) 2008-2010 VideoLAN
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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

26
#include "mdate.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
27

28 29 30 31 32 33 34 35 36
#include <QObject>

class   QReadWriteLock;
class   QMutex;

class   Clip;
class   WaitCondition;
class   LightVideoFrame;

37 38 39 40 41 42
namespace LibVLCpp
{
    class   MediaPlayer;
    class   Media;
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
43 44 45 46 47
class   ClipWorkflow : public QObject
{
    Q_OBJECT

    public:
48 49
        enum        State
        {
50
            None = -1,
51
            /// \brief  Used when the clipworkflow hasn't been started yet
52
            Stopped,            //0
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
53 54 55 56 57
            /**
             *  \brief  Used when initializing is in progress (until the mediaplayer
             *          enters the playing state.
             */
            Initializing,       //1
58
            /// \brief  Used when the clipworkflow is launched and active
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
59
            Rendering,          //2
60
            /// \brief  Used when stopping
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
61
            Stopping,           //3
62 63
            /// \brief  Used when end is reached, IE no more frame has to be rendered, but the trackworkflow
            ///         may eventually ask for some.
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
64
            EndReached,         //4
65 66 67
            // Here starts internal states :
            /// \brief  This state will be used when an unpause
            ///         has been required
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
68
            UnpauseRequired,    //5
69 70
            /// \brief  This state will be used when a pause
            ///         has been required
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
71
            PauseRequired,      //6
72 73
            /// \brief  This state will be used when the media player is paused,
            ///         because of a sufficient number of computed buffers
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
74
            Paused,             //7
75 76
        };

77 78 79 80 81 82 83 84 85 86 87
        /**
         *  \brief  Used to know which way you want to get a computed output.
         *          Pop: the buffer is popped and returned
         *          Get: the buffer is just returned (for paused mode for instance)
         */
        enum        GetMode
        {
            Pop,
            Get,
        };

88
        ClipWorkflow( Clip* clip );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
89 90
        virtual ~ClipWorkflow();

91
        /**
92 93 94
         *  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.
95
         */
96
        virtual void*           getOutput( ClipWorkflow::GetMode mode ) = 0;
97
        bool                    preGetOutput();
98
        void                    postGetOutput();
99
        virtual void            initVlcOutput() = 0;
100
        void                    initialize();
101

102 103 104 105
        /**
         *  Return true ONLY if the state is equal to EndReached.
         *  In any other cases, this will return false.
         */
106
        bool                    isEndReached() const;
107 108 109 110 111 112 113 114

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

        /**
115
         *  Return true ONLY if the state is equal to Rendering.
116 117 118 119 120 121
         *  In any other cases, this will return false.
         */
        bool                    isRendering() const;

        /**
         *  Returns the current workflow state.
122 123 124
         *  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.
125 126 127
         */
        State                   getState() const;

128 129 130 131 132
        /**
            \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.
        */
133
        Clip*                   getClip();
134 135 136 137
        /**
            \brief  Stop this workflow.
        */
        void                    stop();
138 139 140 141
        /**
         *  \brief  Set the rendering position
         *  \param  time    The position in millisecond
         */
142
        void                    setTime( qint64 time );
143

144 145 146 147 148 149 150 151
        /**
         *  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 );
152

153 154 155 156 157 158 159
        /**
         *  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();

160
        void                    waitForCompleteInit();
161

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
162 163
        virtual void*           getLockCallback() const = 0;
        virtual void*           getUnlockCallback() const = 0;
164

165 166
        LibVLCpp::MediaPlayer*  getMediaPlayer();

167 168 169 170 171
        /**
         *  \sa MainWorkflow::setFullSpeedRender();
         */
        void                    setFullSpeedRender( bool val );

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
172
    private:
173
        void                    setState( State state );
174
        void                    adjustBegin();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
175

176
    protected:
177
        void                    computePtsDiff( qint64 pts );
178
        void                    commonUnlock();
179 180 181 182 183
        /**
         *  \warning    Must be called from a thread safe context.
         *              This thread safe context has to be set
         *              from the underlying ClipWorkflow implementation.
         */
184 185
        virtual quint32         getNbComputedBuffers() const = 0;
        virtual quint32         getMaxComputedBuffers() const = 0;
186 187 188 189 190
        /**
         *  \brief  Will empty the computed buffers stack.
         *          This has to be implemented in the underlying
         *          clipworkflow implementation.
         */
191 192
        virtual void            flushComputedBuffers() = 0;

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
193
    private:
194
        WaitCondition*          m_initWaitCond;
195
        WaitCondition*          m_pausingStateWaitCond;
196

197
    protected:
198
        LibVLCpp::MediaPlayer*  m_mediaPlayer;
199
        Clip*                   m_clip;
200 201 202
        QMutex*                 m_renderLock;
        QReadWriteLock*         m_stateLock;
        State                   m_state;
203 204
        qint64                  m_previousPts;
        qint64                  m_currentPts;
205 206 207 208
        /**
         *  \brief  The VLC media used to render
         */
        LibVLCpp::Media*        m_vlcMedia;
209 210 211 212 213
        /**
         *  \brief  This is used for basic synchronisation when
         *          the clipworkflow hasn't generate a frame yet,
         *          while the renderer asks for one.
         */
214 215 216 217
        QMutex*                 m_computedBuffersMutex;
        QMutex*                 m_availableBuffersMutex;
        qint64                  m_beginPausePts;
        qint64                  m_pauseDuration;
218
        bool                    m_fullSpeedRender;
219
        int                     debugType;
220

221
    private slots:
222
        void                    loadingComplete();
223
        void                    clipEndReached();
224 225
        void                    mediaPlayerPaused();
        void                    mediaPlayerUnpaused();
226
        void                    resyncClipWorkflow();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
227 228 229
};

#endif // CLIPWORKFLOW_H