75a94fc073594903030e733eb65daab6a9805c5c
[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 stdin_callback(struct pa_mainloop_api*a, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
121     size_t l, w = 0;
122     ssize_t r;
123     assert(a == mainloop_api && id && fd == STDIN_FILENO && events == PA_MAINLOOP_API_IO_EVENT_INPUT && stdin_source == id);
124
125     if (buffer) {
126         mainloop_api->enable_io(mainloop_api, stdin_source, PA_MAINLOOP_API_IO_EVENT_NULL);
127         return;
128     }
129
130     if (!stream || !pa_stream_is_ready(stream) || !(l = w = pa_stream_writable_size(stream)))
131         l = 4096;
132     
133     buffer = malloc(l);
134     assert(buffer);
135     if ((r = read(fd, buffer, l)) <= 0) {
136         if (r == 0) {
137             fprintf(stderr, "Got EOF.\n");
138             if (pa_context_drain(context, context_drain_complete, NULL) < 0)
139                 quit(0);
140             else
141                 fprintf(stderr, "Draining connection to server.\n");
142         } else {
143             fprintf(stderr, "read() failed: %s\n", strerror(errno));
144             quit(1);
145         }
146
147         mainloop_api->cancel_io(mainloop_api, stdin_source);
148         stdin_source = NULL;
149         return;
150     }
151
152     buffer_length = r;
153     buffer_index = 0;
154
155     if (w)
156         do_write(w);
157 }
158
159
160 static void exit_signal_callback(void *id, int sig, void *userdata) {
161     fprintf(stderr, "Got SIGINT, exiting.\n");
162     quit(0);
163 }
164
165 int main(int argc, char *argv[]) {
166     struct pa_mainloop* m;
167     int ret = 1, r;
168
169     if (!(m = pa_mainloop_new())) {
170         fprintf(stderr, "pa_mainloop_new() failed.\n");
171         goto quit;
172     }
173
174     mainloop_api = pa_mainloop_get_api(m);
175
176     r = pa_signal_init(mainloop_api);
177     assert(r == 0);
178     pa_signal_register(SIGINT, exit_signal_callback, NULL);
179     signal(SIGPIPE, SIG_IGN);
180     
181     if (!(stdin_source = mainloop_api->source_io(mainloop_api, STDIN_FILENO, PA_MAINLOOP_API_IO_EVENT_INPUT, stdin_callback, NULL))) {
182         fprintf(stderr, "source_io() failed.\n");
183         goto quit;
184     }
185     
186     if (!(context = pa_context_new(mainloop_api, argv[0]))) {
187         fprintf(stderr, "pa_context_new() failed.\n");
188         goto quit;
189     }
190
191     if (pa_context_connect(context, NULL, context_complete_callback, NULL) < 0) {
192         fprintf(stderr, "pa_context_connext() failed.\n");
193         goto quit;
194     }
195         
196     pa_context_set_die_callback(context, context_die_callback, NULL);
197
198     if (pa_mainloop_run(m, &ret) < 0) {
199         fprintf(stderr, "pa_mainloop_run() failed.\n");
200         goto quit;
201     }
202     
203 quit:
204     if (stream)
205         pa_stream_free(stream);
206     if (context)
207         pa_context_free(context);
208     if (m)
209         pa_mainloop_free(m);
210     if (buffer)
211         free(buffer);
212     
213     return ret;
214 }