]> code.delx.au - pulseaudio/blob - src/tests/connect-stress.c
tests: add a connection stress test
[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 <pulse/pulseaudio.h>
33 #include <pulse/mainloop.h>
34
35 #define NSTREAMS 16
36 #define NTESTS 1000
37 #define SAMPLE_HZ 44100
38
39 static pa_context *context = NULL;
40 static pa_stream *streams[NSTREAMS];
41 static pa_threaded_mainloop *mainloop = NULL;
42
43 static const pa_sample_spec sample_spec = {
44 .format = PA_SAMPLE_FLOAT32,
45 .rate = SAMPLE_HZ,
46 .channels = 1
47 };
48
49 static void context_state_callback(pa_context *c, void *userdata);
50
51 static void connect(const char *name, int *try) {
52 int ret;
53 pa_mainloop_api *api;
54
55 /* Set up a new main loop */
56 mainloop = pa_threaded_mainloop_new();
57 assert(mainloop);
58
59 api = pa_threaded_mainloop_get_api(mainloop);
60 context = pa_context_new(api, name);
61 assert(context);
62
63 pa_context_set_state_callback(context, context_state_callback, try);
64
65 /* Connect the context */
66 if (pa_context_connect(context, NULL, 0, NULL) < 0) {
67 fprintf(stderr, "pa_context_connect() failed.\n");
68 abort();
69 }
70
71 ret = pa_threaded_mainloop_start(mainloop);
72 assert(ret == 0);
73 }
74
75 static void disconnect(void) {
76 int i;
77
78 assert(mainloop);
79 assert(context);
80
81 pa_threaded_mainloop_lock(mainloop);
82
83 for (i = 0; i < NSTREAMS; i++)
84 if (streams[i]) {
85 pa_stream_disconnect(streams[i]);
86 pa_stream_unref(streams[i]);
87 streams[i] = NULL;
88 }
89
90 pa_context_disconnect(context);
91 context = NULL;
92
93 pa_threaded_mainloop_unlock(mainloop);
94 pa_threaded_mainloop_stop(mainloop);
95 pa_threaded_mainloop_free(mainloop);
96 mainloop = NULL;
97 }
98
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,
104 .fragsize = 0
105 };
106
107 static void stream_write_callback(pa_stream *stream, size_t nbytes, void *userdata) {
108 char silence[8192];
109
110 memset(silence, 0, sizeof(silence));
111
112 while (nbytes) {
113 int n = MIN(sizeof(silence), nbytes);
114 pa_stream_write(stream, silence, n, NULL, 0, 0);
115 nbytes -= n;
116 }
117 }
118
119 static void stream_state_callback(pa_stream *s, void *userdata) {
120 assert(s);
121
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:
127 break;
128
129 default:
130 case PA_STREAM_FAILED:
131 fprintf(stderr, "Stream error: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
132 abort();
133 }
134 }
135
136 static void context_state_callback(pa_context *c, void *userdata) {
137 int *try;
138
139 assert(c);
140 assert(userdata);
141
142 try = (int*)userdata;
143
144 switch (pa_context_get_state(c)) {
145 case PA_CONTEXT_CONNECTING:
146 case PA_CONTEXT_AUTHORIZING:
147 case PA_CONTEXT_SETTING_NAME:
148 break;
149
150 case PA_CONTEXT_READY: {
151
152 int i;
153 fprintf(stderr, "Connection (%d of %d) established.\n", (*try)+1, NTESTS);
154
155 for (i = 0; i < NSTREAMS; i++) {
156 char name[64];
157
158 snprintf(name, sizeof(name), "stream #%i", i);
159 streams[i] = pa_stream_new(c, name, &sample_spec, NULL);
160 assert(streams[i]);
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);
164 }
165
166 break;
167 }
168
169 case PA_CONTEXT_TERMINATED:
170 fprintf(stderr, "Connection terminated.\n");
171 pa_context_unref(context);
172 context = NULL;
173 break;
174
175 case PA_CONTEXT_FAILED:
176 default:
177 fprintf(stderr, "Context error: %s\n", pa_strerror(pa_context_errno(c)));
178 abort();
179 }
180 }
181
182 int main(int argc, char *argv[]) {
183 int i;
184
185 for (i = 0; i < NSTREAMS; i++)
186 streams[i] = NULL;
187
188 for (i = 0; i < NTESTS; i++) {
189 connect(argv[0], &i);
190 usleep(random() % 500000);
191 disconnect();
192 usleep(random() % 500000);
193 }
194
195 fprintf(stderr, "Done.\n");
196
197 return 0;
198 }