summaryrefslogtreecommitdiff
path: root/playback/player/qt/qgstplayer.h
blob: c41f66c3bb25bd6b6779aa3305813d2376941a85 (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
/* GStreamer
 *
 * Copyright (C) 2015 Alexandre Moreno <alexmorenocano@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifndef QGSTPLAYER_H
#define QGSTPLAYER_H

#include <QObject>
#include <QUrl>
#include <QSize>
//#include <QtGui/qwindowdefs.h>
#include <QVariant>
#include <QList>
#include <gst/player/player.h>

namespace QGstPlayer {

class VideoRenderer;
class MediaInfo;
class StreamInfo;
class VideInfo;
class AudioInfo;
class SubtitleInfo;

class Player : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
    Q_PROPERTY(qint64 duration READ duration NOTIFY durationChanged)
    Q_PROPERTY(qint64 position READ position NOTIFY positionUpdated)
    Q_PROPERTY(quint32 positionUpdateInterval READ positionUpdateInterval
               WRITE setPositionUpdateInterval)
    Q_PROPERTY(qreal volume READ volume WRITE setVolume NOTIFY volumeChanged)
    Q_PROPERTY(bool muted READ isMuted WRITE setMuted NOTIFY mutedChanged)
    Q_PROPERTY(int buffering READ buffering NOTIFY bufferingChanged)
    Q_PROPERTY(QSize resolution READ resolution WRITE setResolution NOTIFY resolutionChanged)
    Q_PROPERTY(State state READ state NOTIFY stateChanged)
    Q_PROPERTY(QObject *mediaInfo READ mediaInfo NOTIFY mediaInfoChanged)
    Q_PROPERTY(bool videoAvailable READ isVideoAvailable NOTIFY videoAvailableChanged)
    Q_PROPERTY(QVariant currentVideo READ currentVideo WRITE setCurrentVideo)
    Q_PROPERTY(QVariant currentAudio READ currentAudio WRITE setCurrentAudio)
    Q_PROPERTY(QVariant currentSubtitle READ currentSubtitle WRITE setCurrentSubtitle)
    Q_PROPERTY(bool subtitleEnabled READ isSubtitleEnabled WRITE setSubtitleEnabled
               NOTIFY subtitleEnabledChanged)
    Q_PROPERTY(bool autoPlay READ autoPlay WRITE setAutoPlay)
    Q_PROPERTY(QList<QUrl> playlist READ playlist WRITE setPlaylist)

    Q_ENUMS(State)

public:
    explicit Player(QObject *parent = 0, VideoRenderer *renderer = 0);
    virtual ~Player();

    typedef GstPlayerError Error;
    enum State {
        STOPPED = GST_PLAYER_STATE_STOPPED,
        BUFFERING = GST_PLAYER_STATE_BUFFERING,
        PAUSED = GST_PLAYER_STATE_PAUSED,
        PLAYING = GST_PLAYER_STATE_PLAYING
    };

    QUrl source() const;
    qint64 duration() const;
    qint64 position() const;
    qreal volume() const;
    bool isMuted() const;
    int buffering() const;
    State state() const;
    GstElement *pipeline() const;
    QSize resolution() const;
    void setResolution(QSize size);
    bool isVideoAvailable() const;
    MediaInfo *mediaInfo() const;
    QVariant currentVideo() const;
    QVariant currentAudio() const;
    QVariant currentSubtitle() const;
    bool isSubtitleEnabled() const;
    quint32 positionUpdateInterval() const;
    bool autoPlay() const;
    QList<QUrl> playlist() const;

signals:
    void stateChanged(State new_state);
    void bufferingChanged(int percent);
    void endOfStream();
    void positionUpdated(qint64 new_position);
    void durationChanged(qint64 duration);
    void resolutionChanged(QSize resolution);
    void volumeChanged(qreal volume);
    void mutedChanged(bool muted);
    void mediaInfoChanged();
    void sourceChanged(QUrl new_url);
    void videoAvailableChanged(bool videoAvailable);
    void subtitleEnabledChanged(bool enabled);

public slots:
    void play();
    void pause();
    void stop();
    void seek(qint64 position);
    void setSource(QUrl const& url);
    void setVolume(qreal val);
    void setMuted(bool val);
    void setPosition(qint64 pos);
    void setCurrentVideo(QVariant track);
    void setCurrentAudio(QVariant track);
    void setCurrentSubtitle(QVariant track);
    void setSubtitleEnabled(bool enabled);
    void setPositionUpdateInterval(quint32 interval);
    void setPlaylist(const QList<QUrl> &playlist);
    void next();
    void previous();
    void setAutoPlay(bool auto_play);

private:
    Q_DISABLE_COPY(Player)
    static void onStateChanged(Player *, GstPlayerState state);
    static void onPositionUpdated(Player *, GstClockTime position);
    static void onDurationChanged(Player *, GstClockTime duration);
    static void onBufferingChanged(Player *, int percent);
    static void onVideoDimensionsChanged(Player *, int w, int h);
    static void onVolumeChanged(Player *);
    static void onMuteChanged(Player *);
    static void onMediaInfoUpdated(Player *, GstPlayerMediaInfo *media_info);
    static void onEndOfStreamReached(Player *);

    void setUri(QUrl url);

    GstPlayer *player_;
    State state_;
    QSize videoDimensions_;
    MediaInfo *mediaInfo_;
    bool videoAvailable_;
    bool subtitleEnabled_;
    bool autoPlay_;
    QList<QUrl> playlist_;
    QList<QUrl>::iterator iter_;
};

class VideoRenderer
{
public:
    GstPlayerVideoRenderer *renderer();
    virtual GstElement *createVideoSink() = 0;
protected:
    VideoRenderer();
    virtual ~VideoRenderer();
private:
    GstPlayerVideoRenderer *renderer_;
};

class MediaInfo : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString uri READ uri NOTIFY uriChanged)
    Q_PROPERTY(bool seekable READ isSeekable NOTIFY seekableChanged)
    Q_PROPERTY(QString title READ title NOTIFY titleChanged)
    Q_PROPERTY(QList<QObject*> videoStreams READ videoStreams CONSTANT)
    Q_PROPERTY(QList<QObject*> audioStreams READ audioStreams CONSTANT)
    Q_PROPERTY(QList<QObject*> subtitleStreams READ subtitleStreams CONSTANT)

public:
    explicit MediaInfo(Player *player = 0);
    QString uri() const;
    QString title() const;
    bool isSeekable() const;
    const QList<QObject*> &videoStreams() const;
    const QList<QObject*> &audioStreams() const;
    const QList<QObject*> &subtitleStreams() const;

signals:
    void uriChanged();
    void seekableChanged();
    void titleChanged();

public Q_SLOTS:
    void update(GstPlayerMediaInfo *info);
private:
    QString uri_;
    QString title_;
    bool isSeekable_;
    QList<QObject*> videoStreams_;
    QList<QObject*> audioStreams_;
    QList<QObject*> subtitleStreams_;
};

class StreamInfo : public QObject
{
    Q_OBJECT
    Q_PROPERTY(int index READ index CONSTANT)
public:
    int index() const;

protected:
    StreamInfo(GstPlayerStreamInfo* info);
private:
    GstPlayerStreamInfo *stream_;
    int index_;
};

class VideoInfo : public StreamInfo
{
    Q_OBJECT
    Q_PROPERTY(QSize resolution READ resolution CONSTANT)
public:
    VideoInfo(GstPlayerVideoInfo *info);
    QSize resolution() const;

private:
    GstPlayerVideoInfo *video_;
    QSize resolution_;
};

class AudioInfo : public StreamInfo
{
    Q_OBJECT
    Q_PROPERTY(QString language READ language CONSTANT)
    Q_PROPERTY(int channels READ channels CONSTANT)
    Q_PROPERTY(int bitRate READ bitRate CONSTANT)
    Q_PROPERTY(int sampleRate READ sampleRate CONSTANT)

public:
    AudioInfo(GstPlayerAudioInfo *info);
    QString const& language() const;
    int channels() const;
    int bitRate() const;
    int sampleRate() const;

private:
    GstPlayerAudioInfo *audio_;
    QString language_;
    int channels_;
    int bitRate_;
    int sampleRate_;
};

class SubtitleInfo : public StreamInfo
{
    Q_OBJECT
    Q_PROPERTY(QString language READ language CONSTANT)
public:
    SubtitleInfo(GstPlayerSubtitleInfo *info);
    QString const& language() const;

private:
    GstPlayerSubtitleInfo *subtitle_;
    QString language_;
};

}

Q_DECLARE_METATYPE(QGstPlayer::Player*)
Q_DECLARE_METATYPE(QGstPlayer::Player::State)

extern "C" {

typedef struct _GstPlayerQtVideoRenderer
    GstPlayerQtVideoRenderer;

typedef struct _GstPlayerQtVideoRendererClass
    GstPlayerQtVideoRendererClass;

#define GST_TYPE_PLAYER_QT_VIDEO_RENDERER             (gst_player_qt_video_renderer_get_type ())
#define GST_IS_PLAYER_QT_VIDEO_RENDERER(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_PLAYER_QT_VIDEO_RENDERER))
#define GST_IS_PLAYER_QT_VIDEO_RENDERER_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_PLAYER_QT_VIDEO_RENDERER))
#define GST_PLAYER_QT_VIDEO_RENDERER_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_PLAYER_QT_VIDEO_RENDERER, GstPlayerQtVideoRendererClass))
#define GST_PLAYER_QT_VIDEO_RENDERER(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_PLAYER_QT_VIDEO_RENDERER, GstPlayerQtVideoRenderer))
#define GST_PLAYER_QT_VIDEO_RENDERER_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_PLAYER_QT_VIDEO_RENDERER, GstPlayerQtVideoRendererClass))
#define GST_PLAYER_QT_VIDEO_RENDERER_CAST(obj)        ((GstPlayerQtVideoRenderer*)(obj))

GType gst_player_qt_video_renderer_get_type (void);

typedef struct _GstPlayerQtSignalDispatcher
    GstPlayerQtSignalDispatcher;

typedef struct _GstPlayerQtSignalDispatcherClass
    GstPlayerQtSignalDispatcherClass;

#define GST_TYPE_PLAYER_QT_SIGNAL_DISPATCHER             (gst_player_qt_signal_dispatcher_get_type ())
#define GST_IS_PLAYER_QT_SIGNAL_DISPATCHER(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_PLAYER_QT_SIGNAL_DISPATCHER))
#define GST_IS_PLAYER_QT_SIGNAL_DISPATCHER_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_PLAYER_QT_SIGNAL_DISPATCHER))
#define GST_PLAYER_QT_SIGNAL_DISPATCHER_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_PLAYER_QT_SIGNAL_DISPATCHER, GstPlayerQtSignalDispatcherClass))
#define GST_PLAYER_QT_SIGNAL_DISPATCHER(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_PLAYER_QT_SIGNAL_DISPATCHER, GstPlayerQtSignalDispatcher))
#define GST_PLAYER_QT_SIGNAL_DISPATCHER_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_PLAYER_QT_SIGNAL_DISPATCHER, GstPlayerQtSignalDispatcherClass))
#define GST_PLAYER_QT_SIGNAL_DISPATCHER_CAST(obj)        ((GstPlayerQtSignalDispatcher*)(obj))

GType gst_player_qt_video_renderer_get_type (void);

GstPlayerSignalDispatcher *
gst_player_qt_signal_dispatcher_new (gpointer player);

}

#endif // QGSTPLAYER_H