]> code.delx.au - pulseaudio/blob - src/tests/interpol-test.c
Make interpol-test build on Win32 and non-pthread systems.
[pulseaudio] / 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 #ifdef HAVE_PTHREAD
37 #include <pthread.h>
38 #endif
39
40 #include <pulse/pulseaudio.h>
41 #include <pulse/mainloop.h>
42
43 static pa_context *context = NULL;
44 static pa_stream *stream = NULL;
45 static pa_mainloop_api *mainloop_api = NULL;
46
47 static void stream_write_cb(pa_stream *p, size_t length, void *userdata) {
48
49 /* Just some silence */
50 pa_stream_write(p, pa_xmalloc0(length), length, pa_xfree, 0, PA_SEEK_RELATIVE);
51 }
52
53 /* This is called whenever the context status changes */
54 static void context_state_callback(pa_context *c, void *userdata) {
55 assert(c);
56
57 switch (pa_context_get_state(c)) {
58 case PA_CONTEXT_CONNECTING:
59 case PA_CONTEXT_AUTHORIZING:
60 case PA_CONTEXT_SETTING_NAME:
61 break;
62
63 case PA_CONTEXT_READY: {
64
65 static const pa_sample_spec ss = {
66 .format = PA_SAMPLE_S16LE,
67 .rate = 44100,
68 .channels = 1
69 };
70
71 fprintf(stderr, "Connection established.\n");
72
73 stream = pa_stream_new(c, "interpol-test", &ss, NULL);
74 assert(stream);
75
76 pa_stream_connect_playback(stream, NULL, NULL, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL);
77 pa_stream_set_write_callback(stream, stream_write_cb, NULL);
78
79 break;
80 }
81
82 case PA_CONTEXT_TERMINATED:
83 break;
84
85 case PA_CONTEXT_FAILED:
86 default:
87 fprintf(stderr, "Context error: %s\n", pa_strerror(pa_context_errno(c)));
88 abort();
89 }
90 }
91
92 int main(int argc, char *argv[]) {
93 pa_threaded_mainloop* m = NULL;
94 int k, r;
95 struct timeval start, last_info = { 0, 0 };
96 pa_usec_t old_t = 0, old_rtc = 0;
97
98 /* Set up a new main loop */
99 m = pa_threaded_mainloop_new();
100 assert(m);
101
102 mainloop_api = pa_threaded_mainloop_get_api(m);
103
104 context = pa_context_new(mainloop_api, argv[0]);
105 assert(context);
106
107 pa_context_set_state_callback(context, context_state_callback, NULL);
108
109 r = pa_context_connect(context, NULL, 0, NULL);
110 assert(r >= 0);
111
112 pa_gettimeofday(&start);
113
114 pa_threaded_mainloop_start(m);
115
116 for (k = 0; k < 5000; k++) {
117 int success = 0, changed = 0;
118 pa_usec_t t, rtc;
119 struct timeval now, tv;
120
121 pa_threaded_mainloop_lock(m);
122
123 if (stream) {
124 const pa_timing_info *info;
125
126 if (pa_stream_get_time(stream, &t) >= 0)
127 success = 1;
128
129 if ((info = pa_stream_get_timing_info(stream)))
130 if (last_info.tv_usec != info->timestamp.tv_usec || last_info.tv_sec != info->timestamp.tv_sec) {
131 changed = 1;
132 last_info = info->timestamp;
133 }
134 }
135
136 pa_threaded_mainloop_unlock(m);
137
138 if (success) {
139 pa_gettimeofday(&now);
140
141 rtc = pa_timeval_diff(&now, &start);
142 printf("%i\t%llu\t%llu\t%llu\t%llu\t%u\n", k, rtc, t, rtc-old_rtc, t-old_t, changed);
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 #ifdef HAVE_PTHREAD
152 pthread_yield();
153 #elif defined(OS_IS_WIN32)
154 Sleep(0);
155 #else
156 ;
157 #endif
158 }
159
160 if (m)
161 pa_threaded_mainloop_stop(m);
162
163 if (stream) {
164 pa_stream_disconnect(stream);
165 pa_stream_unref(stream);
166 }
167
168 if (context) {
169 pa_context_disconnect(context);
170 pa_context_unref(context);
171 }
172
173 if (m)
174 pa_threaded_mainloop_free(m);
175
176 return 0;
177 }