ClipWorkflow.h 9.64 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"
27 28

#include "EffectUser.h"
29
#include "ClipHelper.h"
30
#include "Types.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
31

32
#include <QObject>
33
#include <QUuid>
34
#include <QXmlStreamWriter>
35

36 37 38 39
class   Clip;
class   Effect;
class   WaitCondition;

40
class   QMutex;
41 42
class   QReadWriteLock;
class   QWaitCondition;
43

44 45 46 47
namespace Workflow
{
    class   Frame;
}
48

49 50 51 52 53 54
namespace LibVLCpp
{
    class   MediaPlayer;
    class   Media;
}

55
class   ClipWorkflow : public EffectUser
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
56 57 58 59
{
    Q_OBJECT

    public:
60 61
        enum        State
        {
62
            None = -1,
63
            /// \brief  Used when the clipworkflow hasn't been started yet
64
            Stopped,            //0
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
65 66 67 68 69
            /**
             *  \brief  Used when initializing is in progress (until the mediaplayer
             *          enters the playing state.
             */
            Initializing,       //1
70
            /// \brief  Used when the clipworkflow is launched and active
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
71
            Rendering,          //2
72
            /// \brief  Used when stopping
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
73
            Stopping,           //3
74 75
            /// \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
76
            EndReached,         //4
77 78 79
            // 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
80
            UnpauseRequired,    //5
81 82
            /// \brief  This state will be used when a pause
            ///         has been required
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
83
            PauseRequired,      //6
84 85
            /// \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
86
            Paused,             //7
87
            /// \brief  This state means a clip is mutted and must not be restarted
88 89 90
            Muted,              //8
            /// \brief  An error was encountered, this ClipWorkflow must not be used anymore.
            Error,              //9
91 92
        };

93 94 95 96 97 98 99 100 101 102 103
        /**
         *  \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,
        };

104
        ClipWorkflow( ClipHelper* clip );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
105 106
        virtual ~ClipWorkflow();

107
        /**
108 109 110
         *  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.
111
         */
112
        virtual Workflow::OutputBuffer      *getOutput( ClipWorkflow::GetMode mode ) = 0;
113
        void                    postGetOutput();
114
        virtual void            initVlcOutput() = 0;
115
        void                    initialize();
116

117
        /**
118 119 120 121
         *  \return             true if the ClipWorkflow is able to, and should render
         *                      a frame.
         *
         *  This is true when the state is not stopped, stopping, nor rendering.
122
         */
123
        bool                    shouldRender() const;
124 125 126

        /**
         *  Returns the current workflow state.
127 128 129
         *  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.
130 131 132
         */
        State                   getState() const;

133
        /**
134
            \brief              Returns the ClipHelper this workflow instance is based
135
                                uppon, so that you can query information on it.
136
            \return             A pointer to a ClipHelper instance.
137
        */
138
        inline ClipHelper*      getClipHelper()
139
        {
140 141 142 143 144
            return m_clipHelper;
        }
        inline Clip*            clip()
        {
            return m_clipHelper->clip();
145 146
        }

147 148 149 150
        /**
            \brief  Stop this workflow.
        */
        void                    stop();
151 152 153
        /**
         *  \brief  Set the rendering position
         *  \param  time    The position in millisecond
154
         *  \param  frame   The new current frame.
155
         */
156
        virtual void            setTime( qint64 time, qint64 frame );
157

158 159 160 161 162 163 164 165
        /**
         *  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 );
166

167 168 169 170 171 172 173
        /**
         *  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();

174
        bool                    waitForCompleteInit();
175

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
176 177
        virtual void*           getLockCallback() const = 0;
        virtual void*           getUnlockCallback() const = 0;
178

179 180
        LibVLCpp::MediaPlayer*  getMediaPlayer();

181 182 183 184 185
        /**
         *  \sa MainWorkflow::setFullSpeedRender();
         */
        void                    setFullSpeedRender( bool val );

186 187 188
        void                    mute();
        void                    unmute();

189 190 191 192 193 194 195 196 197
        void                    requireResync();
        /**
         *  \return true if a resync is required.
         *
         *  If a resync is required, true will be returned, and the flag will be
         *  set back to false
         */
        bool                    isResyncRequired();

198 199 200 201 202 203 204
        /**
         *  \brief              Stop the renderer part of the ClipWorkflow
         *
         *  IE. just stop the VLC thread, and do not flush anything.
         */
        void                    stopRenderer();

205 206
        void                    save( QXmlStreamWriter& project ) const;

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
207
    private:
208
        void                    setState( State state );
209
        void                    adjustBegin();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
210

211
    protected:
212
        void                    computePtsDiff( qint64 pts );
213
        void                    commonUnlock();
214 215 216 217 218
        /**
         *  \warning    Must be called from a thread safe context.
         *              This thread safe context has to be set
         *              from the underlying ClipWorkflow implementation.
         */
219 220
        virtual quint32         getNbComputedBuffers() const = 0;
        virtual quint32         getMaxComputedBuffers() const = 0;
221 222 223 224 225
        /**
         *  \brief  Will empty the computed buffers stack.
         *          This has to be implemented in the underlying
         *          clipworkflow implementation.
         */
226 227
        virtual void            flushComputedBuffers() = 0;

228 229 230 231 232
        /**
         *  \brief  Release the preallocated buffers
         */
        virtual void            releasePrealocated() = 0;

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
233
    private:
234
        WaitCondition           *m_initWaitCond;
235 236 237 238 239 240 241
        /**
         *  \brief              Used by the trackworkflow to query a clipworkflow resync.
         *
         *  Basically, this will be used when a clip is moved, and therefore has to be
         *  updated.
         */
        QAtomicInt              m_resyncRequired;
242

243
    protected:
244
        LibVLCpp::MediaPlayer*  m_mediaPlayer;
245
        ClipHelper*             m_clipHelper;
246 247 248
        QMutex*                 m_renderLock;
        QReadWriteLock*         m_stateLock;
        State                   m_state;
249 250
        qint64                  m_previousPts;
        qint64                  m_currentPts;
251 252 253 254
        /**
         *  \brief  The VLC media used to render
         */
        LibVLCpp::Media*        m_vlcMedia;
255 256 257 258 259
        /**
         *  \brief  This is used for basic synchronisation when
         *          the clipworkflow hasn't generate a frame yet,
         *          while the renderer asks for one.
         */
260
        QWaitCondition          *m_renderWaitCond;
261 262
        qint64                  m_beginPausePts;
        qint64                  m_pauseDuration;
263
        bool                    m_fullSpeedRender;
264

265
    private slots:
266
        void                    loadingComplete();
267
        void                    clipEndReached();
268 269
        void                    mediaPlayerPaused();
        void                    mediaPlayerUnpaused();
270
        void                    resyncClipWorkflow();
271 272 273 274
        void                    errorEncountered();

    signals:
        void                    error();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
275 276 277
};

#endif // CLIPWORKFLOW_H