ClipWorkflow.h 7.83 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
/*****************************************************************************
 * 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>
34
#include "WaitCondition.hpp"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
35
#include <QObject>
36
#include <QQueue>
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
37 38 39 40 41 42 43 44 45

#include "Clip.h"
#include "VLCMediaPlayer.h"

class   ClipWorkflow : public QObject
{
    Q_OBJECT

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

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

63
        /**
64 65 66
         *  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.
67
         */
68
        unsigned char*          getOutput();
69
        void                    initialize();
70 71 72 73 74
        /**
         *  Return true ONLY if the state is equal to Ready.
         *  If the state is Rendering, EndReached or anything else, this will
         *  return false.
         */
75
        bool                    isReady() const;
76 77 78 79
        /**
         *  Return true ONLY if the state is equal to EndReached.
         *  In any other cases, this will return false.
         */
80
        bool                    isEndReached() const;
81 82 83 84 85 86 87 88

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

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

94
        bool                    isSleeping() const;
95

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

111 112 113 114 115
        /**
            \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.
        */
116
        Clip*                   getClip();
117 118 119 120
        /**
            \brief  Stop this workflow.
        */
        void                    stop();
121
        void                    pause();
122
        void                    setPosition( float pos );
123

124 125 126 127 128 129 130 131
        /**
         *  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 );
132

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
        /**
         *  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();

150
        void                    unpause();
151

152
        void                    waitForCompleteInit();
153
        void                    waitForCompleteRender( bool dontCheckRenderStarted = false );
154
        QMutex*                 getSleepMutex();
155

156 157
        LibVLCpp::MediaPlayer*  getMediaPlayer();

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
158
    private:
159 160
        static void             lock( ClipWorkflow* clipWorkflow, void** pp_ret, int size );
        static void             unlock( ClipWorkflow* clipWorkflow, void* buffer, int width, int height, int bpp, int size );
161
        void                    setVmem();
162
        void                    setState( State state );
163
        void                    checkSynchronisation( State newState );
164 165 166
        /**
         *  Don't ever call this method from anywhere else than the unlock() method
         */
167
        void                    checkStateChange();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
168 169 170

    private:
        Clip*                   m_clip;
171 172 173 174 175 176

        /**
         *  \brief  The VLC media used to render
         */
        LibVLCpp::Media*        m_vlcMedia;

177 178
        unsigned char*          m_buffer;
        QMutex*                 m_renderLock;
179 180

        LibVLCpp::MediaPlayer*  m_mediaPlayer;
181

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
182
        QMutex*                 m_condMutex;
183
        QWaitCondition*         m_waitCond;
184

185 186
        State                   m_state;
        QReadWriteLock*         m_stateLock;
187 188
        State                   m_requiredState;
        QMutex*                 m_requiredStateLock;
189

190
        WaitCondition*          m_initWaitCond;
191 192
        WaitCondition*          m_renderWaitCond;
        WaitCondition*          m_pausingStateWaitCond;
193

194 195 196 197 198 199 200 201 202 203 204 205
        /**
         *  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)
         */
        bool                                    m_rendering;
        /**
         *  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...
         */
        bool                                    m_initFlag;

206
    private slots:
207
        void                    pauseAfterPlaybackStarted();
208
        void                    initializedMediaPlayer();
209
        void                    setPositionAfterPlayback();
210
        void                    pausedMediaPlayer();
211
        void                    unpausedMediaPlayer();
212 213 214

    public slots:
        void                    clipEndReached();
215 216 217

    signals:
        void                    renderComplete( ClipWorkflow* );
218 219
        void                    paused();
        void                    unpaused();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
220 221 222
};

#endif // CLIPWORKFLOW_H