• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  *
3  * Copyright (C) 2019-2020 Stephan Hesse <stephan@emliri.com>
4  * Copyright (C) 2020 Philippe Normand <philn@igalia.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include "gstplay.h"
27 #include "gstplay-signal-adapter.h"
28 #include "gstplay-message-private.h"
29 
30 GST_DEBUG_CATEGORY_STATIC (gst_play_signal_adapter_debug);
31 #define GST_CAT_DEFAULT gst_play_signal_adapter_debug
32 
33 enum
34 {
35   SIGNAL_URI_LOADED,
36   SIGNAL_POSITION_UPDATED,
37   SIGNAL_DURATION_CHANGED,
38   SIGNAL_STATE_CHANGED,
39   SIGNAL_BUFFERING,
40   SIGNAL_END_OF_STREAM,
41   SIGNAL_ERROR,
42   SIGNAL_WARNING,
43   SIGNAL_VIDEO_DIMENSIONS_CHANGED,
44   SIGNAL_MEDIA_INFO_UPDATED,
45   SIGNAL_VOLUME_CHANGED,
46   SIGNAL_MUTE_CHANGED,
47   SIGNAL_SEEK_DONE,
48   SIGNAL_LAST
49 };
50 
51 enum
52 {
53   PROP_0,
54   PROP_PLAY,
55   PROP_LAST
56 };
57 
58 static GParamSpec *param_specs[PROP_LAST] = { NULL, };
59 
60 struct _GstPlaySignalAdapter
61 {
62   GObject parent;
63   GstBus *bus;
64   GstPlay *play;
65   GSource *source;
66 };
67 
68 struct _GstPlaySignalAdapterClass
69 {
70   GObjectClass parent_class;
71 };
72 
73 #define _do_init \
74   GST_DEBUG_CATEGORY_INIT (gst_play_signal_adapter_debug, "gst-play-signal-adapter", \
75       0, "GstPlay signal adapter")
76 
77 #define parent_class gst_play_signal_adapter_parent_class
78 G_DEFINE_TYPE_WITH_CODE (GstPlaySignalAdapter, gst_play_signal_adapter,
79     G_TYPE_OBJECT, _do_init);
80 
81 static guint signals[SIGNAL_LAST] = { 0, };
82 
83 static void
gst_play_signal_adapter_emit(GstPlaySignalAdapter * self,const GstStructure * message_data)84 gst_play_signal_adapter_emit (GstPlaySignalAdapter * self,
85     const GstStructure * message_data)
86 {
87   GstPlayMessage play_message_type;
88   g_return_if_fail (g_str_equal (gst_structure_get_name (message_data),
89           GST_PLAY_MESSAGE_DATA));
90 
91   GST_LOG ("Emitting message %" GST_PTR_FORMAT, message_data);
92   gst_structure_get (message_data, GST_PLAY_MESSAGE_DATA_TYPE,
93       GST_TYPE_PLAY_MESSAGE, &play_message_type, NULL);
94 
95   switch (play_message_type) {
96     case GST_PLAY_MESSAGE_URI_LOADED:{
97       const gchar *uri =
98           gst_structure_get_string (message_data, GST_PLAY_MESSAGE_DATA_URI);
99       g_signal_emit (self, signals[SIGNAL_URI_LOADED], 0, uri);
100       break;
101     }
102     case GST_PLAY_MESSAGE_POSITION_UPDATED:{
103       GstClockTime pos = GST_CLOCK_TIME_NONE;
104       gst_structure_get (message_data, GST_PLAY_MESSAGE_DATA_POSITION,
105           GST_TYPE_CLOCK_TIME, &pos, NULL);
106       g_signal_emit (self, signals[SIGNAL_POSITION_UPDATED], 0, pos);
107       break;
108     }
109     case GST_PLAY_MESSAGE_DURATION_CHANGED:{
110       GstClockTime duration = GST_CLOCK_TIME_NONE;
111       gst_structure_get (message_data, GST_PLAY_MESSAGE_DATA_DURATION,
112           GST_TYPE_CLOCK_TIME, &duration, NULL);
113       g_signal_emit (self, signals[SIGNAL_DURATION_CHANGED], 0, duration);
114       break;
115     }
116     case GST_PLAY_MESSAGE_STATE_CHANGED:{
117       GstPlayState state = 0;
118       gst_structure_get (message_data, GST_PLAY_MESSAGE_DATA_PLAY_STATE,
119           GST_TYPE_PLAY_STATE, &state, NULL);
120       g_signal_emit (self, signals[SIGNAL_STATE_CHANGED], 0, state);
121       break;
122     }
123     case GST_PLAY_MESSAGE_BUFFERING:{
124       guint percent = 0;
125       gst_structure_get (message_data,
126           GST_PLAY_MESSAGE_DATA_BUFFERING_PERCENT, G_TYPE_UINT, &percent, NULL);
127       g_signal_emit (self, signals[SIGNAL_BUFFERING], 0, percent);
128       break;
129     }
130     case GST_PLAY_MESSAGE_END_OF_STREAM:
131       g_signal_emit (self, signals[SIGNAL_END_OF_STREAM], 0);
132       break;
133     case GST_PLAY_MESSAGE_ERROR:{
134       GError *error = NULL;
135       GstStructure *details = NULL;
136       gst_structure_get (message_data, GST_PLAY_MESSAGE_DATA_ERROR,
137           G_TYPE_ERROR, &error, GST_PLAY_MESSAGE_DATA_ERROR_DETAILS,
138           GST_TYPE_STRUCTURE, &details, NULL);
139       g_signal_emit (self, signals[SIGNAL_ERROR], 0, error, details);
140       g_error_free (error);
141       if (details)
142         gst_structure_free (details);
143       break;
144     }
145     case GST_PLAY_MESSAGE_WARNING:{
146       GError *error = NULL;
147       GstStructure *details = NULL;
148       gst_structure_get (message_data, GST_PLAY_MESSAGE_DATA_WARNING,
149           G_TYPE_ERROR, &error, GST_PLAY_MESSAGE_DATA_WARNING_DETAILS,
150           GST_TYPE_STRUCTURE, &details, NULL);
151       g_signal_emit (self, signals[SIGNAL_WARNING], 0, error, details);
152       g_error_free (error);
153       if (details)
154         gst_structure_free (details);
155       break;
156     }
157     case GST_PLAY_MESSAGE_VIDEO_DIMENSIONS_CHANGED:{
158       guint width = 0;
159       guint height = 0;
160       gst_structure_get (message_data,
161           GST_PLAY_MESSAGE_DATA_VIDEO_WIDTH, G_TYPE_UINT, &width,
162           GST_PLAY_MESSAGE_DATA_VIDEO_HEIGHT, G_TYPE_UINT, &height, NULL);
163       g_signal_emit (self, signals[SIGNAL_VIDEO_DIMENSIONS_CHANGED], 0,
164           width, height);
165       break;
166     }
167     case GST_PLAY_MESSAGE_MEDIA_INFO_UPDATED:{
168       GstPlayMediaInfo *media_info;
169       gst_structure_get (message_data, GST_PLAY_MESSAGE_DATA_MEDIA_INFO,
170           GST_TYPE_PLAY_MEDIA_INFO, &media_info, NULL);
171       g_signal_emit (self, signals[SIGNAL_MEDIA_INFO_UPDATED], 0, media_info);
172       g_object_unref (media_info);
173       break;
174     }
175     case GST_PLAY_MESSAGE_VOLUME_CHANGED:{
176       gdouble volume;
177       gst_structure_get (message_data, GST_PLAY_MESSAGE_DATA_VOLUME,
178           G_TYPE_DOUBLE, &volume, NULL);
179       g_signal_emit (self, signals[SIGNAL_VOLUME_CHANGED], 0, volume);
180       break;
181     }
182     case GST_PLAY_MESSAGE_MUTE_CHANGED:{
183       gboolean is_muted;
184       gst_structure_get (message_data, GST_PLAY_MESSAGE_DATA_IS_MUTED,
185           G_TYPE_BOOLEAN, &is_muted, NULL);
186       g_signal_emit (self, signals[SIGNAL_MUTE_CHANGED], 0, is_muted);
187       break;
188     }
189     case GST_PLAY_MESSAGE_SEEK_DONE:{
190       GstClockTime pos;
191       gst_structure_get (message_data, GST_PLAY_MESSAGE_DATA_POSITION,
192           GST_TYPE_CLOCK_TIME, &pos, NULL);
193       g_signal_emit (self, signals[SIGNAL_SEEK_DONE], 0, pos);
194       break;
195     }
196     default:
197       g_assert_not_reached ();
198       break;
199   }
200 }
201 
202 /*
203  * callback for the bus-message in-sync handling
204  */
205 static GstBusSyncReply
gst_play_signal_adapter_bus_sync_handler(GstBus * bus,GstMessage * message,gpointer user_data)206     gst_play_signal_adapter_bus_sync_handler
207     (GstBus * bus, GstMessage * message, gpointer user_data)
208 {
209   GstPlaySignalAdapter *self = GST_PLAY_SIGNAL_ADAPTER (user_data);
210   const GstStructure *message_data = gst_message_get_structure (message);
211   gst_play_signal_adapter_emit (self, message_data);
212   gst_message_unref (message);
213   return GST_BUS_DROP;
214 }
215 
216 /*
217  * callback for the bus-watch
218  * pre: there is a message on the bus
219  */
220 static gboolean
gst_play_signal_adapter_on_message(GstBus * bus,GstMessage * message,gpointer user_data)221 gst_play_signal_adapter_on_message (GstBus * bus,
222     GstMessage * message, gpointer user_data)
223 {
224   GstPlaySignalAdapter *self = GST_PLAY_SIGNAL_ADAPTER (user_data);
225   const GstStructure *message_data = gst_message_get_structure (message);
226   gst_play_signal_adapter_emit (self, message_data);
227   return TRUE;
228 }
229 
230 /**
231  * gst_play_signal_adapter_new:
232  * @play: (transfer none): #GstPlay instance to emit signals for.
233  *
234  * A bus-watching #GSource will be created and attached to the the
235  * thread-default #GMainContext. The attached callback will emit the
236  * corresponding signal for the message received. Matching signals for play
237  * messages from the bus will be emitted by it on the created adapter object.
238  *
239  * Returns: (transfer full): A new #GstPlaySignalAdapter to connect signal handlers to.
240  *
241  * Since: 1.20
242  */
243 GstPlaySignalAdapter *
gst_play_signal_adapter_new(GstPlay * play)244 gst_play_signal_adapter_new (GstPlay * play)
245 {
246   GstPlaySignalAdapter *self = NULL;
247   GMainContext *context = NULL;
248 
249   g_return_val_if_fail (GST_IS_PLAY (play), NULL);
250 
251   self = g_object_new (GST_TYPE_PLAY_SIGNAL_ADAPTER, NULL);
252   self->play = play;
253   self->bus = gst_play_get_message_bus (play);
254   self->source = gst_bus_create_watch (self->bus);
255 
256   context = g_main_context_get_thread_default ();
257   g_source_attach (self->source, context);
258   g_source_set_callback (self->source,
259       (GSourceFunc) gst_play_signal_adapter_on_message, self, NULL);
260   return self;
261 }
262 
263 /**
264  * gst_play_signal_adapter_new_with_main_context:
265  * @play: (transfer none): #GstPlay instance to emit signals for.
266  * @context: A #GMainContext on which the main-loop will process play bus messages on.
267  *
268  * A bus-watching #GSource will be created and attached to the @context. The
269  * attached callback will emit the corresponding signal for the message
270  * received. Matching signals for play messages from the bus will be emitted by
271  * it on the created adapter object.
272  *
273  * Returns: (transfer full): A new #GstPlaySignalAdapter to connect signal handlers to.
274  *
275  * Since: 1.20
276  */
277 GstPlaySignalAdapter *
gst_play_signal_adapter_new_with_main_context(GstPlay * play,GMainContext * context)278 gst_play_signal_adapter_new_with_main_context (GstPlay * play,
279     GMainContext * context)
280 {
281   GstPlaySignalAdapter *self = NULL;
282 
283   g_return_val_if_fail (GST_IS_PLAY (play), NULL);
284   g_return_val_if_fail (context != NULL, NULL);
285 
286   self = g_object_new (GST_TYPE_PLAY_SIGNAL_ADAPTER, NULL);
287   self->play = play;
288   self->bus = gst_play_get_message_bus (play);
289   self->source = gst_bus_create_watch (self->bus);
290 
291   g_source_attach (self->source, context);
292   g_source_set_callback (self->source,
293       (GSourceFunc) gst_play_signal_adapter_on_message, self, NULL);
294   return self;
295 }
296 
297 /**
298  * gst_play_signal_adapter_new_sync_emit:
299  * @play: (transfer none): #GstPlay instance to emit signals for.
300  *
301  * Create an adapter that synchronously emits its signals, from the thread in
302  * which the messages have been posted.
303  *
304  * Returns: (transfer full): A new #GstPlaySignalAdapter to connect signal handlers to.
305  *
306  * Since: 1.20
307  */
308 GstPlaySignalAdapter *
gst_play_signal_adapter_new_sync_emit(GstPlay * play)309 gst_play_signal_adapter_new_sync_emit (GstPlay * play)
310 {
311   GstBus *bus = NULL;
312   GstPlaySignalAdapter *self = NULL;
313 
314   g_return_val_if_fail (GST_IS_PLAY (play), NULL);
315 
316   bus = gst_play_get_message_bus (play);
317 
318   self = g_object_new (GST_TYPE_PLAY_SIGNAL_ADAPTER, NULL);
319   self->play = play;
320   self->bus = bus;
321   gst_bus_set_sync_handler (self->bus,
322       gst_play_signal_adapter_bus_sync_handler, self, NULL);
323   return self;
324 }
325 
326 
327 /**
328  * gst_play_signal_adapter_get_play:
329  * @adapter: #GstPlaySignalAdapter instance
330  *
331  * Returns: (transfer none): The #GstPlay owning this signal adapter.
332  *
333  * Since: 1.20
334  */
335 GstPlay *
gst_play_signal_adapter_get_play(GstPlaySignalAdapter * adapter)336 gst_play_signal_adapter_get_play (GstPlaySignalAdapter * adapter)
337 {
338   g_return_val_if_fail (GST_IS_PLAY_SIGNAL_ADAPTER (adapter), NULL);
339   return adapter->play;
340 }
341 
342 static void
gst_play_signal_adapter_init(GstPlaySignalAdapter * self)343 gst_play_signal_adapter_init (GstPlaySignalAdapter * self)
344 {
345   self->source = NULL;
346 }
347 
348 static void
gst_play_signal_adapter_dispose(GObject * object)349 gst_play_signal_adapter_dispose (GObject * object)
350 {
351   GstPlaySignalAdapter *self = GST_PLAY_SIGNAL_ADAPTER (object);
352 
353   if (self->source) {
354     g_source_destroy (self->source);
355     g_source_unref (self->source);
356     self->source = NULL;
357   }
358 
359   gst_clear_object (&self->bus);
360 
361   G_OBJECT_CLASS (parent_class)->dispose (object);
362 }
363 
364 static void
gst_play_signal_adapter_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)365 gst_play_signal_adapter_get_property (GObject * object, guint prop_id,
366     GValue * value, GParamSpec * pspec)
367 {
368   GstPlaySignalAdapter *self = GST_PLAY_SIGNAL_ADAPTER (object);
369 
370   switch (prop_id) {
371     case PROP_PLAY:
372       g_value_set_object (value, self->play);
373       break;
374     default:
375       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
376       break;
377   }
378 }
379 
380 static void
gst_play_signal_adapter_class_init(GstPlaySignalAdapterClass * klass)381 gst_play_signal_adapter_class_init (GstPlaySignalAdapterClass * klass)
382 {
383   GObjectClass *gobject_class = (GObjectClass *) klass;
384 
385   gobject_class->dispose = gst_play_signal_adapter_dispose;
386   gobject_class->get_property = gst_play_signal_adapter_get_property;
387 
388   param_specs[PROP_PLAY] =
389       g_param_spec_object ("play", "Play",
390       "GstPlay owning this adapter",
391       GST_TYPE_PLAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
392 
393   signals[SIGNAL_URI_LOADED] =
394       g_signal_new ("uri-loaded", G_TYPE_FROM_CLASS (klass),
395       G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0, NULL,
396       NULL, NULL, G_TYPE_NONE, 1, G_TYPE_STRING);
397 
398   signals[SIGNAL_POSITION_UPDATED] =
399       g_signal_new ("position-updated", G_TYPE_FROM_CLASS (klass),
400       G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0, NULL,
401       NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_CLOCK_TIME);
402 
403   signals[SIGNAL_DURATION_CHANGED] =
404       g_signal_new ("duration-changed", G_TYPE_FROM_CLASS (klass),
405       G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0, NULL,
406       NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_CLOCK_TIME);
407 
408   signals[SIGNAL_STATE_CHANGED] =
409       g_signal_new ("state-changed", G_TYPE_FROM_CLASS (klass),
410       G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0, NULL,
411       NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_PLAY_STATE);
412 
413   signals[SIGNAL_BUFFERING] =
414       g_signal_new ("buffering", G_TYPE_FROM_CLASS (klass),
415       G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0, NULL,
416       NULL, NULL, G_TYPE_NONE, 1, G_TYPE_INT);
417 
418   signals[SIGNAL_END_OF_STREAM] =
419       g_signal_new ("end-of-stream", G_TYPE_FROM_CLASS (klass),
420       G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0, NULL,
421       NULL, NULL, G_TYPE_NONE, 0, G_TYPE_INVALID);
422 
423   signals[SIGNAL_ERROR] =
424       g_signal_new ("error", G_TYPE_FROM_CLASS (klass),
425       G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0, NULL,
426       NULL, NULL, G_TYPE_NONE, 2, G_TYPE_ERROR, GST_TYPE_STRUCTURE);
427 
428   signals[SIGNAL_VIDEO_DIMENSIONS_CHANGED] =
429       g_signal_new ("video-dimensions-changed", G_TYPE_FROM_CLASS (klass),
430       G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0, NULL,
431       NULL, NULL, G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
432 
433   signals[SIGNAL_MEDIA_INFO_UPDATED] =
434       g_signal_new ("media-info-updated", G_TYPE_FROM_CLASS (klass),
435       G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0, NULL,
436       NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_PLAY_MEDIA_INFO);
437 
438   signals[SIGNAL_VOLUME_CHANGED] =
439       g_signal_new ("volume-changed", G_TYPE_FROM_CLASS (klass),
440       G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0, NULL,
441       NULL, NULL, G_TYPE_NONE, 1, G_TYPE_DOUBLE);
442 
443   signals[SIGNAL_MUTE_CHANGED] =
444       g_signal_new ("mute-changed", G_TYPE_FROM_CLASS (klass),
445       G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0, NULL,
446       NULL, NULL, G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
447 
448   signals[SIGNAL_WARNING] =
449       g_signal_new ("warning", G_TYPE_FROM_CLASS (klass),
450       G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0, NULL,
451       NULL, NULL, G_TYPE_NONE, 2, G_TYPE_ERROR, GST_TYPE_STRUCTURE);
452 
453   signals[SIGNAL_SEEK_DONE] =
454       g_signal_new ("seek-done", G_TYPE_FROM_CLASS (klass),
455       G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0, NULL,
456       NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_CLOCK_TIME);
457 
458   g_object_class_install_properties (gobject_class, PROP_LAST, param_specs);
459 }
460