Move files from gst-examples into the "subprojects/gst-examples/" subdir
[platform/upstream/gstreamer.git] / subprojects / gst-examples / playback / player / qt / extension / qgstplayer.h
1 /* GStreamer
2  *
3  * Copyright (C) 2015 Alexandre Moreno <alexmorenocano@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #ifndef QGSTPLAYER_H
22 #define QGSTPLAYER_H
23
24 #include <QObject>
25 #include <QUrl>
26 #include <QSize>
27 //#include <QtGui/qwindowdefs.h>
28 #include <QVariant>
29 #include <QList>
30 #include <QImage>
31 #include <gst/player/player.h>
32
33 namespace QGstPlayer {
34
35 class VideoRenderer;
36 class MediaInfo;
37 class StreamInfo;
38 class VideInfo;
39 class AudioInfo;
40 class SubtitleInfo;
41
42 class Player : public QObject
43 {
44     Q_OBJECT
45     Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
46     Q_PROPERTY(qint64 duration READ duration NOTIFY durationChanged)
47     Q_PROPERTY(qint64 position READ position NOTIFY positionUpdated)
48     Q_PROPERTY(qreal volume READ volume WRITE setVolume NOTIFY volumeChanged)
49     Q_PROPERTY(bool muted READ isMuted WRITE setMuted NOTIFY mutedChanged)
50     Q_PROPERTY(int buffering READ buffering NOTIFY bufferingChanged)
51     Q_PROPERTY(QSize resolution READ resolution WRITE setResolution NOTIFY resolutionChanged)
52     Q_PROPERTY(State state READ state NOTIFY stateChanged)
53     Q_PROPERTY(QObject *mediaInfo READ mediaInfo NOTIFY mediaInfoChanged)
54     Q_PROPERTY(bool videoAvailable READ isVideoAvailable NOTIFY videoAvailableChanged)
55     Q_PROPERTY(QVariant currentVideo READ currentVideo WRITE setCurrentVideo)
56     Q_PROPERTY(QVariant currentAudio READ currentAudio WRITE setCurrentAudio)
57     Q_PROPERTY(QVariant currentSubtitle READ currentSubtitle WRITE setCurrentSubtitle)
58     Q_PROPERTY(bool subtitleEnabled READ isSubtitleEnabled WRITE setSubtitleEnabled
59                NOTIFY subtitleEnabledChanged)
60     Q_PROPERTY(bool autoPlay READ autoPlay WRITE setAutoPlay)
61     Q_PROPERTY(QList<QUrl> playlist READ playlist WRITE setPlaylist)
62
63     Q_ENUMS(State)
64
65 public:
66     explicit Player(QObject *parent = 0, VideoRenderer *renderer = 0);
67     virtual ~Player();
68
69     typedef GstPlayerError Error;
70     enum State {
71         STOPPED = GST_PLAYER_STATE_STOPPED,
72         BUFFERING = GST_PLAYER_STATE_BUFFERING,
73         PAUSED = GST_PLAYER_STATE_PAUSED,
74         PLAYING = GST_PLAYER_STATE_PLAYING
75     };
76
77     QUrl source() const;
78     qint64 duration() const;
79     qint64 position() const;
80     qreal volume() const;
81     bool isMuted() const;
82     int buffering() const;
83     State state() const;
84     GstElement *pipeline() const;
85     QSize resolution() const;
86     void setResolution(QSize size);
87     bool isVideoAvailable() const;
88     MediaInfo *mediaInfo() const;
89     QVariant currentVideo() const;
90     QVariant currentAudio() const;
91     QVariant currentSubtitle() const;
92     bool isSubtitleEnabled() const;
93     bool autoPlay() const;
94     QList<QUrl> playlist() const;
95
96 signals:
97     void stateChanged(State new_state);
98     void bufferingChanged(int percent);
99     void endOfStream();
100     void positionUpdated(qint64 new_position);
101     void durationChanged(qint64 duration);
102     void resolutionChanged(QSize resolution);
103     void volumeChanged(qreal volume);
104     void mutedChanged(bool muted);
105     void mediaInfoChanged();
106     void sourceChanged(QUrl new_url);
107     void videoAvailableChanged(bool videoAvailable);
108     void subtitleEnabledChanged(bool enabled);
109
110 public slots:
111     void play();
112     void pause();
113     void stop();
114     void seek(qint64 position);
115     void setSource(QUrl const& url);
116     void setVolume(qreal val);
117     void setMuted(bool val);
118     void setPosition(qint64 pos);
119     void setCurrentVideo(QVariant track);
120     void setCurrentAudio(QVariant track);
121     void setCurrentSubtitle(QVariant track);
122     void setSubtitleEnabled(bool enabled);
123     void setPlaylist(const QList<QUrl> &playlist);
124     void next();
125     void previous();
126     void setAutoPlay(bool auto_play);
127
128 private:
129     Q_DISABLE_COPY(Player)
130     static void onStateChanged(Player *, GstPlayerState state);
131     static void onPositionUpdated(Player *, GstClockTime position);
132     static void onDurationChanged(Player *, GstClockTime duration);
133     static void onBufferingChanged(Player *, int percent);
134     static void onVideoDimensionsChanged(Player *, int w, int h);
135     static void onVolumeChanged(Player *);
136     static void onMuteChanged(Player *);
137     static void onMediaInfoUpdated(Player *, GstPlayerMediaInfo *media_info);
138     static void onEndOfStreamReached(Player *);
139
140     void setUri(QUrl url);
141
142     GstPlayer *player_;
143     State state_;
144     QSize videoDimensions_;
145     MediaInfo *mediaInfo_;
146     bool videoAvailable_;
147     bool subtitleEnabled_;
148     bool autoPlay_;
149     QList<QUrl> playlist_;
150     QList<QUrl>::iterator iter_;
151 };
152
153 class VideoRenderer
154 {
155 public:
156     GstPlayerVideoRenderer *renderer();
157     virtual GstElement *createVideoSink() = 0;
158 protected:
159     VideoRenderer();
160     virtual ~VideoRenderer();
161 private:
162     GstPlayerVideoRenderer *renderer_;
163 };
164
165 class MediaInfo : public QObject
166 {
167     Q_OBJECT
168     Q_PROPERTY(QString uri READ uri NOTIFY uriChanged)
169     Q_PROPERTY(bool seekable READ isSeekable NOTIFY seekableChanged)
170     Q_PROPERTY(QString title READ title NOTIFY titleChanged)
171     Q_PROPERTY(QList<QObject*> videoStreams READ videoStreams CONSTANT)
172     Q_PROPERTY(QList<QObject*> audioStreams READ audioStreams CONSTANT)
173     Q_PROPERTY(QList<QObject*> subtitleStreams READ subtitleStreams CONSTANT)
174     Q_PROPERTY(QImage sample READ sample NOTIFY sampleChanged)
175
176 public:
177     explicit MediaInfo(Player *player = 0);
178     QString uri() const;
179     QString title() const;
180     bool isSeekable() const;
181     const QList<QObject*> &videoStreams() const;
182     const QList<QObject*> &audioStreams() const;
183     const QList<QObject*> &subtitleStreams() const;
184     const QImage &sample();
185
186 signals:
187     void uriChanged();
188     void seekableChanged();
189     void titleChanged();
190     void sampleChanged();
191
192 public Q_SLOTS:
193     void update(GstPlayerMediaInfo *info);
194 private:
195     QString uri_;
196     QString title_;
197     bool isSeekable_;
198     QList<QObject*> videoStreams_;
199     QList<QObject*> audioStreams_;
200     QList<QObject*> subtitleStreams_;
201     QImage sample_;
202 };
203
204 class StreamInfo : public QObject
205 {
206     Q_OBJECT
207     Q_PROPERTY(int index READ index CONSTANT)
208 public:
209     int index() const;
210
211 protected:
212     StreamInfo(GstPlayerStreamInfo* info);
213 private:
214     GstPlayerStreamInfo *stream_;
215     int index_;
216 };
217
218 class VideoInfo : public StreamInfo
219 {
220     Q_OBJECT
221     Q_PROPERTY(QSize resolution READ resolution CONSTANT)
222 public:
223     VideoInfo(GstPlayerVideoInfo *info);
224     QSize resolution() const;
225
226 private:
227     GstPlayerVideoInfo *video_;
228     QSize resolution_;
229 };
230
231 class AudioInfo : public StreamInfo
232 {
233     Q_OBJECT
234     Q_PROPERTY(QString language READ language CONSTANT)
235     Q_PROPERTY(int channels READ channels CONSTANT)
236     Q_PROPERTY(int bitRate READ bitRate CONSTANT)
237     Q_PROPERTY(int sampleRate READ sampleRate CONSTANT)
238
239 public:
240     AudioInfo(GstPlayerAudioInfo *info);
241     QString const& language() const;
242     int channels() const;
243     int bitRate() const;
244     int sampleRate() const;
245
246 private:
247     GstPlayerAudioInfo *audio_;
248     QString language_;
249     int channels_;
250     int bitRate_;
251     int sampleRate_;
252 };
253
254 class SubtitleInfo : public StreamInfo
255 {
256     Q_OBJECT
257     Q_PROPERTY(QString language READ language CONSTANT)
258 public:
259     SubtitleInfo(GstPlayerSubtitleInfo *info);
260     QString const& language() const;
261
262 private:
263     GstPlayerSubtitleInfo *subtitle_;
264     QString language_;
265 };
266
267 }
268
269 Q_DECLARE_METATYPE(QGstPlayer::Player*)
270 Q_DECLARE_METATYPE(QGstPlayer::Player::State)
271 Q_DECLARE_METATYPE(QGstPlayer::MediaInfo*)
272
273 extern "C" {
274
275 typedef struct _GstPlayerQtVideoRenderer
276     GstPlayerQtVideoRenderer;
277
278 typedef struct _GstPlayerQtVideoRendererClass
279     GstPlayerQtVideoRendererClass;
280
281 #define GST_TYPE_PLAYER_QT_VIDEO_RENDERER             (gst_player_qt_video_renderer_get_type ())
282 #define GST_IS_PLAYER_QT_VIDEO_RENDERER(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_PLAYER_QT_VIDEO_RENDERER))
283 #define GST_IS_PLAYER_QT_VIDEO_RENDERER_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_PLAYER_QT_VIDEO_RENDERER))
284 #define GST_PLAYER_QT_VIDEO_RENDERER_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_PLAYER_QT_VIDEO_RENDERER, GstPlayerQtVideoRendererClass))
285 #define GST_PLAYER_QT_VIDEO_RENDERER(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_PLAYER_QT_VIDEO_RENDERER, GstPlayerQtVideoRenderer))
286 #define GST_PLAYER_QT_VIDEO_RENDERER_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_PLAYER_QT_VIDEO_RENDERER, GstPlayerQtVideoRendererClass))
287 #define GST_PLAYER_QT_VIDEO_RENDERER_CAST(obj)        ((GstPlayerQtVideoRenderer*)(obj))
288
289 GType gst_player_qt_video_renderer_get_type (void);
290
291 typedef struct _GstPlayerQtSignalDispatcher
292     GstPlayerQtSignalDispatcher;
293
294 typedef struct _GstPlayerQtSignalDispatcherClass
295     GstPlayerQtSignalDispatcherClass;
296
297 #define GST_TYPE_PLAYER_QT_SIGNAL_DISPATCHER             (gst_player_qt_signal_dispatcher_get_type ())
298 #define GST_IS_PLAYER_QT_SIGNAL_DISPATCHER(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_PLAYER_QT_SIGNAL_DISPATCHER))
299 #define GST_IS_PLAYER_QT_SIGNAL_DISPATCHER_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_PLAYER_QT_SIGNAL_DISPATCHER))
300 #define GST_PLAYER_QT_SIGNAL_DISPATCHER_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_PLAYER_QT_SIGNAL_DISPATCHER, GstPlayerQtSignalDispatcherClass))
301 #define GST_PLAYER_QT_SIGNAL_DISPATCHER(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_PLAYER_QT_SIGNAL_DISPATCHER, GstPlayerQtSignalDispatcher))
302 #define GST_PLAYER_QT_SIGNAL_DISPATCHER_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_PLAYER_QT_SIGNAL_DISPATCHER, GstPlayerQtSignalDispatcherClass))
303 #define GST_PLAYER_QT_SIGNAL_DISPATCHER_CAST(obj)        ((GstPlayerQtSignalDispatcher*)(obj))
304
305 GType gst_player_qt_video_renderer_get_type (void);
306
307 GstPlayerSignalDispatcher *
308 gst_player_qt_signal_dispatcher_new (gpointer player);
309
310 }
311
312 #endif // QGSTPLAYER_H