ClipWorkflow.h 8.3 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"
35
#include "mdate.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
36 37 38 39 40 41

class   ClipWorkflow : public QObject
{
    Q_OBJECT

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

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

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

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

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

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

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

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

117 118 119 120 121
        /**
            \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.
        */
122
        Clip*                   getClip();
123 124 125 126
        /**
            \brief  Stop this workflow.
        */
        void                    stop();
127
        void                    setTime( qint64 time );
128

129 130 131 132 133 134 135 136
        /**
         *  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 );
137

138 139 140 141 142 143 144
        /**
         *  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();

145
        void                    waitForCompleteInit();
146

147 148 149
        virtual void*           getLockCallback() = 0;
        virtual void*           getUnlockCallback() = 0;

150 151
        LibVLCpp::MediaPlayer*  getMediaPlayer();

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
152
    private:
153
        void                    setState( State state );
154
        void                    adjustBegin();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
155

156
    protected:
157
        void                    computePtsDiff( qint64 pts );
158
        void                    commonUnlock();
159 160
        /// \warning    Must be called from a thread safe context.
        ///             This thread safe context has to be set from the underlying ClipWorkflow implementation.
161
        virtual uint32_t        getComputedBuffers() const = 0;
162
        virtual uint32_t        getMaxComputedBuffers() const = 0;
163

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
164
    private:
165
        LibVLCpp::MediaPlayer*  m_mediaPlayer;
166

167 168
        State                   m_requiredState;
        QMutex*                 m_requiredStateLock;
169

170
        WaitCondition*          m_initWaitCond;
171
        WaitCondition*          m_pausingStateWaitCond;
172

173 174 175 176
        /**
         *  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)
         */
177
        bool                    m_rendering;
178 179 180 181 182
        /**
         *  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...
         */
183 184
        bool                    m_initFlag;

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

208 209 210

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

215
    private slots:
216
        void                    loadingComplete();
217
        void                    clipEndReached();
218 219
        void                    mediaPlayerPaused();
        void                    mediaPlayerUnpaused();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
220 221 222
};

#endif // CLIPWORKFLOW_H