4 This file is part of polypaudio.
6 polypaudio 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.
11 polypaudio 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.
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
37 #include <polyp/polypaudio.h>
38 #include <polyp/mainloop.h>
40 static pa_context *context = NULL;
41 static pa_stream *stream = NULL;
42 static pa_mainloop_api *mainloop_api = NULL;
44 static void stream_write_cb(pa_stream *p, size_t length, void *userdata) {
46 /* Just some silence */
47 pa_stream_write(p, pa_xmalloc0(length), length, pa_xfree, 0, PA_SEEK_RELATIVE);
50 /* This is called whenever the context status changes */
51 static void context_state_callback(pa_context *c, void *userdata) {
54 switch (pa_context_get_state(c)) {
55 case PA_CONTEXT_CONNECTING:
56 case PA_CONTEXT_AUTHORIZING:
57 case PA_CONTEXT_SETTING_NAME:
60 case PA_CONTEXT_READY: {
62 static const pa_sample_spec ss = {
63 .format = PA_SAMPLE_S16LE,
68 fprintf(stderr, "Connection established.\n");
70 stream = pa_stream_new(c, "interpol-test", &ss, NULL);
73 pa_stream_connect_playback(stream, NULL, NULL, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL);
74 pa_stream_set_write_callback(stream, stream_write_cb, NULL);
79 case PA_CONTEXT_TERMINATED:
82 case PA_CONTEXT_FAILED:
84 fprintf(stderr, "Context error: %s\n", pa_strerror(pa_context_errno(c)));
89 int main(int argc, char *argv[]) {
90 pa_threaded_mainloop* m = NULL;
92 struct timeval start, last_info = { 0, 0 };
93 pa_usec_t old_t = 0, old_rtc = 0;
95 /* Set up a new main loop */
96 m = pa_threaded_mainloop_new();
99 mainloop_api = pa_threaded_mainloop_get_api(m);
101 context = pa_context_new(mainloop_api, argv[0]);
104 pa_context_set_state_callback(context, context_state_callback, NULL);
106 r = pa_context_connect(context, NULL, 0, NULL);
109 pa_gettimeofday(&start);
111 pa_threaded_mainloop_start(m);
113 for (k = 0; k < 5000; k++) {
114 int success = 0, changed = 0;
116 struct timeval now, tv;
118 pa_threaded_mainloop_lock(m);
121 const pa_timing_info *info;
123 if (pa_stream_get_time(stream, &t) >= 0)
126 if ((info = pa_stream_get_timing_info(stream)))
127 if (last_info.tv_usec != info->timestamp.tv_usec || last_info.tv_sec != info->timestamp.tv_sec) {
129 last_info = info->timestamp;
133 pa_threaded_mainloop_unlock(m);
136 pa_gettimeofday(&now);
138 rtc = pa_timeval_diff(&now, &start);
139 printf("%i\t%llu\t%llu\t%llu\t%llu\t%u\n", k, rtc, t, rtc-old_rtc, t-old_t, changed*2000000);
144 /* Spin loop, eerks but normal usleep is just to badly grained */
147 while (pa_timeval_diff(pa_gettimeofday(&now), &tv) < 1000)
152 pa_threaded_mainloop_stop(m);
155 pa_stream_disconnect(stream);
156 pa_stream_unref(stream);
160 pa_context_disconnect(context);
161 pa_context_unref(context);
165 pa_threaded_mainloop_free(m);