11 static struct pa_context *context = NULL;
12 static struct pa_stream *stream = NULL;
13 static struct pa_mainloop_api *mainloop_api = NULL;
15 static void *buffer = NULL;
16 static size_t buffer_length = 0, buffer_index = 0;
18 static void* stdin_source = NULL;
20 static void context_die_callback(struct pa_context *c, void *userdata) {
22 fprintf(stderr, "Connection to server shut down, exiting.\n");
23 mainloop_api->quit(mainloop_api, 1);
26 static void stream_die_callback(struct pa_stream *s, void *userdata) {
28 fprintf(stderr, "Stream deleted, exiting.\n");
29 mainloop_api->quit(mainloop_api, 1);
32 static void stream_write_callback(struct pa_stream *s, size_t length, void *userdata) {
36 mainloop_api->enable_io(mainloop_api, stdin_source, PA_STREAM_PLAYBACK);
41 assert(buffer_length);
44 if (l > buffer_length)
47 pa_stream_write(s, buffer+buffer_index, l);
54 buffer_index = buffer_length = 0;
58 static void stream_complete_callback(struct pa_context*c, struct pa_stream *s, void *userdata) {
62 fprintf(stderr, "Stream creation failed.\n");
63 mainloop_api->quit(mainloop_api, 1);
67 pa_stream_set_die_callback(stream, stream_die_callback, NULL);
68 pa_stream_set_write_callback(stream, stream_write_callback, NULL);
71 static void context_complete_callback(struct pa_context *c, int success, void *userdata) {
72 static const struct pa_sample_spec ss = {
73 .format = SAMPLE_S16NE,
81 fprintf(stderr, "Connection failed\n");
85 if (pa_stream_new(c, PA_STREAM_PLAYBACK, NULL, "pacat", &ss, NULL, stream_complete_callback, NULL) < 0) {
86 fprintf(stderr, "pa_stream_new() failed.\n");
93 mainloop_api->quit(mainloop_api, 1);
96 static void stdin_callback(struct pa_mainloop_api*a, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
99 assert(a == mainloop_api && id && fd == STDIN_FILENO && events == PA_MAINLOOP_API_IO_EVENT_INPUT);
102 mainloop_api->enable_io(mainloop_api, stdin_source, PA_MAINLOOP_API_IO_EVENT_NULL);
106 if (!(l = pa_stream_writable_size(stream)))
110 if ((r = read(fd, buffer, l)) <= 0) {
112 mainloop_api->quit(mainloop_api, 0);
114 fprintf(stderr, "read() failed: %s\n", strerror(errno));
115 mainloop_api->quit(mainloop_api, 1);
125 int main(int argc, char *argv[]) {
126 struct pa_mainloop* m;
129 if (!(m = pa_mainloop_new())) {
130 fprintf(stderr, "pa_mainloop_new() failed.\n");
134 mainloop_api = pa_mainloop_get_api(m);
136 if (!(stdin_source = mainloop_api->source_io(mainloop_api, STDIN_FILENO, PA_MAINLOOP_API_IO_EVENT_INPUT, stdin_callback, NULL))) {
137 fprintf(stderr, "source_io() failed.\n");
141 if (!(context = pa_context_new(mainloop_api, argv[0]))) {
142 fprintf(stderr, "pa_context_new() failed.\n");
146 if (pa_context_connect(context, NULL, context_complete_callback, NULL) < 0) {
147 fprintf(stderr, "pa_context_connext() failed.\n");
151 pa_context_set_die_callback(context, context_die_callback, NULL);
153 if (pa_mainloop_run(m, &ret) < 0) {
154 fprintf(stderr, "pa_mainloop_run() failed.\n");
160 pa_stream_free(stream);
162 pa_context_free(context);