]> code.delx.au - pulseaudio/blob - src/tests/interpol-test.c
Merge branch 'master' of ssh://rootserver/home/lennart/git/public/pulseaudio
[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 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 static pa_context *context = NULL;
40 static pa_stream *stream = NULL;
41 static pa_mainloop_api *mainloop_api = NULL;
42 static pa_bool_t playback = TRUE;
43
44 static void stream_write_cb(pa_stream *p, size_t nbytes, void *userdata) {
45 /* Just some silence */
46 pa_assert_se(pa_stream_write(p, pa_xmalloc0(nbytes), nbytes, pa_xfree, 0, PA_SEEK_RELATIVE) == 0);
47 }
48
49 static void stream_read_cb(pa_stream *p, size_t nbytes, void *userdata) {
50 /* We don't care, just drop the data */
51
52 while (pa_stream_readable_size(p) > 0) {
53 const void *d;
54 size_t b;
55
56 pa_assert_se(pa_stream_peek(p, &d, &b) == 0);
57 pa_assert_se(pa_stream_drop(p) == 0);
58 }
59 }
60
61 /* This is called whenever the context status changes */
62 static void context_state_callback(pa_context *c, void *userdata) {
63 assert(c);
64
65 switch (pa_context_get_state(c)) {
66 case PA_CONTEXT_CONNECTING:
67 case PA_CONTEXT_AUTHORIZING:
68 case PA_CONTEXT_SETTING_NAME:
69 break;
70
71 case PA_CONTEXT_READY: {
72
73 static const pa_sample_spec ss = {
74 .format = PA_SAMPLE_S16LE,
75 .rate = 44100,
76 .channels = 2
77 };
78
79 fprintf(stderr, "Connection established.\n");
80
81 stream = pa_stream_new(c, "interpol-test", &ss, NULL);
82 assert(stream);
83
84 if (playback) {
85 pa_assert_se(pa_stream_connect_playback(stream, NULL, NULL, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL) == 0);
86 pa_stream_set_write_callback(stream, stream_write_cb, NULL);
87 } else {
88 pa_assert_se(pa_stream_connect_record(stream, NULL, NULL, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE) == 0);
89 pa_stream_set_read_callback(stream, stream_read_cb, NULL);
90 }
91
92 break;
93 }
94
95 case PA_CONTEXT_TERMINATED:
96 break;
97
98 case PA_CONTEXT_FAILED:
99 default:
100 fprintf(stderr, "Context error: %s\n", pa_strerror(pa_context_errno(c)));
101 abort();
102 }
103 }
104
105 int main(int argc, char *argv[]) {
106 pa_threaded_mainloop* m = NULL;
107 int k, r;
108 struct timeval start, last_info = { 0, 0 };
109 pa_usec_t old_t = 0, old_rtc = 0;
110
111 playback = argc <= 1 || !pa_streq(argv[1], "-r");
112
113 /* Set up a new main loop */
114 m = pa_threaded_mainloop_new();
115 assert(m);
116
117 mainloop_api = pa_threaded_mainloop_get_api(m);
118
119 context = pa_context_new(mainloop_api, argv[0]);
120 assert(context);
121
122 pa_context_set_state_callback(context, context_state_callback, NULL);
123
124 r = pa_context_connect(context, NULL, 0, NULL);
125 assert(r >= 0);
126
127 pa_gettimeofday(&start);
128
129 r = pa_threaded_mainloop_start(m);
130 assert(r >= 0);
131
132 for (k = 0; k < 5000; k++) {
133 pa_bool_t success = FALSE, changed = FALSE;
134 pa_usec_t t, rtc;
135 struct timeval now, tv;
136 pa_bool_t playing = FALSE;
137
138 pa_threaded_mainloop_lock(m);
139
140 if (stream) {
141 const pa_timing_info *info;
142
143 if (pa_stream_get_time(stream, &t) >= 0)
144 success = TRUE;
145
146 if ((info = pa_stream_get_timing_info(stream))) {
147 if (memcmp(&last_info, &info->timestamp, sizeof(struct timeval))) {
148 changed = TRUE;
149 last_info = info->timestamp;
150 }
151 if (info->playing)
152 playing = TRUE;
153 }
154 }
155
156 pa_threaded_mainloop_unlock(m);
157
158 pa_gettimeofday(&now);
159
160 if (success) {
161 rtc = pa_timeval_diff(&now, &start);
162 printf("%i\t%llu\t%llu\t%llu\t%llu\t%u\t%u\n", k,
163 (unsigned long long) rtc,
164 (unsigned long long) t,
165 (unsigned long long) (rtc-old_rtc),
166 (unsigned long long) (t-old_t),
167 changed,
168 playing);
169
170 fflush(stdout);
171 old_t = t;
172 old_rtc = rtc;
173 }
174
175 /* Spin loop, ugly but normal usleep() is just too badly grained */
176
177 tv = now;
178 while (pa_timeval_diff(pa_gettimeofday(&now), &tv) < 1000)
179 pa_thread_yield();
180 }
181
182 if (m)
183 pa_threaded_mainloop_stop(m);
184
185 if (stream) {
186 pa_stream_disconnect(stream);
187 pa_stream_unref(stream);
188 }
189
190 if (context) {
191 pa_context_disconnect(context);
192 pa_context_unref(context);
193 }
194
195 if (m)
196 pa_threaded_mainloop_free(m);
197
198 return 0;
199 }