summaryrefslogtreecommitdiff
path: root/tests/icles/audio-trickplay.c
blob: a97871e6ed299e58b594cc5114662929b5743e58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * audio-trickplay.c
 *
 * Builds a pipeline with two audiotestsources mixed with adder. Assigns
 * controller patterns to the audio generators and test various trick modes.
 *
 * There are currently several issues:
 * - adder only work with flushing seeks
 * - there is a gap of almost 4 seconds before backwards playback
 *   - it is "waiting for free space"
 *   - using sync=false on the sink does not help (but has some other weird effects)
 *   - using fakesink shows same behaviour
 *
 * GST_DEBUG_NO_COLOR=1 GST_DEBUG="*:2,default:3,*sink*:4,*ring*:4,*pulse*:5" ./audio-trickplay 2>log.txt
 * GST_DEBUG_NO_COLOR=1 GST_DEBUG="*:2,default:3,*sink*:4,*ring*:4,*pulse*:5" ./audio-trickplay -a -f 2>log-af.txt
 */

#include <string.h>
#include <gst/gst.h>
#include <gst/controller/gstinterpolationcontrolsource.h>

static void
check_position (GstElement * elem, GstQuery * pos, const gchar * info)
{
  if (gst_element_query (elem, pos)) {
    gint64 play_pos;
    gst_query_parse_position (pos, NULL, &play_pos);
    GST_INFO ("pos : %" GST_TIME_FORMAT " %s", GST_TIME_ARGS (play_pos), info);
  } else {
    GST_WARNING ("position query failed");
  }
}

static GstPadProbeReturn
print_buffer_ts (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
{
  GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER (info);

  GST_DEBUG_OBJECT (pad, "  ts: %" GST_TIME_FORMAT,
      GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));

  return GST_PAD_PROBE_OK;
}

gint
main (gint argc, gchar ** argv)
{
  gint res = 1;
  GstElement *src, *mix = NULL, *sink;
  GstElement *bin;
  GstInterpolationControlSource *csource1, *csource2;
  GstTimedValueControlSource *cs;
  GstClock *clock;
  GstClockID clock_id;
  GstClockReturn wait_ret;
  GstEvent *pos_seek, *rate_seek1, *rate_seek2;
  GstQuery *pos;
  GstSeekFlags flags;
  GstPad *src_pad;
  /* options */
  gboolean use_adder = FALSE;
  gboolean use_flush = FALSE;
  gboolean be_quiet = FALSE;

  gst_init (&argc, &argv);

  if (argc) {
    gint arg;
    for (arg = 0; arg < argc; arg++) {
      if (!strcmp (argv[arg], "-a"))
        use_adder = TRUE;
      else if (!strcmp (argv[arg], "-f"))
        use_flush = TRUE;
      else if (!strcmp (argv[arg], "-q"))
        be_quiet = TRUE;
    }
  }

  /* build pipeline */
  bin = gst_pipeline_new ("pipeline");
  clock = gst_pipeline_get_clock (GST_PIPELINE (bin));
  src = gst_element_factory_make ("audiotestsrc", NULL);
  if (!src) {
    GST_WARNING ("need audiotestsrc from gst-plugins-base");
    goto Error;
  }
  if (use_adder) {
    mix = gst_element_factory_make ("adder", NULL);
    if (!mix) {
      GST_WARNING ("need adder from gst-plugins-base");
      goto Error;
    }
  }
  sink = gst_element_factory_make ((be_quiet ? "fakesink" : "autoaudiosink"),
      NULL);
  if (!sink) {
    GST_WARNING ("need autoaudiosink from gst-plugins-base");
    goto Error;
  }

  if (use_adder) {
    gst_bin_add_many (GST_BIN (bin), src, mix, sink, NULL);
    if (!gst_element_link_many (src, mix, sink, NULL)) {
      GST_WARNING ("can't link elements");
      goto Error;
    }
  } else {
    gst_bin_add_many (GST_BIN (bin), src, sink, NULL);
    if (!gst_element_link_many (src, sink, NULL)) {
      GST_WARNING ("can't link elements");
      goto Error;
    }
  }

  /* use 10 buffers per second */
  g_object_set (src, "samplesperbuffer", 44100 / 10, NULL);

  if (be_quiet) {
    g_object_set (sink, "sync", TRUE, NULL);
  }

  src_pad = gst_element_get_static_pad (src, "src");
  gst_pad_add_probe (src_pad, GST_PAD_PROBE_TYPE_BUFFER, print_buffer_ts, NULL,
      NULL);
  gst_object_unref (src_pad);

  csource1 = gst_interpolation_control_source_new ();
  csource2 = gst_interpolation_control_source_new ();

  gst_object_add_control_binding (GST_OBJECT_CAST (src),
      gst_control_binding_direct_new (GST_OBJECT_CAST (src), "volume",
          GST_CONTROL_SOURCE (csource1)));
  gst_object_add_control_binding (GST_OBJECT_CAST (src),
      gst_control_binding_direct_new (GST_OBJECT_CAST (src), "freq",
          GST_CONTROL_SOURCE (csource2)));

  /* Set interpolation mode */

  g_object_set (csource1, "mode", GST_INTERPOLATION_MODE_LINEAR, NULL);
  g_object_set (csource2, "mode", GST_INTERPOLATION_MODE_LINEAR, NULL);

  /* set control values */
  cs = (GstTimedValueControlSource *) csource1;
  gst_timed_value_control_source_set (cs, 0 * GST_SECOND, 0.0);
  gst_timed_value_control_source_set (cs, 5 * GST_SECOND, 1.0);

  gst_object_unref (csource1);

  cs = (GstTimedValueControlSource *) csource2;
  gst_timed_value_control_source_set (cs, 0 * GST_SECOND, 20000.0 / 220.0);
  gst_timed_value_control_source_set (cs, 2 * GST_SECOND, 20000.0 / 3520.0);
  gst_timed_value_control_source_set (cs, 6 * GST_SECOND, 20000.0 / 440.0);

  gst_object_unref (csource2);

  /* prepare events */
  flags = use_flush ? GST_SEEK_FLAG_FLUSH : GST_SEEK_FLAG_NONE;
  pos_seek = gst_event_new_seek (1.0, GST_FORMAT_TIME, flags,
      GST_SEEK_TYPE_SET, 3 * GST_SECOND,
      GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE);
  rate_seek1 = gst_event_new_seek (0.5, GST_FORMAT_TIME, flags,
      GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE,
      GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE);
  rate_seek2 = gst_event_new_seek (-1.0, GST_FORMAT_TIME, flags,
      GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE,
      GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE);

  /* prepare queries */
  pos = gst_query_new_position (GST_FORMAT_TIME);


  /* run the show */
  if (gst_element_set_state (bin, GST_STATE_PAUSED) != GST_STATE_CHANGE_FAILURE) {

    /* run for 5 seconds */
    clock_id =
        gst_clock_new_single_shot_id (clock,
        gst_clock_get_time (clock) + (5 * GST_SECOND));

    if (gst_element_set_state (bin,
            GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE) {
      check_position (bin, pos, "start");
      if ((wait_ret = gst_clock_id_wait (clock_id, NULL)) != GST_CLOCK_OK) {
        GST_WARNING ("clock_id_wait returned: %d", wait_ret);
      }
    }
    gst_clock_id_unref (clock_id);

    check_position (bin, pos, "before seek to new pos");

    /* seek to 3:00 sec (back 2 sec) */
    if (!gst_element_send_event (sink, pos_seek)) {
      GST_WARNING ("element failed to seek to new position");
    }

    check_position (bin, pos, "after seek to new pos");

    /* run for 2 seconds */
    clock_id =
        gst_clock_new_single_shot_id (clock,
        gst_clock_get_time (clock) + (2 * GST_SECOND));
    if ((wait_ret = gst_clock_id_wait (clock_id, NULL)) != GST_CLOCK_OK) {
      GST_WARNING ("clock_id_wait returned: %d", wait_ret);
    }
    gst_clock_id_unref (clock_id);

    check_position (bin, pos, "before slow down rate change");

    /* change playback rate to 0.5 */
    if (!gst_element_send_event (sink, rate_seek1)) {
      GST_WARNING ("element failed to change playback rate");
    }

    check_position (bin, pos, "after slow down rate change");

    /* run for 4 seconds */
    clock_id =
        gst_clock_new_single_shot_id (clock,
        gst_clock_get_time (clock) + (4 * GST_SECOND));
    if ((wait_ret = gst_clock_id_wait (clock_id, NULL)) != GST_CLOCK_OK) {
      GST_WARNING ("clock_id_wait returned: %d", wait_ret);
    }
    gst_clock_id_unref (clock_id);

    check_position (bin, pos, "before reverse rate change");

    /* change playback rate to -1.0  */
    if (!gst_element_send_event (sink, rate_seek2)) {
      GST_WARNING ("element failed to change playback rate");
    }

    check_position (bin, pos, "after reverse rate change");

    /* run for 7 seconds */
    clock_id =
        gst_clock_new_single_shot_id (clock,
        gst_clock_get_time (clock) + (7 * GST_SECOND));
    if ((wait_ret = gst_clock_id_wait (clock_id, NULL)) != GST_CLOCK_OK) {
      GST_WARNING ("clock_id_wait returned: %d", wait_ret);
    }
    gst_clock_id_unref (clock_id);

    check_position (bin, pos, "done");

    gst_element_set_state (bin, GST_STATE_NULL);
  }

  /* cleanup */
  gst_query_unref (pos);
  gst_object_unref (G_OBJECT (clock));
  gst_object_unref (G_OBJECT (bin));
  res = 0;
Error:
  return (res);
}