add output stream draining
[platform/upstream/pulseaudio.git] / src / pacat.c
1 #include <signal.h>
2 #include <string.h>
3 #include <errno.h>
4 #include <unistd.h>
5 #include <assert.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 #include "polyp.h"
10 #include "polyp-error.h"
11 #include "mainloop.h"
12 #include "mainloop-signal.h"
13
14 static struct pa_context *context = NULL;
15 static struct pa_stream *stream = NULL;
16 static struct pa_mainloop_api *mainloop_api = NULL;
17
18 static void *buffer = NULL;
19 static size_t buffer_length = 0, buffer_index = 0;
20
21 static void* stdin_source = NULL;
22
23 static void quit(int ret) {
24     assert(mainloop_api);
25     mainloop_api->quit(mainloop_api, ret);
26 }
27
28 static void context_die_callback(struct pa_context *c, void *userdata) {
29     assert(c);
30     fprintf(stderr, "Connection to server shut down, exiting.\n");
31     quit(1);
32 }
33
34 static void stream_die_callback(struct pa_stream *s, void *userdata) {
35     assert(s);
36     fprintf(stderr, "Stream deleted, exiting.\n");
37     quit(1);
38 }
39
40 static void do_write(size_t length) {
41     size_t l;
42     assert(length);
43
44     if (!buffer || !buffer_length)
45         return;
46     
47     l = length;
48     if (l > buffer_length)
49         l = buffer_length;
50     
51     pa_stream_write(stream, buffer+buffer_index, l);
52     buffer_length -= l;
53     buffer_index += l;
54     
55     if (!buffer_length) {
56         free(buffer);
57         buffer = NULL;
58         buffer_index = buffer_length = 0;
59     }
60 }
61
62 static void stream_write_callback(struct pa_stream *s, size_t length, void *userdata) {
63     assert(s && length);
64
65     if (stdin_source)
66         mainloop_api->enable_io(mainloop_api, stdin_source, PA_MAINLOOP_API_IO_EVENT_INPUT);
67
68     if (!buffer)
69         return;
70
71     do_write(length);
72 }
73
74 static void stream_complete_callback(struct pa_stream*s, int success, void *userdata) {
75     assert(s);
76
77     if (!success) {
78         fprintf(stderr, "Stream creation failed: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
79         quit(1);
80         return;
81     }
82
83     fprintf(stderr, "Stream created.\n");
84 }
85
86 static void context_complete_callback(struct pa_context *c, int success, void *userdata) {
87     static const struct pa_sample_spec ss = {
88         .format = PA_SAMPLE_S16LE,
89         .rate = 44100,
90         .channels = 2
91     };
92         
93     assert(c && !stream);
94
95     if (!success) {
96         fprintf(stderr, "Connection failed: %s\n", pa_strerror(pa_context_errno(c)));
97         goto fail;
98     }
99
100     fprintf(stderr, "Connection established.\n");
101     
102     if (!(stream = pa_stream_new(c, PA_STREAM_PLAYBACK, NULL, "pacat", &ss, NULL, stream_complete_callback, NULL))) {
103         fprintf(stderr, "pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(c)));
104         goto fail;
105     }
106
107     pa_stream_set_die_callback(stream, stream_die_callback, NULL);
108     pa_stream_set_write_callback(stream, stream_write_callback, NULL);
109     
110     return;
111     
112 fail:
113     quit(1);
114 }
115
116 static void context_drain_complete(struct pa_context*c, void *userdata) {
117     quit(0);
118 }
119
120 static void stream_drain_complete(struct pa_stream*s, void *userdata) {
121     fprintf(stderr, "Playback stream drained.\n");
122
123     pa_stream_free(stream);
124     stream = NULL;
125     
126     if (pa_context_drain(context, context_drain_complete, NULL) < 0)
127         quit(0);
128     else
129         fprintf(stderr, "Draining connection to server.\n");
130 }
131
132 static void stdin_callback(struct pa_mainloop_api*a, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
133     size_t l, w = 0;
134     ssize_t r;
135     assert(a == mainloop_api && id && fd == STDIN_FILENO && events == PA_MAINLOOP_API_IO_EVENT_INPUT && stdin_source == id);
136
137     if (buffer) {
138         mainloop_api->enable_io(mainloop_api, stdin_source, PA_MAINLOOP_API_IO_EVENT_NULL);
139         return;
140     }
141
142     if (!stream || !pa_stream_is_ready(stream) || !(l = w = pa_stream_writable_size(stream)))
143         l = 4096;
144     
145     buffer = malloc(l);
146     assert(buffer);
147     if ((r = read(fd, buffer, l)) <= 0) {
148         if (r == 0) {
149             fprintf(stderr, "Got EOF.\n");
150             pa_stream_drain(stream, stream_drain_complete, NULL);
151         } else {
152             fprintf(stderr, "read() failed: %s\n", strerror(errno));
153             quit(1);
154         }
155
156         mainloop_api->cancel_io(mainloop_api, stdin_source);
157         stdin_source = NULL;
158         return;
159     }
160
161     buffer_length = r;
162     buffer_index = 0;
163
164     if (w)
165         do_write(w);
166 }
167
168
169 static void exit_signal_callback(void *id, int sig, void *userdata) {
170     fprintf(stderr, "Got SIGINT, exiting.\n");
171     quit(0);
172 }
173
174 int main(int argc, char *argv[]) {
175     struct pa_mainloop* m;
176     int ret = 1, r;
177
178     if (!(m = pa_mainloop_new())) {
179         fprintf(stderr, "pa_mainloop_new() failed.\n");
180         goto quit;
181     }
182
183     mainloop_api = pa_mainloop_get_api(m);
184
185     r = pa_signal_init(mainloop_api);
186     assert(r == 0);
187     pa_signal_register(SIGINT, exit_signal_callback, NULL);
188     signal(SIGPIPE, SIG_IGN);
189     
190     if (!(stdin_source = mainloop_api->source_io(mainloop_api, STDIN_FILENO, PA_MAINLOOP_API_IO_EVENT_INPUT, stdin_callback, NULL))) {
191         fprintf(stderr, "source_io() failed.\n");
192         goto quit;
193     }
194     
195     if (!(context = pa_context_new(mainloop_api, argv[0]))) {
196         fprintf(stderr, "pa_context_new() failed.\n");
197         goto quit;
198     }
199
200     if (pa_context_connect(context, NULL, context_complete_callback, NULL) < 0) {
201         fprintf(stderr, "pa_context_connext() failed.\n");
202         goto quit;
203     }
204         
205     pa_context_set_die_callback(context, context_die_callback, NULL);
206
207     if (pa_mainloop_run(m, &ret) < 0) {
208         fprintf(stderr, "pa_mainloop_run() failed.\n");
209         goto quit;
210     }
211     
212 quit:
213     if (stream)
214         pa_stream_free(stream);
215     if (context)
216         pa_context_free(context);
217     if (m)
218         pa_mainloop_free(m);
219     if (buffer)
220         free(buffer);
221     
222     return ret;
223 }