]> code.delx.au - pulseaudio/blob - src/tests/interpol-test.c
tests: More useful output of make check
[pulseaudio] / 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 <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
41 static pa_context *context = NULL;
42 static pa_stream *stream = NULL;
43 static pa_mainloop_api *mainloop_api = NULL;
44 static pa_bool_t playback = TRUE;
45 static pa_usec_t latency = 0;
46
47 static void stream_write_cb(pa_stream *p, size_t nbytes, void *userdata) {
48 /* Just some silence */
49
50 for (;;) {
51 void *data;
52
53 pa_assert_se((nbytes = pa_stream_writable_size(p)) != (size_t) -1);
54
55 if (nbytes <= 0)
56 break;
57
58 pa_assert_se(pa_stream_begin_write(p, &data, &nbytes) == 0);
59 pa_memzero(data, nbytes);
60 pa_assert_se(pa_stream_write(p, data, nbytes, NULL, 0, PA_SEEK_RELATIVE) == 0);
61 }
62 }
63
64 static void stream_read_cb(pa_stream *p, size_t nbytes, void *userdata) {
65 /* We don't care about the data, just drop it */
66
67 for (;;) {
68 const void *data;
69
70 pa_assert_se((nbytes = pa_stream_readable_size(p)) != (size_t) -1);
71
72 if (nbytes <= 0)
73 break;
74
75 pa_assert_se(pa_stream_peek(p, &data, &nbytes) == 0);
76 pa_assert_se(pa_stream_drop(p) == 0);
77 }
78 }
79
80 static void stream_latency_cb(pa_stream *p, void *userdata) {
81 #ifndef INTERPOLATE
82 pa_operation *o;
83
84 o = pa_stream_update_timing_info(p, NULL, NULL);
85 pa_operation_unref(o);
86 #endif
87 }
88
89 /* This is called whenever the context status changes */
90 static void context_state_callback(pa_context *c, void *userdata) {
91 pa_assert(c);
92
93 switch (pa_context_get_state(c)) {
94 case PA_CONTEXT_CONNECTING:
95 case PA_CONTEXT_AUTHORIZING:
96 case PA_CONTEXT_SETTING_NAME:
97 break;
98
99 case PA_CONTEXT_READY: {
100 pa_stream_flags_t flags = PA_STREAM_AUTO_TIMING_UPDATE;
101 pa_buffer_attr attr;
102 static const pa_sample_spec ss = {
103 .format = PA_SAMPLE_S16LE,
104 .rate = 44100,
105 .channels = 2
106 };
107
108 pa_zero(attr);
109 attr.maxlength = (uint32_t) -1;
110 attr.tlength = latency > 0 ? (uint32_t) pa_usec_to_bytes(latency, &ss) : (uint32_t) -1;
111 attr.prebuf = (uint32_t) -1;
112 attr.minreq = (uint32_t) -1;
113 attr.fragsize = (uint32_t) -1;
114
115 #ifdef INTERPOLATE
116 flags |= PA_STREAM_INTERPOLATE_TIMING;
117 #endif
118
119 if (latency > 0)
120 flags |= PA_STREAM_ADJUST_LATENCY;
121
122 pa_log("Connection established");
123
124 pa_assert_se(stream = pa_stream_new(c, "interpol-test", &ss, NULL));
125
126 if (playback) {
127 pa_assert_se(pa_stream_connect_playback(stream, NULL, &attr, flags, NULL, NULL) == 0);
128 pa_stream_set_write_callback(stream, stream_write_cb, NULL);
129 } else {
130 pa_assert_se(pa_stream_connect_record(stream, NULL, &attr, flags) == 0);
131 pa_stream_set_read_callback(stream, stream_read_cb, NULL);
132 }
133
134 pa_stream_set_latency_update_callback(stream, stream_latency_cb, NULL);
135
136 break;
137 }
138
139 case PA_CONTEXT_TERMINATED:
140 break;
141
142 case PA_CONTEXT_FAILED:
143 default:
144 pa_log_error("Context error: %s", pa_strerror(pa_context_errno(c)));
145 abort();
146 }
147 }
148
149 int main(int argc, char *argv[]) {
150 pa_threaded_mainloop* m = NULL;
151 int k;
152 struct timeval start, last_info = { 0, 0 };
153 pa_usec_t old_t = 0, old_rtc = 0;
154 #ifdef CORK
155 pa_bool_t corked = FALSE;
156 #endif
157
158 if (!getenv("MAKE_CHECK"))
159 pa_log_set_level(PA_LOG_DEBUG);
160
161 playback = argc <= 1 || !pa_streq(argv[1], "-r");
162 latency = (argc >= 2 && !pa_streq(argv[1], "-r")) ? atoi(argv[1]) : (argc >= 3 ? atoi(argv[2]) : 0);
163
164 /* Set up a new main loop */
165 pa_assert_se(m = pa_threaded_mainloop_new());
166 pa_assert_se(mainloop_api = pa_threaded_mainloop_get_api(m));
167 pa_assert_se(context = pa_context_new(mainloop_api, argv[0]));
168
169 pa_context_set_state_callback(context, context_state_callback, NULL);
170
171 pa_assert_se(pa_context_connect(context, NULL, 0, NULL) >= 0);
172
173 pa_gettimeofday(&start);
174
175 pa_assert_se(pa_threaded_mainloop_start(m) >= 0);
176
177 /* #ifdef CORK */
178 for (k = 0; k < 20000; k++)
179 /* #else */
180 /* for (k = 0; k < 2000; k++) */
181 /* #endif */
182 {
183 pa_bool_t success = FALSE, changed = FALSE;
184 pa_usec_t t, rtc, d;
185 struct timeval now, tv;
186 pa_bool_t playing = FALSE;
187
188 pa_threaded_mainloop_lock(m);
189
190 if (stream) {
191 const pa_timing_info *info;
192
193 if (pa_stream_get_time(stream, &t) >= 0 &&
194 pa_stream_get_latency(stream, &d, NULL) >= 0)
195 success = TRUE;
196
197 if ((info = pa_stream_get_timing_info(stream))) {
198 if (memcmp(&last_info, &info->timestamp, sizeof(struct timeval))) {
199 changed = TRUE;
200 last_info = info->timestamp;
201 }
202 if (info->playing)
203 playing = TRUE;
204 }
205 }
206
207 pa_threaded_mainloop_unlock(m);
208
209 pa_gettimeofday(&now);
210
211 if (success) {
212 #ifdef CORK
213 pa_bool_t cork_now;
214 #endif
215 rtc = pa_timeval_diff(&now, &start);
216 pa_log_info("%i\t%llu\t%llu\t%llu\t%llu\t%lli\t%u\t%u\t%llu\t%llu\n", k,
217 (unsigned long long) rtc,
218 (unsigned long long) t,
219 (unsigned long long) (rtc-old_rtc),
220 (unsigned long long) (t-old_t),
221 (signed long long) rtc - (signed long long) t,
222 changed,
223 playing,
224 (unsigned long long) latency,
225 (unsigned long long) d);
226
227 fflush(stdout);
228 old_t = t;
229 old_rtc = rtc;
230
231 #ifdef CORK
232 cork_now = (rtc / (2*PA_USEC_PER_SEC)) % 2 == 1;
233
234 if (corked != cork_now) {
235 pa_threaded_mainloop_lock(m);
236 pa_operation_unref(pa_stream_cork(stream, cork_now, NULL, NULL));
237 pa_threaded_mainloop_unlock(m);
238
239 pa_log(cork_now ? "Corking" : "Uncorking");
240
241 corked = cork_now;
242 }
243 #endif
244 }
245
246 /* Spin loop, ugly but normal usleep() is just too badly grained */
247 tv = now;
248 while (pa_timeval_diff(pa_gettimeofday(&now), &tv) < 1000)
249 pa_thread_yield();
250 }
251
252 if (m)
253 pa_threaded_mainloop_stop(m);
254
255 if (stream) {
256 pa_stream_disconnect(stream);
257 pa_stream_unref(stream);
258 }
259
260 if (context) {
261 pa_context_disconnect(context);
262 pa_context_unref(context);
263 }
264
265 if (m)
266 pa_threaded_mainloop_free(m);
267
268 return 0;
269 }