Fix #23 on issue tracker.
[deb_shairplay.git] / src / shairplay.c
CommitLineData
668e175b
JVH
1/**
2 * Copyright (C) 2012-2013 Juho Vähä-Herttua
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
ad58f7a2
JVH
24#include <stdlib.h>
25#include <stdio.h>
26#include <math.h>
27#include <string.h>
28#include <unistd.h>
29#include <assert.h>
30
31#ifdef WIN32
2c4250f1 32# include <windows.h>
ad58f7a2
JVH
33#endif
34
35#include <shairplay/dnssd.h>
36#include <shairplay/raop.h>
37
38#include <ao/ao.h>
39
40#include "config.h"
41
42typedef struct {
43 char apname[56];
44 char password[56];
45 unsigned short port;
c4312a61 46 char hwaddr[6];
ad58f7a2
JVH
47
48 char ao_driver[56];
49 char ao_devicename[56];
50 char ao_deviceid[16];
51} shairplay_options_t;
52
53typedef struct {
54 ao_device *device;
55
56 int buffering;
57 int buflen;
58 char buffer[8192];
59
60 float volume;
61} shairplay_session_t;
62
63
258e9fb8
JVH
64static int running;
65
2c4250f1
JVH
66#ifndef WIN32
67
68#include <signal.h>
69static void
70signal_handler(int sig)
71{
72 switch (sig) {
73 case SIGINT:
74 case SIGTERM:
75 running = 0;
76 break;
77 }
78}
79static void
80init_signals(void)
81{
82 struct sigaction sigact;
83
84 sigact.sa_handler = signal_handler;
85 sigemptyset(&sigact.sa_mask);
86 sigact.sa_flags = 0;
87 sigaction(SIGINT, &sigact, NULL);
88 sigaction(SIGTERM, &sigact, NULL);
89}
90
91#endif
92
258e9fb8 93
c4312a61
JVH
94static int
95parse_hwaddr(const char *str, char *hwaddr, int hwaddrlen)
96{
97 int slen, i;
c4312a61
JVH
98
99 slen = 3*hwaddrlen-1;
100 if (strlen(str) != slen) {
101 return 1;
102 }
103 for (i=0; i<slen; i++) {
104 if (str[i] == ':' && (i%3 == 2)) {
105 continue;
106 }
107 if (str[i] >= '0' && str[i] <= '9') {
108 continue;
109 }
110 if (str[i] >= 'a' && str[i] <= 'f') {
111 continue;
112 }
113 return 1;
114 }
115 for (i=0; i<hwaddrlen; i++) {
116 hwaddr[i] = (char) strtol(str+(i*3), NULL, 16);
117 }
118 return 0;
119}
120
ad58f7a2
JVH
121static ao_device *
122audio_open_device(shairplay_options_t *opt, int bits, int channels, int samplerate)
123{
124 ao_device *device = NULL;
125 ao_option *ao_options = NULL;
126 ao_sample_format format;
127 int driver_id;
128
129 /* Get the libao driver ID */
130 if (strlen(opt->ao_driver)) {
131 driver_id = ao_driver_id(opt->ao_driver);
132 } else {
133 driver_id = ao_default_driver_id();
134 }
135
136 /* Add all available libao options */
ad58f7a2
JVH
137 if (strlen(opt->ao_devicename)) {
138 ao_append_option(&ao_options, "dev", opt->ao_devicename);
139 }
140 if (strlen(opt->ao_deviceid)) {
141 ao_append_option(&ao_options, "id", opt->ao_deviceid);
142 }
143
144 /* Set audio format */
145 memset(&format, 0, sizeof(format));
146 format.bits = bits;
147 format.channels = channels;
148 format.rate = samplerate;
149 format.byte_format = AO_FMT_NATIVE;
150
151 /* Try opening the actual device */
152 device = ao_open_live(driver_id, &format, ao_options);
153 ao_free_options(ao_options);
154 return device;
155}
156
157static void *
158audio_init(void *cls, int bits, int channels, int samplerate)
159{
160 shairplay_options_t *options = cls;
161 shairplay_session_t *session;
162
163 session = calloc(1, sizeof(shairplay_session_t));
164 assert(session);
165
166 session->device = audio_open_device(options, bits, channels, samplerate);
167 if (session->device == NULL) {
168 printf("Error opening device %d\n", errno);
169 }
170 assert(session->device);
171
172 session->buffering = 1;
173 session->volume = 1.0f;
174 return session;
175}
176
177static int
178audio_output(shairplay_session_t *session, const void *buffer, int buflen)
179{
180 short *shortbuf;
181 char tmpbuf[4096];
182 int tmpbuflen, i;
183
184 tmpbuflen = (buflen > sizeof(tmpbuf)) ? sizeof(tmpbuf) : buflen;
185 memcpy(tmpbuf, buffer, tmpbuflen);
186 if (ao_is_big_endian()) {
187 for (i=0; i<tmpbuflen/2; i++) {
188 char tmpch = tmpbuf[i*2];
189 tmpbuf[i*2] = tmpbuf[i*2+1];
190 tmpbuf[i*2+1] = tmpch;
191 }
192 }
193 shortbuf = (short *)tmpbuf;
194 for (i=0; i<tmpbuflen/2; i++) {
195 shortbuf[i] = shortbuf[i] * session->volume;
196 }
197 ao_play(session->device, tmpbuf, tmpbuflen);
198 return tmpbuflen;
199}
200
201static void
202audio_process(void *cls, void *opaque, const void *buffer, int buflen)
203{
204 shairplay_session_t *session = opaque;
205 int processed;
206
207 if (session->buffering) {
208 printf("Buffering...\n");
209 if (session->buflen+buflen < sizeof(session->buffer)) {
210 memcpy(session->buffer+session->buflen, buffer, buflen);
211 session->buflen += buflen;
212 return;
213 }
214 session->buffering = 0;
215 printf("Finished buffering...\n");
216
217 processed = 0;
218 while (processed < session->buflen) {
219 processed += audio_output(session,
220 session->buffer+processed,
221 session->buflen-processed);
222 }
223 session->buflen = 0;
224 }
225
226 processed = 0;
227 while (processed < buflen) {
228 processed += audio_output(session,
229 buffer+processed,
230 buflen-processed);
231 }
232}
233
234static void
235audio_destroy(void *cls, void *opaque)
236{
237 shairplay_session_t *session = opaque;
238
239 ao_close(session->device);
240 free(session);
241}
242
243static void
244audio_set_volume(void *cls, void *opaque, float volume)
245{
246 shairplay_session_t *session = opaque;
247 session->volume = pow(10.0, 0.05*volume);
248}
249
250static int
251parse_options(shairplay_options_t *opt, int argc, char *argv[])
252{
c4312a61
JVH
253 const char default_hwaddr[] = { 0x48, 0x5d, 0x60, 0x7c, 0xee, 0x22 };
254
ad58f7a2
JVH
255 char *path = argv[0];
256 char *arg;
257
093f6e00
JVH
258 /* Set default values for apname and port */
259 strncpy(opt->apname, "Shairplay", sizeof(opt->apname)-1);
ad58f7a2 260 opt->port = 5000;
c4312a61 261 memcpy(opt->hwaddr, default_hwaddr, sizeof(opt->hwaddr));
ad58f7a2
JVH
262
263 while ((arg = *++argv)) {
264 if (!strcmp(arg, "-a")) {
265 strncpy(opt->apname, *++argv, sizeof(opt->apname)-1);
266 } else if (!strncmp(arg, "--apname=", 9)) {
267 strncpy(opt->apname, arg+9, sizeof(opt->apname)-1);
268 } else if (!strcmp(arg, "-p")) {
269 strncpy(opt->password, *++argv, sizeof(opt->password)-1);
270 } else if (!strncmp(arg, "--password=", 11)) {
271 strncpy(opt->password, arg+11, sizeof(opt->password)-1);
272 } else if (!strcmp(arg, "-o")) {
273 opt->port = atoi(*++argv);
274 } else if (!strncmp(arg, "--server_port=", 14)) {
275 opt->port = atoi(arg+14);
c4312a61
JVH
276 } else if (!strncmp(arg, "--hwaddr=", 9)) {
277 if (parse_hwaddr(arg+9, opt->hwaddr, sizeof(opt->hwaddr))) {
278 fprintf(stderr, "Invalid format given for hwaddr, aborting...\n");
279 fprintf(stderr, "Please use hwaddr format: 01:45:89:ab:cd:ef\n");
280 return 1;
281 }
ad58f7a2
JVH
282 } else if (!strncmp(arg, "--ao_driver=", 12)) {
283 strncpy(opt->ao_driver, arg+12, sizeof(opt->ao_driver)-1);
284 } else if (!strncmp(arg, "--ao_devicename=", 16)) {
285 strncpy(opt->ao_devicename, arg+16, sizeof(opt->ao_devicename)-1);
286 } else if (!strncmp(arg, "--ao_deviceid=", 14)) {
287 strncpy(opt->ao_deviceid, arg+14, sizeof(opt->ao_deviceid)-1);
288 } else if (!strcmp(arg, "-h") || !strcmp(arg, "--help")) {
289 fprintf(stderr, "Shairplay version %s\n", VERSION);
290 fprintf(stderr, "Usage: %s [OPTION...]\n", path);
291 fprintf(stderr, "\n");
292 fprintf(stderr, " -a, --apname=AirPort Sets Airport name\n");
293 fprintf(stderr, " -p, --password=secret Sets password\n");
294 fprintf(stderr, " -o, --server_port=5000 Sets port for RAOP service\n");
c8920724 295 fprintf(stderr, " --hwaddr=address Sets the MAC address, useful if running multiple instances\n");
ad58f7a2
JVH
296 fprintf(stderr, " --ao_driver=driver Sets the ao driver (optional)\n");
297 fprintf(stderr, " --ao_devicename=devicename Sets the ao device name (optional)\n");
298 fprintf(stderr, " --ao_deviceid=id Sets the ao device id (optional)\n");
299 fprintf(stderr, " -h, --help This help\n");
300 fprintf(stderr, "\n");
301 return 1;
302 }
303 }
304
ad58f7a2
JVH
305 return 0;
306}
307
308int
309main(int argc, char *argv[])
310{
ad58f7a2
JVH
311 shairplay_options_t options;
312 ao_device *device = NULL;
313
314 dnssd_t *dnssd;
315 raop_t *raop;
316 raop_callbacks_t raop_cbs;
c6e5e8b9 317 char *password = NULL;
ad58f7a2 318
61c2f5d5
JVH
319 int error;
320
2c4250f1
JVH
321#ifndef WIN32
322 init_signals();
323#endif
324
ad58f7a2
JVH
325 memset(&options, 0, sizeof(options));
326 if (parse_options(&options, argc, argv)) {
327 return 0;
328 }
329
330 ao_initialize();
331
332 device = audio_open_device(&options, 16, 2, 44100);
333 if (device == NULL) {
334 fprintf(stderr, "Error opening audio device %d\n", errno);
335 fprintf(stderr, "Please check your libao settings and try again\n");
336 return -1;
337 } else {
338 ao_close(device);
339 device = NULL;
340 }
341
342 memset(&raop_cbs, 0, sizeof(raop_cbs));
343 raop_cbs.cls = &options;
344 raop_cbs.audio_init = audio_init;
345 raop_cbs.audio_process = audio_process;
346 raop_cbs.audio_destroy = audio_destroy;
347 raop_cbs.audio_set_volume = audio_set_volume;
348
cf9b3c34 349 raop = raop_init_from_keyfile(10, &raop_cbs, "airport.key", NULL);
ad58f7a2
JVH
350 if (raop == NULL) {
351 fprintf(stderr, "Could not initialize the RAOP service\n");
352 return -1;
353 }
354
c6e5e8b9
JVH
355 if (strlen(options.password)) {
356 password = options.password;
357 }
ad58f7a2 358 raop_set_log_level(raop, RAOP_LOG_DEBUG);
c4312a61 359 raop_start(raop, &options.port, options.hwaddr, sizeof(options.hwaddr), password);
ad58f7a2 360
61c2f5d5
JVH
361 error = 0;
362 dnssd = dnssd_init(&error);
363 if (error) {
364 fprintf(stderr, "ERROR: Could not initialize dnssd library!\n");
365 fprintf(stderr, "------------------------------------------\n");
366 fprintf(stderr, "You could try the following resolutions based on your OS:\n");
367 fprintf(stderr, "Windows: Try installing http://support.apple.com/kb/DL999\n");
f95c4d2d 368 fprintf(stderr, "Debian/Ubuntu: Try installing libavahi-compat-libdnssd-dev package\n");
61c2f5d5
JVH
369 raop_destroy(raop);
370 return -1;
371 }
372
c4312a61 373 dnssd_register_raop(dnssd, options.apname, options.port, options.hwaddr, sizeof(options.hwaddr), 0);
ad58f7a2 374
258e9fb8
JVH
375 running = 1;
376 while (running) {
ad58f7a2 377#ifndef WIN32
258e9fb8 378 sleep(1);
ad58f7a2 379#else
258e9fb8 380 Sleep(1000);
ad58f7a2 381#endif
258e9fb8 382 }
ad58f7a2
JVH
383
384 dnssd_unregister_raop(dnssd);
385 dnssd_destroy(dnssd);
386
387 raop_stop(raop);
388 raop_destroy(raop);
389
390 ao_shutdown();
391
392 return 0;
393}