Make hwaddr configurable from the command line
[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 char *ptr, *endptr;
99
100 slen = 3*hwaddrlen-1;
101 if (strlen(str) != slen) {
102 return 1;
103 }
104 for (i=0; i<slen; i++) {
105 if (str[i] == ':' && (i%3 == 2)) {
106 continue;
107 }
108 if (str[i] >= '0' && str[i] <= '9') {
109 continue;
110 }
111 if (str[i] >= 'a' && str[i] <= 'f') {
112 continue;
113 }
114 return 1;
115 }
116 for (i=0; i<hwaddrlen; i++) {
117 hwaddr[i] = (char) strtol(str+(i*3), NULL, 16);
118 }
119 return 0;
120 }
121
122 static ao_device *
123 audio_open_device(shairplay_options_t *opt, int bits, int channels, int samplerate)
124 {
125 ao_device *device = NULL;
126 ao_option *ao_options = NULL;
127 ao_sample_format format;
128 int driver_id;
129
130 /* Get the libao driver ID */
131 if (strlen(opt->ao_driver)) {
132 driver_id = ao_driver_id(opt->ao_driver);
133 } else {
134 driver_id = ao_default_driver_id();
135 }
136
137 /* Add all available libao options */
138 if (strlen(opt->ao_devicename)) {
139 ao_append_option(&ao_options, "dev", opt->ao_devicename);
140 }
141 if (strlen(opt->ao_deviceid)) {
142 ao_append_option(&ao_options, "id", opt->ao_deviceid);
143 }
144
145 /* Set audio format */
146 memset(&format, 0, sizeof(format));
147 format.bits = bits;
148 format.channels = channels;
149 format.rate = samplerate;
150 format.byte_format = AO_FMT_NATIVE;
151
152 /* Try opening the actual device */
153 device = ao_open_live(driver_id, &format, ao_options);
154 ao_free_options(ao_options);
155 return device;
156 }
157
158 static void *
159 audio_init(void *cls, int bits, int channels, int samplerate)
160 {
161 shairplay_options_t *options = cls;
162 shairplay_session_t *session;
163
164 session = calloc(1, sizeof(shairplay_session_t));
165 assert(session);
166
167 session->device = audio_open_device(options, bits, channels, samplerate);
168 if (session->device == NULL) {
169 printf("Error opening device %d\n", errno);
170 }
171 assert(session->device);
172
173 session->buffering = 1;
174 session->volume = 1.0f;
175 return session;
176 }
177
178 static int
179 audio_output(shairplay_session_t *session, const void *buffer, int buflen)
180 {
181 short *shortbuf;
182 char tmpbuf[4096];
183 int tmpbuflen, i;
184
185 tmpbuflen = (buflen > sizeof(tmpbuf)) ? sizeof(tmpbuf) : buflen;
186 memcpy(tmpbuf, buffer, tmpbuflen);
187 if (ao_is_big_endian()) {
188 for (i=0; i<tmpbuflen/2; i++) {
189 char tmpch = tmpbuf[i*2];
190 tmpbuf[i*2] = tmpbuf[i*2+1];
191 tmpbuf[i*2+1] = tmpch;
192 }
193 }
194 shortbuf = (short *)tmpbuf;
195 for (i=0; i<tmpbuflen/2; i++) {
196 shortbuf[i] = shortbuf[i] * session->volume;
197 }
198 ao_play(session->device, tmpbuf, tmpbuflen);
199 return tmpbuflen;
200 }
201
202 static void
203 audio_process(void *cls, void *opaque, const void *buffer, int buflen)
204 {
205 shairplay_session_t *session = opaque;
206 int processed;
207
208 if (session->buffering) {
209 printf("Buffering...\n");
210 if (session->buflen+buflen < sizeof(session->buffer)) {
211 memcpy(session->buffer+session->buflen, buffer, buflen);
212 session->buflen += buflen;
213 return;
214 }
215 session->buffering = 0;
216 printf("Finished buffering...\n");
217
218 processed = 0;
219 while (processed < session->buflen) {
220 processed += audio_output(session,
221 session->buffer+processed,
222 session->buflen-processed);
223 }
224 session->buflen = 0;
225 }
226
227 processed = 0;
228 while (processed < buflen) {
229 processed += audio_output(session,
230 buffer+processed,
231 buflen-processed);
232 }
233 }
234
235 static void
236 audio_destroy(void *cls, void *opaque)
237 {
238 shairplay_session_t *session = opaque;
239
240 ao_close(session->device);
241 free(session);
242 }
243
244 static void
245 audio_set_volume(void *cls, void *opaque, float volume)
246 {
247 shairplay_session_t *session = opaque;
248 session->volume = pow(10.0, 0.05*volume);
249 }
250
251 static int
252 parse_options(shairplay_options_t *opt, int argc, char *argv[])
253 {
254 const char default_hwaddr[] = { 0x48, 0x5d, 0x60, 0x7c, 0xee, 0x22 };
255
256 char *path = argv[0];
257 char *arg;
258
259 /* Set default values for apname and port */
260 strncpy(opt->apname, "Shairplay", sizeof(opt->apname)-1);
261 opt->port = 5000;
262 memcpy(opt->hwaddr, default_hwaddr, sizeof(opt->hwaddr));
263
264 while ((arg = *++argv)) {
265 if (!strcmp(arg, "-a")) {
266 strncpy(opt->apname, *++argv, sizeof(opt->apname)-1);
267 } else if (!strncmp(arg, "--apname=", 9)) {
268 strncpy(opt->apname, arg+9, sizeof(opt->apname)-1);
269 } else if (!strcmp(arg, "-p")) {
270 strncpy(opt->password, *++argv, sizeof(opt->password)-1);
271 } else if (!strncmp(arg, "--password=", 11)) {
272 strncpy(opt->password, arg+11, sizeof(opt->password)-1);
273 } else if (!strcmp(arg, "-o")) {
274 opt->port = atoi(*++argv);
275 } else if (!strncmp(arg, "--server_port=", 14)) {
276 opt->port = atoi(arg+14);
277 } else if (!strncmp(arg, "--hwaddr=", 9)) {
278 if (parse_hwaddr(arg+9, opt->hwaddr, sizeof(opt->hwaddr))) {
279 fprintf(stderr, "Invalid format given for hwaddr, aborting...\n");
280 fprintf(stderr, "Please use hwaddr format: 01:45:89:ab:cd:ef\n");
281 return 1;
282 }
283 } else if (!strncmp(arg, "--ao_driver=", 12)) {
284 strncpy(opt->ao_driver, arg+12, sizeof(opt->ao_driver)-1);
285 } else if (!strncmp(arg, "--ao_devicename=", 16)) {
286 strncpy(opt->ao_devicename, arg+16, sizeof(opt->ao_devicename)-1);
287 } else if (!strncmp(arg, "--ao_deviceid=", 14)) {
288 strncpy(opt->ao_deviceid, arg+14, sizeof(opt->ao_deviceid)-1);
289 } else if (!strcmp(arg, "-h") || !strcmp(arg, "--help")) {
290 fprintf(stderr, "Shairplay version %s\n", VERSION);
291 fprintf(stderr, "Usage: %s [OPTION...]\n", path);
292 fprintf(stderr, "\n");
293 fprintf(stderr, " -a, --apname=AirPort Sets Airport name\n");
294 fprintf(stderr, " -p, --password=secret Sets password\n");
295 fprintf(stderr, " -o, --server_port=5000 Sets port for RAOP service\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 return -1;
353 }
354
355 if (strlen(options.password)) {
356 password = options.password;
357 }
358 raop_set_log_level(raop, RAOP_LOG_DEBUG);
359 raop_start(raop, &options.port, options.hwaddr, sizeof(options.hwaddr), password);
360
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");
368 fprintf(stderr, "Debian/Ubuntu: Try installing libavahi-compat-libdnssd-dev package\n");
369 raop_destroy(raop);
370 return -1;
371 }
372
373 dnssd_register_raop(dnssd, options.apname, options.port, options.hwaddr, sizeof(options.hwaddr), 0);
374
375 running = 1;
376 while (running) {
377 #ifndef WIN32
378 sleep(1);
379 #else
380 Sleep(1000);
381 #endif
382 }
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 }