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