Remove client_name libao option, it might cause problems
[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 */
ad58f7a2
JVH
109 if (strlen(opt->ao_devicename)) {
110 ao_append_option(&ao_options, "dev", opt->ao_devicename);
111 }
112 if (strlen(opt->ao_deviceid)) {
113 ao_append_option(&ao_options, "id", opt->ao_deviceid);
114 }
115
116 /* Set audio format */
117 memset(&format, 0, sizeof(format));
118 format.bits = bits;
119 format.channels = channels;
120 format.rate = samplerate;
121 format.byte_format = AO_FMT_NATIVE;
122
123 /* Try opening the actual device */
124 device = ao_open_live(driver_id, &format, ao_options);
125 ao_free_options(ao_options);
126 return device;
127}
128
129static void *
130audio_init(void *cls, int bits, int channels, int samplerate)
131{
132 shairplay_options_t *options = cls;
133 shairplay_session_t *session;
134
135 session = calloc(1, sizeof(shairplay_session_t));
136 assert(session);
137
138 session->device = audio_open_device(options, bits, channels, samplerate);
139 if (session->device == NULL) {
140 printf("Error opening device %d\n", errno);
141 }
142 assert(session->device);
143
144 session->buffering = 1;
145 session->volume = 1.0f;
146 return session;
147}
148
149static int
150audio_output(shairplay_session_t *session, const void *buffer, int buflen)
151{
152 short *shortbuf;
153 char tmpbuf[4096];
154 int tmpbuflen, i;
155
156 tmpbuflen = (buflen > sizeof(tmpbuf)) ? sizeof(tmpbuf) : buflen;
157 memcpy(tmpbuf, buffer, tmpbuflen);
158 if (ao_is_big_endian()) {
159 for (i=0; i<tmpbuflen/2; i++) {
160 char tmpch = tmpbuf[i*2];
161 tmpbuf[i*2] = tmpbuf[i*2+1];
162 tmpbuf[i*2+1] = tmpch;
163 }
164 }
165 shortbuf = (short *)tmpbuf;
166 for (i=0; i<tmpbuflen/2; i++) {
167 shortbuf[i] = shortbuf[i] * session->volume;
168 }
169 ao_play(session->device, tmpbuf, tmpbuflen);
170 return tmpbuflen;
171}
172
173static void
174audio_process(void *cls, void *opaque, const void *buffer, int buflen)
175{
176 shairplay_session_t *session = opaque;
177 int processed;
178
179 if (session->buffering) {
180 printf("Buffering...\n");
181 if (session->buflen+buflen < sizeof(session->buffer)) {
182 memcpy(session->buffer+session->buflen, buffer, buflen);
183 session->buflen += buflen;
184 return;
185 }
186 session->buffering = 0;
187 printf("Finished buffering...\n");
188
189 processed = 0;
190 while (processed < session->buflen) {
191 processed += audio_output(session,
192 session->buffer+processed,
193 session->buflen-processed);
194 }
195 session->buflen = 0;
196 }
197
198 processed = 0;
199 while (processed < buflen) {
200 processed += audio_output(session,
201 buffer+processed,
202 buflen-processed);
203 }
204}
205
206static void
207audio_destroy(void *cls, void *opaque)
208{
209 shairplay_session_t *session = opaque;
210
211 ao_close(session->device);
212 free(session);
213}
214
215static void
216audio_set_volume(void *cls, void *opaque, float volume)
217{
218 shairplay_session_t *session = opaque;
219 session->volume = pow(10.0, 0.05*volume);
220}
221
222static int
223parse_options(shairplay_options_t *opt, int argc, char *argv[])
224{
225 char *path = argv[0];
226 char *arg;
227
093f6e00
JVH
228 /* Set default values for apname and port */
229 strncpy(opt->apname, "Shairplay", sizeof(opt->apname)-1);
ad58f7a2
JVH
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
ad58f7a2
JVH
267 return 0;
268}
269
270int
271main(int argc, char *argv[])
272{
273 const char hwaddr[] = { 0x48, 0x5d, 0x60, 0x7c, 0xee, 0x22 };
274
275 shairplay_options_t options;
276 ao_device *device = NULL;
277
278 dnssd_t *dnssd;
279 raop_t *raop;
280 raop_callbacks_t raop_cbs;
c6e5e8b9 281 char *password = NULL;
ad58f7a2 282
61c2f5d5
JVH
283 int error;
284
2c4250f1
JVH
285#ifndef WIN32
286 init_signals();
287#endif
288
ad58f7a2
JVH
289 memset(&options, 0, sizeof(options));
290 if (parse_options(&options, argc, argv)) {
291 return 0;
292 }
293
294 ao_initialize();
295
296 device = audio_open_device(&options, 16, 2, 44100);
297 if (device == NULL) {
298 fprintf(stderr, "Error opening audio device %d\n", errno);
299 fprintf(stderr, "Please check your libao settings and try again\n");
300 return -1;
301 } else {
302 ao_close(device);
303 device = NULL;
304 }
305
306 memset(&raop_cbs, 0, sizeof(raop_cbs));
307 raop_cbs.cls = &options;
308 raop_cbs.audio_init = audio_init;
309 raop_cbs.audio_process = audio_process;
310 raop_cbs.audio_destroy = audio_destroy;
311 raop_cbs.audio_set_volume = audio_set_volume;
312
cf9b3c34 313 raop = raop_init_from_keyfile(10, &raop_cbs, "airport.key", NULL);
ad58f7a2
JVH
314 if (raop == NULL) {
315 fprintf(stderr, "Could not initialize the RAOP service\n");
316 return -1;
317 }
318
c6e5e8b9
JVH
319 if (strlen(options.password)) {
320 password = options.password;
321 }
ad58f7a2 322 raop_set_log_level(raop, RAOP_LOG_DEBUG);
c6e5e8b9 323 raop_start(raop, &options.port, hwaddr, sizeof(hwaddr), password);
ad58f7a2 324
61c2f5d5
JVH
325 error = 0;
326 dnssd = dnssd_init(&error);
327 if (error) {
328 fprintf(stderr, "ERROR: Could not initialize dnssd library!\n");
329 fprintf(stderr, "------------------------------------------\n");
330 fprintf(stderr, "You could try the following resolutions based on your OS:\n");
331 fprintf(stderr, "Windows: Try installing http://support.apple.com/kb/DL999\n");
f95c4d2d 332 fprintf(stderr, "Debian/Ubuntu: Try installing libavahi-compat-libdnssd-dev package\n");
61c2f5d5
JVH
333 raop_destroy(raop);
334 return -1;
335 }
336
ad58f7a2
JVH
337 dnssd_register_raop(dnssd, options.apname, options.port, hwaddr, sizeof(hwaddr), 0);
338
258e9fb8
JVH
339 running = 1;
340 while (running) {
ad58f7a2 341#ifndef WIN32
258e9fb8 342 sleep(1);
ad58f7a2 343#else
258e9fb8 344 Sleep(1000);
ad58f7a2 345#endif
258e9fb8 346 }
ad58f7a2
JVH
347
348 dnssd_unregister_raop(dnssd);
349 dnssd_destroy(dnssd);
350
351 raop_stop(raop);
352 raop_destroy(raop);
353
354 ao_shutdown();
355
356 return 0;
357}