ClipWorkflow.h 7.81 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
 * 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 "VLCMediaPlayer.h"
27
#include "mdate.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
28

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

class   QReadWriteLock;
class   QMutex;

class   Clip;
class   WaitCondition;
class   LightVideoFrame;

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
38 39 40 41 42
class   ClipWorkflow : public QObject
{
    Q_OBJECT

    public:
43 44
        enum        State
        {
45
            None = -1,
46
            /// \brief  Used when the clipworkflow hasn't been started yet
47
            Stopped,            //0
48
            /// \brief  Used when the clipworkflow is launched and active
49
            Rendering,          //1
50
            /// \brief  Used when stopping
51
            Stopping,           //2
52 53
            /// \brief  Used when end is reached, IE no more frame has to be rendered, but the trackworkflow
            ///         may eventually ask for some.
54
            EndReached,         //3
55 56 57
            // Here starts internal states :
            /// \brief  This state will be used when an unpause
            ///         has been required
58
            UnpauseRequired,    //4
59 60
            /// \brief  This state will be used when a pause
            ///         has been required
61
            PauseRequired,      //5
62 63
            /// \brief  This state will be used when the media player is paused,
            ///         because of a sufficient number of computed buffers
64
            Paused,             //6
65 66
        };

67 68 69 70 71 72 73 74 75 76 77
        /**
         *  \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,
        };

78
        ClipWorkflow( Clip* clip );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
79 80
        virtual ~ClipWorkflow();

81
        /**
82 83 84
         *  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.
85
         */
86
        virtual void*           getOutput( ClipWorkflow::GetMode mode ) = 0;
87
        bool                    preGetOutput();
88
        void                    postGetOutput();
89
        virtual void            initVlcOutput() = 0;
90
        void                    initialize();
91

92 93 94 95
        /**
         *  Return true ONLY if the state is equal to EndReached.
         *  In any other cases, this will return false.
         */
96
        bool                    isEndReached() const;
97 98 99 100 101 102 103 104

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

        /**
105
         *  Return true ONLY if the state is equal to Rendering.
106 107 108 109 110 111
         *  In any other cases, this will return false.
         */
        bool                    isRendering() const;

        /**
         *  Returns the current workflow state.
112 113 114
         *  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.
115 116 117
         */
        State                   getState() const;

118 119 120 121 122
        /**
            \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.
        */
123
        Clip*                   getClip();
124 125 126 127
        /**
            \brief  Stop this workflow.
        */
        void                    stop();
128 129 130 131
        /**
         *  \brief  Set the rendering position
         *  \param  time    The position in millisecond
         */
132
        void                    setTime( qint64 time );
133

134 135 136 137 138 139 140 141
        /**
         *  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 );
142

143 144 145 146 147 148 149
        /**
         *  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();

150
        void                    waitForCompleteInit();
151

152 153 154
        virtual void*           getLockCallback() = 0;
        virtual void*           getUnlockCallback() = 0;

155 156
        LibVLCpp::MediaPlayer*  getMediaPlayer();

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
157
    private:
158
        void                    setState( State state );
159
        void                    adjustBegin();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
160

161
    protected:
162
        void                    computePtsDiff( qint64 pts );
163
        void                    commonUnlock();
164 165 166 167 168 169
        /**
         *  \warning    Must be called from a thread safe context.
         *              This thread safe context has to be set
         *              from the underlying ClipWorkflow implementation.
         */
        virtual uint32_t        getNbComputedBuffers() const = 0;
170
        virtual uint32_t        getMaxComputedBuffers() const = 0;
171 172 173 174 175 176
        /**
         *  \brief  Will empty the computed buffers stack.
         *          This has to be implemented in the underlying
         *          clipworkflow implementation.
         */
        virtual void            flushComputedBuffers() = 0;
177

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
178
    private:
179
        LibVLCpp::MediaPlayer*  m_mediaPlayer;
180

181
        WaitCondition*          m_initWaitCond;
182
        WaitCondition*          m_pausingStateWaitCond;
183

184
    protected:
185
        Clip*                   m_clip;
186 187 188
        QMutex*                 m_renderLock;
        QReadWriteLock*         m_stateLock;
        State                   m_state;
189 190
        qint64                  m_previousPts;
        qint64                  m_currentPts;
191 192 193 194
        /**
         *  \brief  The VLC media used to render
         */
        LibVLCpp::Media*        m_vlcMedia;
195 196 197 198 199
        /**
         *  \brief  This is used for basic synchronisation when
         *          the clipworkflow hasn't generate a frame yet,
         *          while the renderer asks for one.
         */
200 201 202 203
        QMutex*                 m_computedBuffersMutex;
        QMutex*                 m_availableBuffersMutex;
        qint64                  m_beginPausePts;
        qint64                  m_pauseDuration;
204
        int                     debugType;
205

206
    private slots:
207
        void                    loadingComplete();
208
        void                    clipEndReached();
209 210
        void                    mediaPlayerPaused();
        void                    mediaPlayerUnpaused();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
211 212 213
};

#endif // CLIPWORKFLOW_H