Update theme submodule
[platform/upstream/gstreamer.git] / examples / tutorials / basic-tutorial-13.c
1 #include <string.h>
2 #include <stdio.h>
3 #include <gst/gst.h>
4
5 typedef struct _CustomData {
6   GstElement *pipeline;
7   GstElement *video_sink;
8   GMainLoop *loop;
9
10   gboolean playing;  /* Playing or Paused */
11   gdouble rate;      /* Current playback rate (can be negative) */
12 } CustomData;
13
14 /* Send seek event to change rate */
15 static void send_seek_event (CustomData *data) {
16   gint64 position;
17   GstEvent *seek_event;
18
19   /* Obtain the current position, needed for the seek event */
20   if (!gst_element_query_position (data->pipeline, GST_FORMAT_TIME, &position)) {
21     g_printerr ("Unable to retrieve current position.\n");
22     return;
23   }
24
25   /* Create the seek event */
26   if (data->rate > 0) {
27     seek_event = gst_event_new_seek (data->rate, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE,
28         GST_SEEK_TYPE_SET, position, GST_SEEK_TYPE_SET, -1);
29   } else {
30     seek_event = gst_event_new_seek (data->rate, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE,
31         GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, position);
32   }
33
34   if (data->video_sink == NULL) {
35     /* If we have not done so, obtain the sink through which we will send the seek events */
36     g_object_get (data->pipeline, "video-sink", &data->video_sink, NULL);
37   }
38
39   /* Send the event */
40   gst_element_send_event (data->video_sink, seek_event);
41
42   g_print ("Current rate: %g\n", data->rate);
43 }
44
45 /* Process keyboard input */
46 static gboolean handle_keyboard (GIOChannel *source, GIOCondition cond, CustomData *data) {
47   gchar *str = NULL;
48
49   if (g_io_channel_read_line (source, &str, NULL, NULL, NULL) != G_IO_STATUS_NORMAL) {
50     return TRUE;
51   }
52
53   switch (g_ascii_tolower (str[0])) {
54   case 'p':
55     data->playing = !data->playing;
56     gst_element_set_state (data->pipeline, data->playing ? GST_STATE_PLAYING : GST_STATE_PAUSED);
57     g_print ("Setting state to %s\n", data->playing ? "PLAYING" : "PAUSE");
58     break;
59   case 's':
60     if (g_ascii_isupper (str[0])) {
61       data->rate *= 2.0;
62     } else {
63       data->rate /= 2.0;
64     }
65     send_seek_event (data);
66     break;
67   case 'd':
68     data->rate *= -1.0;
69     send_seek_event (data);
70     break;
71   case 'n':
72     if (data->video_sink == NULL) {
73       /* If we have not done so, obtain the sink through which we will send the step events */
74       g_object_get (data->pipeline, "video-sink", &data->video_sink, NULL);
75     }
76
77     gst_element_send_event (data->video_sink,
78         gst_event_new_step (GST_FORMAT_BUFFERS, 1, data->rate, TRUE, FALSE));
79     g_print ("Stepping one frame\n");
80     break;
81   case 'q':
82     g_main_loop_quit (data->loop);
83     break;
84   default:
85     break;
86   }
87
88   g_free (str);
89
90   return TRUE;
91 }
92
93 int main(int argc, char *argv[]) {
94   CustomData data;
95   GstStateChangeReturn ret;
96   GIOChannel *io_stdin;
97
98   /* Initialize GStreamer */
99   gst_init (&argc, &argv);
100
101   /* Initialize our data structure */
102   memset (&data, 0, sizeof (data));
103
104   /* Print usage map */
105   g_print (
106     "USAGE: Choose one of the following options, then press enter:\n"
107     " 'P' to toggle between PAUSE and PLAY\n"
108     " 'S' to increase playback speed, 's' to decrease playback speed\n"
109     " 'D' to toggle playback direction\n"
110     " 'N' to move to next frame (in the current direction, better in PAUSE)\n"
111     " 'Q' to quit\n");
112
113   /* Build the pipeline */
114   data.pipeline = gst_parse_launch ("playbin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm", NULL);
115
116   /* Add a keyboard watch so we get notified of keystrokes */
117 #ifdef G_OS_WIN32
118   io_stdin = g_io_channel_win32_new_fd (fileno (stdin));
119 #else
120   io_stdin = g_io_channel_unix_new (fileno (stdin));
121 #endif
122   g_io_add_watch (io_stdin, G_IO_IN, (GIOFunc)handle_keyboard, &data);
123
124   /* Start playing */
125   ret = gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
126   if (ret == GST_STATE_CHANGE_FAILURE) {
127     g_printerr ("Unable to set the pipeline to the playing state.\n");
128     gst_object_unref (data.pipeline);
129     return -1;
130   }
131   data.playing = TRUE;
132   data.rate = 1.0;
133
134   /* Create a GLib Main Loop and set it to run */
135   data.loop = g_main_loop_new (NULL, FALSE);
136   g_main_loop_run (data.loop);
137
138   /* Free resources */
139   g_main_loop_unref (data.loop);
140   g_io_channel_unref (io_stdin);
141   gst_element_set_state (data.pipeline, GST_STATE_NULL);
142   if (data.video_sink != NULL)
143     gst_object_unref (data.video_sink);
144   gst_object_unref (data.pipeline);
145   return 0;
146 }