Update the Joyent http_parser.c to version 2.0, cleanup code accordingly
[deb_shairplay.git] / src / shairplay.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <math.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <assert.h>
7
8 #ifdef WIN32
9 #include <windows.h>
10 #endif
11
12 #include <shairplay/dnssd.h>
13 #include <shairplay/raop.h>
14
15 #include <ao/ao.h>
16
17 #include "config.h"
18
19 typedef struct {
20 char apname[56];
21 char password[56];
22 unsigned short port;
23
24 char ao_driver[56];
25 char ao_devicename[56];
26 char ao_deviceid[16];
27 } shairplay_options_t;
28
29 typedef struct {
30 ao_device *device;
31
32 int buffering;
33 int buflen;
34 char buffer[8192];
35
36 float volume;
37 } shairplay_session_t;
38
39
40 static ao_device *
41 audio_open_device(shairplay_options_t *opt, int bits, int channels, int samplerate)
42 {
43 ao_device *device = NULL;
44 ao_option *ao_options = NULL;
45 ao_sample_format format;
46 int driver_id;
47
48 /* Get the libao driver ID */
49 if (strlen(opt->ao_driver)) {
50 driver_id = ao_driver_id(opt->ao_driver);
51 } else {
52 driver_id = ao_default_driver_id();
53 }
54
55 /* Add all available libao options */
56 ao_append_option(&ao_options, "client_name", opt->apname);
57 if (strlen(opt->ao_devicename)) {
58 ao_append_option(&ao_options, "dev", opt->ao_devicename);
59 }
60 if (strlen(opt->ao_deviceid)) {
61 ao_append_option(&ao_options, "id", opt->ao_deviceid);
62 }
63
64 /* Set audio format */
65 memset(&format, 0, sizeof(format));
66 format.bits = bits;
67 format.channels = channels;
68 format.rate = samplerate;
69 format.byte_format = AO_FMT_NATIVE;
70
71 /* Try opening the actual device */
72 device = ao_open_live(driver_id, &format, ao_options);
73 ao_free_options(ao_options);
74 return device;
75 }
76
77 static void *
78 audio_init(void *cls, int bits, int channels, int samplerate)
79 {
80 shairplay_options_t *options = cls;
81 shairplay_session_t *session;
82
83 session = calloc(1, sizeof(shairplay_session_t));
84 assert(session);
85
86 session->device = audio_open_device(options, bits, channels, samplerate);
87 if (session->device == NULL) {
88 printf("Error opening device %d\n", errno);
89 }
90 assert(session->device);
91
92 session->buffering = 1;
93 session->volume = 1.0f;
94 return session;
95 }
96
97 static int
98 audio_output(shairplay_session_t *session, const void *buffer, int buflen)
99 {
100 short *shortbuf;
101 char tmpbuf[4096];
102 int tmpbuflen, i;
103
104 tmpbuflen = (buflen > sizeof(tmpbuf)) ? sizeof(tmpbuf) : buflen;
105 memcpy(tmpbuf, buffer, tmpbuflen);
106 if (ao_is_big_endian()) {
107 for (i=0; i<tmpbuflen/2; i++) {
108 char tmpch = tmpbuf[i*2];
109 tmpbuf[i*2] = tmpbuf[i*2+1];
110 tmpbuf[i*2+1] = tmpch;
111 }
112 }
113 shortbuf = (short *)tmpbuf;
114 for (i=0; i<tmpbuflen/2; i++) {
115 shortbuf[i] = shortbuf[i] * session->volume;
116 }
117 ao_play(session->device, tmpbuf, tmpbuflen);
118 return tmpbuflen;
119 }
120
121 static void
122 audio_process(void *cls, void *opaque, const void *buffer, int buflen)
123 {
124 shairplay_session_t *session = opaque;
125 int processed;
126
127 if (session->buffering) {
128 printf("Buffering...\n");
129 if (session->buflen+buflen < sizeof(session->buffer)) {
130 memcpy(session->buffer+session->buflen, buffer, buflen);
131 session->buflen += buflen;
132 return;
133 }
134 session->buffering = 0;
135 printf("Finished buffering...\n");
136
137 processed = 0;
138 while (processed < session->buflen) {
139 processed += audio_output(session,
140 session->buffer+processed,
141 session->buflen-processed);
142 }
143 session->buflen = 0;
144 }
145
146 processed = 0;
147 while (processed < buflen) {
148 processed += audio_output(session,
149 buffer+processed,
150 buflen-processed);
151 }
152 }
153
154 static void
155 audio_destroy(void *cls, void *opaque)
156 {
157 shairplay_session_t *session = opaque;
158
159 ao_close(session->device);
160 free(session);
161 }
162
163 static void
164 audio_set_volume(void *cls, void *opaque, float volume)
165 {
166 shairplay_session_t *session = opaque;
167 session->volume = pow(10.0, 0.05*volume);
168 }
169
170 static int
171 parse_options(shairplay_options_t *opt, int argc, char *argv[])
172 {
173 char *path = argv[0];
174 char *arg;
175
176 strcpy(opt->apname, "Shairplay");
177 opt->port = 5000;
178
179 while ((arg = *++argv)) {
180 if (!strcmp(arg, "-a")) {
181 strncpy(opt->apname, *++argv, sizeof(opt->apname)-1);
182 } else if (!strncmp(arg, "--apname=", 9)) {
183 strncpy(opt->apname, arg+9, sizeof(opt->apname)-1);
184 } else if (!strcmp(arg, "-p")) {
185 strncpy(opt->password, *++argv, sizeof(opt->password)-1);
186 } else if (!strncmp(arg, "--password=", 11)) {
187 strncpy(opt->password, arg+11, sizeof(opt->password)-1);
188 } else if (!strcmp(arg, "-o")) {
189 opt->port = atoi(*++argv);
190 } else if (!strncmp(arg, "--server_port=", 14)) {
191 opt->port = atoi(arg+14);
192 } else if (!strncmp(arg, "--ao_driver=", 12)) {
193 strncpy(opt->ao_driver, arg+12, sizeof(opt->ao_driver)-1);
194 } else if (!strncmp(arg, "--ao_devicename=", 16)) {
195 strncpy(opt->ao_devicename, arg+16, sizeof(opt->ao_devicename)-1);
196 } else if (!strncmp(arg, "--ao_deviceid=", 14)) {
197 strncpy(opt->ao_deviceid, arg+14, sizeof(opt->ao_deviceid)-1);
198 } else if (!strcmp(arg, "-h") || !strcmp(arg, "--help")) {
199 fprintf(stderr, "Shairplay version %s\n", VERSION);
200 fprintf(stderr, "Usage: %s [OPTION...]\n", path);
201 fprintf(stderr, "\n");
202 fprintf(stderr, " -a, --apname=AirPort Sets Airport name\n");
203 fprintf(stderr, " -p, --password=secret Sets password\n");
204 fprintf(stderr, " -o, --server_port=5000 Sets port for RAOP service\n");
205 fprintf(stderr, " --ao_driver=driver Sets the ao driver (optional)\n");
206 fprintf(stderr, " --ao_devicename=devicename Sets the ao device name (optional)\n");
207 fprintf(stderr, " --ao_deviceid=id Sets the ao device id (optional)\n");
208 fprintf(stderr, " -h, --help This help\n");
209 fprintf(stderr, "\n");
210 return 1;
211 }
212 }
213
214 /* Set default values for apname and port */
215 if (!strlen(opt->apname)) {
216 strncpy(opt->apname, "Shairplay", sizeof(opt->apname)-1);
217 }
218 if (!opt->port) {
219 opt->port = 5000;
220 }
221 return 0;
222 }
223
224 int
225 main(int argc, char *argv[])
226 {
227 const char hwaddr[] = { 0x48, 0x5d, 0x60, 0x7c, 0xee, 0x22 };
228
229 shairplay_options_t options;
230 ao_device *device = NULL;
231
232 dnssd_t *dnssd;
233 raop_t *raop;
234 raop_callbacks_t raop_cbs;
235
236 memset(&options, 0, sizeof(options));
237 if (parse_options(&options, argc, argv)) {
238 return 0;
239 }
240
241 ao_initialize();
242
243 device = audio_open_device(&options, 16, 2, 44100);
244 if (device == NULL) {
245 fprintf(stderr, "Error opening audio device %d\n", errno);
246 fprintf(stderr, "Please check your libao settings and try again\n");
247 return -1;
248 } else {
249 ao_close(device);
250 device = NULL;
251 }
252
253 memset(&raop_cbs, 0, sizeof(raop_cbs));
254 raop_cbs.cls = &options;
255 raop_cbs.audio_init = audio_init;
256 raop_cbs.audio_process = audio_process;
257 raop_cbs.audio_destroy = audio_destroy;
258 raop_cbs.audio_set_volume = audio_set_volume;
259
260 raop = raop_init_from_keyfile(10, &raop_cbs, "airport.key", NULL);
261 if (raop == NULL) {
262 fprintf(stderr, "Could not initialize the RAOP service\n");
263 return -1;
264 }
265
266 raop_set_log_level(raop, RAOP_LOG_DEBUG);
267 raop_start(raop, &options.port, hwaddr, sizeof(hwaddr), NULL);
268
269 dnssd = dnssd_init(NULL);
270 dnssd_register_raop(dnssd, options.apname, options.port, hwaddr, sizeof(hwaddr), 0);
271
272 #ifndef WIN32
273 sleep(100);
274 #else
275 Sleep(100*1000);
276 #endif
277
278 dnssd_unregister_raop(dnssd);
279 dnssd_destroy(dnssd);
280
281 raop_stop(raop);
282 raop_destroy(raop);
283
284 ao_shutdown();
285
286 return 0;
287 }