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

class   ClipWorkflow : public QObject
{
    Q_OBJECT

    public:
41 42
        enum        State
        {
43
            None = -1,
44 45 46 47 48 49 50
            Stopped,        //0
            Initializing,   //1
            Ready,          //2
            Rendering,      //3
            Sleeping,       //4
            Pausing,        //5
            Paused,         //6
51 52
            Stopping,       //7
            EndReached,     //8
53 54
        };

55 56 57 58 59 60 61 62 63 64 65
        /**
         *  \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,
        };

66
        ClipWorkflow( Clip* clip );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
67 68
        virtual ~ClipWorkflow();

69
        /**
70 71 72
         *  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.
73
         */
74
        virtual void*           getOutput( ClipWorkflow::GetMode mode ) = 0;
75
        virtual void            initVlcOutput() = 0;
76
        void                    initialize( bool preloading = false );
77

78 79 80 81 82
        /**
         *  Return true ONLY if the state is equal to Ready.
         *  If the state is Rendering, EndReached or anything else, this will
         *  return false.
         */
83
        bool                    isReady() const;
84 85 86 87
        /**
         *  Return true ONLY if the state is equal to EndReached.
         *  In any other cases, this will return false.
         */
88
        bool                    isEndReached() const;
89 90 91 92 93 94 95 96

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

        /**
97
         *  Return true ONLY if the state is equal to Rendering.
98 99 100 101
         *  In any other cases, this will return false.
         */
        bool                    isRendering() const;

102
        bool                    isSleeping() const;
103

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

        /**
         *  This method start the effective render, ie calling the play() method
         *  on the media player.
         *  If the media player isn't ready, this method waits.
         */
117
        void                    startRender( bool startInPausedMode );
118

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

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

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

148
        void                    unpause();
149

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                    checkSynchronisation( State newState );
160
        void                    adjustBegin();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
161

162
    protected:
163
        void                    computePtsDiff( qint64 pts );
164 165
        void                    commonUnlock();

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

169 170
        State                   m_requiredState;
        QMutex*                 m_requiredStateLock;
171

172
        WaitCondition*          m_initWaitCond;
173
        WaitCondition*          m_pausingStateWaitCond;
174

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

187
    protected:
188
        Clip*                   m_clip;
189 190 191
        QMutex*                 m_renderLock;
        QReadWriteLock*         m_stateLock;
        State                   m_state;
192 193
        qint64                  m_previousPts;
        qint64                  m_currentPts;
194 195 196 197 198 199 200
        /**
         *  \brief  The VLC media used to render
         */
        LibVLCpp::Media*        m_vlcMedia;

    protected:
        /**
201
         *  \warning    Don't ever call this method from anywhere else than the unlock() method
202 203
         */
        void                    checkStateChange();
204

205
    private slots:
206 207 208
        /**
         *  \brief  This slot is used when preloading, to pause the mediaplayer once fully loaded.
         */
209
        void                    pauseAfterPlaybackStarted();
210 211 212
        /**
         *  \brief  When preloading, this slot is used to mark that the media player has been paused again.
         */
213
        void                    initializedMediaPlayer();
214
        void                    loadingComplete();
215
        void                    pausedMediaPlayer();
216
        void                    unpausedMediaPlayer();
217 218 219

    public slots:
        void                    clipEndReached();
220 221

    signals:
222 223
        void                    paused();
        void                    unpaused();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
224 225 226
};

#endif // CLIPWORKFLOW_H