summaryrefslogtreecommitdiff
path: root/playback/player/qt/qgstplayer.cpp
blob: c2fb3fbab7105507be9c9d8393872aaee36a6be8 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/* 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.
 */

#include "qgstplayer.h"
#include <QDebug>
#include <QSize>

#include <QMetaObject>

class QGstPlayerRegisterMetaTypes
{
public:
    QGstPlayerRegisterMetaTypes()
    {
        qRegisterMetaType<QGstPlayer::State>("State");
    }

} _register;

QGstPlayer::MediaInfo::MediaInfo(GstPlayerMediaInfo *media_info)
    : mediaInfo_(media_info)
{

}

QString QGstPlayer::MediaInfo::title() const
{
    QString title = QString::fromLocal8Bit
        (gst_player_media_info_get_title(mediaInfo_));

    // if media has no title, return the file name
    if (title.isEmpty()) {
        QUrl url(gst_player_media_info_get_uri(mediaInfo_));
        title = url.fileName();
    }

    return title;
}

bool QGstPlayer::MediaInfo::isSeekable() const
{
    return gst_player_media_info_is_seekable(mediaInfo_);
}

bool QGstPlayer::isVideoAvailable() const
{
    GstPlayerVideoInfo *video_info;

    video_info = gst_player_get_current_video_track (player_);
    if (video_info) {
      g_object_unref (video_info);
      return true;
    }

    return false;
}

QQmlPropertyMap *QGstPlayer::mediaInfo() const
{
    return mediaInfoMap_;
}

QGstPlayer::QGstPlayer(QObject *parent, QGstPlayer::VideoRenderer *renderer)
    : QObject(parent)
    , player_()
    , state_(STOPPED)
    , videoDimensions_(QSize())
    , mediaInfoMap_()
    , videoAvailable_(false)
{

    player_ = gst_player_new_full(renderer ? renderer->renderer() : 0, 0);

    g_object_connect(player_,
        "swapped-signal::state-changed", G_CALLBACK (QGstPlayer::onStateChanged), this,
        "swapped-signal::position-updated", G_CALLBACK (QGstPlayer::onPositionUpdated), this,
        "swapped-signal::duration-changed", G_CALLBACK (QGstPlayer::onDurationChanged), this,
        "swapped-signal::buffering", G_CALLBACK (QGstPlayer::onBufferingChanged), this,
        "swapped-signal::video-dimensions-changed", G_CALLBACK (QGstPlayer::onVideoDimensionsChanged), this,
        "swapped-signal::volume-changed", G_CALLBACK (QGstPlayer::onVolumeChanged), this,
        "swapped-signal::mute-changed", G_CALLBACK (QGstPlayer::onMuteChanged), this,
        "swapped-signal::media-info-updated", G_CALLBACK (QGstPlayer::onMediaInfoUpdated), this, NULL);

    mediaInfoMap_ = new QQmlPropertyMap(this);
}

void
QGstPlayer::onStateChanged(QGstPlayer * player, GstPlayerState state)
{
    player->state_ =  static_cast<QGstPlayer::State>(state);

    emit player->stateChanged(player->state_);
}

void
QGstPlayer::onPositionUpdated(QGstPlayer * player, GstClockTime position)
{
    emit player->positionChanged(position);
}

void
QGstPlayer::onDurationChanged(QGstPlayer * player, GstClockTime duration)
{
    emit player->durationChanged(duration);
}

void
QGstPlayer::onBufferingChanged(QGstPlayer * player, int percent)
{
    emit player->bufferingChanged(percent);
}

void
QGstPlayer::onVideoDimensionsChanged(QGstPlayer * player, int w, int h)
{
    QSize res(w,h);

    player->setResolution(res);

    emit player->resolutionChanged(res);
}

void
QGstPlayer::onVolumeChanged(QGstPlayer *player)
{
    qreal new_val;

    new_val = gst_player_get_volume (player->player_);

    emit player->volumeChanged(new_val);
}

void
QGstPlayer::onMuteChanged(QGstPlayer *player)
{
    bool new_val;

    new_val = gst_player_get_mute (player->player_);

    emit player->mutedChanged(new_val);
}

void
QGstPlayer::onMediaInfoUpdated(QGstPlayer *player, GstPlayerMediaInfo *media_info)
{
    MediaInfo mediaInfo(media_info);

    player->mediaInfoMap_->insert(QLatin1String("title"),
                                  QVariant(mediaInfo.title()));
    player->mediaInfoMap_->insert(QLatin1String("isSeekable"),
                                  QVariant(mediaInfo.isSeekable()));

    bool val = player->isVideoAvailable();

    if (player->videoAvailable_ != val) {
        player->videoAvailable_ = val;
        emit player->videoAvailableChanged(val);
    }

    emit player->mediaInfoChanged();
}

QUrl QGstPlayer::source() const
{
    Q_ASSERT(player_ != 0);
    QString url = QString::fromLocal8Bit(gst_player_get_uri(player_));

    return QUrl(url);
}

qint64 QGstPlayer::duration() const
{
    Q_ASSERT(player_ != 0);

    return gst_player_get_duration(player_);
}

qint64 QGstPlayer::position() const
{
    Q_ASSERT(player_ != 0);

    return gst_player_get_position(player_);
}

qreal QGstPlayer::volume() const
{
    Q_ASSERT(player_ != 0);

    return gst_player_get_volume(player_);
}

bool QGstPlayer::isMuted() const
{
    Q_ASSERT(player_ != 0);

    return gst_player_get_mute(player_);
}

int QGstPlayer::buffering() const
{
    return 0;
}

QSize QGstPlayer::resolution() const
{
    return videoDimensions_;
}

void QGstPlayer::setResolution(QSize size)
{
    videoDimensions_ = size;
}

QGstPlayer::State QGstPlayer::state() const
{
    return state_;
}

GstElement *QGstPlayer::pipeline() const
{
    Q_ASSERT(player_ != 0);

    return gst_player_get_pipeline(player_);
}

void QGstPlayer::play()
{
    Q_ASSERT(player_ != 0);

    gst_player_play(player_);
}

void QGstPlayer::pause()
{
    Q_ASSERT(player_ != 0);

    gst_player_pause(player_);
}

void QGstPlayer::stop()
{
    Q_ASSERT(player_ != 0);

    gst_player_stop(player_);
}

void QGstPlayer::seek(qint64 position)
{
    Q_ASSERT(player_ != 0);

    gst_player_seek(player_, position);
}

void QGstPlayer::setSource(QUrl const& url)
{
    Q_ASSERT(player_ != 0);
    QByteArray uri = url.toString().toLocal8Bit();

    gst_player_set_uri(player_, uri.data());

    emit sourceChanged(url);
}

void QGstPlayer::setVolume(qreal val)
{
    Q_ASSERT(player_ != 0);

    gst_player_set_volume(player_, val);
}

void QGstPlayer::setMuted(bool val)
{
    Q_ASSERT(player_ != 0);

    gst_player_set_mute(player_, val);
}

void QGstPlayer::setPosition(qint64 pos)
{
    Q_ASSERT(player_ != 0);

    gst_player_seek(player_, pos);
}

GstPlayerVideoRenderer *QGstPlayer::VideoRenderer::renderer()
{
    return renderer_;
}

QGstPlayer::VideoRenderer::VideoRenderer()
{
    renderer_ = static_cast<GstPlayerVideoRenderer*>
        (g_object_new (GST_TYPE_PLAYER_QT_VIDEO_RENDERER, "renderer", this, NULL));
}

QGstPlayer::VideoRenderer::~VideoRenderer()
{
    if (renderer_) gst_object_unref(renderer_);
}

struct _GstPlayerQtVideoRenderer
{
  GObject parent;

  gpointer renderer;
};

struct _GstPlayerQtVideoRendererClass
{
  GObjectClass parent_class;
};

static void
gst_player_qt_video_renderer_interface_init
    (GstPlayerVideoRendererInterface * iface);

G_DEFINE_TYPE_WITH_CODE (GstPlayerQtVideoRenderer,
    gst_player_qt_video_renderer, G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (GST_TYPE_PLAYER_VIDEO_RENDERER,
        gst_player_qt_video_renderer_interface_init))

enum
{
  QT_VIDEO_RENDERER_PROP_0,
  QT_VIDEO_RENDERER_PROP_RENDERER,
  QT_VIDEO_RENDERER_PROP_LAST
};

static GParamSpec * qt_video_renderer_param_specs
    [QT_VIDEO_RENDERER_PROP_LAST] = { NULL, };

static void
gst_player_qt_video_renderer_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec)
{
  GstPlayerQtVideoRenderer *self = GST_PLAYER_QT_VIDEO_RENDERER (object);

  switch (prop_id) {
    case QT_VIDEO_RENDERER_PROP_RENDERER:
      qDebug() << "setting renderer";
      self->renderer = g_value_get_pointer (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_player_qt_video_renderer_get_property (GObject * object,
    guint prop_id, GValue * value, GParamSpec * pspec)
{
  GstPlayerQtVideoRenderer *self = GST_PLAYER_QT_VIDEO_RENDERER (object);

  switch (prop_id) {
    case QT_VIDEO_RENDERER_PROP_RENDERER:
      g_value_set_pointer (value, self->renderer);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_player_qt_video_renderer_finalize (GObject * object)
{
  GstPlayerQtVideoRenderer *self = GST_PLAYER_QT_VIDEO_RENDERER (object);

  G_OBJECT_CLASS
      (gst_player_qt_video_renderer_parent_class)->finalize(object);
}

static void
gst_player_qt_video_renderer_class_init
    (GstPlayerQtVideoRendererClass * klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->set_property =
      gst_player_qt_video_renderer_set_property;
  gobject_class->get_property =
      gst_player_qt_video_renderer_get_property;
  gobject_class->finalize = gst_player_qt_video_renderer_finalize;

  qt_video_renderer_param_specs
      [QT_VIDEO_RENDERER_PROP_RENDERER] =
      g_param_spec_pointer ("renderer", "Qt Renderer", "",
      static_cast<GParamFlags>(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_properties (gobject_class,
      QT_VIDEO_RENDERER_PROP_LAST,
      qt_video_renderer_param_specs);
}

static void
gst_player_qt_video_renderer_init (GstPlayerQtVideoRenderer * self)
{

}

static GstElement *
gst_player_qt_video_renderer_create_video_sink
    (GstPlayerVideoRenderer * iface, GstPlayer *player)
{
    GstPlayerQtVideoRenderer *self = GST_PLAYER_QT_VIDEO_RENDERER (iface);

    g_assert(self->renderer != NULL);

    return static_cast<QGstPlayer::VideoRenderer*>(self->renderer)->createVideoSink();
}

static void
gst_player_qt_video_renderer_interface_init
    (GstPlayerVideoRendererInterface * iface)
{
  iface->create_video_sink = gst_player_qt_video_renderer_create_video_sink;
}