sink, source: Really set the fixed latency in set_fixed_latency_within_thread(),...
[platform/upstream/pulseaudio.git] / src / tests / interpol-test.c
1 /***
2   This file is part of PulseAudio.
3
4   PulseAudio is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as published
6   by the Free Software Foundation; either version 2.1 of the License,
7   or (at your option) any later version.
8
9   PulseAudio is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with PulseAudio; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <signal.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include <check.h>
32
33 #include <pulse/pulseaudio.h>
34 #include <pulse/mainloop.h>
35
36 #include <pulsecore/log.h>
37 #include <pulsecore/macro.h>
38 #include <pulsecore/thread.h>
39
40 #define INTERPOLATE
41 //#define CORK
42
43 static pa_context *context = NULL;
44 static pa_stream *stream = NULL;
45 static pa_mainloop_api *mainloop_api = NULL;
46 static pa_bool_t playback = TRUE;
47 static pa_usec_t latency = 0;
48 static const char *bname = NULL;
49
50 static void stream_write_cb(pa_stream *p, size_t nbytes, void *userdata) {
51     /* Just some silence */
52
53     for (;;) {
54         void *data;
55
56         fail_unless((nbytes = pa_stream_writable_size(p)) != (size_t) -1);
57
58         if (nbytes <= 0)
59             break;
60
61         fail_unless(pa_stream_begin_write(p, &data, &nbytes) == 0);
62         pa_memzero(data, nbytes);
63         fail_unless(pa_stream_write(p, data, nbytes, NULL, 0, PA_SEEK_RELATIVE) == 0);
64     }
65 }
66
67 static void stream_read_cb(pa_stream *p, size_t nbytes, void *userdata) {
68     /* We don't care about the data, just drop it */
69
70     for (;;) {
71         const void *data;
72
73         pa_assert_se((nbytes = pa_stream_readable_size(p)) != (size_t) -1);
74
75         if (nbytes <= 0)
76             break;
77
78         fail_unless(pa_stream_peek(p, &data, &nbytes) == 0);
79         fail_unless(pa_stream_drop(p) == 0);
80     }
81 }
82
83 static void stream_latency_cb(pa_stream *p, void *userdata) {
84 #ifndef INTERPOLATE
85     pa_operation *o;
86
87     o = pa_stream_update_timing_info(p, NULL, NULL);
88     pa_operation_unref(o);
89 #endif
90 }
91
92 /* This is called whenever the context status changes */
93 static void context_state_callback(pa_context *c, void *userdata) {
94     fail_unless(c != NULL);
95
96     switch (pa_context_get_state(c)) {
97         case PA_CONTEXT_CONNECTING:
98         case PA_CONTEXT_AUTHORIZING:
99         case PA_CONTEXT_SETTING_NAME:
100             break;
101
102         case PA_CONTEXT_READY: {
103             pa_stream_flags_t flags = PA_STREAM_AUTO_TIMING_UPDATE;
104             pa_buffer_attr attr;
105             static const pa_sample_spec ss = {
106                 .format = PA_SAMPLE_S16LE,
107                 .rate = 44100,
108                 .channels = 2
109             };
110
111             pa_zero(attr);
112             attr.maxlength = (uint32_t) -1;
113             attr.tlength = latency > 0 ? (uint32_t) pa_usec_to_bytes(latency, &ss) : (uint32_t) -1;
114             attr.prebuf = (uint32_t) -1;
115             attr.minreq = (uint32_t) -1;
116             attr.fragsize = (uint32_t) -1;
117
118 #ifdef INTERPOLATE
119             flags |= PA_STREAM_INTERPOLATE_TIMING;
120 #endif
121
122             if (latency > 0)
123                 flags |= PA_STREAM_ADJUST_LATENCY;
124
125             pa_log("Connection established");
126
127             stream = pa_stream_new(c, "interpol-test", &ss, NULL);
128             fail_unless(stream != NULL);
129
130             if (playback) {
131                 pa_assert_se(pa_stream_connect_playback(stream, NULL, &attr, flags, NULL, NULL) == 0);
132                 pa_stream_set_write_callback(stream, stream_write_cb, NULL);
133             } else {
134                 pa_assert_se(pa_stream_connect_record(stream, NULL, &attr, flags) == 0);
135                 pa_stream_set_read_callback(stream, stream_read_cb, NULL);
136             }
137
138             pa_stream_set_latency_update_callback(stream, stream_latency_cb, NULL);
139
140             break;
141         }
142
143         case PA_CONTEXT_TERMINATED:
144             break;
145
146         case PA_CONTEXT_FAILED:
147         default:
148             pa_log_error("Context error: %s", pa_strerror(pa_context_errno(c)));
149             fail();
150     }
151 }
152
153 START_TEST (interpol_test) {
154     pa_threaded_mainloop* m = NULL;
155     int k;
156     struct timeval start, last_info = { 0, 0 };
157     pa_usec_t old_t = 0, old_rtc = 0;
158 #ifdef CORK
159     pa_bool_t corked = FALSE;
160 #endif
161
162     /* Set up a new main loop */
163     m = pa_threaded_mainloop_new();
164     fail_unless(m != NULL);
165     mainloop_api = pa_threaded_mainloop_get_api(m);
166     fail_unless(mainloop_api != NULL);
167     context = pa_context_new(mainloop_api, bname);
168     fail_unless(context != NULL);
169
170     pa_context_set_state_callback(context, context_state_callback, NULL);
171
172     fail_unless(pa_context_connect(context, NULL, 0, NULL) >= 0);
173
174     pa_gettimeofday(&start);
175
176     fail_unless(pa_threaded_mainloop_start(m) >= 0);
177
178 /* #ifdef CORK */
179     for (k = 0; k < 20000; k++)
180 /* #else */
181 /*     for (k = 0; k < 2000; k++) */
182 /* #endif */
183     {
184         pa_bool_t success = FALSE, changed = FALSE;
185         pa_usec_t t, rtc, d;
186         struct timeval now, tv;
187         pa_bool_t playing = FALSE;
188
189         pa_threaded_mainloop_lock(m);
190
191         if (stream) {
192             const pa_timing_info *info;
193
194             if (pa_stream_get_time(stream, &t) >= 0 &&
195                 pa_stream_get_latency(stream, &d, NULL) >= 0)
196                 success = TRUE;
197
198             if ((info = pa_stream_get_timing_info(stream))) {
199                 if (memcmp(&last_info, &info->timestamp, sizeof(struct timeval))) {
200                     changed = TRUE;
201                     last_info = info->timestamp;
202                 }
203                 if (info->playing)
204                     playing = TRUE;
205             }
206         }
207
208         pa_threaded_mainloop_unlock(m);
209
210         pa_gettimeofday(&now);
211
212         if (success) {
213 #ifdef CORK
214             pa_bool_t cork_now;
215 #endif
216             rtc = pa_timeval_diff(&now, &start);
217             pa_log_info("%i\t%llu\t%llu\t%llu\t%llu\t%lli\t%u\t%u\t%llu\t%llu\n", k,
218                    (unsigned long long) rtc,
219                    (unsigned long long) t,
220                    (unsigned long long) (rtc-old_rtc),
221                    (unsigned long long) (t-old_t),
222                    (signed long long) rtc - (signed long long) t,
223                    changed,
224                    playing,
225                    (unsigned long long) latency,
226                    (unsigned long long) d);
227
228             fflush(stdout);
229             old_t = t;
230             old_rtc = rtc;
231
232 #ifdef CORK
233             cork_now = (rtc / (2*PA_USEC_PER_SEC)) % 2 == 1;
234
235             if (corked != cork_now) {
236                 pa_threaded_mainloop_lock(m);
237                 pa_operation_unref(pa_stream_cork(stream, cork_now, NULL, NULL));
238                 pa_threaded_mainloop_unlock(m);
239
240                 pa_log(cork_now ? "Corking" : "Uncorking");
241
242                 corked = cork_now;
243             }
244 #endif
245         }
246
247         /* Spin loop, ugly but normal usleep() is just too badly grained */
248         tv = now;
249         while (pa_timeval_diff(pa_gettimeofday(&now), &tv) < 1000)
250             pa_thread_yield();
251     }
252
253     if (m)
254         pa_threaded_mainloop_stop(m);
255
256     if (stream) {
257         pa_stream_disconnect(stream);
258         pa_stream_unref(stream);
259     }
260
261     if (context) {
262         pa_context_disconnect(context);
263         pa_context_unref(context);
264     }
265
266     if (m)
267         pa_threaded_mainloop_free(m);
268 }
269 END_TEST
270
271 int main(int argc, char *argv[]) {
272     int failed = 0;
273     Suite *s;
274     TCase *tc;
275     SRunner *sr;
276
277     if (!getenv("MAKE_CHECK"))
278         pa_log_set_level(PA_LOG_DEBUG);
279
280     bname = argv[0];
281     playback = argc <= 1 || !pa_streq(argv[1], "-r");
282     latency = (argc >= 2 && !pa_streq(argv[1], "-r")) ? atoi(argv[1]) : (argc >= 3 ? atoi(argv[2]) : 0);
283
284     s = suite_create("Interpol");
285     tc = tcase_create("interpol");
286     tcase_add_test(tc, interpol_test);
287     tcase_set_timeout(tc, 5 * 60);
288     suite_add_tcase(s, tc);
289
290     sr = srunner_create(s);
291     srunner_run_all(sr, CK_NORMAL);
292     failed = srunner_ntests_failed(sr);
293     srunner_free(sr);
294
295     return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
296 }