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>
39 #define SAMPLE_HZ 8000
41 static pa_context
*context
= NULL
;
42 static pa_stream
*streams
[NSTREAMS
];
43 static pa_mainloop_api
*mainloop_api
= NULL
;
44 static const char *bname
;
46 static float data
[SAMPLE_HZ
]; /* one second space */
48 static int n_streams_ready
= 0;
50 static const pa_buffer_attr buffer_attr
= {
51 .maxlength
= SAMPLE_HZ
*sizeof(float)*NSTREAMS
, /* exactly space for the entire play time */
52 .tlength
= (uint32_t) -1,
53 .prebuf
= 0, /* Setting prebuf to 0 guarantees us the streams will run synchronously, no matter what */
54 .minreq
= (uint32_t) -1,
58 static void nop_free_cb(void *p
) {}
60 static void underflow_cb(struct pa_stream
*s
, void *userdata
) {
61 int i
= (int) (long) userdata
;
63 fprintf(stderr
, "Stream %i finished\n", i
);
65 if (++n_streams_ready
>= 2*NSTREAMS
) {
66 fprintf(stderr
, "We're done\n");
67 mainloop_api
->quit(mainloop_api
, 0);
71 /* This routine is called whenever the stream state changes */
72 static void stream_state_callback(pa_stream
*s
, void *userdata
) {
73 fail_unless(s
!= NULL
);
75 switch (pa_stream_get_state(s
)) {
76 case PA_STREAM_UNCONNECTED
:
77 case PA_STREAM_CREATING
:
78 case PA_STREAM_TERMINATED
:
81 case PA_STREAM_READY
: {
83 int r
, i
= (int) (long) userdata
;
85 fprintf(stderr
, "Writing data to stream %i.\n", i
);
87 r
= pa_stream_write(s
, data
, sizeof(data
), nop_free_cb
, (int64_t) sizeof(data
) * (int64_t) i
, PA_SEEK_ABSOLUTE
);
90 /* Be notified when this stream is drained */
91 pa_stream_set_underflow_callback(s
, underflow_cb
, userdata
);
93 /* All streams have been set up, let's go! */
94 if (++n_streams_ready
>= NSTREAMS
) {
95 fprintf(stderr
, "Uncorking\n");
96 pa_operation_unref(pa_stream_cork(s
, 0, NULL
, NULL
));
103 case PA_STREAM_FAILED
:
104 fprintf(stderr
, "Stream error: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s
))));
109 /* This is called whenever the context status changes */
110 static void context_state_callback(pa_context
*c
, void *userdata
) {
111 fail_unless(c
!= NULL
);
113 switch (pa_context_get_state(c
)) {
114 case PA_CONTEXT_CONNECTING
:
115 case PA_CONTEXT_AUTHORIZING
:
116 case PA_CONTEXT_SETTING_NAME
:
119 case PA_CONTEXT_READY
: {
122 fprintf(stderr
, "Connection established.\n");
124 for (i
= 0; i
< NSTREAMS
; i
++) {
126 pa_format_info
*formats
[1];
128 formats
[0] = pa_format_info_new();
129 formats
[0]->encoding
= PA_ENCODING_PCM
;
130 pa_format_info_set_sample_format(formats
[0], PA_SAMPLE_FLOAT32
);
131 pa_format_info_set_rate(formats
[0], SAMPLE_HZ
);
132 pa_format_info_set_channels(formats
[0], 1);
134 fprintf(stderr
, "Creating stream %i\n", i
);
136 snprintf(name
, sizeof(name
), "stream #%i", i
);
138 streams
[i
] = pa_stream_new_extended(c
, name
, formats
, 1, NULL
);
139 fail_unless(streams
[i
] != NULL
);
140 pa_stream_set_state_callback(streams
[i
], stream_state_callback
, (void*) (long) i
);
141 pa_stream_connect_playback(streams
[i
], NULL
, &buffer_attr
, PA_STREAM_START_CORKED
, NULL
, i
== 0 ? NULL
: streams
[0]);
143 pa_format_info_free(formats
[0]);
149 case PA_CONTEXT_TERMINATED
:
150 mainloop_api
->quit(mainloop_api
, 0);
153 case PA_CONTEXT_FAILED
:
155 fprintf(stderr
, "Context error: %s\n", pa_strerror(pa_context_errno(c
)));
160 START_TEST (extended_test
) {
161 pa_mainloop
* m
= NULL
;
164 for (i
= 0; i
< SAMPLE_HZ
; i
++)
165 data
[i
] = (float) sin(((double) i
/SAMPLE_HZ
)*2*M_PI
*SINE_HZ
)/2;
167 for (i
= 0; i
< NSTREAMS
; i
++)
170 /* Set up a new main loop */
171 m
= pa_mainloop_new();
172 fail_unless(m
!= NULL
);
174 mainloop_api
= pa_mainloop_get_api(m
);
176 context
= pa_context_new(mainloop_api
, bname
);
177 fail_unless(context
!= NULL
);
179 pa_context_set_state_callback(context
, context_state_callback
, NULL
);
181 /* Connect the context */
182 if (pa_context_connect(context
, NULL
, 0, NULL
) < 0) {
183 fprintf(stderr
, "pa_context_connect() failed.\n");
187 if (pa_mainloop_run(m
, &ret
) < 0)
188 fprintf(stderr
, "pa_mainloop_run() failed.\n");
191 pa_context_unref(context
);
193 for (i
= 0; i
< NSTREAMS
; i
++)
195 pa_stream_unref(streams
[i
]);
199 fail_unless(ret
== 0);
203 int main(int argc
, char *argv
[]) {
211 s
= suite_create("Extended");
212 tc
= tcase_create("extended");
213 tcase_add_test(tc
, extended_test
);
214 suite_add_tcase(s
, tc
);
216 sr
= srunner_create(s
);
217 srunner_run_all(sr
, CK_NORMAL
);
218 failed
= srunner_ntests_failed(sr
);
221 return (failed
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;