10 #include "polyp-error.h"
12 #include "mainloop-signal.h"
14 static enum { RECORD, PLAYBACK } mode = PLAYBACK;
16 static struct pa_context *context = NULL;
17 static struct pa_stream *stream = NULL;
18 static struct pa_mainloop_api *mainloop_api = NULL;
20 static void *buffer = NULL;
21 static size_t buffer_length = 0, buffer_index = 0;
23 static void* stdio_source = NULL;
25 static void quit(int ret) {
27 mainloop_api->quit(mainloop_api, ret);
30 static void context_die_callback(struct pa_context *c, void *userdata) {
32 fprintf(stderr, "Connection to server shut down, exiting.\n");
36 static void stream_die_callback(struct pa_stream *s, void *userdata) {
38 fprintf(stderr, "Stream deleted, exiting.\n");
42 static void do_stream_write(size_t length) {
46 if (!buffer || !buffer_length)
50 if (l > buffer_length)
53 pa_stream_write(stream, buffer+buffer_index, l);
60 buffer_index = buffer_length = 0;
64 static void stream_write_callback(struct pa_stream *s, size_t length, void *userdata) {
68 mainloop_api->enable_io(mainloop_api, stdio_source, PA_MAINLOOP_API_IO_EVENT_INPUT);
73 do_stream_write(length);
76 static void stream_read_callback(struct pa_stream *s, const void*data, size_t length, void *userdata) {
77 assert(s && data && length);
80 mainloop_api->enable_io(mainloop_api, stdio_source, PA_MAINLOOP_API_IO_EVENT_OUTPUT);
83 fprintf(stderr, "Buffer overrrun, dropping incoming data\n");
87 buffer = malloc(buffer_length = length);
89 memcpy(buffer, data, length);
93 static void stream_complete_callback(struct pa_stream*s, int success, void *userdata) {
97 fprintf(stderr, "Stream creation failed: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
102 fprintf(stderr, "Stream created.\n");
105 static void context_complete_callback(struct pa_context *c, int success, void *userdata) {
106 static const struct pa_sample_spec ss = {
107 .format = PA_SAMPLE_S16LE,
112 assert(c && !stream);
115 fprintf(stderr, "Connection failed: %s\n", pa_strerror(pa_context_errno(c)));
119 fprintf(stderr, "Connection established.\n");
121 if (!(stream = pa_stream_new(c, mode == PLAYBACK ? PA_STREAM_PLAYBACK : PA_STREAM_RECORD, NULL, "pacat", &ss, NULL, stream_complete_callback, NULL))) {
122 fprintf(stderr, "pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(c)));
126 pa_stream_set_die_callback(stream, stream_die_callback, NULL);
127 pa_stream_set_write_callback(stream, stream_write_callback, NULL);
128 pa_stream_set_read_callback(stream, stream_read_callback, NULL);
136 static void context_drain_complete(struct pa_context*c, void *userdata) {
140 static void stream_drain_complete(struct pa_stream*s, void *userdata) {
141 fprintf(stderr, "Playback stream drained.\n");
143 pa_stream_free(stream);
146 if (pa_context_drain(context, context_drain_complete, NULL) < 0)
149 fprintf(stderr, "Draining connection to server.\n");
152 static void stdin_callback(struct pa_mainloop_api*a, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
155 assert(a == mainloop_api && id && stdio_source == id);
158 mainloop_api->enable_io(mainloop_api, stdio_source, PA_MAINLOOP_API_IO_EVENT_NULL);
162 if (!stream || !pa_stream_is_ready(stream) || !(l = w = pa_stream_writable_size(stream)))
167 if ((r = read(fd, buffer, l)) <= 0) {
169 fprintf(stderr, "Got EOF.\n");
170 pa_stream_drain(stream, stream_drain_complete, NULL);
172 fprintf(stderr, "read() failed: %s\n", strerror(errno));
176 mainloop_api->cancel_io(mainloop_api, stdio_source);
188 static void stdout_callback(struct pa_mainloop_api*a, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
190 assert(a == mainloop_api && id && stdio_source == id);
193 mainloop_api->enable_io(mainloop_api, stdio_source, PA_MAINLOOP_API_IO_EVENT_NULL);
197 assert(buffer_length);
199 if ((r = write(fd, buffer+buffer_index, buffer_length)) <= 0) {
200 fprintf(stderr, "write() failed: %s\n", strerror(errno));
203 mainloop_api->cancel_io(mainloop_api, stdio_source);
211 if (!buffer_length) {
214 buffer_length = buffer_index = 0;
218 static void exit_signal_callback(void *id, int sig, void *userdata) {
219 fprintf(stderr, "Got SIGINT, exiting.\n");
224 static void stream_get_latency_callback(struct pa_stream *s, uint32_t latency, void *userdata) {
227 if (latency == (uint32_t) -1) {
228 fprintf(stderr, "Failed to get latency: %s\n", strerror(errno));
233 fprintf(stderr, "Current latency is %u usecs.\n", latency);
236 static void sigusr1_signal_callback(void *id, int sig, void *userdata) {
237 if (mode == PLAYBACK) {
238 fprintf(stderr, "Got SIGUSR1, requesting latency.\n");
239 pa_stream_get_latency(stream, stream_get_latency_callback, NULL);
243 int main(int argc, char *argv[]) {
244 struct pa_mainloop* m = NULL;
248 if (!(bn = strrchr(argv[0], '/')))
251 if (strstr(bn, "rec") || strstr(bn, "mon"))
253 else if (strstr(bn, "cat") || strstr(bn, "play"))
256 fprintf(stderr, "Opening a %s stream.\n", mode == RECORD ? "recording" : "playback");
258 if (!(m = pa_mainloop_new())) {
259 fprintf(stderr, "pa_mainloop_new() failed.\n");
263 mainloop_api = pa_mainloop_get_api(m);
265 r = pa_signal_init(mainloop_api);
267 pa_signal_register(SIGINT, exit_signal_callback, NULL);
268 pa_signal_register(SIGUSR1, sigusr1_signal_callback, NULL);
269 signal(SIGPIPE, SIG_IGN);
271 if (!(stdio_source = mainloop_api->source_io(mainloop_api,
272 mode == PLAYBACK ? STDIN_FILENO : STDOUT_FILENO,
273 mode == PLAYBACK ? PA_MAINLOOP_API_IO_EVENT_INPUT : PA_MAINLOOP_API_IO_EVENT_OUTPUT,
274 mode == PLAYBACK ? stdin_callback : stdout_callback, NULL))) {
275 fprintf(stderr, "source_io() failed.\n");
279 if (!(context = pa_context_new(mainloop_api, argv[0]))) {
280 fprintf(stderr, "pa_context_new() failed.\n");
284 if (pa_context_connect(context, NULL, context_complete_callback, NULL) < 0) {
285 fprintf(stderr, "pa_context_connext() failed.\n");
289 pa_context_set_die_callback(context, context_die_callback, NULL);
291 if (pa_mainloop_run(m, &ret) < 0) {
292 fprintf(stderr, "pa_mainloop_run() failed.\n");
298 pa_stream_free(stream);
300 pa_context_free(context);