really create glitch-free branch
[platform/upstream/pulseaudio.git] / src / tests / interpol-test.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   PulseAudio is free software; you can redistribute it and/or modify
7   it under the terms of the GNU Lesser General Public License as published
8   by the Free Software Foundation; either version 2 of the License,
9   or (at your option) any later version.
10
11   PulseAudio is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with PulseAudio; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <signal.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <assert.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <getopt.h>
34 #include <math.h>
35
36 #include <pulse/pulseaudio.h>
37 #include <pulse/mainloop.h>
38
39 #include <pulsecore/thread.h>
40
41 static pa_context *context = NULL;
42 static pa_stream *stream = NULL;
43 static pa_mainloop_api *mainloop_api = NULL;
44
45 static void stream_write_cb(pa_stream *p, size_t length, void *userdata) {
46
47     /* Just some silence */
48     pa_stream_write(p, pa_xmalloc0(length), length, pa_xfree, 0, PA_SEEK_RELATIVE);
49 }
50
51 /* This is called whenever the context status changes */
52 static void context_state_callback(pa_context *c, void *userdata) {
53     assert(c);
54
55     switch (pa_context_get_state(c)) {
56         case PA_CONTEXT_CONNECTING:
57         case PA_CONTEXT_AUTHORIZING:
58         case PA_CONTEXT_SETTING_NAME:
59             break;
60
61         case PA_CONTEXT_READY: {
62
63             static const pa_sample_spec ss = {
64                 .format = PA_SAMPLE_S16LE,
65                 .rate = 44100,
66                 .channels = 1
67             };
68
69             fprintf(stderr, "Connection established.\n");
70
71             stream = pa_stream_new(c, "interpol-test", &ss, NULL);
72             assert(stream);
73
74             pa_stream_connect_playback(stream, NULL, NULL, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL);
75             pa_stream_set_write_callback(stream, stream_write_cb, NULL);
76
77             break;
78         }
79
80         case PA_CONTEXT_TERMINATED:
81             break;
82
83         case PA_CONTEXT_FAILED:
84         default:
85             fprintf(stderr, "Context error: %s\n", pa_strerror(pa_context_errno(c)));
86             abort();
87     }
88 }
89
90 int main(int argc, char *argv[]) {
91     pa_threaded_mainloop* m = NULL;
92     int k, r;
93     struct timeval start, last_info = { 0, 0 };
94     pa_usec_t old_t = 0, old_rtc = 0;
95
96     /* Set up a new main loop */
97     m = pa_threaded_mainloop_new();
98     assert(m);
99
100     mainloop_api = pa_threaded_mainloop_get_api(m);
101
102     context = pa_context_new(mainloop_api, argv[0]);
103     assert(context);
104
105     pa_context_set_state_callback(context, context_state_callback, NULL);
106
107     r = pa_context_connect(context, NULL, 0, NULL);
108     assert(r >= 0);
109
110     pa_gettimeofday(&start);
111
112     pa_threaded_mainloop_start(m);
113
114     for (k = 0; k < 5000; k++) {
115         int success = 0, changed = 0;
116         pa_usec_t t, rtc;
117         struct timeval now, tv;
118
119         pa_threaded_mainloop_lock(m);
120
121         if (stream) {
122             const pa_timing_info *info;
123
124             if (pa_stream_get_time(stream, &t) >= 0)
125                 success = 1;
126
127             if ((info = pa_stream_get_timing_info(stream)))
128                 if (last_info.tv_usec != info->timestamp.tv_usec || last_info.tv_sec != info->timestamp.tv_sec) {
129                     changed = 1;
130                     last_info = info->timestamp;
131                 }
132         }
133
134         pa_threaded_mainloop_unlock(m);
135
136         if (success) {
137             pa_gettimeofday(&now);
138
139             rtc = pa_timeval_diff(&now, &start);
140             printf("%i\t%llu\t%llu\t%llu\t%llu\t%u\n", k, (unsigned long long) rtc, (unsigned long long) t, (unsigned long long) (rtc-old_rtc), (unsigned long long) (t-old_t), changed);
141             old_t = t;
142             old_rtc = rtc;
143         }
144
145         /* Spin loop, ugly but normal usleep() is just too badly grained */
146
147         tv = now;
148         while (pa_timeval_diff(pa_gettimeofday(&now), &tv) < 1000)
149             pa_thread_yield();
150     }
151
152     if (m)
153         pa_threaded_mainloop_stop(m);
154
155     if (stream) {
156         pa_stream_disconnect(stream);
157         pa_stream_unref(stream);
158     }
159
160     if (context) {
161         pa_context_disconnect(context);
162         pa_context_unref(context);
163     }
164
165     if (m)
166         pa_threaded_mainloop_free(m);
167
168     return 0;
169 }