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