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