1 /* GStreamer interactive test for accurate segment seeking
2 * Copyright (C) 2014 Tim-Philipp Müller <tim centricular com>
3 *
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.
8 *
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.
13 *
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.
18 *
19 */
20
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.
24 */
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <gst/gst.h>
30
31 #define SEGMENT_DURATION (1 * GST_SECOND)
32
33 int
main(int argc,char ** argv)34 main (int argc, char **argv)
35 {
36 GstElement *playbin;
37 GstMessage *msg;
38 gchar *uri;
39 gint64 dur, start, stop;
40 gboolean prerolled = FALSE;
41
42 if (argc < 2) {
43 g_printerr ("Usage: %s FILENAME\n", argv[0]);
44 return -1;
45 }
46
47 gst_init (&argc, &argv);
48
49 if (gst_uri_is_valid (argv[1]))
50 uri = g_strdup (argv[1]);
51 else
52 uri = gst_filename_to_uri (argv[1], NULL);
53
54 g_print ("uri: %s\n", uri);
55
56 playbin = gst_element_factory_make ("playbin", NULL);
57 g_object_set (playbin, "uri", uri, NULL);
58
59 #if 0
60 {
61 GstElement *src;
62
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);
68 }
69 #endif
70
71 gst_element_set_state (playbin, GST_STATE_PAUSED);
72
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);
76
77 g_assert (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_ERROR);
78 prerolled = TRUE;
79
80 gst_message_unref (msg);
81
82 if (!gst_element_query_duration (playbin, GST_FORMAT_TIME, &dur))
83 g_error ("Failed to query duration!\n");
84
85 g_print ("Duration: %" GST_TIME_FORMAT "\n", GST_TIME_ARGS (dur));
86
87 start = 0;
88 do {
89 GstSeekFlags seek_flags;
90 gboolean ret;
91 gboolean segment_done = FALSE;
92
93 seek_flags = GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_SEGMENT;
94
95 if (start == 0) {
96 prerolled = FALSE;
97 seek_flags |= GST_SEEK_FLAG_FLUSH;
98 }
99
100 stop = start + SEGMENT_DURATION;
101
102 g_print ("Segment: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "\n",
103 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
104
105 ret = gst_element_seek (playbin, 1.0, GST_FORMAT_TIME, seek_flags,
106 GST_SEEK_TYPE_SET, start, GST_SEEK_TYPE_SET, stop);
107
108 g_assert (ret);
109
110 if (!prerolled) {
111 while (!prerolled) {
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);
116
117 g_assert (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_ERROR);
118 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_SEGMENT_DONE) {
119 segment_done = TRUE;
120 } else {
121 prerolled = TRUE;
122 }
123 gst_message_unref (msg);
124 }
125
126 gst_element_set_state (playbin, GST_STATE_PLAYING);
127 }
128
129 /* wait for end of segment if we didn't get it above already */
130 if (!segment_done) {
131 msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (playbin),
132 GST_CLOCK_TIME_NONE, GST_MESSAGE_SEGMENT_DONE | GST_MESSAGE_ERROR);
133
134 g_assert (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_ERROR);
135
136 gst_message_unref (msg);
137 }
138
139 start = stop;
140 }
141 while (start < dur);
142
143 gst_element_set_state (playbin, GST_STATE_NULL);
144 gst_object_unref (playbin);
145 return 0;
146 }
147