1 /* GStreamer
2 *
3 * unit test for audiotestsrc basetime handling
4 *
5 * Copyright (C) 2009 Maemo Multimedia <multimedia at maemo dot org>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #ifdef HAVE_VALGRIND
28 #include <valgrind/valgrind.h>
29 #endif
30
31 #include <gst/check/gstcheck.h>
32
33 #ifndef GST_DISABLE_PARSE
34
35 static GstClockTime old_ts = GST_CLOCK_TIME_NONE;
36
37 static gboolean
break_mainloop(gpointer data)38 break_mainloop (gpointer data)
39 {
40 GMainLoop *loop;
41
42 loop = (GMainLoop *) data;
43 g_main_loop_quit (loop);
44
45 return FALSE;
46 }
47
48 static GstPadProbeReturn
buffer_probe_cb(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)49 buffer_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
50 {
51 GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER (info);
52 GstClockTime new_ts = GST_BUFFER_TIMESTAMP (buffer);
53
54 GST_LOG ("ts = %" GST_TIME_FORMAT, GST_TIME_ARGS (new_ts));
55 if (old_ts != GST_CLOCK_TIME_NONE) {
56 fail_unless (new_ts != old_ts,
57 "Two buffers had same timestamp: %" GST_TIME_FORMAT,
58 GST_TIME_ARGS (old_ts));
59 }
60 old_ts = new_ts;
61
62 return GST_PAD_PROBE_OK;
63 }
64
GST_START_TEST(test_basetime_calculation)65 GST_START_TEST (test_basetime_calculation)
66 {
67 GstElement *p1, *bin;
68 GstElement *asrc, *asink;
69 GstPad *pad;
70 GMainLoop *loop;
71
72 loop = g_main_loop_new (NULL, FALSE);
73
74 /* The "main" pipeline */
75 p1 = gst_parse_launch ("fakesrc ! identity sleep-time=1 ! fakesink", NULL);
76 fail_if (p1 == NULL);
77
78 /* Create a sub-bin that is activated only in "certain situations" */
79 asrc = gst_element_factory_make ("audiotestsrc", NULL);
80 if (asrc == NULL) {
81 GST_WARNING ("Cannot run test. 'audiotestsrc' not available");
82 gst_element_set_state (p1, GST_STATE_NULL);
83 gst_object_unref (p1);
84 return;
85 }
86 asink = gst_element_factory_make ("fakesink", NULL);
87
88 bin = gst_bin_new ("audiobin");
89 gst_bin_add_many (GST_BIN (bin), asrc, asink, NULL);
90 gst_element_link (asrc, asink);
91
92 gst_bin_add (GST_BIN (p1), bin);
93 gst_element_set_state (p1, GST_STATE_READY);
94
95 pad = gst_element_get_static_pad (asink, "sink");
96 fail_unless (pad != NULL, "Could not get pad out of sink");
97
98 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BUFFER, buffer_probe_cb, NULL,
99 NULL);
100 gst_element_set_locked_state (bin, TRUE);
101
102 /* Run main pipeline first */
103 gst_element_set_state (p1, GST_STATE_PLAYING);
104 g_timeout_add_seconds (2, break_mainloop, loop);
105 g_main_loop_run (loop);
106
107 /* Now activate the audio pipeline */
108 gst_element_set_locked_state (bin, FALSE);
109 gst_element_set_state (p1, GST_STATE_PAUSED);
110
111 /* Normally our custom audiobin would send this message */
112 gst_element_post_message (asrc,
113 gst_message_new_clock_provide (GST_OBJECT (asrc), NULL, TRUE));
114
115 /* At this point a new clock is selected */
116 gst_element_set_state (p1, GST_STATE_PLAYING);
117
118 g_timeout_add_seconds (2, break_mainloop, loop);
119 g_main_loop_run (loop);
120
121 gst_object_unref (pad);
122 gst_element_set_state (p1, GST_STATE_NULL);
123 gst_object_unref (p1);
124
125 g_main_loop_unref (loop);
126 }
127
128 GST_END_TEST;
129
130 #endif /* #ifndef GST_DISABLE_PARSE */
131
132 static Suite *
baseaudiosrc_suite(void)133 baseaudiosrc_suite (void)
134 {
135 Suite *s = suite_create ("baseaudiosrc");
136 TCase *tc_chain = tcase_create ("general");
137 guint timeout;
138
139 /* timeout 6 sec */
140 timeout = 6;
141
142 #ifdef HAVE_VALGRIND
143 {
144 if (RUNNING_ON_VALGRIND)
145 timeout *= 4;
146 }
147 #endif
148
149 tcase_set_timeout (tc_chain, timeout);
150 suite_add_tcase (s, tc_chain);
151
152 #ifndef GST_DISABLE_PARSE
153 tcase_add_test (tc_chain, test_basetime_calculation);
154 #endif
155
156 return s;
157 }
158
159 GST_CHECK_MAIN (baseaudiosrc);
160