1 /* GStreamer interactive test for accurate 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.
19 * Based on python script by Kibeom Kim <kkb110@gmail.com>
28 #include <gst/base/base.h>
29 #include <gst/audio/audio.h>
30 #include <gst/app/app.h>
32 #define SAMPLE_FREQ 44100
35 _memmem (const guint8 * haystack, gsize hlen, const guint8 * needle, gsize nlen)
37 const guint8 *p = haystack;
44 needle_first = *(unsigned char *) needle;
46 while (plen >= nlen && (p = memchr (p, needle_first, plen - nlen + 1))) {
47 if (!memcmp (p, needle, nlen))
51 plen = hlen - (p - haystack);
58 sample_to_nanotime (guint sample)
60 return (guint64) ((1.0 * sample * GST_SECOND / SAMPLE_FREQ) + 0.5);
64 nanotime_to_sample (GstClockTime nanotime)
66 return gst_util_uint64_scale_round (nanotime, SAMPLE_FREQ, GST_SECOND);
70 generate_test_data (guint N)
72 gint16 *left, *right, *stereo;
75 /* 32767 = (2 ** 15) - 1 */
76 /* 32768 = (2 ** 15) */
77 largeN = ((N + 32767) / 32768) * 32768;
78 left = g_new0 (gint16, largeN);
79 right = g_new0 (gint16, largeN);
80 stereo = g_new0 (gint16, 2 * largeN);
82 for (i = 0; i < (largeN / 32768); ++i) {
85 for (j = i * 32768; j < ((i + 1) * 32768); ++j) {
97 /* could just fill stereo directly from the start, but keeping original code for now */
98 for (i = 0; i < largeN; ++i) {
99 stereo[(2 * i) + 0] = left[i];
100 stereo[(2 * i) + 1] = right[i];
105 return gst_buffer_new_wrapped (stereo, 2 * largeN * sizeof (gint16));
109 generate_test_sound (const gchar * fn, const gchar * launch_string,
112 GstElement *pipeline, *src, *parse, *enc_bin, *sink;
118 pipeline = gst_pipeline_new (NULL);
120 src = gst_element_factory_make ("appsrc", NULL);
122 caps = gst_caps_new_simple ("audio/x-raw",
123 "format", G_TYPE_STRING, GST_AUDIO_NE (S16),
124 "rate", G_TYPE_INT, SAMPLE_FREQ, "channels", G_TYPE_INT, 2,
125 "layout", G_TYPE_STRING, "interleaved",
126 "channel-mask", GST_TYPE_BITMASK, (guint64) 3, NULL);
127 g_object_set (src, "caps", caps, "format", GST_FORMAT_TIME, NULL);
128 gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_TIME);
129 gst_caps_unref (caps);
131 /* audioparse to put proper timestamps on buffers for us, without which
132 * vorbisenc in particular is unhappy (or oggmux, rather) */
133 parse = gst_element_factory_make ("audioparse", NULL);
135 g_object_set (parse, "use-sink-caps", TRUE, NULL);
137 parse = gst_element_factory_make ("identity", NULL);
138 g_warning ("audioparse element not available, vorbis/ogg might not work\n");
141 enc_bin = gst_parse_bin_from_description (launch_string, TRUE, NULL);
143 sink = gst_element_factory_make ("filesink", NULL);
144 g_object_set (sink, "location", fn, NULL);
146 gst_bin_add_many (GST_BIN (pipeline), src, parse, enc_bin, sink, NULL);
148 gst_element_link_many (src, parse, enc_bin, sink, NULL);
150 gst_element_set_state (pipeline, GST_STATE_PLAYING);
152 buf = generate_test_data (num_samples);
153 flow = gst_app_src_push_buffer (GST_APP_SRC (src), buf);
154 g_assert (flow == GST_FLOW_OK);
156 gst_app_src_end_of_stream (GST_APP_SRC (src));
158 /*g_print ("generating test sound %s, waiting for EOS..\n", fn); */
160 msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (pipeline),
161 GST_CLOCK_TIME_NONE, GST_MESSAGE_EOS | GST_MESSAGE_ERROR);
163 g_assert (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS);
164 gst_message_unref (msg);
166 gst_element_set_state (pipeline, GST_STATE_NULL);
167 gst_object_unref (pipeline);
169 /* g_print ("Done %s\n", fn); */
173 test_seek_FORMAT_TIME_by_sample (const gchar * fn, GList * seek_positions)
175 GstElement *pipeline, *src, *sink;
179 gconstpointer answer;
182 pipeline = gst_parse_launch ("filesrc name=src ! decodebin ! "
183 "audioconvert dithering=0 ! appsink name=sink", NULL);
185 src = gst_bin_get_by_name (GST_BIN (pipeline), "src");
186 g_object_set (src, "location", fn, NULL);
187 gst_object_unref (src);
189 sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
190 caps = gst_caps_new_simple ("audio/x-raw",
191 "format", G_TYPE_STRING, GST_AUDIO_NE (S16),
192 "rate", G_TYPE_INT, SAMPLE_FREQ, "channels", G_TYPE_INT, 2, NULL);
193 g_object_set (sink, "caps", caps, "sync", FALSE, NULL);
194 gst_caps_unref (caps);
196 gst_element_set_state (pipeline, GST_STATE_PLAYING);
198 /* wait for preroll, so we can seek */
199 gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (pipeline), GST_CLOCK_TIME_NONE,
200 GST_MESSAGE_ASYNC_DONE);
202 /* first, read entire file to end */
203 adapter = gst_adapter_new ();
204 while ((sample = gst_app_sink_pull_sample (GST_APP_SINK (sink)))) {
205 gst_adapter_push (adapter, gst_buffer_ref (gst_sample_get_buffer (sample)));
206 gst_sample_unref (sample);
208 answer_size = gst_adapter_available (adapter);
209 answer = gst_adapter_map (adapter, answer_size);
210 /* g_print ("%s: read %u bytes\n", fn, answer_size); */
212 g_print ("%10s\t%10s\t%10s\n", "requested", "sample per ts", "actual(data)");
214 while (seek_positions != NULL) {
219 guint actual_position, buffer_timestamp_position;
222 seek_sample = GPOINTER_TO_UINT (seek_positions->data);
224 ret = gst_element_seek_simple (pipeline, GST_FORMAT_TIME,
225 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE,
226 sample_to_nanotime (seek_sample));
230 sample = gst_app_sink_pull_sample (GST_APP_SINK (sink));
232 buf = gst_sample_get_buffer (sample);
233 gst_buffer_map (buf, &map, GST_MAP_READ);
234 GST_MEMDUMP ("answer", answer, answer_size);
235 GST_MEMDUMP ("buffer", map.data, map.size);
236 found = _memmem (answer, answer_size, map.data, map.size);
237 gst_buffer_unmap (buf, &map);
239 g_assert (found != NULL);
240 actual_position = ((goffset) ((guint8 *) found - (guint8 *) answer)) / 4;
241 buffer_timestamp_position = nanotime_to_sample (GST_BUFFER_PTS (buf));
242 g_print ("%10u\t%10u\t%10u\n", seek_sample, buffer_timestamp_position,
244 gst_sample_unref (sample);
246 seek_positions = seek_positions->next;
249 gst_element_set_state (pipeline, GST_STATE_NULL);
250 gst_object_unref (sink);
251 gst_object_unref (pipeline);
252 g_object_unref (adapter);
256 create_test_samples (guint from, guint to, guint step)
258 GQueue q = G_QUEUE_INIT;
261 for (i = from; i < to; i += step)
262 g_queue_push_tail (&q, GUINT_TO_POINTER (i));
270 main (int argc, char **argv)
274 gst_init (&argc, &argv);
276 test_samples = create_test_samples (SAMPLE_FREQ, SAMPLE_FREQ * 2, 5000);
278 g_print ("\nwav:\n");
279 generate_test_sound ("test.wav", "wavenc", SAMPLE_FREQ * SECS);
280 test_seek_FORMAT_TIME_by_sample ("test.wav", test_samples);
282 g_print ("\nflac:\n");
283 generate_test_sound ("test.flac", "flacenc", SAMPLE_FREQ * SECS);
284 test_seek_FORMAT_TIME_by_sample ("test.flac", test_samples);
286 g_print ("\nogg:\n");
287 generate_test_sound ("test.ogg",
288 "audioconvert dithering=0 ! vorbisenc quality=1 ! oggmux",
290 test_seek_FORMAT_TIME_by_sample ("test.ogg", test_samples);
292 g_print ("\nmp3:\n");
293 generate_test_sound ("test.mp3", "lamemp3enc bitrate=320",
295 test_seek_FORMAT_TIME_by_sample ("test.mp3", test_samples);
297 g_list_free (test_samples);