ClipWorkflow.h 9.15 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
#include <QObject>

class   QMutex;
31
32
class   QReadWriteLock;
class   QWaitCondition;
33
34
35
36
37

class   Clip;
class   WaitCondition;
class   LightVideoFrame;

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

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

    public:
49
50
        enum        State
        {
51
            None = -1,
52
            /// \brief  Used when the clipworkflow hasn't been started yet
53
            Stopped,            //0
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
54
55
56
57
58
            /**
             *  \brief  Used when initializing is in progress (until the mediaplayer
             *          enters the playing state.
             */
            Initializing,       //1
59
            /// \brief  Used when the clipworkflow is launched and active
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
60
            Rendering,          //2
61
            /// \brief  Used when stopping
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
62
            Stopping,           //3
63
64
            /// \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
65
            EndReached,         //4
66
67
68
            // 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
69
            UnpauseRequired,    //5
70
71
            /// \brief  This state will be used when a pause
            ///         has been required
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
72
            PauseRequired,      //6
73
74
            /// \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
75
            Paused,             //7
76
77
            /// \brief  This state means a clip is mutted and must not be restarted
            Muted,
78
79
        };

80
81
82
83
84
85
86
87
88
89
90
        /**
         *  \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,
        };

91
        ClipWorkflow( Clip* clip );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
92
93
        virtual ~ClipWorkflow();

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

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

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

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

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

131
132
133
134
135
        /**
            \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.
        */
136
137
138
139
140
        inline Clip*            getClip()
        {
            return m_clip;
        }

141
142
143
144
        /**
            \brief  Stop this workflow.
        */
        void                    stop();
145
146
147
148
        /**
         *  \brief  Set the rendering position
         *  \param  time    The position in millisecond
         */
149
        void                    setTime( qint64 time );
150

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

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

167
        void                    waitForCompleteInit();
168

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
169
170
        virtual void*           getLockCallback() const = 0;
        virtual void*           getUnlockCallback() const = 0;
171

172
173
        LibVLCpp::MediaPlayer*  getMediaPlayer();

174
175
176
177
178
        /**
         *  \sa MainWorkflow::setFullSpeedRender();
         */
        void                    setFullSpeedRender( bool val );

179
180
181
        void                    mute();
        void                    unmute();

182
183
184
185
186
187
188
189
190
        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();

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
191
    private:
192
        void                    setState( State state );
193
        void                    adjustBegin();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
194

195
    protected:
196
        void                    computePtsDiff( qint64 pts );
197
        void                    commonUnlock();
198
199
200
201
202
        /**
         *  \warning    Must be called from a thread safe context.
         *              This thread safe context has to be set
         *              from the underlying ClipWorkflow implementation.
         */
203
204
        virtual quint32         getNbComputedBuffers() const = 0;
        virtual quint32         getMaxComputedBuffers() const = 0;
205
206
207
208
209
        /**
         *  \brief  Will empty the computed buffers stack.
         *          This has to be implemented in the underlying
         *          clipworkflow implementation.
         */
210
211
        virtual void            flushComputedBuffers() = 0;

212
213
214
215
216
        /**
         *  \brief  Release the preallocated buffers
         */
        virtual void            releasePrealocated() = 0;

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
217
    private:
218
219
        WaitCondition           *m_initWaitCond;
        WaitCondition           *m_pausingStateWaitCond;
220
221
222
223
224
225
226
        /**
         *  \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;
227

228
    protected:
229
        LibVLCpp::MediaPlayer*  m_mediaPlayer;
230
        Clip*                   m_clip;
231
232
233
        QMutex*                 m_renderLock;
        QReadWriteLock*         m_stateLock;
        State                   m_state;
234
235
        qint64                  m_previousPts;
        qint64                  m_currentPts;
236
237
238
239
        /**
         *  \brief  The VLC media used to render
         */
        LibVLCpp::Media*        m_vlcMedia;
240
241
242
243
244
        /**
         *  \brief  This is used for basic synchronisation when
         *          the clipworkflow hasn't generate a frame yet,
         *          while the renderer asks for one.
         */
245
        QWaitCondition          *m_renderWaitCond;
246
247
        qint64                  m_beginPausePts;
        qint64                  m_pauseDuration;
248
        bool                    m_fullSpeedRender;
249

250
    private slots:
251
        void                    loadingComplete();
252
        void                    clipEndReached();
253
254
        void                    mediaPlayerPaused();
        void                    mediaPlayerUnpaused();
255
        void                    resyncClipWorkflow();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
256
257
258
};

#endif // CLIPWORKFLOW_H