34c75c978f12c7dbb14acc89825ca242731092ac
[platform/upstream/gstreamer.git] / subprojects / gst-docs / examples / tutorials / basic-tutorial-5.c
1 #include <string.h>
2
3 #include <gtk/gtk.h>
4 #include <gst/gst.h>
5 #include <gst/video/videooverlay.h>
6
7 #include <gdk/gdk.h>
8 #if defined (GDK_WINDOWING_X11)
9 #include <gdk/gdkx.h>
10 #elif defined (GDK_WINDOWING_WIN32)
11 #include <gdk/gdkwin32.h>
12 #elif defined (GDK_WINDOWING_QUARTZ)
13 #include <gdk/gdkquartz.h>
14 #if GTK_CHECK_VERSION(3, 24, 10)
15 #include <AppKit/AppKit.h>
16 NSView *gdk_quartz_window_get_nsview (GdkWindow * window);
17 #endif
18 #endif
19
20 /* Structure to contain all our information, so we can pass it around */
21 typedef struct _CustomData
22 {
23   GstElement *playbin;          /* Our one and only pipeline */
24
25   GtkWidget *slider;            /* Slider widget to keep track of current position */
26   GtkWidget *streams_list;      /* Text widget to display info about the streams */
27   gulong slider_update_signal_id;       /* Signal ID for the slider update signal */
28
29   GstState state;               /* Current state of the pipeline */
30   gint64 duration;              /* Duration of the clip, in nanoseconds */
31 } CustomData;
32
33 /* This function is called when the GUI toolkit creates the physical window that will hold the video.
34  * At this point we can retrieve its handler (which has a different meaning depending on the windowing system)
35  * and pass it to GStreamer through the XOverlay interface. */
36 static void
37 realize_cb (GtkWidget * widget, CustomData * data)
38 {
39   GdkWindow *window = gtk_widget_get_window (widget);
40   guintptr window_handle;
41
42   if (!gdk_window_ensure_native (window))
43     g_error ("Couldn't create native window needed for GstXOverlay!");
44
45   /* Retrieve window handler from GDK */
46 #if defined (GDK_WINDOWING_WIN32)
47   window_handle = (guintptr) GDK_WINDOW_HWND (window);
48 #elif defined (GDK_WINDOWING_QUARTZ)
49   window_handle = gdk_quartz_window_get_nsview (window);
50 #elif defined (GDK_WINDOWING_X11)
51   window_handle = GDK_WINDOW_XID (window);
52 #endif
53   /* Pass it to playbin, which implements XOverlay and will forward it to the video sink */
54   gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (data->playbin),
55       window_handle);
56 }
57
58 /* This function is called when the PLAY button is clicked */
59 static void
60 play_cb (GtkButton * button, CustomData * data)
61 {
62   gst_element_set_state (data->playbin, GST_STATE_PLAYING);
63 }
64
65 /* This function is called when the PAUSE button is clicked */
66 static void
67 pause_cb (GtkButton * button, CustomData * data)
68 {
69   gst_element_set_state (data->playbin, GST_STATE_PAUSED);
70 }
71
72 /* This function is called when the STOP button is clicked */
73 static void
74 stop_cb (GtkButton * button, CustomData * data)
75 {
76   gst_element_set_state (data->playbin, GST_STATE_READY);
77 }
78
79 /* This function is called when the main window is closed */
80 static void
81 delete_event_cb (GtkWidget * widget, GdkEvent * event, CustomData * data)
82 {
83   stop_cb (NULL, data);
84   gtk_main_quit ();
85 }
86
87 /* This function is called everytime the video window needs to be redrawn (due to damage/exposure,
88  * rescaling, etc). GStreamer takes care of this in the PAUSED and PLAYING states, otherwise,
89  * we simply draw a black rectangle to avoid garbage showing up. */
90 static gboolean
91 draw_cb (GtkWidget * widget, cairo_t * cr, CustomData * data)
92 {
93   if (data->state < GST_STATE_PAUSED) {
94     GtkAllocation allocation;
95
96     /* Cairo is a 2D graphics library which we use here to clean the video window.
97      * It is used by GStreamer for other reasons, so it will always be available to us. */
98     gtk_widget_get_allocation (widget, &allocation);
99     cairo_set_source_rgb (cr, 0, 0, 0);
100     cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);
101     cairo_fill (cr);
102   }
103
104   return FALSE;
105 }
106
107 /* This function is called when the slider changes its position. We perform a seek to the
108  * new position here. */
109 static void
110 slider_cb (GtkRange * range, CustomData * data)
111 {
112   gdouble value = gtk_range_get_value (GTK_RANGE (data->slider));
113   gst_element_seek_simple (data->playbin, GST_FORMAT_TIME,
114       GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT,
115       (gint64) (value * GST_SECOND));
116 }
117
118 /* This creates all the GTK+ widgets that compose our application, and registers the callbacks */
119 static void
120 create_ui (CustomData * data)
121 {
122   GtkWidget *main_window;       /* The uppermost window, containing all other windows */
123   GtkWidget *video_window;      /* The drawing area where the video will be shown */
124   GtkWidget *main_box;          /* VBox to hold main_hbox and the controls */
125   GtkWidget *main_hbox;         /* HBox to hold the video_window and the stream info text widget */
126   GtkWidget *controls;          /* HBox to hold the buttons and the slider */
127   GtkWidget *play_button, *pause_button, *stop_button;  /* Buttons */
128
129   main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
130   g_signal_connect (G_OBJECT (main_window), "delete-event",
131       G_CALLBACK (delete_event_cb), data);
132
133   video_window = gtk_drawing_area_new ();
134   gtk_widget_set_double_buffered (video_window, FALSE);
135   g_signal_connect (video_window, "realize", G_CALLBACK (realize_cb), data);
136   g_signal_connect (video_window, "draw", G_CALLBACK (draw_cb), data);
137
138   play_button =
139       gtk_button_new_from_icon_name ("media-playback-start",
140       GTK_ICON_SIZE_SMALL_TOOLBAR);
141   g_signal_connect (G_OBJECT (play_button), "clicked", G_CALLBACK (play_cb),
142       data);
143
144   pause_button =
145       gtk_button_new_from_icon_name ("media-playback-pause",
146       GTK_ICON_SIZE_SMALL_TOOLBAR);
147   g_signal_connect (G_OBJECT (pause_button), "clicked", G_CALLBACK (pause_cb),
148       data);
149
150   stop_button =
151       gtk_button_new_from_icon_name ("media-playback-stop",
152       GTK_ICON_SIZE_SMALL_TOOLBAR);
153   g_signal_connect (G_OBJECT (stop_button), "clicked", G_CALLBACK (stop_cb),
154       data);
155
156   data->slider =
157       gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL, 0, 100, 1);
158   gtk_scale_set_draw_value (GTK_SCALE (data->slider), 0);
159   data->slider_update_signal_id =
160       g_signal_connect (G_OBJECT (data->slider), "value-changed",
161       G_CALLBACK (slider_cb), data);
162
163   data->streams_list = gtk_text_view_new ();
164   gtk_text_view_set_editable (GTK_TEXT_VIEW (data->streams_list), FALSE);
165
166   controls = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
167   gtk_box_pack_start (GTK_BOX (controls), play_button, FALSE, FALSE, 2);
168   gtk_box_pack_start (GTK_BOX (controls), pause_button, FALSE, FALSE, 2);
169   gtk_box_pack_start (GTK_BOX (controls), stop_button, FALSE, FALSE, 2);
170   gtk_box_pack_start (GTK_BOX (controls), data->slider, TRUE, TRUE, 2);
171
172   main_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
173   gtk_box_pack_start (GTK_BOX (main_hbox), video_window, TRUE, TRUE, 0);
174   gtk_box_pack_start (GTK_BOX (main_hbox), data->streams_list, FALSE, FALSE, 2);
175
176   main_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
177   gtk_box_pack_start (GTK_BOX (main_box), main_hbox, TRUE, TRUE, 0);
178   gtk_box_pack_start (GTK_BOX (main_box), controls, FALSE, FALSE, 0);
179   gtk_container_add (GTK_CONTAINER (main_window), main_box);
180   gtk_window_set_default_size (GTK_WINDOW (main_window), 640, 480);
181
182   gtk_widget_show_all (main_window);
183 }
184
185 /* This function is called periodically to refresh the GUI */
186 static gboolean
187 refresh_ui (CustomData * data)
188 {
189   gint64 current = -1;
190
191   /* We do not want to update anything unless we are in the PAUSED or PLAYING states */
192   if (data->state < GST_STATE_PAUSED)
193     return TRUE;
194
195   /* If we didn't know it yet, query the stream duration */
196   if (!GST_CLOCK_TIME_IS_VALID (data->duration)) {
197     if (!gst_element_query_duration (data->playbin, GST_FORMAT_TIME,
198             &data->duration)) {
199       g_printerr ("Could not query current duration.\n");
200     } else {
201       /* Set the range of the slider to the clip duration, in SECONDS */
202       gtk_range_set_range (GTK_RANGE (data->slider), 0,
203           (gdouble) data->duration / GST_SECOND);
204     }
205   }
206
207   if (gst_element_query_position (data->playbin, GST_FORMAT_TIME, &current)) {
208     /* Block the "value-changed" signal, so the slider_cb function is not called
209      * (which would trigger a seek the user has not requested) */
210     g_signal_handler_block (data->slider, data->slider_update_signal_id);
211     /* Set the position of the slider to the current pipeline positoin, in SECONDS */
212     gtk_range_set_value (GTK_RANGE (data->slider),
213         (gdouble) current / GST_SECOND);
214     /* Re-enable the signal */
215     g_signal_handler_unblock (data->slider, data->slider_update_signal_id);
216   }
217   return TRUE;
218 }
219
220 /* This function is called when new metadata is discovered in the stream */
221 static void
222 tags_cb (GstElement * playbin, gint stream, CustomData * data)
223 {
224   /* We are possibly in a GStreamer working thread, so we notify the main
225    * thread of this event through a message in the bus */
226   gst_element_post_message (playbin,
227       gst_message_new_application (GST_OBJECT (playbin),
228           gst_structure_new_empty ("tags-changed")));
229 }
230
231 /* This function is called when an error message is posted on the bus */
232 static void
233 error_cb (GstBus * bus, GstMessage * msg, CustomData * data)
234 {
235   GError *err;
236   gchar *debug_info;
237
238   /* Print error details on the screen */
239   gst_message_parse_error (msg, &err, &debug_info);
240   g_printerr ("Error received from element %s: %s\n",
241       GST_OBJECT_NAME (msg->src), err->message);
242   g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
243   g_clear_error (&err);
244   g_free (debug_info);
245
246   /* Set the pipeline to READY (which stops playback) */
247   gst_element_set_state (data->playbin, GST_STATE_READY);
248 }
249
250 /* This function is called when an End-Of-Stream message is posted on the bus.
251  * We just set the pipeline to READY (which stops playback) */
252 static void
253 eos_cb (GstBus * bus, GstMessage * msg, CustomData * data)
254 {
255   g_print ("End-Of-Stream reached.\n");
256   gst_element_set_state (data->playbin, GST_STATE_READY);
257 }
258
259 /* This function is called when the pipeline changes states. We use it to
260  * keep track of the current state. */
261 static void
262 state_changed_cb (GstBus * bus, GstMessage * msg, CustomData * data)
263 {
264   GstState old_state, new_state, pending_state;
265   gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);
266   if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data->playbin)) {
267     data->state = new_state;
268     g_print ("State set to %s\n", gst_element_state_get_name (new_state));
269     if (old_state == GST_STATE_READY && new_state == GST_STATE_PAUSED) {
270       /* For extra responsiveness, we refresh the GUI as soon as we reach the PAUSED state */
271       refresh_ui (data);
272     }
273   }
274 }
275
276 /* Extract metadata from all the streams and write it to the text widget in the GUI */
277 static void
278 analyze_streams (CustomData * data)
279 {
280   gint i;
281   GstTagList *tags;
282   gchar *str, *total_str;
283   guint rate;
284   gint n_video, n_audio, n_text;
285   GtkTextBuffer *text;
286
287   /* Clean current contents of the widget */
288   text = gtk_text_view_get_buffer (GTK_TEXT_VIEW (data->streams_list));
289   gtk_text_buffer_set_text (text, "", -1);
290
291   /* Read some properties */
292   g_object_get (data->playbin, "n-video", &n_video, NULL);
293   g_object_get (data->playbin, "n-audio", &n_audio, NULL);
294   g_object_get (data->playbin, "n-text", &n_text, NULL);
295
296   for (i = 0; i < n_video; i++) {
297     tags = NULL;
298     /* Retrieve the stream's video tags */
299     g_signal_emit_by_name (data->playbin, "get-video-tags", i, &tags);
300     if (tags) {
301       total_str = g_strdup_printf ("video stream %d:\n", i);
302       gtk_text_buffer_insert_at_cursor (text, total_str, -1);
303       g_free (total_str);
304       gst_tag_list_get_string (tags, GST_TAG_VIDEO_CODEC, &str);
305       total_str = g_strdup_printf ("  codec: %s\n", str ? str : "unknown");
306       gtk_text_buffer_insert_at_cursor (text, total_str, -1);
307       g_free (total_str);
308       g_free (str);
309       gst_tag_list_free (tags);
310     }
311   }
312
313   for (i = 0; i < n_audio; i++) {
314     tags = NULL;
315     /* Retrieve the stream's audio tags */
316     g_signal_emit_by_name (data->playbin, "get-audio-tags", i, &tags);
317     if (tags) {
318       total_str = g_strdup_printf ("\naudio stream %d:\n", i);
319       gtk_text_buffer_insert_at_cursor (text, total_str, -1);
320       g_free (total_str);
321       if (gst_tag_list_get_string (tags, GST_TAG_AUDIO_CODEC, &str)) {
322         total_str = g_strdup_printf ("  codec: %s\n", str);
323         gtk_text_buffer_insert_at_cursor (text, total_str, -1);
324         g_free (total_str);
325         g_free (str);
326       }
327       if (gst_tag_list_get_string (tags, GST_TAG_LANGUAGE_CODE, &str)) {
328         total_str = g_strdup_printf ("  language: %s\n", str);
329         gtk_text_buffer_insert_at_cursor (text, total_str, -1);
330         g_free (total_str);
331         g_free (str);
332       }
333       if (gst_tag_list_get_uint (tags, GST_TAG_BITRATE, &rate)) {
334         total_str = g_strdup_printf ("  bitrate: %d\n", rate);
335         gtk_text_buffer_insert_at_cursor (text, total_str, -1);
336         g_free (total_str);
337       }
338       gst_tag_list_free (tags);
339     }
340   }
341
342   for (i = 0; i < n_text; i++) {
343     tags = NULL;
344     /* Retrieve the stream's subtitle tags */
345     g_signal_emit_by_name (data->playbin, "get-text-tags", i, &tags);
346     if (tags) {
347       total_str = g_strdup_printf ("\nsubtitle stream %d:\n", i);
348       gtk_text_buffer_insert_at_cursor (text, total_str, -1);
349       g_free (total_str);
350       if (gst_tag_list_get_string (tags, GST_TAG_LANGUAGE_CODE, &str)) {
351         total_str = g_strdup_printf ("  language: %s\n", str);
352         gtk_text_buffer_insert_at_cursor (text, total_str, -1);
353         g_free (total_str);
354         g_free (str);
355       }
356       gst_tag_list_free (tags);
357     }
358   }
359 }
360
361 /* This function is called when an "application" message is posted on the bus.
362  * Here we retrieve the message posted by the tags_cb callback */
363 static void
364 application_cb (GstBus * bus, GstMessage * msg, CustomData * data)
365 {
366   if (g_strcmp0 (gst_structure_get_name (gst_message_get_structure (msg)),
367           "tags-changed") == 0) {
368     /* If the message is the "tags-changed" (only one we are currently issuing), update
369      * the stream info GUI */
370     analyze_streams (data);
371   }
372 }
373
374 int
375 main (int argc, char *argv[])
376 {
377   CustomData data;
378   GstStateChangeReturn ret;
379   GstBus *bus;
380
381   /* Initialize GTK */
382   gtk_init (&argc, &argv);
383
384   /* Initialize GStreamer */
385   gst_init (&argc, &argv);
386
387   /* Initialize our data structure */
388   memset (&data, 0, sizeof (data));
389   data.duration = GST_CLOCK_TIME_NONE;
390
391   /* Create the elements */
392   data.playbin = gst_element_factory_make ("playbin", "playbin");
393
394   if (!data.playbin) {
395     g_printerr ("Not all elements could be created.\n");
396     return -1;
397   }
398
399   /* Set the URI to play */
400   g_object_set (data.playbin, "uri",
401       "https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm",
402       NULL);
403
404   /* Connect to interesting signals in playbin */
405   g_signal_connect (G_OBJECT (data.playbin), "video-tags-changed",
406       (GCallback) tags_cb, &data);
407   g_signal_connect (G_OBJECT (data.playbin), "audio-tags-changed",
408       (GCallback) tags_cb, &data);
409   g_signal_connect (G_OBJECT (data.playbin), "text-tags-changed",
410       (GCallback) tags_cb, &data);
411
412   /* Create the GUI */
413   create_ui (&data);
414
415   /* Instruct the bus to emit signals for each received message, and connect to the interesting signals */
416   bus = gst_element_get_bus (data.playbin);
417   gst_bus_add_signal_watch (bus);
418   g_signal_connect (G_OBJECT (bus), "message::error", (GCallback) error_cb,
419       &data);
420   g_signal_connect (G_OBJECT (bus), "message::eos", (GCallback) eos_cb, &data);
421   g_signal_connect (G_OBJECT (bus), "message::state-changed",
422       (GCallback) state_changed_cb, &data);
423   g_signal_connect (G_OBJECT (bus), "message::application",
424       (GCallback) application_cb, &data);
425   gst_object_unref (bus);
426
427   /* Start playing */
428   ret = gst_element_set_state (data.playbin, GST_STATE_PLAYING);
429   if (ret == GST_STATE_CHANGE_FAILURE) {
430     g_printerr ("Unable to set the pipeline to the playing state.\n");
431     gst_object_unref (data.playbin);
432     return -1;
433   }
434
435   /* Register a function that GLib will call every second */
436   g_timeout_add_seconds (1, (GSourceFunc) refresh_ui, &data);
437
438   /* Start the GTK main loop. We will not regain control until gtk_main_quit is called. */
439   gtk_main ();
440
441   /* Free resources */
442   gst_element_set_state (data.playbin, GST_STATE_NULL);
443   gst_object_unref (data.playbin);
444   return 0;
445 }