]> code.delx.au - pulseaudio/blob - src/tests/interpol-test.c
Merge commit 'origin/master-tx'
[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 <assert.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <getopt.h>
32 #include <math.h>
33
34 #include <pulse/pulseaudio.h>
35 #include <pulse/mainloop.h>
36
37 #include <pulsecore/thread.h>
38
39 #define INTERPOLATE
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
46 static void stream_write_cb(pa_stream *p, size_t nbytes, void *userdata) {
47 /* Just some silence */
48 pa_assert_se(pa_stream_write(p, pa_xmalloc0(nbytes), nbytes, pa_xfree, 0, PA_SEEK_RELATIVE) == 0);
49 }
50
51 static void stream_read_cb(pa_stream *p, size_t nbytes, void *userdata) {
52 /* We don't care, just drop the data */
53
54 while (pa_stream_readable_size(p) > 0) {
55 const void *d;
56 size_t b;
57
58 pa_assert_se(pa_stream_peek(p, &d, &b) == 0);
59 pa_assert_se(pa_stream_drop(p) == 0);
60 }
61 }
62
63 static void stream_latency_cb(pa_stream *p, void *userdata) {
64 #ifndef INTERPOLATE
65 pa_operation *o;
66
67 o = pa_stream_update_timing_info(p, NULL, NULL);
68 pa_operation_unref(o);
69 #endif
70 }
71
72 /* This is called whenever the context status changes */
73 static void context_state_callback(pa_context *c, void *userdata) {
74 assert(c);
75
76 switch (pa_context_get_state(c)) {
77 case PA_CONTEXT_CONNECTING:
78 case PA_CONTEXT_AUTHORIZING:
79 case PA_CONTEXT_SETTING_NAME:
80 break;
81
82 case PA_CONTEXT_READY: {
83 pa_stream_flags_t flags = PA_STREAM_AUTO_TIMING_UPDATE;
84
85 static const pa_sample_spec ss = {
86 .format = PA_SAMPLE_S16LE,
87 .rate = 44100,
88 .channels = 2
89 };
90
91 #ifdef INTERPOLATE
92 flags |= PA_STREAM_INTERPOLATE_TIMING;
93 #endif
94
95 fprintf(stderr, "Connection established.\n");
96
97 stream = pa_stream_new(c, "interpol-test", &ss, NULL);
98 assert(stream);
99
100 if (playback) {
101 pa_assert_se(pa_stream_connect_playback(stream, NULL, NULL, flags, NULL, NULL) == 0);
102 pa_stream_set_write_callback(stream, stream_write_cb, NULL);
103 } else {
104 pa_assert_se(pa_stream_connect_record(stream, NULL, NULL, flags) == 0);
105 pa_stream_set_read_callback(stream, stream_read_cb, NULL);
106 }
107
108 pa_stream_set_latency_update_callback(stream, stream_latency_cb, NULL);
109
110 break;
111 }
112
113 case PA_CONTEXT_TERMINATED:
114 break;
115
116 case PA_CONTEXT_FAILED:
117 default:
118 fprintf(stderr, "Context error: %s\n", pa_strerror(pa_context_errno(c)));
119 abort();
120 }
121 }
122
123 int main(int argc, char *argv[]) {
124 pa_threaded_mainloop* m = NULL;
125 int k, r;
126 struct timeval start, last_info = { 0, 0 };
127 pa_usec_t old_t = 0, old_rtc = 0;
128 pa_bool_t corked = FALSE;
129
130 pa_log_set_level(PA_LOG_DEBUG);
131
132 playback = argc <= 1 || !pa_streq(argv[1], "-r");
133
134 /* Set up a new main loop */
135 m = pa_threaded_mainloop_new();
136 assert(m);
137
138 mainloop_api = pa_threaded_mainloop_get_api(m);
139
140 context = pa_context_new(mainloop_api, argv[0]);
141 assert(context);
142
143 pa_context_set_state_callback(context, context_state_callback, NULL);
144
145 r = pa_context_connect(context, NULL, 0, NULL);
146 assert(r >= 0);
147
148 pa_gettimeofday(&start);
149
150 r = pa_threaded_mainloop_start(m);
151 assert(r >= 0);
152
153 for (k = 0; k < 20000; k++) {
154 pa_bool_t success = FALSE, changed = FALSE;
155 pa_usec_t t, rtc;
156 struct timeval now, tv;
157 pa_bool_t playing = FALSE;
158
159 pa_threaded_mainloop_lock(m);
160
161 if (stream) {
162 const pa_timing_info *info;
163
164 if (pa_stream_get_time(stream, &t) >= 0)
165 success = TRUE;
166
167 if ((info = pa_stream_get_timing_info(stream))) {
168 if (memcmp(&last_info, &info->timestamp, sizeof(struct timeval))) {
169 changed = TRUE;
170 last_info = info->timestamp;
171 }
172 if (info->playing)
173 playing = TRUE;
174 }
175 }
176
177 pa_threaded_mainloop_unlock(m);
178
179 pa_gettimeofday(&now);
180
181 if (success) {
182 pa_bool_t cork_now;
183
184 rtc = pa_timeval_diff(&now, &start);
185 printf("%i\t%llu\t%llu\t%llu\t%llu\t%lli\t%u\t%u\n", k,
186 (unsigned long long) rtc,
187 (unsigned long long) t,
188 (unsigned long long) (rtc-old_rtc),
189 (unsigned long long) (t-old_t),
190 (signed long long) rtc - (signed long long) t,
191 changed,
192 playing);
193
194 fflush(stdout);
195 old_t = t;
196 old_rtc = rtc;
197
198 cork_now = (rtc / (2*PA_USEC_PER_SEC)) % 2 == 1;
199
200 if (corked != cork_now) {
201 pa_threaded_mainloop_lock(m);
202 pa_operation_unref(pa_stream_cork(stream, cork_now, NULL, NULL));
203 pa_threaded_mainloop_unlock(m);
204
205 pa_log(cork_now ? "Corking" : "Uncorking");
206
207 corked = cork_now;
208 }
209 }
210
211 /* Spin loop, ugly but normal usleep() is just too badly grained */
212
213 tv = now;
214 while (pa_timeval_diff(pa_gettimeofday(&now), &tv) < 1000)
215 pa_thread_yield();
216 }
217
218 if (m)
219 pa_threaded_mainloop_stop(m);
220
221 if (stream) {
222 pa_stream_disconnect(stream);
223 pa_stream_unref(stream);
224 }
225
226 if (context) {
227 pa_context_disconnect(context);
228 pa_context_unref(context);
229 }
230
231 if (m)
232 pa_threaded_mainloop_free(m);
233
234 return 0;
235 }