5 typedef struct _CustomData {
7 GstElement *video_sink;
10 gboolean playing; /* Playing or Paused */
11 gdouble rate; /* Current playback rate (can be negative) */
14 /* Send seek event to change rate */
15 static void send_seek_event (CustomData *data) {
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");
25 /* Create the seek event */
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);
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);
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);
40 gst_element_send_event (data->video_sink, seek_event);
42 g_print ("Current rate: %g\n", data->rate);
45 /* Process keyboard input */
46 static gboolean handle_keyboard (GIOChannel *source, GIOCondition cond, CustomData *data) {
49 if (g_io_channel_read_line (source, &str, NULL, NULL, NULL) != G_IO_STATUS_NORMAL) {
53 switch (g_ascii_tolower (str[0])) {
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");
60 if (g_ascii_isupper (str[0])) {
65 send_seek_event (data);
69 send_seek_event (data);
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);
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");
82 g_main_loop_quit (data->loop);
93 int main(int argc, char *argv[]) {
95 GstStateChangeReturn ret;
98 /* Initialize GStreamer */
99 gst_init (&argc, &argv);
101 /* Initialize our data structure */
102 memset (&data, 0, sizeof (data));
104 /* Print usage map */
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"
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);
116 /* Add a keyboard watch so we get notified of keystrokes */
118 io_stdin = g_io_channel_win32_new_fd (fileno (stdin));
120 io_stdin = g_io_channel_unix_new (fileno (stdin));
122 g_io_add_watch (io_stdin, G_IO_IN, (GIOFunc)handle_keyboard, &data);
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);
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);
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);