1 /* GStreamer interactive test for accurate segment seeking
2 * Copyright (C) 2014 Tim-Philipp Müller <tim centricular com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 /* Plays the provided file one second at a time using segment seeks.
22 * Theoretically this should be just as smooth as if we played the
23 * file from start to stop in one go, certainly without hickups.
31 #define SEGMENT_DURATION (1 * GST_SECOND)
34 main (int argc, char **argv)
39 gint64 dur, start, stop;
40 gboolean prerolled = FALSE;
43 g_printerr ("Usage: %s FILENAME\n", argv[0]);
47 gst_init (&argc, &argv);
49 if (gst_uri_is_valid (argv[1]))
50 uri = g_strdup (argv[1]);
52 uri = gst_filename_to_uri (argv[1], NULL);
54 g_print ("uri: %s\n", uri);
56 playbin = gst_element_factory_make ("playbin", NULL);
57 g_object_set (playbin, "uri", uri, NULL);
63 playbin = gst_parse_launch ("uridecodebin name=d ! queue ! "
64 "filesink location=/tmp/raw1.data", NULL);
65 src = gst_bin_get_by_name (GST_BIN (playbin), "d");
66 g_object_set (src, "uri", uri, NULL);
67 gst_object_unref (src);
71 gst_element_set_state (playbin, GST_STATE_PAUSED);
73 /* wait for preroll */
74 msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (playbin),
75 GST_CLOCK_TIME_NONE, GST_MESSAGE_ASYNC_DONE | GST_MESSAGE_ERROR);
77 g_assert (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_ERROR);
80 gst_message_unref (msg);
82 if (!gst_element_query_duration (playbin, GST_FORMAT_TIME, &dur))
83 g_error ("Failed to query duration!\n");
85 g_print ("Duration: %" GST_TIME_FORMAT "\n", GST_TIME_ARGS (dur));
89 GstSeekFlags seek_flags;
91 gboolean segment_done = FALSE;
93 seek_flags = GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_SEGMENT;
97 seek_flags |= GST_SEEK_FLAG_FLUSH;
100 stop = start + SEGMENT_DURATION;
102 g_print ("Segment: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "\n",
103 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
105 ret = gst_element_seek (playbin, 1.0, GST_FORMAT_TIME, seek_flags,
106 GST_SEEK_TYPE_SET, start, GST_SEEK_TYPE_SET, stop);
112 /* wait for preroll again */
113 msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (playbin),
114 GST_CLOCK_TIME_NONE, GST_MESSAGE_SEGMENT_DONE |
115 GST_MESSAGE_ASYNC_DONE | GST_MESSAGE_ERROR);
117 g_assert (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_ERROR);
118 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_SEGMENT_DONE) {
123 gst_message_unref (msg);
126 gst_element_set_state (playbin, GST_STATE_PLAYING);
129 /* wait for end of segment if we didn't get it above already */
131 msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (playbin),
132 GST_CLOCK_TIME_NONE, GST_MESSAGE_SEGMENT_DONE | GST_MESSAGE_ERROR);
134 g_assert (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_ERROR);
136 gst_message_unref (msg);
143 gst_element_set_state (playbin, GST_STATE_NULL);
144 gst_object_unref (playbin);