ClipWorkflow.h 8.1 KB
Newer Older
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
1 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 29 30 31 32 33 34
/*****************************************************************************
 * 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

//TODO: THIS HAS TO GO REALLY REALLY REALLY SOON !!!!
//FIXME:
//UGLY:
#define VIDEOWIDTH 640
#define VIDEOHEIGHT 480

#include <QReadWriteLock>
#include <QMutex>
#include <QObject>
35
#include <QQueue>
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
36 37

#include "Clip.h"
38
#include "WaitCondition.hpp"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
39
#include "VLCMediaPlayer.h"
40
#include "LightVideoFrame.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
41 42 43 44 45 46

class   ClipWorkflow : public QObject
{
    Q_OBJECT

    public:
47 48
        enum        State
        {
49
            None = -1,
50 51 52 53 54 55 56
            Stopped,        //0
            Initializing,   //1
            Ready,          //2
            Rendering,      //3
            Sleeping,       //4
            Pausing,        //5
            Paused,         //6
57 58
            Stopping,       //7
            EndReached,     //8
59 60
        };

61
        ClipWorkflow( Clip* clip );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
62 63
        virtual ~ClipWorkflow();

64
        /**
65 66 67
         *  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.
68
         */
69 70
        virtual void*           getOutput() = 0;
        virtual void            initVlcOutput() = 0;
71
        void                    initialize( bool preloading = false );
72

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

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

        /**
92
         *  Return true ONLY if the state is equal to Rendering.
93 94 95 96
         *  In any other cases, this will return false.
         */
        bool                    isRendering() const;

97
        bool                    isSleeping() const;
98

99 100
        /**
         *  Returns the current workflow state.
101 102 103
         *  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.
104 105 106 107 108 109 110 111
         */
        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.
         */
112
        void                    startRender( bool startInPausedMode );
113

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

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

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
        /**
         *  This method will wake the renderer thread for one iteration.
         */
        void                    wake();

        /**
         *  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();

        /**
         *  Put back the ClipWorkflow in its initial state.
         */
        void                    reinitialize();

153
        void                    unpause();
154

155
        void                    waitForCompleteInit();
156
        void                    waitForCompleteRender( bool dontCheckRenderStarted = false );
157
        QMutex*                 getSleepMutex();
158

159 160 161
        virtual void*           getLockCallback() = 0;
        virtual void*           getUnlockCallback() = 0;

162 163
        LibVLCpp::MediaPlayer*  getMediaPlayer();

164 165
        void                    setFullSpeedRender( bool value );

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
166
    private:
167
        void                    setState( State state );
168
        void                    checkSynchronisation( State newState );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
169 170 171

    private:
        Clip*                   m_clip;
172

173
        LibVLCpp::MediaPlayer*  m_mediaPlayer;
174

175 176
        State                   m_requiredState;
        QMutex*                 m_requiredStateLock;
177

178
        WaitCondition*          m_initWaitCond;
179
        WaitCondition*          m_pausingStateWaitCond;
180

181 182 183 184
        /**
         *  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)
         */
185
        bool                    m_rendering;
186 187 188 189 190
        /**
         *  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...
         */
191 192
        bool                    m_initFlag;

193 194 195 196 197 198 199
    protected:
        QMutex*                 m_renderLock;
        QReadWriteLock*         m_stateLock;
        QMutex*                 m_condMutex;
        State                   m_state;
        WaitCondition*          m_renderWaitCond;
        QWaitCondition*         m_waitCond;
200
        bool                    m_fullSpeedRender;
201 202 203 204 205 206 207 208 209 210
        /**
         *  \brief  The VLC media used to render
         */
        LibVLCpp::Media*        m_vlcMedia;

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

212
    private slots:
213 214 215
        /**
         *  \brief  This slot is used when preloading, to pause the mediaplayer once fully loaded.
         */
216
        void                    pauseAfterPlaybackStarted();
217 218 219
        /**
         *  \brief  When preloading, this slot is used to mark that the media player has been paused again.
         */
220
        void                    initializedMediaPlayer();
221
        void                    loadingComplete();
222
        void                    pausedMediaPlayer();
223
        void                    unpausedMediaPlayer();
224 225 226

    public slots:
        void                    clipEndReached();
227 228 229

    signals:
        void                    renderComplete( ClipWorkflow* );
230 231
        void                    paused();
        void                    unpaused();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
232 233 234
};

#endif // CLIPWORKFLOW_H