2 This file is part of PulseAudio.
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.
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.
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
32 #include <pulse/pulseaudio.h>
33 #include <pulse/mainloop.h>
37 #define SAMPLE_HZ 44100
39 static pa_context
*context
= NULL
;
40 static pa_stream
*streams
[NSTREAMS
];
41 static pa_threaded_mainloop
*mainloop
= NULL
;
43 static const pa_sample_spec sample_spec
= {
44 .format
= PA_SAMPLE_FLOAT32
,
49 static void context_state_callback(pa_context
*c
, void *userdata
);
51 static void connect(const char *name
, int *try) {
55 /* Set up a new main loop */
56 mainloop
= pa_threaded_mainloop_new();
59 api
= pa_threaded_mainloop_get_api(mainloop
);
60 context
= pa_context_new(api
, name
);
63 pa_context_set_state_callback(context
, context_state_callback
, try);
65 /* Connect the context */
66 if (pa_context_connect(context
, NULL
, 0, NULL
) < 0) {
67 fprintf(stderr
, "pa_context_connect() failed.\n");
71 ret
= pa_threaded_mainloop_start(mainloop
);
75 static void disconnect(void) {
81 pa_threaded_mainloop_lock(mainloop
);
83 for (i
= 0; i
< NSTREAMS
; i
++)
85 pa_stream_disconnect(streams
[i
]);
86 pa_stream_unref(streams
[i
]);
90 pa_context_disconnect(context
);
93 pa_threaded_mainloop_unlock(mainloop
);
94 pa_threaded_mainloop_stop(mainloop
);
95 pa_threaded_mainloop_free(mainloop
);
99 static const pa_buffer_attr buffer_attr
= {
100 .maxlength
= SAMPLE_HZ
* sizeof(float) * NSTREAMS
,
101 .tlength
= (uint32_t) -1,
102 .prebuf
= 0, /* Setting prebuf to 0 guarantees us the the streams will run synchronously, no matter what */
103 .minreq
= (uint32_t) -1,
107 static void stream_write_callback(pa_stream
*stream
, size_t nbytes
, void *userdata
) {
110 memset(silence
, 0, sizeof(silence
));
113 int n
= MIN(sizeof(silence
), nbytes
);
114 pa_stream_write(stream
, silence
, n
, NULL
, 0, 0);
119 static void stream_state_callback(pa_stream
*s
, void *userdata
) {
122 switch (pa_stream_get_state(s
)) {
123 case PA_STREAM_UNCONNECTED
:
124 case PA_STREAM_CREATING
:
125 case PA_STREAM_TERMINATED
:
126 case PA_STREAM_READY
:
130 case PA_STREAM_FAILED
:
131 fprintf(stderr
, "Stream error: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
136 static void context_state_callback(pa_context
*c
, void *userdata
) {
142 try = (int*)userdata
;
144 switch (pa_context_get_state(c
)) {
145 case PA_CONTEXT_CONNECTING
:
146 case PA_CONTEXT_AUTHORIZING
:
147 case PA_CONTEXT_SETTING_NAME
:
150 case PA_CONTEXT_READY
: {
153 fprintf(stderr
, "Connection (%d of %d) established.\n", (*try)+1, NTESTS
);
155 for (i
= 0; i
< NSTREAMS
; i
++) {
158 snprintf(name
, sizeof(name
), "stream #%i", i
);
159 streams
[i
] = pa_stream_new(c
, name
, &sample_spec
, NULL
);
161 pa_stream_set_state_callback(streams
[i
], stream_state_callback
, NULL
);
162 pa_stream_set_write_callback(streams
[i
], stream_write_callback
, NULL
);
163 pa_stream_connect_playback(streams
[i
], NULL
, &buffer_attr
, 0, NULL
, NULL
);
169 case PA_CONTEXT_TERMINATED
:
170 fprintf(stderr
, "Connection terminated.\n");
171 pa_context_unref(context
);
175 case PA_CONTEXT_FAILED
:
177 fprintf(stderr
, "Context error: %s\n", pa_strerror(pa_context_errno(c
)));
182 int main(int argc
, char *argv
[]) {
185 for (i
= 0; i
< NSTREAMS
; i
++)
188 for (i
= 0; i
< NTESTS
; i
++) {
189 connect(argv
[0], &i
);
190 usleep(random() % 500000);
192 usleep(random() % 500000);
195 fprintf(stderr
, "Done.\n");