• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, see <http://www.gnu.org/licenses/>.
16 ***/
17 
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21 
22 #include <signal.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <assert.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <math.h>
29 
30 #include <check.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 static const char *bname = NULL;
43 
44 static float data[SAMPLE_HZ]; /* one second space */
45 
46 static int n_streams_ready = 0;
47 
48 static const pa_sample_spec sample_spec = {
49     .format = PA_SAMPLE_FLOAT32,
50     .rate = SAMPLE_HZ,
51     .channels = 1
52 };
53 
54 static const pa_buffer_attr buffer_attr = {
55     .maxlength = SAMPLE_HZ*sizeof(float)*NSTREAMS, /* exactly space for the entire play time */
56     .tlength = (uint32_t) -1,
57     .prebuf = 0, /* Setting prebuf to 0 guarantees us the streams will run synchronously, no matter what */
58     .minreq = (uint32_t) -1,
59     .fragsize = 0
60 };
61 
nop_free_cb(void * p)62 static void nop_free_cb(void *p) {}
63 
underflow_cb(struct pa_stream * s,void * userdata)64 static void underflow_cb(struct pa_stream *s, void *userdata) {
65     int i = (int) (long) userdata;
66 
67     fprintf(stderr, "Stream %i finished\n", i);
68 
69     if (++n_streams_ready >= 2*NSTREAMS) {
70         fprintf(stderr, "We're done\n");
71         mainloop_api->quit(mainloop_api, 0);
72     }
73 }
74 
75 /* This routine is called whenever the stream state changes */
stream_state_callback(pa_stream * s,void * userdata)76 static void stream_state_callback(pa_stream *s, void *userdata) {
77     fail_unless(s != NULL);
78 
79     switch (pa_stream_get_state(s)) {
80         case PA_STREAM_UNCONNECTED:
81         case PA_STREAM_CREATING:
82         case PA_STREAM_TERMINATED:
83             break;
84 
85         case PA_STREAM_READY: {
86 
87             int r, i = (int) (long) userdata;
88 
89             fprintf(stderr, "Writing data to stream %i.\n", i);
90 
91             r = pa_stream_write(s, data, sizeof(data), nop_free_cb, (int64_t) sizeof(data) * (int64_t) i, PA_SEEK_ABSOLUTE);
92             fail_unless(r == 0);
93 
94             /* Be notified when this stream is drained */
95             pa_stream_set_underflow_callback(s, underflow_cb, userdata);
96 
97             /* All streams have been set up, let's go! */
98             if (++n_streams_ready >= NSTREAMS) {
99                 fprintf(stderr, "Uncorking\n");
100                 pa_operation_unref(pa_stream_cork(s, 0, NULL, NULL));
101             }
102 
103             break;
104         }
105 
106         default:
107         case PA_STREAM_FAILED:
108             fprintf(stderr, "Stream error: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
109             ck_abort();
110     }
111 }
112 
113 /* This is called whenever the context status changes */
context_state_callback(pa_context * c,void * userdata)114 static void context_state_callback(pa_context *c, void *userdata) {
115     fail_unless(c != NULL);
116 
117     switch (pa_context_get_state(c)) {
118         case PA_CONTEXT_CONNECTING:
119         case PA_CONTEXT_AUTHORIZING:
120         case PA_CONTEXT_SETTING_NAME:
121             break;
122 
123         case PA_CONTEXT_READY: {
124 
125             int i;
126             fprintf(stderr, "Connection established.\n");
127 
128             for (i = 0; i < NSTREAMS; i++) {
129                 char name[64];
130 
131                 fprintf(stderr, "Creating stream %i\n", i);
132 
133                 snprintf(name, sizeof(name), "stream #%i", i);
134 
135                 streams[i] = pa_stream_new(c, name, &sample_spec, NULL);
136                 fail_unless(streams[i] != NULL);
137                 pa_stream_set_state_callback(streams[i], stream_state_callback, (void*) (long) i);
138                 pa_stream_connect_playback(streams[i], NULL, &buffer_attr, PA_STREAM_START_CORKED, NULL, i == 0 ? NULL : streams[0]);
139             }
140 
141             break;
142         }
143 
144         case PA_CONTEXT_TERMINATED:
145             mainloop_api->quit(mainloop_api, 0);
146             break;
147 
148         case PA_CONTEXT_FAILED:
149         default:
150             fprintf(stderr, "Context error: %s\n", pa_strerror(pa_context_errno(c)));
151             ck_abort();
152     }
153 }
154 
START_TEST(sync_playback_test)155 START_TEST (sync_playback_test) {
156     pa_mainloop* m = NULL;
157     int i, ret = 0;
158 
159     for (i = 0; i < SAMPLE_HZ; i++)
160         data[i] = (float) sin(((double) i/SAMPLE_HZ)*2*M_PI*SINE_HZ)/2;
161 
162     for (i = 0; i < NSTREAMS; i++)
163         streams[i] = NULL;
164 
165     /* Set up a new main loop */
166     m = pa_mainloop_new();
167     fail_unless(m != NULL);
168 
169     mainloop_api = pa_mainloop_get_api(m);
170 
171     context = pa_context_new(mainloop_api, bname);
172     fail_unless(context != NULL);
173 
174     pa_context_set_state_callback(context, context_state_callback, NULL);
175 
176     /* Connect the context */
177     if (pa_context_connect(context, NULL, 0, NULL) < 0) {
178         fprintf(stderr, "pa_context_connect() failed.\n");
179         goto quit;
180     }
181 
182     if (pa_mainloop_run(m, &ret) < 0)
183         fprintf(stderr, "pa_mainloop_run() failed.\n");
184 
185 quit:
186     pa_context_unref(context);
187 
188     for (i = 0; i < NSTREAMS; i++)
189         if (streams[i])
190             pa_stream_unref(streams[i]);
191 
192     pa_mainloop_free(m);
193 
194     fail_unless(ret == 0);
195 }
196 END_TEST
197 
main(int argc,char * argv[])198 int main(int argc, char *argv[]) {
199     int failed = 0;
200     Suite *s;
201     TCase *tc;
202     SRunner *sr;
203 
204     bname = argv[0];
205 
206     s = suite_create("Sync Playback");
207     tc = tcase_create("syncplayback");
208     tcase_add_test(tc, sync_playback_test);
209     /* 4s of audio, 0.5s grace time */
210     tcase_set_timeout(tc, 4.5);
211     suite_add_tcase(s, tc);
212 
213     sr = srunner_create(s);
214     srunner_run_all(sr, CK_NORMAL);
215     failed = srunner_ntests_failed(sr);
216     srunner_free(sr);
217 
218     return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
219 }
220