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