Add more informative error message
[deb_shairplay.git] / src / shairplay.c
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
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
32 # include <windows.h>
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
42 typedef struct {
43 char apname[56];
44 char password[56];
45 unsigned short port;
46 char hwaddr[6];
47
48 char ao_driver[56];
49 char ao_devicename[56];
50 char ao_deviceid[16];
51 } shairplay_options_t;
52
53 typedef 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
64 static int running;
65
66 #ifndef WIN32
67
68 #include <signal.h>
69 static void
70 signal_handler(int sig)
71 {
72 switch (sig) {
73 case SIGINT:
74 case SIGTERM:
75 running = 0;
76 break;
77 }
78 }
79 static void
80 init_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
93
94 static int
95 parse_hwaddr(const char *str, char *hwaddr, int hwaddrlen)
96 {
97 int slen, i;
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
121 static ao_device *
122 audio_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 */
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
157 static void *
158 audio_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
177 static int
178 audio_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
201 static void
202 audio_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
234 static void
235 audio_destroy(void *cls, void *opaque)
236 {
237 shairplay_session_t *session = opaque;
238
239 ao_close(session->device);
240 free(session);
241 }
242
243 static void
244 audio_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
250 static int
251 parse_options(shairplay_options_t *opt, int argc, char *argv[])
252 {
253 const char default_hwaddr[] = { 0x48, 0x5d, 0x60, 0x7c, 0xee, 0x22 };
254
255 char *path = argv[0];
256 char *arg;
257
258 /* Set default values for apname and port */
259 strncpy(opt->apname, "Shairplay", sizeof(opt->apname)-1);
260 opt->port = 5000;
261 memcpy(opt->hwaddr, default_hwaddr, sizeof(opt->hwaddr));
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);
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 }
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");
295 fprintf(stderr, " --hwaddr=address Sets the MAC address, useful if running multiple instances\n");
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
305 return 0;
306 }
307
308 int
309 main(int argc, char *argv[])
310 {
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;
317 char *password = NULL;
318
319 int error;
320
321 #ifndef WIN32
322 init_signals();
323 #endif
324
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
349 raop = raop_init_from_keyfile(10, &raop_cbs, "airport.key", NULL);
350 if (raop == NULL) {
351 fprintf(stderr, "Could not initialize the RAOP service\n");
352 fprintf(stderr, "Please make sure the airport.key file is in the current directory.\n");
353 return -1;
354 }
355
356 if (strlen(options.password)) {
357 password = options.password;
358 }
359 raop_set_log_level(raop, RAOP_LOG_DEBUG);
360 raop_start(raop, &options.port, options.hwaddr, sizeof(options.hwaddr), password);
361
362 error = 0;
363 dnssd = dnssd_init(&error);
364 if (error) {
365 fprintf(stderr, "ERROR: Could not initialize dnssd library!\n");
366 fprintf(stderr, "------------------------------------------\n");
367 fprintf(stderr, "You could try the following resolutions based on your OS:\n");
368 fprintf(stderr, "Windows: Try installing http://support.apple.com/kb/DL999\n");
369 fprintf(stderr, "Debian/Ubuntu: Try installing libavahi-compat-libdnssd-dev package\n");
370 raop_destroy(raop);
371 return -1;
372 }
373
374 dnssd_register_raop(dnssd, options.apname, options.port, options.hwaddr, sizeof(options.hwaddr), 0);
375
376 running = 1;
377 while (running) {
378 #ifndef WIN32
379 sleep(1);
380 #else
381 Sleep(1000);
382 #endif
383 }
384
385 dnssd_unregister_raop(dnssd);
386 dnssd_destroy(dnssd);
387
388 raop_stop(raop);
389 raop_destroy(raop);
390
391 ao_shutdown();
392
393 return 0;
394 }