summaryrefslogtreecommitdiff
path: root/clutter-gst/clutter-gst-player.c
blob: cca31bc42784384bbf188cd05b2e5f7ddcb835ca (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
/*
 * Clutter-GStreamer.
 *
 * GStreamer integration library for Clutter.
 *
 * clutter-gst-player.c - Wrap some convenience functions around playbin
 *
 * Authored By Damien Lespiau    <damien.lespiau@intel.com>
 *             Lionel Landwerlin <lionel.g.landwerlin@linux.intel.com>
 *             Matthew Allum     <mallum@openedhand.com>
 *             Emmanuele Bassi   <ebassi@linux.intel.com>
 *             Andre Moreira Magalhaes <andre.magalhaes@collabora.co.uk>
 *
 * Copyright (C) 2006 OpenedHand
 * Copyright (C) 2009-2013 Intel Corporation
 * Copyright (C) 2012 Collabora Ltd. <http://www.collabora.co.uk/>
 *
 * 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 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
 * 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, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
 * SECTION:clutter-gst-player
 * @short_description: An interface for controlling playback of media data
 *
 * #ClutterGstPlayer is an interface for controlling playback of media
 *  sources.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "clutter-gst-enum-types.h"
#include "clutter-gst-marshal.h"
#include "clutter-gst-player.h"
#include "clutter-gst-private.h"

typedef ClutterGstPlayerIface ClutterGstPlayerInterface;

G_DEFINE_INTERFACE (ClutterGstPlayer, clutter_gst_player, G_TYPE_OBJECT)

enum
{
  NEW_FRAME,
  READY_SIGNAL,
  EOS_SIGNAL,
  SIZE_CHANGE,
  ERROR_SIGNAL, /* can't be called 'ERROR' otherwise it clashes with wingdi.h */

  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0, };

static void
clutter_gst_player_default_init (ClutterGstPlayerIface *iface)
{
  GParamSpec *pspec;

  /**
   * ClutterGstPlayer:playing:
   *
   * Whether the #ClutterGstPlayer actor is playing.
   */
  pspec = g_param_spec_boolean ("playing",
                                "Playing",
                                "Whether the player is playing",
                                FALSE,
                                CLUTTER_GST_PARAM_READWRITE |
                                G_PARAM_DEPRECATED);
  g_object_interface_install_property (iface, pspec);

  /**
   * ClutterGstPlayer:audio-volume:
   *
   * The volume of the audio, as a normalized value between
   * 0.0 and 1.0.
   */
  pspec = g_param_spec_double ("audio-volume",
                               "Audio Volume",
                               "The volume of the audio",
                               0.0, 1.0, 0.5,
                               CLUTTER_GST_PARAM_READWRITE |
                               G_PARAM_DEPRECATED);
  g_object_interface_install_property (iface, pspec);

  /**
   * ClutterGstPlayer:idle:
   *
   * Whether the #ClutterGstPlayer is in idle mode.
   *
   * Since: 1.4
   */
  pspec = g_param_spec_boolean ("idle",
                                "Idle",
                                "Idle state of the player's pipeline",
                                TRUE,
                                CLUTTER_GST_PARAM_READABLE);
  g_object_interface_install_property (iface, pspec);

  /* Signals */

  /**
   * ClutterGstPlayer::new-frame:
   * @player: the #ClutterGstPlayer instance that received the signal
   * @frame: the #ClutterGstFrame newly receive from the video sink
   *
   * The ::ready signal is emitted each time the gstreamer pipeline
   * becomes ready.
   */
  signals[NEW_FRAME] =
    g_signal_new ("new-frame",
                  CLUTTER_GST_TYPE_PLAYER,
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (ClutterGstPlayerIface, new_frame),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__BOXED,
                  G_TYPE_NONE, 1,
                  CLUTTER_GST_TYPE_FRAME);
  /**
   * ClutterGstPlayer::ready:
   * @player: the #ClutterGstPlayer instance that received the signal
   *
   * The ::ready signal is emitted each time the gstreamer pipeline
   * becomes ready.
   */
  signals[READY_SIGNAL] =
    g_signal_new ("ready",
                  CLUTTER_GST_TYPE_PLAYER,
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (ClutterGstPlayerIface, ready),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
  /**
   * ClutterGstPlayer::eos:
   * @player: the #ClutterGstPlayer instance that received the signal
   *
   * The ::eos signal is emitted each time the media stream ends.
   */
  signals[EOS_SIGNAL] =
    g_signal_new ("eos",
                  CLUTTER_GST_TYPE_PLAYER,
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (ClutterGstPlayerIface, eos),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
  /**
   * ClutterGstPlayer::error:
   * @player: the #ClutterGstPlayer instance that received the signal
   * @error: the #GError
   *
   * The ::error signal is emitted each time an error occurred.
   */
  signals[ERROR_SIGNAL] =
    g_signal_new ("error",
                  CLUTTER_GST_TYPE_PLAYER,
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (ClutterGstPlayerIface, error),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__BOXED,
                  G_TYPE_NONE, 1,
                  G_TYPE_ERROR);

  /**
   * ClutterGstPlayer::size-change:
   * @player: the #ClutterGstPlayer instance that received the signal
   * @width: new width of the frames
   * @height: new height of the frames
   *
   * The ::size-change signal is emitted each time the gstreamer pipeline
   * becomes ready.
   */
  signals[SIZE_CHANGE] =
    g_signal_new ("size-change",
                  CLUTTER_GST_TYPE_PLAYER,
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (ClutterGstPlayerIface, size_change),
                  NULL, NULL,
                  _clutter_gst_marshal_VOID__INT_INT,
                  G_TYPE_NONE, 2,
                  G_TYPE_INT, G_TYPE_INT);
}

/* ClutterGstIface */

/**
 * clutter_gst_player_get_frame:
 * @self: a #ClutterGstPlayer
 *
 * Retrieves the #ClutterGstFrame of the last frame produced by @self.
 *
 * Return value: (transfer none): the #ClutterGstFrame of the last frame.
 *
 * Since: 3.0
 */
ClutterGstFrame *
clutter_gst_player_get_frame (ClutterGstPlayer *self)
{
  ClutterGstPlayerIface *iface;

  g_return_val_if_fail (CLUTTER_GST_IS_PLAYER (self), NULL);

  iface = CLUTTER_GST_PLAYER_GET_INTERFACE (self);

  return iface->get_frame (self);
}

/**
 * clutter_gst_player_get_pipeline:
 * @self: a #ClutterGstPlayer
 *
 * Retrieves the #GstPipeline used by the @self, for direct use with
 * GStreamer API.
 *
 * Return value: (transfer none): the #GstPipeline element used by the player
 *
 * Since: 3.0
 */
GstElement *
clutter_gst_player_get_pipeline (ClutterGstPlayer *self)
{
  ClutterGstPlayerIface *iface;

  g_return_val_if_fail (CLUTTER_GST_IS_PLAYER (self), NULL);

  iface = CLUTTER_GST_PLAYER_GET_INTERFACE (self);

  return iface->get_pipeline (self);
}

/**
 * clutter_gst_player_get_video_sink:
 * @self: a #ClutterGstPlayer
 *
 * Retrieves the #ClutterGstVideoSink used by the @self.
 *
 * Return value: (transfer none): the #ClutterGstVideoSink element used by the player
 *
 * Since: 3.0
 */
ClutterGstVideoSink *
clutter_gst_player_get_video_sink (ClutterGstPlayer *self)
{
  ClutterGstPlayerIface *iface;

  g_return_val_if_fail (CLUTTER_GST_IS_PLAYER (self), NULL);

  iface = CLUTTER_GST_PLAYER_GET_INTERFACE (self);

  return iface->get_video_sink (self);

}

/**
 * clutter_gst_player_get_playing:
 * @self: A #ClutterGstPlayer object
 *
 * Retrieves the playing status of @self.
 *
 * Return value: %TRUE if playing, %FALSE if stopped.
 *
 * Since: 3.0
 */
gboolean
clutter_gst_player_get_playing (ClutterGstPlayer *self)
{
  ClutterGstPlayerIface *iface;

  g_return_val_if_fail (CLUTTER_GST_IS_PLAYER (self), TRUE);

  iface = CLUTTER_GST_PLAYER_GET_INTERFACE (self);

  return iface->get_playing (self);
}

/**
 * clutter_gst_player_set_playing:
 * @self: a #ClutterGstPlayer
 * @playing: %TRUE to start playing
 *
 * Starts or stops playing of @self.
 *
 * The implementation might be asynchronous, so the way to know whether
 * the actual playing state of the @self is to use the #GObject::notify
 * signal on the #ClutterGstPlayer:playing property and then retrieve the
 * current state with clutter_gst_player_get_playing(). ClutterGstVideoActor
 * in clutter-gst is an example of such an asynchronous implementation.
 *
 * Since: 3.0
 */
void
clutter_gst_player_set_playing (ClutterGstPlayer *self,
                                gboolean          playing)
{
  ClutterGstPlayerIface *iface;

  g_return_if_fail (CLUTTER_GST_IS_PLAYER (self));

  iface = CLUTTER_GST_PLAYER_GET_INTERFACE (self);

  iface->set_playing (self, playing);
}

/**
 * clutter_gst_player_get_audio_volume:
 * @self: a #ClutterGstPlayer
 *
 * Retrieves the playback volume of @self.
 *
 * Return value: The playback volume between 0.0 and 1.0
 *
 * Since: 3.0
 */
gdouble
clutter_gst_player_get_audio_volume (ClutterGstPlayer *self)
{
  ClutterGstPlayerIface *iface;

  g_return_val_if_fail (CLUTTER_GST_IS_PLAYER (self), TRUE);

  iface = CLUTTER_GST_PLAYER_GET_INTERFACE (self);

  return iface->get_audio_volume (self);
}

/**
 * clutter_gst_player_set_audio_volume:
 * @self: a #ClutterGstPlayer
 * @volume: the volume as a double between 0.0 and 1.0
 *
 * Sets the playback volume of @self to @volume.
 *
 * Since: 3.0
 */
void
clutter_gst_player_set_audio_volume (ClutterGstPlayer *self,
                                     gdouble           volume)
{
  ClutterGstPlayerIface *iface;

  g_return_if_fail (CLUTTER_GST_IS_PLAYER (self));

  iface = CLUTTER_GST_PLAYER_GET_INTERFACE (self);

  iface->set_audio_volume (self, volume);
}

/**
 * clutter_gst_player_get_idle:
 * @self: a #ClutterGstPlayer
 *
 * Get the idle state of the pipeline.
 *
 * Return value: TRUE if the pipline is in idle mode, FALSE otherwise.
 *
 * Since: 3.0
 */
gboolean
clutter_gst_player_get_idle (ClutterGstPlayer *self)
{
  ClutterGstPlayerIface *iface;

  g_return_val_if_fail (CLUTTER_GST_IS_PLAYER (self), TRUE);

  iface = CLUTTER_GST_PLAYER_GET_INTERFACE (self);

  return iface->get_idle (self);
}

/* Internal functions */

void
clutter_gst_player_update_frame (ClutterGstPlayer *player,
                                 ClutterGstFrame **frame,
                                 ClutterGstFrame  *new_frame)
{
  ClutterGstFrame *old_frame = *frame;

  *frame = g_boxed_copy (CLUTTER_GST_TYPE_FRAME, new_frame);

  if (old_frame == NULL ||
      new_frame->resolution.width != old_frame->resolution.width ||
      new_frame->resolution.height != old_frame->resolution.height ||
      new_frame->resolution.par_n != old_frame->resolution.par_n ||
      new_frame->resolution.par_d != old_frame->resolution.par_d)
    {
      g_signal_emit (player, signals[SIZE_CHANGE], 0,
                     new_frame->resolution.width,
                     new_frame->resolution.height);
    }

  if (old_frame)
    g_boxed_free (CLUTTER_GST_TYPE_FRAME, old_frame);

  g_signal_emit (player, signals[NEW_FRAME], 0, new_frame);
}

void
clutter_gst_frame_update_pixel_aspect_ratio (ClutterGstFrame  *frame,
                                             ClutterGstVideoSink *sink)
{
  GValue value = G_VALUE_INIT;

  g_value_init (&value, GST_TYPE_FRACTION);
  g_object_get_property (G_OBJECT (sink),
                         "pixel-aspect-ratio",
                         &value);

  frame->resolution.par_n = gst_value_get_fraction_numerator (&value);
  frame->resolution.par_d = gst_value_get_fraction_denominator (&value);

  g_value_unset (&value);
}