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
34 #include <pulse/pulseaudio.h>
35 #include <pulse/mainloop.h>
37 #include <pulsecore/sink.h>
39 /* Set the number of streams such that it allows two simultaneous instances of
40 * connect-stress to be run and not go above the max limit for streams-per-sink.
41 * This leaves enough room for a couple other streams from regular system usage,
42 * which makes a non-error abort less likely (although still easily possible of
43 * playing >=3 streams outside of the test - including internal loopback, rtp,
44 * combine, remap streams etc.) */
45 #define NSTREAMS ((PA_MAX_INPUTS_PER_SINK/2) - 1)
47 #define SAMPLE_HZ 44100
49 static pa_context *context = NULL;
50 static pa_stream *streams[NSTREAMS];
51 static pa_threaded_mainloop *mainloop = NULL;
54 static const pa_sample_spec sample_spec = {
55 .format = PA_SAMPLE_FLOAT32,
60 static void context_state_callback(pa_context *c, void *userdata);
62 static void connect(const char *name, int *try) {
66 /* Set up a new main loop */
67 mainloop = pa_threaded_mainloop_new();
68 fail_unless(mainloop != NULL);
70 api = pa_threaded_mainloop_get_api(mainloop);
71 context = pa_context_new(api, name);
72 fail_unless(context != NULL);
74 pa_context_set_state_callback(context, context_state_callback, try);
76 /* Connect the context */
77 if (pa_context_connect(context, NULL, 0, NULL) < 0) {
78 fprintf(stderr, "pa_context_connect() failed.\n");
82 ret = pa_threaded_mainloop_start(mainloop);
83 fail_unless(ret == 0);
86 static void disconnect(void) {
89 fail_unless(mainloop != NULL);
90 fail_unless(context != NULL);
92 pa_threaded_mainloop_lock(mainloop);
94 for (i = 0; i < NSTREAMS; i++)
96 pa_stream_disconnect(streams[i]);
97 pa_stream_unref(streams[i]);
101 pa_context_disconnect(context);
104 pa_threaded_mainloop_unlock(mainloop);
105 pa_threaded_mainloop_stop(mainloop);
106 pa_threaded_mainloop_free(mainloop);
110 static const pa_buffer_attr buffer_attr = {
111 .maxlength = SAMPLE_HZ * sizeof(float) * NSTREAMS,
112 .tlength = (uint32_t) -1,
113 .prebuf = 0, /* Setting prebuf to 0 guarantees us the streams will run synchronously, no matter what */
114 .minreq = (uint32_t) -1,
118 static void stream_write_callback(pa_stream *stream, size_t nbytes, void *userdata) {
121 memset(silence, 0, sizeof(silence));
124 int n = PA_MIN(sizeof(silence), nbytes);
125 pa_stream_write(stream, silence, n, NULL, 0, 0);
130 static void stream_state_callback(pa_stream *s, void *userdata) {
131 fail_unless(s != NULL);
133 switch (pa_stream_get_state(s)) {
134 case PA_STREAM_UNCONNECTED:
135 case PA_STREAM_CREATING:
136 case PA_STREAM_TERMINATED:
137 case PA_STREAM_READY:
141 case PA_STREAM_FAILED:
142 fprintf(stderr, "Stream error: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
147 static void context_state_callback(pa_context *c, void *userdata) {
150 fail_unless(c != NULL);
151 fail_unless(userdata != NULL);
153 try = (int*)userdata;
155 switch (pa_context_get_state(c)) {
156 case PA_CONTEXT_CONNECTING:
157 case PA_CONTEXT_AUTHORIZING:
158 case PA_CONTEXT_SETTING_NAME:
161 case PA_CONTEXT_READY: {
164 fprintf(stderr, "Connection (%d of %d) established.\n", (*try)+1, NTESTS);
166 for (i = 0; i < NSTREAMS; i++) {
169 snprintf(name, sizeof(name), "stream #%i", i);
170 streams[i] = pa_stream_new(c, name, &sample_spec, NULL);
171 fail_unless(streams[i] != NULL);
172 pa_stream_set_state_callback(streams[i], stream_state_callback, NULL);
173 pa_stream_set_write_callback(streams[i], stream_write_callback, NULL);
174 pa_stream_connect_playback(streams[i], NULL, &buffer_attr, 0, NULL, NULL);
180 case PA_CONTEXT_TERMINATED:
181 fprintf(stderr, "Connection terminated.\n");
182 pa_context_unref(context);
186 case PA_CONTEXT_FAILED:
188 fprintf(stderr, "Context error: %s\n", pa_strerror(pa_context_errno(c)));
193 START_TEST (connect_stress_test) {
196 for (i = 0; i < NSTREAMS; i++)
199 for (i = 0; i < NTESTS; i++) {
201 usleep(rand() % 500000);
203 usleep(rand() % 500000);
206 fprintf(stderr, "Done.\n");
210 int main(int argc, char *argv[]) {
218 s = suite_create("Connect Stress");
219 tc = tcase_create("connectstress");
220 tcase_add_test(tc, connect_stress_test);
221 tcase_set_timeout(tc, 20 * 60);
222 suite_add_tcase(s, tc);
224 sr = srunner_create(s);
225 srunner_run_all(sr, CK_NORMAL);
226 failed = srunner_ntests_failed(sr);
229 return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;