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