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>
35 #include <pulsecore/sink.h>
37 /* Set the number of streams such that it allows two simultaneous instances of
38 * connect-stress to be run and not go above the max limit for streams-per-sink.
39 * This leaves enough room for a couple other streams from regular system usage,
40 * which makes a non-error abort less likely (although still easily possible of
41 * playing >=3 streams outside of the test - including internal loopback, rtp,
42 * combine, remap streams etc.) */
43 #define NSTREAMS ((PA_MAX_INPUTS_PER_SINK/2) - 1)
45 #define SAMPLE_HZ 44100
47 static pa_context *context = NULL;
48 static pa_stream *streams[NSTREAMS];
49 static pa_threaded_mainloop *mainloop = NULL;
51 static const pa_sample_spec sample_spec = {
52 .format = PA_SAMPLE_FLOAT32,
57 static void context_state_callback(pa_context *c, void *userdata);
59 static void connect(const char *name, int *try) {
63 /* Set up a new main loop */
64 mainloop = pa_threaded_mainloop_new();
67 api = pa_threaded_mainloop_get_api(mainloop);
68 context = pa_context_new(api, name);
71 pa_context_set_state_callback(context, context_state_callback, try);
73 /* Connect the context */
74 if (pa_context_connect(context, NULL, 0, NULL) < 0) {
75 fprintf(stderr, "pa_context_connect() failed.\n");
79 ret = pa_threaded_mainloop_start(mainloop);
83 static void disconnect(void) {
89 pa_threaded_mainloop_lock(mainloop);
91 for (i = 0; i < NSTREAMS; i++)
93 pa_stream_disconnect(streams[i]);
94 pa_stream_unref(streams[i]);
98 pa_context_disconnect(context);
101 pa_threaded_mainloop_unlock(mainloop);
102 pa_threaded_mainloop_stop(mainloop);
103 pa_threaded_mainloop_free(mainloop);
107 static const pa_buffer_attr buffer_attr = {
108 .maxlength = SAMPLE_HZ * sizeof(float) * NSTREAMS,
109 .tlength = (uint32_t) -1,
110 .prebuf = 0, /* Setting prebuf to 0 guarantees us the the streams will run synchronously, no matter what */
111 .minreq = (uint32_t) -1,
115 static void stream_write_callback(pa_stream *stream, size_t nbytes, void *userdata) {
118 memset(silence, 0, sizeof(silence));
121 int n = PA_MIN(sizeof(silence), nbytes);
122 pa_stream_write(stream, silence, n, NULL, 0, 0);
127 static void stream_state_callback(pa_stream *s, void *userdata) {
130 switch (pa_stream_get_state(s)) {
131 case PA_STREAM_UNCONNECTED:
132 case PA_STREAM_CREATING:
133 case PA_STREAM_TERMINATED:
134 case PA_STREAM_READY:
138 case PA_STREAM_FAILED:
139 fprintf(stderr, "Stream error: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
144 static void context_state_callback(pa_context *c, void *userdata) {
150 try = (int*)userdata;
152 switch (pa_context_get_state(c)) {
153 case PA_CONTEXT_CONNECTING:
154 case PA_CONTEXT_AUTHORIZING:
155 case PA_CONTEXT_SETTING_NAME:
158 case PA_CONTEXT_READY: {
161 fprintf(stderr, "Connection (%d of %d) established.\n", (*try)+1, NTESTS);
163 for (i = 0; i < NSTREAMS; i++) {
166 snprintf(name, sizeof(name), "stream #%i", i);
167 streams[i] = pa_stream_new(c, name, &sample_spec, NULL);
169 pa_stream_set_state_callback(streams[i], stream_state_callback, NULL);
170 pa_stream_set_write_callback(streams[i], stream_write_callback, NULL);
171 pa_stream_connect_playback(streams[i], NULL, &buffer_attr, 0, NULL, NULL);
177 case PA_CONTEXT_TERMINATED:
178 fprintf(stderr, "Connection terminated.\n");
179 pa_context_unref(context);
183 case PA_CONTEXT_FAILED:
185 fprintf(stderr, "Context error: %s\n", pa_strerror(pa_context_errno(c)));
190 int main(int argc, char *argv[]) {
193 for (i = 0; i < NSTREAMS; i++)
196 for (i = 0; i < NTESTS; i++) {
197 connect(argv[0], &i);
198 usleep(rand() % 500000);
200 usleep(rand() % 500000);
203 fprintf(stderr, "Done.\n");