1 /*
2  * audio-trickplay.c
3  *
4  * Builds a pipeline with two audiotestsources mixed with adder. Assigns
5  * controller patterns to the audio generators and test various trick modes.
6  *
7  * There are currently several issues:
8  * - adder only work with flushing seeks
9  * - there is a gap of almost 4 seconds before backwards playback
10  *   - it is "waiting for free space"
11  *   - using sync=false on the sink does not help (but has some other weird effects)
12  *   - using fakesink shows same behaviour
13  *
14  * GST_DEBUG_NO_COLOR=1 GST_DEBUG="*:2,default:3,*sink*:4,*ring*:4,*pulse*:5" ./audio-trickplay 2>log.txt
15  * GST_DEBUG_NO_COLOR=1 GST_DEBUG="*:2,default:3,*sink*:4,*ring*:4,*pulse*:5" ./audio-trickplay -a -f 2>log-af.txt
16  */
17 
18 #include <string.h>
19 #include <gst/gst.h>
20 #include <gst/controller/gstinterpolationcontrolsource.h>
21 #include <gst/controller/gstdirectcontrolbinding.h>
22 
23 static void
check_position(GstElement * elem,GstQuery * pos,const gchar * info)24 check_position (GstElement * elem, GstQuery * pos, const gchar * info)
25 {
26   if (gst_element_query (elem, pos)) {
27     gint64 play_pos;
28     gst_query_parse_position (pos, NULL, &play_pos);
29     GST_INFO ("pos : %" GST_TIME_FORMAT " %s", GST_TIME_ARGS (play_pos), info);
30   } else {
31     GST_WARNING ("position query failed");
32   }
33 }
34 
35 static GstPadProbeReturn
print_buffer_ts(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)36 print_buffer_ts (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
37 {
38   GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER (info);
39 
40   GST_DEBUG_OBJECT (pad, "  ts: %" GST_TIME_FORMAT,
41       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
42 
43   return GST_PAD_PROBE_OK;
44 }
45 
46 gint
main(gint argc,gchar ** argv)47 main (gint argc, gchar ** argv)
48 {
49   gint res = 1;
50   GstElement *src, *mix = NULL, *sink;
51   GstElement *bin;
52   GstControlSource *cs1, *cs2;
53   GstTimedValueControlSource *tvcs;
54   GstClock *clock;
55   GstClockID clock_id;
56   GstClockReturn wait_ret;
57   GstEvent *pos_seek, *rate_seek1, *rate_seek2;
58   GstQuery *pos;
59   GstSeekFlags flags;
60   GstPad *src_pad;
61   /* options */
62   gboolean use_adder = FALSE;
63   gboolean use_flush = FALSE;
64   gboolean be_quiet = FALSE;
65 
66   gst_init (&argc, &argv);
67 
68   if (argc) {
69     gint arg;
70     for (arg = 0; arg < argc; arg++) {
71       if (!strcmp (argv[arg], "-a"))
72         use_adder = TRUE;
73       else if (!strcmp (argv[arg], "-f"))
74         use_flush = TRUE;
75       else if (!strcmp (argv[arg], "-q"))
76         be_quiet = TRUE;
77     }
78   }
79 
80   /* build pipeline */
81   bin = gst_pipeline_new ("pipeline");
82   clock = gst_pipeline_get_clock (GST_PIPELINE (bin));
83   src = gst_element_factory_make ("audiotestsrc", NULL);
84   if (!src) {
85     GST_WARNING ("need audiotestsrc from gst-plugins-base");
86     goto Error;
87   }
88   if (use_adder) {
89     mix = gst_element_factory_make ("adder", NULL);
90     if (!mix) {
91       GST_WARNING ("need adder from gst-plugins-base");
92       goto Error;
93     }
94   }
95   sink = gst_element_factory_make ((be_quiet ? "fakesink" : "autoaudiosink"),
96       NULL);
97   if (!sink) {
98     GST_WARNING ("need autoaudiosink from gst-plugins-base");
99     goto Error;
100   }
101 
102   if (use_adder) {
103     gst_bin_add_many (GST_BIN (bin), src, mix, sink, NULL);
104     if (!gst_element_link_many (src, mix, sink, NULL)) {
105       GST_WARNING ("can't link elements");
106       goto Error;
107     }
108   } else {
109     gst_bin_add_many (GST_BIN (bin), src, sink, NULL);
110     if (!gst_element_link_many (src, sink, NULL)) {
111       GST_WARNING ("can't link elements");
112       goto Error;
113     }
114   }
115 
116   /* use 10 buffers per second */
117   g_object_set (src, "samplesperbuffer", 44100 / 10, NULL);
118 
119   if (be_quiet) {
120     g_object_set (sink, "sync", TRUE, NULL);
121   }
122 
123   src_pad = gst_element_get_static_pad (src, "src");
124   gst_pad_add_probe (src_pad, GST_PAD_PROBE_TYPE_BUFFER, print_buffer_ts, NULL,
125       NULL);
126   gst_object_unref (src_pad);
127 
128   cs1 = gst_interpolation_control_source_new ();
129   cs2 = gst_interpolation_control_source_new ();
130 
131   gst_object_add_control_binding (GST_OBJECT_CAST (src),
132       gst_direct_control_binding_new (GST_OBJECT_CAST (src), "volume", cs1));
133   gst_object_add_control_binding (GST_OBJECT_CAST (src),
134       gst_direct_control_binding_new (GST_OBJECT_CAST (src), "freq", cs2));
135 
136   /* Set interpolation mode */
137 
138   g_object_set (cs1, "mode", GST_INTERPOLATION_MODE_LINEAR, NULL);
139   g_object_set (cs2, "mode", GST_INTERPOLATION_MODE_LINEAR, NULL);
140 
141   /* set control values */
142   tvcs = (GstTimedValueControlSource *) cs1;
143   gst_timed_value_control_source_set (tvcs, 0 * GST_SECOND, 0.0);
144   gst_timed_value_control_source_set (tvcs, 5 * GST_SECOND, 1.0);
145 
146   gst_object_unref (cs1);
147 
148   tvcs = (GstTimedValueControlSource *) cs2;
149   gst_timed_value_control_source_set (tvcs, 0 * GST_SECOND, 20000.0 / 220.0);
150   gst_timed_value_control_source_set (tvcs, 2 * GST_SECOND, 20000.0 / 3520.0);
151   gst_timed_value_control_source_set (tvcs, 6 * GST_SECOND, 20000.0 / 440.0);
152 
153   gst_object_unref (cs2);
154 
155   /* prepare events */
156   flags = use_flush ? GST_SEEK_FLAG_FLUSH : GST_SEEK_FLAG_NONE;
157   pos_seek = gst_event_new_seek (1.0, GST_FORMAT_TIME, flags,
158       GST_SEEK_TYPE_SET, 3 * GST_SECOND,
159       GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE);
160   rate_seek1 = gst_event_new_seek (0.5, GST_FORMAT_TIME, flags,
161       GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE,
162       GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE);
163   rate_seek2 = gst_event_new_seek (-1.0, GST_FORMAT_TIME, flags,
164       GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE,
165       GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE);
166 
167   /* prepare queries */
168   pos = gst_query_new_position (GST_FORMAT_TIME);
169 
170 
171   /* run the show */
172   if (gst_element_set_state (bin, GST_STATE_PAUSED) != GST_STATE_CHANGE_FAILURE) {
173 
174     /* run for 5 seconds */
175     clock_id =
176         gst_clock_new_single_shot_id (clock,
177         gst_clock_get_time (clock) + (5 * GST_SECOND));
178 
179     if (gst_element_set_state (bin,
180             GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE) {
181       check_position (bin, pos, "start");
182       if ((wait_ret = gst_clock_id_wait (clock_id, NULL)) != GST_CLOCK_OK) {
183         GST_WARNING ("clock_id_wait returned: %d", wait_ret);
184       }
185     }
186     gst_clock_id_unref (clock_id);
187 
188     check_position (bin, pos, "before seek to new pos");
189 
190     /* seek to 3:00 sec (back 2 sec) */
191     if (!gst_element_send_event (sink, pos_seek)) {
192       GST_WARNING ("element failed to seek to new position");
193     }
194 
195     check_position (bin, pos, "after seek to new pos");
196 
197     /* run for 2 seconds */
198     clock_id =
199         gst_clock_new_single_shot_id (clock,
200         gst_clock_get_time (clock) + (2 * GST_SECOND));
201     if ((wait_ret = gst_clock_id_wait (clock_id, NULL)) != GST_CLOCK_OK) {
202       GST_WARNING ("clock_id_wait returned: %d", wait_ret);
203     }
204     gst_clock_id_unref (clock_id);
205 
206     check_position (bin, pos, "before slow down rate change");
207 
208     /* change playback rate to 0.5 */
209     if (!gst_element_send_event (sink, rate_seek1)) {
210       GST_WARNING ("element failed to change playback rate");
211     }
212 
213     check_position (bin, pos, "after slow down rate change");
214 
215     /* run for 4 seconds */
216     clock_id =
217         gst_clock_new_single_shot_id (clock,
218         gst_clock_get_time (clock) + (4 * GST_SECOND));
219     if ((wait_ret = gst_clock_id_wait (clock_id, NULL)) != GST_CLOCK_OK) {
220       GST_WARNING ("clock_id_wait returned: %d", wait_ret);
221     }
222     gst_clock_id_unref (clock_id);
223 
224     check_position (bin, pos, "before reverse rate change");
225 
226     /* change playback rate to -1.0  */
227     if (!gst_element_send_event (sink, rate_seek2)) {
228       GST_WARNING ("element failed to change playback rate");
229     }
230 
231     check_position (bin, pos, "after reverse rate change");
232 
233     /* run for 7 seconds */
234     clock_id =
235         gst_clock_new_single_shot_id (clock,
236         gst_clock_get_time (clock) + (7 * GST_SECOND));
237     if ((wait_ret = gst_clock_id_wait (clock_id, NULL)) != GST_CLOCK_OK) {
238       GST_WARNING ("clock_id_wait returned: %d", wait_ret);
239     }
240     gst_clock_id_unref (clock_id);
241 
242     check_position (bin, pos, "done");
243 
244     gst_element_set_state (bin, GST_STATE_NULL);
245   }
246 
247   /* cleanup */
248   gst_query_unref (pos);
249   gst_object_unref (clock);
250   gst_object_unref (bin);
251   res = 0;
252 Error:
253   return (res);
254 }
255