]> code.delx.au - pulseaudio/blob - src/tests/connect-stress.c
sink, source: Add hooks for mute changes
[pulseaudio] / src / tests / connect-stress.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 <string.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <assert.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include <check.h>
33
34 #include <pulse/pulseaudio.h>
35 #include <pulse/mainloop.h>
36
37 #include <pulsecore/sink.h>
38
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)
46 #define NTESTS 1000
47 #define SAMPLE_HZ 44100
48
49 static pa_context *context = NULL;
50 static pa_stream *streams[NSTREAMS];
51 static pa_threaded_mainloop *mainloop = NULL;
52 static char *bname;
53
54 static const pa_sample_spec sample_spec = {
55 .format = PA_SAMPLE_FLOAT32,
56 .rate = SAMPLE_HZ,
57 .channels = 1
58 };
59
60 static void context_state_callback(pa_context *c, void *userdata);
61
62 static void connect(const char *name, int *try) {
63 int ret;
64 pa_mainloop_api *api;
65
66 /* Set up a new main loop */
67 mainloop = pa_threaded_mainloop_new();
68 fail_unless(mainloop != NULL);
69
70 api = pa_threaded_mainloop_get_api(mainloop);
71 context = pa_context_new(api, name);
72 fail_unless(context != NULL);
73
74 pa_context_set_state_callback(context, context_state_callback, try);
75
76 /* Connect the context */
77 if (pa_context_connect(context, NULL, 0, NULL) < 0) {
78 fprintf(stderr, "pa_context_connect() failed.\n");
79 fail();
80 }
81
82 ret = pa_threaded_mainloop_start(mainloop);
83 fail_unless(ret == 0);
84 }
85
86 static void disconnect(void) {
87 int i;
88
89 fail_unless(mainloop != NULL);
90 fail_unless(context != NULL);
91
92 pa_threaded_mainloop_lock(mainloop);
93
94 for (i = 0; i < NSTREAMS; i++)
95 if (streams[i]) {
96 pa_stream_disconnect(streams[i]);
97 pa_stream_unref(streams[i]);
98 streams[i] = NULL;
99 }
100
101 pa_context_disconnect(context);
102 context = NULL;
103
104 pa_threaded_mainloop_unlock(mainloop);
105 pa_threaded_mainloop_stop(mainloop);
106 pa_threaded_mainloop_free(mainloop);
107 mainloop = NULL;
108 }
109
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,
115 .fragsize = 0
116 };
117
118 static void stream_write_callback(pa_stream *stream, size_t nbytes, void *userdata) {
119 char silence[8192];
120
121 memset(silence, 0, sizeof(silence));
122
123 while (nbytes) {
124 int n = PA_MIN(sizeof(silence), nbytes);
125 pa_stream_write(stream, silence, n, NULL, 0, 0);
126 nbytes -= n;
127 }
128 }
129
130 static void stream_state_callback(pa_stream *s, void *userdata) {
131 fail_unless(s != NULL);
132
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:
138 break;
139
140 default:
141 case PA_STREAM_FAILED:
142 fprintf(stderr, "Stream error: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
143 fail();
144 }
145 }
146
147 static void context_state_callback(pa_context *c, void *userdata) {
148 int *try;
149
150 fail_unless(c != NULL);
151 fail_unless(userdata != NULL);
152
153 try = (int*)userdata;
154
155 switch (pa_context_get_state(c)) {
156 case PA_CONTEXT_CONNECTING:
157 case PA_CONTEXT_AUTHORIZING:
158 case PA_CONTEXT_SETTING_NAME:
159 break;
160
161 case PA_CONTEXT_READY: {
162
163 int i;
164 fprintf(stderr, "Connection (%d of %d) established.\n", (*try)+1, NTESTS);
165
166 for (i = 0; i < NSTREAMS; i++) {
167 char name[64];
168
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);
175 }
176
177 break;
178 }
179
180 case PA_CONTEXT_TERMINATED:
181 fprintf(stderr, "Connection terminated.\n");
182 pa_context_unref(context);
183 context = NULL;
184 break;
185
186 case PA_CONTEXT_FAILED:
187 default:
188 fprintf(stderr, "Context error: %s\n", pa_strerror(pa_context_errno(c)));
189 fail();
190 }
191 }
192
193 START_TEST (connect_stress_test) {
194 int i;
195
196 for (i = 0; i < NSTREAMS; i++)
197 streams[i] = NULL;
198
199 for (i = 0; i < NTESTS; i++) {
200 connect(bname, &i);
201 usleep(rand() % 500000);
202 disconnect();
203 usleep(rand() % 500000);
204 }
205
206 fprintf(stderr, "Done.\n");
207 }
208 END_TEST
209
210 int main(int argc, char *argv[]) {
211 int failed = 0;
212 Suite *s;
213 TCase *tc;
214 SRunner *sr;
215
216 bname = argv[0];
217
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);
223
224 sr = srunner_create(s);
225 srunner_run_all(sr, CK_NORMAL);
226 failed = srunner_ntests_failed(sr);
227 srunner_free(sr);
228
229 return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
230 }