ClipWorkflow.h 6.76 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 35 36 37 38 39 40 41 42 43 44
/*****************************************************************************
 * 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 <QWaitCondition>
#include <QObject>

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

class   ClipWorkflow : public QObject
{
    Q_OBJECT

    public:
45 46
        enum        State
        {
47
            None = -1,
48 49 50 51
            Stopped,
            Initializing,
            Ready,
            Rendering,
52 53
            Sleeping,
            Stopping,
54
            EndReached,
55
        };
56
       int                     debugId;
57

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

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

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

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

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

107 108 109 110 111
        /**
            \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.
        */
112
        Clip*                   getClip();
113 114 115 116 117
        /**
            \brief  Stop this workflow.
        */
        void                    stop();
        void                    setPosition( float pos );
118

119 120 121 122 123 124 125 126
        /**
         *  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 );
127

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        /**
         *  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();

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
145 146 147
    private:
        static void             lock( ClipWorkflow* clipWorkflow, void** pp_ret );
        static void             unlock( ClipWorkflow* clipWorkflow );
148
        void                    setVmem();
149
        void                    setState( State state );
150 151 152
        /**
         *  Don't ever call this method from anywhere else than the unlock() method
         */
153
        void                    checkStateChange();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
154 155 156

    private:
        Clip*                   m_clip;
157 158 159 160 161 162

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

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
163
        unsigned char*          m_buffer;
164
        //unsigned char*          m_backBuffer;
165 166 167 168 169 170 171 172
        /**
         *  This allow the render procedure to know in which buffer it should render.
         *  If true, then the render occurs in the back buffer, which means the
         *  returned buffer much be the "front" buffer.
         *  In other term :
         *  - When m_usingBackBuffer == false, lock() will return m_buffer, and getOutput() m_backBuffer
         *  - When m_usingBackBuffer == true, lock() will return m_backBuffer, and getOutput() m_buffer
         */
173 174
        //bool                    m_usingBackBuffer;
        //QReadWriteLock*         m_backBufferLock;
175 176

        LibVLCpp::MediaPlayer*  m_mediaPlayer;
177

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
178 179
        QMutex*                 m_condMutex;
        QWaitCondition*         m_waitCond;
180

181 182
        State                   m_state;
        QReadWriteLock*         m_stateLock;
183 184
        State                   m_requiredState;
        QMutex*                 m_requiredStateLock;
185

186

187
    private slots:
188 189
        void                    pauseAfterPlaybackStarted();
        void                    pausedMediaPlayer();
190
        void                    setPositionAfterPlayback();
191 192 193

    public slots:
        void                    clipEndReached();
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
194 195 196
};

#endif // CLIPWORKFLOW_H