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