Commit | Line | Data |
---|---|---|
ad58f7a2 JVH |
1 | #include <stdlib.h> |
2 | #include <stdio.h> | |
3 | #include <math.h> | |
4 | #include <string.h> | |
5 | #include <unistd.h> | |
6 | #include <assert.h> | |
7 | ||
8 | #ifdef WIN32 | |
9 | #include <windows.h> | |
10 | #endif | |
11 | ||
12 | #include <shairplay/dnssd.h> | |
13 | #include <shairplay/raop.h> | |
14 | ||
15 | #include <ao/ao.h> | |
16 | ||
17 | #include "config.h" | |
18 | ||
19 | typedef struct { | |
20 | char apname[56]; | |
21 | char password[56]; | |
22 | unsigned short port; | |
23 | ||
24 | char ao_driver[56]; | |
25 | char ao_devicename[56]; | |
26 | char ao_deviceid[16]; | |
27 | } shairplay_options_t; | |
28 | ||
29 | typedef struct { | |
30 | ao_device *device; | |
31 | ||
32 | int buffering; | |
33 | int buflen; | |
34 | char buffer[8192]; | |
35 | ||
36 | float volume; | |
37 | } shairplay_session_t; | |
38 | ||
39 | ||
258e9fb8 JVH |
40 | static int running; |
41 | ||
42 | ||
ad58f7a2 JVH |
43 | static ao_device * |
44 | audio_open_device(shairplay_options_t *opt, int bits, int channels, int samplerate) | |
45 | { | |
46 | ao_device *device = NULL; | |
47 | ao_option *ao_options = NULL; | |
48 | ao_sample_format format; | |
49 | int driver_id; | |
50 | ||
51 | /* Get the libao driver ID */ | |
52 | if (strlen(opt->ao_driver)) { | |
53 | driver_id = ao_driver_id(opt->ao_driver); | |
54 | } else { | |
55 | driver_id = ao_default_driver_id(); | |
56 | } | |
57 | ||
58 | /* Add all available libao options */ | |
59 | ao_append_option(&ao_options, "client_name", opt->apname); | |
60 | if (strlen(opt->ao_devicename)) { | |
61 | ao_append_option(&ao_options, "dev", opt->ao_devicename); | |
62 | } | |
63 | if (strlen(opt->ao_deviceid)) { | |
64 | ao_append_option(&ao_options, "id", opt->ao_deviceid); | |
65 | } | |
66 | ||
67 | /* Set audio format */ | |
68 | memset(&format, 0, sizeof(format)); | |
69 | format.bits = bits; | |
70 | format.channels = channels; | |
71 | format.rate = samplerate; | |
72 | format.byte_format = AO_FMT_NATIVE; | |
73 | ||
74 | /* Try opening the actual device */ | |
75 | device = ao_open_live(driver_id, &format, ao_options); | |
76 | ao_free_options(ao_options); | |
77 | return device; | |
78 | } | |
79 | ||
80 | static void * | |
81 | audio_init(void *cls, int bits, int channels, int samplerate) | |
82 | { | |
83 | shairplay_options_t *options = cls; | |
84 | shairplay_session_t *session; | |
85 | ||
86 | session = calloc(1, sizeof(shairplay_session_t)); | |
87 | assert(session); | |
88 | ||
89 | session->device = audio_open_device(options, bits, channels, samplerate); | |
90 | if (session->device == NULL) { | |
91 | printf("Error opening device %d\n", errno); | |
92 | } | |
93 | assert(session->device); | |
94 | ||
95 | session->buffering = 1; | |
96 | session->volume = 1.0f; | |
97 | return session; | |
98 | } | |
99 | ||
100 | static int | |
101 | audio_output(shairplay_session_t *session, const void *buffer, int buflen) | |
102 | { | |
103 | short *shortbuf; | |
104 | char tmpbuf[4096]; | |
105 | int tmpbuflen, i; | |
106 | ||
107 | tmpbuflen = (buflen > sizeof(tmpbuf)) ? sizeof(tmpbuf) : buflen; | |
108 | memcpy(tmpbuf, buffer, tmpbuflen); | |
109 | if (ao_is_big_endian()) { | |
110 | for (i=0; i<tmpbuflen/2; i++) { | |
111 | char tmpch = tmpbuf[i*2]; | |
112 | tmpbuf[i*2] = tmpbuf[i*2+1]; | |
113 | tmpbuf[i*2+1] = tmpch; | |
114 | } | |
115 | } | |
116 | shortbuf = (short *)tmpbuf; | |
117 | for (i=0; i<tmpbuflen/2; i++) { | |
118 | shortbuf[i] = shortbuf[i] * session->volume; | |
119 | } | |
120 | ao_play(session->device, tmpbuf, tmpbuflen); | |
121 | return tmpbuflen; | |
122 | } | |
123 | ||
124 | static void | |
125 | audio_process(void *cls, void *opaque, const void *buffer, int buflen) | |
126 | { | |
127 | shairplay_session_t *session = opaque; | |
128 | int processed; | |
129 | ||
130 | if (session->buffering) { | |
131 | printf("Buffering...\n"); | |
132 | if (session->buflen+buflen < sizeof(session->buffer)) { | |
133 | memcpy(session->buffer+session->buflen, buffer, buflen); | |
134 | session->buflen += buflen; | |
135 | return; | |
136 | } | |
137 | session->buffering = 0; | |
138 | printf("Finished buffering...\n"); | |
139 | ||
140 | processed = 0; | |
141 | while (processed < session->buflen) { | |
142 | processed += audio_output(session, | |
143 | session->buffer+processed, | |
144 | session->buflen-processed); | |
145 | } | |
146 | session->buflen = 0; | |
147 | } | |
148 | ||
149 | processed = 0; | |
150 | while (processed < buflen) { | |
151 | processed += audio_output(session, | |
152 | buffer+processed, | |
153 | buflen-processed); | |
154 | } | |
155 | } | |
156 | ||
157 | static void | |
158 | audio_destroy(void *cls, void *opaque) | |
159 | { | |
160 | shairplay_session_t *session = opaque; | |
161 | ||
162 | ao_close(session->device); | |
163 | free(session); | |
164 | } | |
165 | ||
166 | static void | |
167 | audio_set_volume(void *cls, void *opaque, float volume) | |
168 | { | |
169 | shairplay_session_t *session = opaque; | |
170 | session->volume = pow(10.0, 0.05*volume); | |
171 | } | |
172 | ||
173 | static int | |
174 | parse_options(shairplay_options_t *opt, int argc, char *argv[]) | |
175 | { | |
176 | char *path = argv[0]; | |
177 | char *arg; | |
178 | ||
179 | strcpy(opt->apname, "Shairplay"); | |
180 | opt->port = 5000; | |
181 | ||
182 | while ((arg = *++argv)) { | |
183 | if (!strcmp(arg, "-a")) { | |
184 | strncpy(opt->apname, *++argv, sizeof(opt->apname)-1); | |
185 | } else if (!strncmp(arg, "--apname=", 9)) { | |
186 | strncpy(opt->apname, arg+9, sizeof(opt->apname)-1); | |
187 | } else if (!strcmp(arg, "-p")) { | |
188 | strncpy(opt->password, *++argv, sizeof(opt->password)-1); | |
189 | } else if (!strncmp(arg, "--password=", 11)) { | |
190 | strncpy(opt->password, arg+11, sizeof(opt->password)-1); | |
191 | } else if (!strcmp(arg, "-o")) { | |
192 | opt->port = atoi(*++argv); | |
193 | } else if (!strncmp(arg, "--server_port=", 14)) { | |
194 | opt->port = atoi(arg+14); | |
195 | } else if (!strncmp(arg, "--ao_driver=", 12)) { | |
196 | strncpy(opt->ao_driver, arg+12, sizeof(opt->ao_driver)-1); | |
197 | } else if (!strncmp(arg, "--ao_devicename=", 16)) { | |
198 | strncpy(opt->ao_devicename, arg+16, sizeof(opt->ao_devicename)-1); | |
199 | } else if (!strncmp(arg, "--ao_deviceid=", 14)) { | |
200 | strncpy(opt->ao_deviceid, arg+14, sizeof(opt->ao_deviceid)-1); | |
201 | } else if (!strcmp(arg, "-h") || !strcmp(arg, "--help")) { | |
202 | fprintf(stderr, "Shairplay version %s\n", VERSION); | |
203 | fprintf(stderr, "Usage: %s [OPTION...]\n", path); | |
204 | fprintf(stderr, "\n"); | |
205 | fprintf(stderr, " -a, --apname=AirPort Sets Airport name\n"); | |
206 | fprintf(stderr, " -p, --password=secret Sets password\n"); | |
207 | fprintf(stderr, " -o, --server_port=5000 Sets port for RAOP service\n"); | |
208 | fprintf(stderr, " --ao_driver=driver Sets the ao driver (optional)\n"); | |
209 | fprintf(stderr, " --ao_devicename=devicename Sets the ao device name (optional)\n"); | |
210 | fprintf(stderr, " --ao_deviceid=id Sets the ao device id (optional)\n"); | |
211 | fprintf(stderr, " -h, --help This help\n"); | |
212 | fprintf(stderr, "\n"); | |
213 | return 1; | |
214 | } | |
215 | } | |
216 | ||
217 | /* Set default values for apname and port */ | |
218 | if (!strlen(opt->apname)) { | |
219 | strncpy(opt->apname, "Shairplay", sizeof(opt->apname)-1); | |
220 | } | |
221 | if (!opt->port) { | |
222 | opt->port = 5000; | |
223 | } | |
224 | return 0; | |
225 | } | |
226 | ||
227 | int | |
228 | main(int argc, char *argv[]) | |
229 | { | |
230 | const char hwaddr[] = { 0x48, 0x5d, 0x60, 0x7c, 0xee, 0x22 }; | |
231 | ||
232 | shairplay_options_t options; | |
233 | ao_device *device = NULL; | |
234 | ||
235 | dnssd_t *dnssd; | |
236 | raop_t *raop; | |
237 | raop_callbacks_t raop_cbs; | |
238 | ||
61c2f5d5 JVH |
239 | int error; |
240 | ||
ad58f7a2 JVH |
241 | memset(&options, 0, sizeof(options)); |
242 | if (parse_options(&options, argc, argv)) { | |
243 | return 0; | |
244 | } | |
245 | ||
246 | ao_initialize(); | |
247 | ||
248 | device = audio_open_device(&options, 16, 2, 44100); | |
249 | if (device == NULL) { | |
250 | fprintf(stderr, "Error opening audio device %d\n", errno); | |
251 | fprintf(stderr, "Please check your libao settings and try again\n"); | |
252 | return -1; | |
253 | } else { | |
254 | ao_close(device); | |
255 | device = NULL; | |
256 | } | |
257 | ||
258 | memset(&raop_cbs, 0, sizeof(raop_cbs)); | |
259 | raop_cbs.cls = &options; | |
260 | raop_cbs.audio_init = audio_init; | |
261 | raop_cbs.audio_process = audio_process; | |
262 | raop_cbs.audio_destroy = audio_destroy; | |
263 | raop_cbs.audio_set_volume = audio_set_volume; | |
264 | ||
cf9b3c34 | 265 | raop = raop_init_from_keyfile(10, &raop_cbs, "airport.key", NULL); |
ad58f7a2 JVH |
266 | if (raop == NULL) { |
267 | fprintf(stderr, "Could not initialize the RAOP service\n"); | |
268 | return -1; | |
269 | } | |
270 | ||
271 | raop_set_log_level(raop, RAOP_LOG_DEBUG); | |
272 | raop_start(raop, &options.port, hwaddr, sizeof(hwaddr), NULL); | |
273 | ||
61c2f5d5 JVH |
274 | error = 0; |
275 | dnssd = dnssd_init(&error); | |
276 | if (error) { | |
277 | fprintf(stderr, "ERROR: Could not initialize dnssd library!\n"); | |
278 | fprintf(stderr, "------------------------------------------\n"); | |
279 | fprintf(stderr, "You could try the following resolutions based on your OS:\n"); | |
280 | fprintf(stderr, "Windows: Try installing http://support.apple.com/kb/DL999\n"); | |
281 | fprintf(stderr, "Debian/Ubuntu: Try installing libavahi-compat-libdnssd1 package\n"); | |
282 | raop_destroy(raop); | |
283 | return -1; | |
284 | } | |
285 | ||
ad58f7a2 | 286 | dnssd_register_raop(dnssd, options.apname, options.port, hwaddr, sizeof(hwaddr), 0); |
61c2f5d5 | 287 | dnssd_register_airplay(dnssd, options.apname, 1234, hwaddr, sizeof(hwaddr)); |
ad58f7a2 | 288 | |
258e9fb8 JVH |
289 | running = 1; |
290 | while (running) { | |
ad58f7a2 | 291 | #ifndef WIN32 |
258e9fb8 | 292 | sleep(1); |
ad58f7a2 | 293 | #else |
258e9fb8 | 294 | Sleep(1000); |
ad58f7a2 | 295 | #endif |
258e9fb8 | 296 | } |
ad58f7a2 JVH |
297 | |
298 | dnssd_unregister_raop(dnssd); | |
299 | dnssd_destroy(dnssd); | |
300 | ||
301 | raop_stop(raop); | |
302 | raop_destroy(raop); | |
303 | ||
304 | ao_shutdown(); | |
305 | ||
306 | return 0; | |
307 | } |