Commit | Line | Data |
---|---|---|
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 | ||
42 | typedef 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 | ||
52 | typedef 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 |
63 | static int running; |
64 | ||
2c4250f1 JVH |
65 | #ifndef WIN32 |
66 | ||
67 | #include <signal.h> | |
68 | static void | |
69 | signal_handler(int sig) | |
70 | { | |
71 | switch (sig) { | |
72 | case SIGINT: | |
73 | case SIGTERM: | |
74 | running = 0; | |
75 | break; | |
76 | } | |
77 | } | |
78 | static void | |
79 | init_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 |
93 | static ao_device * |
94 | audio_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 | ||
130 | static void * | |
131 | audio_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 | ||
150 | static int | |
151 | audio_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 | ||
174 | static void | |
175 | audio_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 | ||
207 | static void | |
208 | audio_destroy(void *cls, void *opaque) | |
209 | { | |
210 | shairplay_session_t *session = opaque; | |
211 | ||
212 | ao_close(session->device); | |
213 | free(session); | |
214 | } | |
215 | ||
216 | static void | |
217 | audio_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 | ||
223 | static int | |
224 | parse_options(shairplay_options_t *opt, int argc, char *argv[]) | |
225 | { | |
226 | char *path = argv[0]; | |
227 | char *arg; | |
228 | ||
093f6e00 JVH |
229 | /* Set default values for apname and port */ |
230 | strncpy(opt->apname, "Shairplay", sizeof(opt->apname)-1); | |
ad58f7a2 JVH |
231 | opt->port = 5000; |
232 | ||
233 | while ((arg = *++argv)) { | |
234 | if (!strcmp(arg, "-a")) { | |
235 | strncpy(opt->apname, *++argv, sizeof(opt->apname)-1); | |
236 | } else if (!strncmp(arg, "--apname=", 9)) { | |
237 | strncpy(opt->apname, arg+9, sizeof(opt->apname)-1); | |
238 | } else if (!strcmp(arg, "-p")) { | |
239 | strncpy(opt->password, *++argv, sizeof(opt->password)-1); | |
240 | } else if (!strncmp(arg, "--password=", 11)) { | |
241 | strncpy(opt->password, arg+11, sizeof(opt->password)-1); | |
242 | } else if (!strcmp(arg, "-o")) { | |
243 | opt->port = atoi(*++argv); | |
244 | } else if (!strncmp(arg, "--server_port=", 14)) { | |
245 | opt->port = atoi(arg+14); | |
246 | } else if (!strncmp(arg, "--ao_driver=", 12)) { | |
247 | strncpy(opt->ao_driver, arg+12, sizeof(opt->ao_driver)-1); | |
248 | } else if (!strncmp(arg, "--ao_devicename=", 16)) { | |
249 | strncpy(opt->ao_devicename, arg+16, sizeof(opt->ao_devicename)-1); | |
250 | } else if (!strncmp(arg, "--ao_deviceid=", 14)) { | |
251 | strncpy(opt->ao_deviceid, arg+14, sizeof(opt->ao_deviceid)-1); | |
252 | } else if (!strcmp(arg, "-h") || !strcmp(arg, "--help")) { | |
253 | fprintf(stderr, "Shairplay version %s\n", VERSION); | |
254 | fprintf(stderr, "Usage: %s [OPTION...]\n", path); | |
255 | fprintf(stderr, "\n"); | |
256 | fprintf(stderr, " -a, --apname=AirPort Sets Airport name\n"); | |
257 | fprintf(stderr, " -p, --password=secret Sets password\n"); | |
258 | fprintf(stderr, " -o, --server_port=5000 Sets port for RAOP service\n"); | |
259 | fprintf(stderr, " --ao_driver=driver Sets the ao driver (optional)\n"); | |
260 | fprintf(stderr, " --ao_devicename=devicename Sets the ao device name (optional)\n"); | |
261 | fprintf(stderr, " --ao_deviceid=id Sets the ao device id (optional)\n"); | |
262 | fprintf(stderr, " -h, --help This help\n"); | |
263 | fprintf(stderr, "\n"); | |
264 | return 1; | |
265 | } | |
266 | } | |
267 | ||
ad58f7a2 JVH |
268 | return 0; |
269 | } | |
270 | ||
271 | int | |
272 | main(int argc, char *argv[]) | |
273 | { | |
274 | const char hwaddr[] = { 0x48, 0x5d, 0x60, 0x7c, 0xee, 0x22 }; | |
275 | ||
276 | shairplay_options_t options; | |
277 | ao_device *device = NULL; | |
278 | ||
279 | dnssd_t *dnssd; | |
280 | raop_t *raop; | |
281 | raop_callbacks_t raop_cbs; | |
c6e5e8b9 | 282 | char *password = NULL; |
ad58f7a2 | 283 | |
61c2f5d5 JVH |
284 | int error; |
285 | ||
2c4250f1 JVH |
286 | #ifndef WIN32 |
287 | init_signals(); | |
288 | #endif | |
289 | ||
ad58f7a2 JVH |
290 | memset(&options, 0, sizeof(options)); |
291 | if (parse_options(&options, argc, argv)) { | |
292 | return 0; | |
293 | } | |
294 | ||
295 | ao_initialize(); | |
296 | ||
297 | device = audio_open_device(&options, 16, 2, 44100); | |
298 | if (device == NULL) { | |
299 | fprintf(stderr, "Error opening audio device %d\n", errno); | |
300 | fprintf(stderr, "Please check your libao settings and try again\n"); | |
301 | return -1; | |
302 | } else { | |
303 | ao_close(device); | |
304 | device = NULL; | |
305 | } | |
306 | ||
307 | memset(&raop_cbs, 0, sizeof(raop_cbs)); | |
308 | raop_cbs.cls = &options; | |
309 | raop_cbs.audio_init = audio_init; | |
310 | raop_cbs.audio_process = audio_process; | |
311 | raop_cbs.audio_destroy = audio_destroy; | |
312 | raop_cbs.audio_set_volume = audio_set_volume; | |
313 | ||
cf9b3c34 | 314 | raop = raop_init_from_keyfile(10, &raop_cbs, "airport.key", NULL); |
ad58f7a2 JVH |
315 | if (raop == NULL) { |
316 | fprintf(stderr, "Could not initialize the RAOP service\n"); | |
317 | return -1; | |
318 | } | |
319 | ||
c6e5e8b9 JVH |
320 | if (strlen(options.password)) { |
321 | password = options.password; | |
322 | } | |
ad58f7a2 | 323 | raop_set_log_level(raop, RAOP_LOG_DEBUG); |
c6e5e8b9 | 324 | raop_start(raop, &options.port, hwaddr, sizeof(hwaddr), password); |
ad58f7a2 | 325 | |
61c2f5d5 JVH |
326 | error = 0; |
327 | dnssd = dnssd_init(&error); | |
328 | if (error) { | |
329 | fprintf(stderr, "ERROR: Could not initialize dnssd library!\n"); | |
330 | fprintf(stderr, "------------------------------------------\n"); | |
331 | fprintf(stderr, "You could try the following resolutions based on your OS:\n"); | |
332 | fprintf(stderr, "Windows: Try installing http://support.apple.com/kb/DL999\n"); | |
f95c4d2d | 333 | fprintf(stderr, "Debian/Ubuntu: Try installing libavahi-compat-libdnssd-dev package\n"); |
61c2f5d5 JVH |
334 | raop_destroy(raop); |
335 | return -1; | |
336 | } | |
337 | ||
ad58f7a2 JVH |
338 | dnssd_register_raop(dnssd, options.apname, options.port, hwaddr, sizeof(hwaddr), 0); |
339 | ||
258e9fb8 JVH |
340 | running = 1; |
341 | while (running) { | |
ad58f7a2 | 342 | #ifndef WIN32 |
258e9fb8 | 343 | sleep(1); |
ad58f7a2 | 344 | #else |
258e9fb8 | 345 | Sleep(1000); |
ad58f7a2 | 346 | #endif |
258e9fb8 | 347 | } |
ad58f7a2 JVH |
348 | |
349 | dnssd_unregister_raop(dnssd); | |
350 | dnssd_destroy(dnssd); | |
351 | ||
352 | raop_stop(raop); | |
353 | raop_destroy(raop); | |
354 | ||
355 | ao_shutdown(); | |
356 | ||
357 | return 0; | |
358 | } |