Update licensing information
[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;
46
47 char ao_driver[56];
48 char ao_devicename[56];
49 char ao_deviceid[16];
50} shairplay_options_t;
51
52typedef struct {
53 ao_device *device;
54
55 int buffering;
56 int buflen;
57 char buffer[8192];
58
59 float volume;
60} shairplay_session_t;
61
62
258e9fb8
JVH
63static int running;
64
2c4250f1
JVH
65#ifndef WIN32
66
67#include <signal.h>
68static void
69signal_handler(int sig)
70{
71 switch (sig) {
72 case SIGINT:
73 case SIGTERM:
74 running = 0;
75 break;
76 }
77}
78static void
79init_signals(void)
80{
81 struct sigaction sigact;
82
83 sigact.sa_handler = signal_handler;
84 sigemptyset(&sigact.sa_mask);
85 sigact.sa_flags = 0;
86 sigaction(SIGINT, &sigact, NULL);
87 sigaction(SIGTERM, &sigact, NULL);
88}
89
90#endif
91
258e9fb8 92
ad58f7a2
JVH
93static ao_device *
94audio_open_device(shairplay_options_t *opt, int bits, int channels, int samplerate)
95{
96 ao_device *device = NULL;
97 ao_option *ao_options = NULL;
98 ao_sample_format format;
99 int driver_id;
100
101 /* Get the libao driver ID */
102 if (strlen(opt->ao_driver)) {
103 driver_id = ao_driver_id(opt->ao_driver);
104 } else {
105 driver_id = ao_default_driver_id();
106 }
107
108 /* Add all available libao options */
109 ao_append_option(&ao_options, "client_name", opt->apname);
110 if (strlen(opt->ao_devicename)) {
111 ao_append_option(&ao_options, "dev", opt->ao_devicename);
112 }
113 if (strlen(opt->ao_deviceid)) {
114 ao_append_option(&ao_options, "id", opt->ao_deviceid);
115 }
116
117 /* Set audio format */
118 memset(&format, 0, sizeof(format));
119 format.bits = bits;
120 format.channels = channels;
121 format.rate = samplerate;
122 format.byte_format = AO_FMT_NATIVE;
123
124 /* Try opening the actual device */
125 device = ao_open_live(driver_id, &format, ao_options);
126 ao_free_options(ao_options);
127 return device;
128}
129
130static void *
131audio_init(void *cls, int bits, int channels, int samplerate)
132{
133 shairplay_options_t *options = cls;
134 shairplay_session_t *session;
135
136 session = calloc(1, sizeof(shairplay_session_t));
137 assert(session);
138
139 session->device = audio_open_device(options, bits, channels, samplerate);
140 if (session->device == NULL) {
141 printf("Error opening device %d\n", errno);
142 }
143 assert(session->device);
144
145 session->buffering = 1;
146 session->volume = 1.0f;
147 return session;
148}
149
150static int
151audio_output(shairplay_session_t *session, const void *buffer, int buflen)
152{
153 short *shortbuf;
154 char tmpbuf[4096];
155 int tmpbuflen, i;
156
157 tmpbuflen = (buflen > sizeof(tmpbuf)) ? sizeof(tmpbuf) : buflen;
158 memcpy(tmpbuf, buffer, tmpbuflen);
159 if (ao_is_big_endian()) {
160 for (i=0; i<tmpbuflen/2; i++) {
161 char tmpch = tmpbuf[i*2];
162 tmpbuf[i*2] = tmpbuf[i*2+1];
163 tmpbuf[i*2+1] = tmpch;
164 }
165 }
166 shortbuf = (short *)tmpbuf;
167 for (i=0; i<tmpbuflen/2; i++) {
168 shortbuf[i] = shortbuf[i] * session->volume;
169 }
170 ao_play(session->device, tmpbuf, tmpbuflen);
171 return tmpbuflen;
172}
173
174static void
175audio_process(void *cls, void *opaque, const void *buffer, int buflen)
176{
177 shairplay_session_t *session = opaque;
178 int processed;
179
180 if (session->buffering) {
181 printf("Buffering...\n");
182 if (session->buflen+buflen < sizeof(session->buffer)) {
183 memcpy(session->buffer+session->buflen, buffer, buflen);
184 session->buflen += buflen;
185 return;
186 }
187 session->buffering = 0;
188 printf("Finished buffering...\n");
189
190 processed = 0;
191 while (processed < session->buflen) {
192 processed += audio_output(session,
193 session->buffer+processed,
194 session->buflen-processed);
195 }
196 session->buflen = 0;
197 }
198
199 processed = 0;
200 while (processed < buflen) {
201 processed += audio_output(session,
202 buffer+processed,
203 buflen-processed);
204 }
205}
206
207static void
208audio_destroy(void *cls, void *opaque)
209{
210 shairplay_session_t *session = opaque;
211
212 ao_close(session->device);
213 free(session);
214}
215
216static void
217audio_set_volume(void *cls, void *opaque, float volume)
218{
219 shairplay_session_t *session = opaque;
220 session->volume = pow(10.0, 0.05*volume);
221}
222
223static int
224parse_options(shairplay_options_t *opt, int argc, char *argv[])
225{
226 char *path = argv[0];
227 char *arg;
228
229 strcpy(opt->apname, "Shairplay");
230 opt->port = 5000;
231
232 while ((arg = *++argv)) {
233 if (!strcmp(arg, "-a")) {
234 strncpy(opt->apname, *++argv, sizeof(opt->apname)-1);
235 } else if (!strncmp(arg, "--apname=", 9)) {
236 strncpy(opt->apname, arg+9, sizeof(opt->apname)-1);
237 } else if (!strcmp(arg, "-p")) {
238 strncpy(opt->password, *++argv, sizeof(opt->password)-1);
239 } else if (!strncmp(arg, "--password=", 11)) {
240 strncpy(opt->password, arg+11, sizeof(opt->password)-1);
241 } else if (!strcmp(arg, "-o")) {
242 opt->port = atoi(*++argv);
243 } else if (!strncmp(arg, "--server_port=", 14)) {
244 opt->port = atoi(arg+14);
245 } else if (!strncmp(arg, "--ao_driver=", 12)) {
246 strncpy(opt->ao_driver, arg+12, sizeof(opt->ao_driver)-1);
247 } else if (!strncmp(arg, "--ao_devicename=", 16)) {
248 strncpy(opt->ao_devicename, arg+16, sizeof(opt->ao_devicename)-1);
249 } else if (!strncmp(arg, "--ao_deviceid=", 14)) {
250 strncpy(opt->ao_deviceid, arg+14, sizeof(opt->ao_deviceid)-1);
251 } else if (!strcmp(arg, "-h") || !strcmp(arg, "--help")) {
252 fprintf(stderr, "Shairplay version %s\n", VERSION);
253 fprintf(stderr, "Usage: %s [OPTION...]\n", path);
254 fprintf(stderr, "\n");
255 fprintf(stderr, " -a, --apname=AirPort Sets Airport name\n");
256 fprintf(stderr, " -p, --password=secret Sets password\n");
257 fprintf(stderr, " -o, --server_port=5000 Sets port for RAOP service\n");
258 fprintf(stderr, " --ao_driver=driver Sets the ao driver (optional)\n");
259 fprintf(stderr, " --ao_devicename=devicename Sets the ao device name (optional)\n");
260 fprintf(stderr, " --ao_deviceid=id Sets the ao device id (optional)\n");
261 fprintf(stderr, " -h, --help This help\n");
262 fprintf(stderr, "\n");
263 return 1;
264 }
265 }
266
267 /* Set default values for apname and port */
268 if (!strlen(opt->apname)) {
269 strncpy(opt->apname, "Shairplay", sizeof(opt->apname)-1);
270 }
271 if (!opt->port) {
272 opt->port = 5000;
273 }
274 return 0;
275}
276
277int
278main(int argc, char *argv[])
279{
280 const char hwaddr[] = { 0x48, 0x5d, 0x60, 0x7c, 0xee, 0x22 };
281
282 shairplay_options_t options;
283 ao_device *device = NULL;
284
285 dnssd_t *dnssd;
286 raop_t *raop;
287 raop_callbacks_t raop_cbs;
288
61c2f5d5
JVH
289 int error;
290
2c4250f1
JVH
291#ifndef WIN32
292 init_signals();
293#endif
294
ad58f7a2
JVH
295 memset(&options, 0, sizeof(options));
296 if (parse_options(&options, argc, argv)) {
297 return 0;
298 }
299
300 ao_initialize();
301
302 device = audio_open_device(&options, 16, 2, 44100);
303 if (device == NULL) {
304 fprintf(stderr, "Error opening audio device %d\n", errno);
305 fprintf(stderr, "Please check your libao settings and try again\n");
306 return -1;
307 } else {
308 ao_close(device);
309 device = NULL;
310 }
311
312 memset(&raop_cbs, 0, sizeof(raop_cbs));
313 raop_cbs.cls = &options;
314 raop_cbs.audio_init = audio_init;
315 raop_cbs.audio_process = audio_process;
316 raop_cbs.audio_destroy = audio_destroy;
317 raop_cbs.audio_set_volume = audio_set_volume;
318
cf9b3c34 319 raop = raop_init_from_keyfile(10, &raop_cbs, "airport.key", NULL);
ad58f7a2
JVH
320 if (raop == NULL) {
321 fprintf(stderr, "Could not initialize the RAOP service\n");
322 return -1;
323 }
324
325 raop_set_log_level(raop, RAOP_LOG_DEBUG);
326 raop_start(raop, &options.port, hwaddr, sizeof(hwaddr), NULL);
327
61c2f5d5
JVH
328 error = 0;
329 dnssd = dnssd_init(&error);
330 if (error) {
331 fprintf(stderr, "ERROR: Could not initialize dnssd library!\n");
332 fprintf(stderr, "------------------------------------------\n");
333 fprintf(stderr, "You could try the following resolutions based on your OS:\n");
334 fprintf(stderr, "Windows: Try installing http://support.apple.com/kb/DL999\n");
f95c4d2d 335 fprintf(stderr, "Debian/Ubuntu: Try installing libavahi-compat-libdnssd-dev package\n");
61c2f5d5
JVH
336 raop_destroy(raop);
337 return -1;
338 }
339
ad58f7a2
JVH
340 dnssd_register_raop(dnssd, options.apname, options.port, hwaddr, sizeof(hwaddr), 0);
341
258e9fb8
JVH
342 running = 1;
343 while (running) {
ad58f7a2 344#ifndef WIN32
258e9fb8 345 sleep(1);
ad58f7a2 346#else
258e9fb8 347 Sleep(1000);
ad58f7a2 348#endif
258e9fb8 349 }
ad58f7a2
JVH
350
351 dnssd_unregister_raop(dnssd);
352 dnssd_destroy(dnssd);
353
354 raop_stop(raop);
355 raop_destroy(raop);
356
357 ao_shutdown();
358
359 return 0;
360}