summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/phonon/ds9/mediaobject.h
blob: fe52604aebf7943940f70b922c2113cca65aa6fe (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*  This file is part of the KDE project.

Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).

This library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2.1 or 3 of the License.

This library 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef PHONON_MEDIAOBJECT_H
#define PHONON_MEDIAOBJECT_H

#include <phonon/mediaobjectinterface.h>
#include <phonon/addoninterface.h>

#include <QtCore/QHash>
#include <QtCore/QObject>
#include <QtCore/QQueue>
#include <QtCore/QBasicTimer>
#include <QtCore/QMutex>
#include <QtCore/QThread>

#include "backendnode.h"
#include "mediagraph.h"

QT_BEGIN_NAMESPACE

namespace Phonon
{
    class MediaSource;

    namespace DS9
    {
        class VideoWidget;
        class AudioOutput;

        class QWinWaitCondition
        {
        public:
            QWinWaitCondition() : m_handle(::CreateEvent(0,0,0,0))
            {
            }

            ~QWinWaitCondition()
            {
                ::CloseHandle(m_handle);
            }

            void reset()
            {
                //will block
                ::ResetEvent(m_handle);
            }

            void set()
            {
                //will unblock
                ::SetEvent(m_handle);
            }

            operator HANDLE()
            {
                return m_handle;
            }

            operator HEVENT()
            {
                return reinterpret_cast<HEVENT>(m_handle);
            }


        private:
            HANDLE m_handle;
        };

        class WorkerThread : public QThread
        {
            Q_OBJECT
        public:
            WorkerThread();
            ~WorkerThread();

            virtual void run();

            //wants to know as soon as the state is set
            void addStateChangeRequest(Graph graph, OAFilterState, QList<Filter> = QList<Filter>());

            quint16 addSeekRequest(Graph graph, qint64 time);
            quint16 addUrlToRender(const QString &url);
            quint16 addFilterToRender(const Filter &filter);

            void replaceGraphForEventManagement(Graph newGraph, Graph oldGraph);

    		void abortCurrentRender(qint16 renderId);

            //tells the thread to stop processing
            void signalStop();

        Q_SIGNALS:
            void asyncRenderFinished(quint16, HRESULT, Graph);
            void asyncSeekingFinished(quint16, qint64);
            void stateReady(Graph, Phonon::State);
            void eventReady(Graph, long eventCode, long param1);

        private:

            enum Task
            {
                Render,
                Seek,
                ChangeState,
                ReplaceGraph //just updates recalls WaitForMultipleObject
            };

            struct Work
            {
                Task task;
                quint16 id;
                Graph graph;
                Graph oldGraph;
                Filter filter;
                QString url;
                union
                {
                    qint64 time;
                    OAFilterState state;
                };
                QList<Filter> decoders; //for the state change requests
            };
            void handleTask();

            Graph m_currentRender;
			qint16 m_currentRenderId;
            QQueue<Work> m_queue;
            bool m_finished;
            quint16 m_currentWorkId;
            QWinWaitCondition m_waitCondition;
            QMutex m_mutex;

            //this is for WaitForMultipleObjects
            struct
            {
                Graph graph;
                HANDLE handle;
            } m_graphHandle[FILTER_COUNT];
        };


        class MediaObject : public BackendNode, public Phonon::MediaObjectInterface
#ifndef QT_NO_PHONON_ABSTRACTMEDIASTREAM
            , public Phonon::AddonInterface
#endif //QT_NO_PHONON_ABSTRACTMEDIASTREAM
        {
            friend class Stream;
            Q_OBJECT
                Q_INTERFACES(Phonon::MediaObjectInterface 
#ifndef QT_NO_PHONON_ABSTRACTMEDIASTREAM
                    Phonon::AddonInterface
#endif //QT_NO_PHONON_ABSTRACTMEDIASTREAM
                    )
        public:
            MediaObject(QObject *parent);
            ~MediaObject();
            Phonon::State state() const;
            bool hasVideo() const;
            bool isSeekable() const;
            qint64 currentTime() const;
            qint32 tickInterval() const;

            void setTickInterval(qint32 newTickInterval);
            void play();
            void pause();
            void stop();
            void ensureStopped();
            void seek(qint64 time);

            QString errorString() const;
            Phonon::ErrorType errorType() const;

#ifndef QT_NO_PHONON_ABSTRACTMEDIASTREAM
            bool hasInterface(Interface) const;
            QVariant interfaceCall(Interface iface, int command, const QList<QVariant> &params);
#endif //QT_NO_PHONON_ABSTRACTMEDIASTREAM

            qint64 totalTime() const;
            qint32 prefinishMark() const;
            void setPrefinishMark(qint32 newPrefinishMark);

            qint32 transitionTime() const;
            void setTransitionTime(qint32);

            qint64 remainingTime() const;

            MediaSource source() const;
            void setSource(const MediaSource &source);
            void setNextSource(const MediaSource &source);


            //COM error management
            bool catchComError(HRESULT hr);

            void grabNode(BackendNode *node);
            bool connectNodes(BackendNode *source, BackendNode *sink);
            bool disconnectNodes(BackendNode *source, BackendNode *sink);

            void switchFilters(int index, Filter oldFilter, Filter newFilter);

            WorkerThread *workerThread();
            void loadingFinished(MediaGraph *mg);
            void seekingFinished(MediaGraph *mg);
            MediaGraph *currentGraph() const;

            //this is used by the backend only
            Phonon::State transactionState;

         private Q_SLOTS:
            void switchToNextSource();
            void slotStateReady(Graph, Phonon::State);
            void handleEvents(Graph, long eventCode, long param1);
            void finishLoading(quint16 workId, HRESULT hr, Graph);
            void finishSeeking(quint16 workId, qint64 time);

         Q_SIGNALS:
            void stateChanged(Phonon::State newstate, Phonon::State oldstate);
            void tick(qint64 time);
            void metaDataChanged(QMultiMap<QString, QString>);
            void seekableChanged(bool);
            void hasVideoChanged(bool);
            void bufferStatus(int);

            // AddonInterface:
            void titleChanged(int);
            void availableTitlesChanged(int);
            void chapterChanged(int);
            void availableChaptersChanged(int);
            void angleChanged(int);
            void availableAnglesChanged(int);

            void finished();
            void prefinishMarkReached(qint32);
            void aboutToFinish();
            void totalTimeChanged(qint64 length) const;
            void currentSourceChanged(const MediaSource &);

        protected:
            void setState(Phonon::State);
            void timerEvent(QTimerEvent *e);

        private:
#ifndef QT_NO_PHONON_VIDEO
            void updateVideoGeometry();
#endif // QT_NO_PHONON_VIDEO
            void handleComplete(IGraphBuilder *graph);
            MediaGraph *nextGraph() const;

            void updateTargetTick();
            void updateStopPosition();

            mutable QString m_errorString;
            mutable Phonon::ErrorType m_errorType;

            Phonon::State m_state;
            Phonon::State m_nextState;
            qint32 m_transitionTime;

            qint32 m_prefinishMark;

            QBasicTimer m_tickTimer;
            qint32 m_tickInterval;

            //the graph(s)
            MediaGraph* m_graphs[FILTER_COUNT];

            //...the videowidgets in the graph
            QList<VideoWidget*> m_videoWidgets;
            QList<AudioOutput*> m_audioOutputs;

            bool m_buffering:1;
            bool m_oldHasVideo:1;
            bool m_prefinishMarkSent:1;
            bool m_aboutToFinishSent:1;
            bool m_nextSourceReadyToStart:1;

            //for TitleInterface (and commands)
#ifndef QT_NO_PHONON_MEDIACONTROLLER
            bool m_autoplayTitles:1;
            QList<qint64> m_titles;
            int m_currentTitle;
            int _iface_availableTitles() const;
            int _iface_currentTitle() const;
            void _iface_setCurrentTitle(int title, bool bseek = true);
            void setTitles(const QList<qint64> &titles);
            qint64 titleAbsolutePosition(int title) const;
#endif //QT_NO_PHONON_MEDIACONTROLLER
            qint64 m_targetTick;

            WorkerThread m_thread;
        };
    }
}

QT_END_NAMESPACE

#endif // PHONON_MEDIAOBJECT_H