]> code.delx.au - pulseaudio/blob - src/tests/sync-playback.c
d675e01cc054521907fbc112b5482380b5a93fba
[pulseaudio] / src / tests / sync-playback.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
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 <polyp/polypaudio.h>
37 #include <polyp/mainloop.h>
38
39 #define NSTREAMS 4
40 #define SINE_HZ 440
41 #define SAMPLE_HZ 8000
42
43 static pa_context *context = NULL;
44 static pa_stream *streams[NSTREAMS];
45 static pa_mainloop_api *mainloop_api = NULL;
46
47 static float data[SAMPLE_HZ]; /* one second space */
48
49 static int n_streams_ready = 0;
50
51 static const pa_sample_spec sample_spec = {
52 .format = PA_SAMPLE_FLOAT32,
53 .rate = SAMPLE_HZ,
54 .channels = 1
55 };
56
57 static const pa_buffer_attr buffer_attr = {
58 .maxlength = SAMPLE_HZ*sizeof(float)*NSTREAMS, /* exactly space for the entire play time */
59 .tlength = 0,
60 .prebuf = 0, /* Setting prebuf to 0 guarantees us the the streams will run synchronously, no matter what */
61 .minreq = 0
62 };
63
64 static void nop_free_cb(void *p) {}
65
66 static void underflow_cb(struct pa_stream *s, void *userdata) {
67 int i = (int) (long) userdata;
68
69 fprintf(stderr, "Stream %i finished\n", i);
70
71 if (++n_streams_ready >= 2*NSTREAMS) {
72 fprintf(stderr, "We're done\n");
73 mainloop_api->quit(mainloop_api, 0);
74 }
75 }
76
77 /* This routine is called whenever the stream state changes */
78 static void stream_state_callback(pa_stream *s, void *userdata) {
79 assert(s);
80
81 switch (pa_stream_get_state(s)) {
82 case PA_STREAM_UNCONNECTED:
83 case PA_STREAM_CREATING:
84 case PA_STREAM_TERMINATED:
85 break;
86
87 case PA_STREAM_READY: {
88
89 int r, i = (int) (long) userdata;
90
91 fprintf(stderr, "Writing data to stream %i.\n", i);
92
93 r = pa_stream_write(s, data, sizeof(data), nop_free_cb, sizeof(data) * i, PA_SEEK_ABSOLUTE);
94 assert(r == 0);
95
96 /* Be notified when this stream is drained */
97 pa_stream_set_underflow_callback(s, underflow_cb, userdata);
98
99 /* All streams have been set up, let's go! */
100 if (++n_streams_ready >= NSTREAMS) {
101 fprintf(stderr, "Uncorking\n");
102 pa_operation_unref(pa_stream_cork(s, 0, NULL, NULL));
103 }
104
105 break;
106 }
107
108 default:
109 case PA_STREAM_FAILED:
110 fprintf(stderr, "Stream error: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
111 abort();
112 }
113 }
114
115 /* This is called whenever the context status changes */
116 static void context_state_callback(pa_context *c, void *userdata) {
117 assert(c);
118
119 switch (pa_context_get_state(c)) {
120 case PA_CONTEXT_CONNECTING:
121 case PA_CONTEXT_AUTHORIZING:
122 case PA_CONTEXT_SETTING_NAME:
123 break;
124
125 case PA_CONTEXT_READY: {
126
127 int i;
128 fprintf(stderr, "Connection established.\n");
129
130 for (i = 0; i < NSTREAMS; i++) {
131 char name[64];
132
133 fprintf(stderr, "Creating stream %i\n", i);
134
135 snprintf(name, sizeof(name), "stream #%i", i);
136
137 streams[i] = pa_stream_new(c, name, &sample_spec, NULL);
138 assert(streams[i]);
139 pa_stream_set_state_callback(streams[i], stream_state_callback, (void*) (long) i);
140 pa_stream_connect_playback(streams[i], NULL, &buffer_attr, PA_STREAM_START_CORKED, NULL, i == 0 ? NULL : streams[0]);
141 }
142
143 break;
144 }
145
146 case PA_CONTEXT_TERMINATED:
147 mainloop_api->quit(mainloop_api, 0);
148 break;
149
150 case PA_CONTEXT_FAILED:
151 default:
152 fprintf(stderr, "Context error: %s\n", pa_strerror(pa_context_errno(c)));
153 abort();
154 }
155 }
156
157 int main(int argc, char *argv[]) {
158 pa_mainloop* m = NULL;
159 int i, ret = 0;
160
161 for (i = 0; i < SAMPLE_HZ; i++)
162 data[i] = (float) sin(((double) i/SAMPLE_HZ)*2*M_PI*SINE_HZ)/2;
163
164 for (i = 0; i < NSTREAMS; i++)
165 streams[i] = NULL;
166
167 /* Set up a new main loop */
168 m = pa_mainloop_new();
169 assert(m);
170
171 mainloop_api = pa_mainloop_get_api(m);
172
173 context = pa_context_new(mainloop_api, argv[0]);
174 assert(context);
175
176 pa_context_set_state_callback(context, context_state_callback, NULL);
177
178 pa_context_connect(context, NULL, 0, NULL);
179
180 if (pa_mainloop_run(m, &ret) < 0)
181 fprintf(stderr, "pa_mainloop_run() failed.\n");
182
183 pa_context_unref(context);
184
185 for (i = 0; i < NSTREAMS; i++)
186 if (streams[i])
187 pa_stream_unref(streams[i]);
188
189 pa_mainloop_free(m);
190
191 return ret;
192 }