Git init
[framework/multimedia/pulseaudio.git] / 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 //#define CORK
41
42 static pa_context *context = NULL;
43 static pa_stream *stream = NULL;
44 static pa_mainloop_api *mainloop_api = NULL;
45 static pa_bool_t playback = TRUE;
46 static pa_usec_t latency = 0;
47
48 static void stream_write_cb(pa_stream *p, size_t nbytes, void *userdata) {
49     /* Just some silence */
50
51     for (;;) {
52         void *data;
53
54         pa_assert_se((nbytes = pa_stream_writable_size(p)) != (size_t) -1);
55
56         if (nbytes <= 0)
57             break;
58
59         pa_assert_se(pa_stream_begin_write(p, &data, &nbytes) == 0);
60         pa_memzero(data, nbytes);
61         pa_assert_se(pa_stream_write(p, data, nbytes, NULL, 0, PA_SEEK_RELATIVE) == 0);
62     }
63 }
64
65 static 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         pa_assert_se(pa_stream_peek(p, &data, &nbytes) == 0);
77         pa_assert_se(pa_stream_drop(p) == 0);
78     }
79 }
80
81 static 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 */
91 static void context_state_callback(pa_context *c, void *userdata) {
92     assert(c);
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             fprintf(stderr, "Connection established.\n");
124
125             pa_assert_se(stream = pa_stream_new(c, "interpol-test", &ss, NULL));
126
127             if (playback) {
128                 pa_assert_se(pa_stream_connect_playback(stream, NULL, &attr, flags, NULL, NULL) == 0);
129                 pa_stream_set_write_callback(stream, stream_write_cb, NULL);
130             } else {
131                 pa_assert_se(pa_stream_connect_record(stream, NULL, &attr, flags) == 0);
132                 pa_stream_set_read_callback(stream, stream_read_cb, NULL);
133             }
134
135             pa_stream_set_latency_update_callback(stream, stream_latency_cb, NULL);
136
137             break;
138         }
139
140         case PA_CONTEXT_TERMINATED:
141             break;
142
143         case PA_CONTEXT_FAILED:
144         default:
145             fprintf(stderr, "Context error: %s\n", pa_strerror(pa_context_errno(c)));
146             abort();
147     }
148 }
149
150 int main(int argc, char *argv[]) {
151     pa_threaded_mainloop* m = NULL;
152     int k;
153     struct timeval start, last_info = { 0, 0 };
154     pa_usec_t old_t = 0, old_rtc = 0;
155 #ifdef CORK
156     pa_bool_t corked = FALSE;
157 #endif
158
159     pa_log_set_level(PA_LOG_DEBUG);
160
161     playback = argc <= 1 || !pa_streq(argv[1], "-r");
162
163     latency =
164         (argc >= 2 && !pa_streq(argv[1], "-r")) ? atoi(argv[1]) :
165         (argc >= 3 ? atoi(argv[2]) : 0);
166
167     /* Set up a new main loop */
168     pa_assert_se(m = pa_threaded_mainloop_new());
169     pa_assert_se(mainloop_api = pa_threaded_mainloop_get_api(m));
170     pa_assert_se(context = pa_context_new(mainloop_api, argv[0]));
171
172     pa_context_set_state_callback(context, context_state_callback, NULL);
173
174     pa_assert_se(pa_context_connect(context, NULL, 0, NULL) >= 0);
175
176     pa_gettimeofday(&start);
177
178     pa_assert_se(pa_threaded_mainloop_start(m) >= 0);
179
180 /* #ifdef CORK */
181     for (k = 0; k < 20000; k++)
182 /* #else */
183 /*     for (k = 0; k < 2000; k++) */
184 /* #endif */
185     {
186         pa_bool_t success = FALSE, changed = FALSE;
187         pa_usec_t t, rtc, d;
188         struct timeval now, tv;
189         pa_bool_t playing = FALSE;
190
191         pa_threaded_mainloop_lock(m);
192
193         if (stream) {
194             const pa_timing_info *info;
195
196             if (pa_stream_get_time(stream, &t) >= 0 &&
197                 pa_stream_get_latency(stream, &d, NULL) >= 0)
198                 success = TRUE;
199
200             if ((info = pa_stream_get_timing_info(stream))) {
201                 if (memcmp(&last_info, &info->timestamp, sizeof(struct timeval))) {
202                     changed = TRUE;
203                     last_info = info->timestamp;
204                 }
205                 if (info->playing)
206                     playing = TRUE;
207             }
208         }
209
210         pa_threaded_mainloop_unlock(m);
211
212         pa_gettimeofday(&now);
213
214         if (success) {
215 #ifdef CORK
216             pa_bool_t cork_now;
217 #endif
218             rtc = pa_timeval_diff(&now, &start);
219             printf("%i\t%llu\t%llu\t%llu\t%llu\t%lli\t%u\t%u\t%llu\t%llu\n", k,
220                    (unsigned long long) rtc,
221                    (unsigned long long) t,
222                    (unsigned long long) (rtc-old_rtc),
223                    (unsigned long long) (t-old_t),
224                    (signed long long) rtc - (signed long long) t,
225                    changed,
226                    playing,
227                    (unsigned long long) latency,
228                    (unsigned long long) d);
229
230             fflush(stdout);
231             old_t = t;
232             old_rtc = rtc;
233
234 #ifdef CORK
235             cork_now = (rtc / (2*PA_USEC_PER_SEC)) % 2 == 1;
236
237             if (corked != cork_now) {
238                 pa_threaded_mainloop_lock(m);
239                 pa_operation_unref(pa_stream_cork(stream, cork_now, NULL, NULL));
240                 pa_threaded_mainloop_unlock(m);
241
242                 pa_log(cork_now ? "Corking" : "Uncorking");
243
244                 corked = cork_now;
245             }
246 #endif
247         }
248
249         /* Spin loop, ugly but normal usleep() is just too badly grained */
250
251         tv = now;
252         while (pa_timeval_diff(pa_gettimeofday(&now), &tv) < 1000)
253             pa_thread_yield();
254     }
255
256     if (m)
257         pa_threaded_mainloop_stop(m);
258
259     if (stream) {
260         pa_stream_disconnect(stream);
261         pa_stream_unref(stream);
262     }
263
264     if (context) {
265         pa_context_disconnect(context);
266         pa_context_unref(context);
267     }
268
269     if (m)
270         pa_threaded_mainloop_free(m);
271
272     return 0;
273 }