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