default: esd is obsolete, hence don't load it anymore by default
[pulseaudio-mirror.git] / src / tests / interpol-test.c
blobffe4ab38db2587a849c51ebcc1abf49f6221641c
1 /***
2 This file is part of PulseAudio.
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.
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.
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 ***/
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #include <signal.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <assert.h>
29 #include <stdio.h>
30 #include <stdlib.h>
32 #include <pulse/pulseaudio.h>
33 #include <pulse/mainloop.h>
35 #include <pulsecore/thread.h>
37 #define INTERPOLATE
38 //#define CORK
40 static pa_context *context = NULL;
41 static pa_stream *stream = NULL;
42 static pa_mainloop_api *mainloop_api = NULL;
43 static pa_bool_t playback = TRUE;
44 static pa_usec_t latency = 0;
46 static void stream_write_cb(pa_stream *p, size_t nbytes, void *userdata) {
47 /* Just some silence */
49 for (;;) {
50 void *data;
52 pa_assert_se((nbytes = pa_stream_writable_size(p)) != (size_t) -1);
54 if (nbytes <= 0)
55 break;
57 pa_assert_se(pa_stream_begin_write(p, &data, &nbytes) == 0);
58 pa_memzero(data, nbytes);
59 pa_assert_se(pa_stream_write(p, data, nbytes, NULL, 0, PA_SEEK_RELATIVE) == 0);
63 static void stream_read_cb(pa_stream *p, size_t nbytes, void *userdata) {
64 /* We don't care about the data, just drop it */
66 for (;;) {
67 const void *data;
69 pa_assert_se((nbytes = pa_stream_readable_size(p)) != (size_t) -1);
71 if (nbytes <= 0)
72 break;
74 pa_assert_se(pa_stream_peek(p, &data, &nbytes) == 0);
75 pa_assert_se(pa_stream_drop(p) == 0);
79 static void stream_latency_cb(pa_stream *p, void *userdata) {
80 #ifndef INTERPOLATE
81 pa_operation *o;
83 o = pa_stream_update_timing_info(p, NULL, NULL);
84 pa_operation_unref(o);
85 #endif
88 /* This is called whenever the context status changes */
89 static void context_state_callback(pa_context *c, void *userdata) {
90 assert(c);
92 switch (pa_context_get_state(c)) {
93 case PA_CONTEXT_CONNECTING:
94 case PA_CONTEXT_AUTHORIZING:
95 case PA_CONTEXT_SETTING_NAME:
96 break;
98 case PA_CONTEXT_READY: {
99 pa_stream_flags_t flags = PA_STREAM_AUTO_TIMING_UPDATE;
100 pa_buffer_attr attr;
101 static const pa_sample_spec ss = {
102 .format = PA_SAMPLE_S16LE,
103 .rate = 44100,
104 .channels = 2
107 pa_zero(attr);
108 attr.maxlength = (uint32_t) -1;
109 attr.tlength = latency > 0 ? (uint32_t) pa_usec_to_bytes(latency, &ss) : (uint32_t) -1;
110 attr.prebuf = (uint32_t) -1;
111 attr.minreq = (uint32_t) -1;
112 attr.fragsize = (uint32_t) -1;
114 #ifdef INTERPOLATE
115 flags |= PA_STREAM_INTERPOLATE_TIMING;
116 #endif
118 if (latency > 0)
119 flags |= PA_STREAM_ADJUST_LATENCY;
121 fprintf(stderr, "Connection established.\n");
123 pa_assert_se(stream = pa_stream_new(c, "interpol-test", &ss, NULL));
125 if (playback) {
126 pa_assert_se(pa_stream_connect_playback(stream, NULL, &attr, flags, NULL, NULL) == 0);
127 pa_stream_set_write_callback(stream, stream_write_cb, NULL);
128 } else {
129 pa_assert_se(pa_stream_connect_record(stream, NULL, &attr, flags) == 0);
130 pa_stream_set_read_callback(stream, stream_read_cb, NULL);
133 pa_stream_set_latency_update_callback(stream, stream_latency_cb, NULL);
135 break;
138 case PA_CONTEXT_TERMINATED:
139 break;
141 case PA_CONTEXT_FAILED:
142 default:
143 fprintf(stderr, "Context error: %s\n", pa_strerror(pa_context_errno(c)));
144 abort();
148 int main(int argc, char *argv[]) {
149 pa_threaded_mainloop* m = NULL;
150 int k;
151 struct timeval start, last_info = { 0, 0 };
152 pa_usec_t old_t = 0, old_rtc = 0;
153 #ifdef CORK
154 pa_bool_t corked = FALSE;
155 #endif
157 pa_log_set_level(PA_LOG_DEBUG);
159 playback = argc <= 1 || !pa_streq(argv[1], "-r");
161 latency =
162 (argc >= 2 && !pa_streq(argv[1], "-r")) ? atoi(argv[1]) :
163 (argc >= 3 ? atoi(argv[2]) : 0);
165 /* Set up a new main loop */
166 pa_assert_se(m = pa_threaded_mainloop_new());
167 pa_assert_se(mainloop_api = pa_threaded_mainloop_get_api(m));
168 pa_assert_se(context = pa_context_new(mainloop_api, argv[0]));
170 pa_context_set_state_callback(context, context_state_callback, NULL);
172 pa_assert_se(pa_context_connect(context, NULL, 0, NULL) >= 0);
174 pa_gettimeofday(&start);
176 pa_assert_se(pa_threaded_mainloop_start(m) >= 0);
178 /* #ifdef CORK */
179 for (k = 0; k < 20000; k++)
180 /* #else */
181 /* for (k = 0; k < 2000; k++) */
182 /* #endif */
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;
189 pa_threaded_mainloop_lock(m);
191 if (stream) {
192 const pa_timing_info *info;
194 if (pa_stream_get_time(stream, &t) >= 0 &&
195 pa_stream_get_latency(stream, &d, NULL) >= 0)
196 success = TRUE;
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;
203 if (info->playing)
204 playing = TRUE;
208 pa_threaded_mainloop_unlock(m);
210 pa_gettimeofday(&now);
212 if (success) {
213 #ifdef CORK
214 pa_bool_t cork_now;
215 #endif
216 rtc = pa_timeval_diff(&now, &start);
217 printf("%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);
228 fflush(stdout);
229 old_t = t;
230 old_rtc = rtc;
232 #ifdef CORK
233 cork_now = (rtc / (2*PA_USEC_PER_SEC)) % 2 == 1;
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);
240 pa_log(cork_now ? "Corking" : "Uncorking");
242 corked = cork_now;
244 #endif
247 /* Spin loop, ugly but normal usleep() is just too badly grained */
249 tv = now;
250 while (pa_timeval_diff(pa_gettimeofday(&now), &tv) < 1000)
251 pa_thread_yield();
254 if (m)
255 pa_threaded_mainloop_stop(m);
257 if (stream) {
258 pa_stream_disconnect(stream);
259 pa_stream_unref(stream);
262 if (context) {
263 pa_context_disconnect(context);
264 pa_context_unref(context);
267 if (m)
268 pa_threaded_mainloop_free(m);
270 return 0;