Commit | Line | Data |
---|---|---|
e83d0967 RK |
1 | /* |
2 | * This file is part of DisOrder. | |
3 | * Copyright (C) 2007 Richard Kettlewell | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
18 | * USA | |
19 | */ | |
28bacdc0 RK |
20 | /** @file clients/playrtp.c |
21 | * @brief RTP player | |
22 | * | |
b0fdc63d | 23 | * This player supports Linux (<a href="http://www.alsa-project.org/">ALSA</a>) |
24 | * and Apple Mac (<a | |
25 | * href="http://developer.apple.com/audio/coreaudio.html">Core Audio</a>) | |
26 | * systems. There is no support for Microsoft Windows yet, and that will in | |
27 | * fact probably an entirely separate program. | |
28 | * | |
189e9830 RK |
29 | * The program runs (at least) three threads. listen_thread() is responsible |
30 | * for reading RTP packets off the wire and adding them to the linked list @ref | |
31 | * received_packets, assuming they are basically sound. queue_thread() takes | |
32 | * packets off this linked list and adds them to @ref packets (an operation | |
33 | * which might be much slower due to contention for @ref lock). | |
b0fdc63d | 34 | * |
35 | * The main thread is responsible for actually playing audio. In ALSA this | |
36 | * means it waits until ALSA says it's ready for more audio which it then | |
c593cf7c | 37 | * plays. See @ref clients/playrtp-alsa.c. |
b0fdc63d | 38 | * |
8e3fe3d8 | 39 | * In Core Audio the main thread is only responsible for starting and stopping |
b0fdc63d | 40 | * play: the system does the actual playback in its own private thread, and |
c593cf7c | 41 | * calls adioproc() to fetch the audio data. See @ref |
42 | * clients/playrtp-coreaudio.c. | |
b0fdc63d | 43 | * |
44 | * Sometimes it happens that there is no audio available to play. This may | |
45 | * because the server went away, or a packet was dropped, or the server | |
46 | * deliberately did not send any sound because it encountered a silence. | |
189e9830 RK |
47 | * |
48 | * Assumptions: | |
49 | * - it is safe to read uint32_t values without a lock protecting them | |
28bacdc0 | 50 | */ |
e83d0967 RK |
51 | |
52 | #include <config.h> | |
53 | #include "types.h" | |
54 | ||
55 | #include <getopt.h> | |
56 | #include <stdio.h> | |
57 | #include <stdlib.h> | |
58 | #include <sys/socket.h> | |
59 | #include <sys/types.h> | |
60 | #include <sys/socket.h> | |
61 | #include <netdb.h> | |
62 | #include <pthread.h> | |
0b75463f | 63 | #include <locale.h> |
2c7c9eae | 64 | #include <sys/uio.h> |
28bacdc0 | 65 | #include <string.h> |
8e3fe3d8 | 66 | #include <assert.h> |
c593cf7c | 67 | #include <errno.h> |
e3426f7b | 68 | #include <netinet/in.h> |
e83d0967 RK |
69 | |
70 | #include "log.h" | |
71 | #include "mem.h" | |
72 | #include "configuration.h" | |
73 | #include "addr.h" | |
74 | #include "syscalls.h" | |
75 | #include "rtp.h" | |
0b75463f | 76 | #include "defs.h" |
28bacdc0 RK |
77 | #include "vector.h" |
78 | #include "heap.h" | |
189e9830 | 79 | #include "timeval.h" |
a7e9570a | 80 | #include "client.h" |
8e3fe3d8 | 81 | #include "playrtp.h" |
e83d0967 | 82 | |
1153fd23 | 83 | #define readahead linux_headers_are_borked |
84 | ||
e3426f7b RK |
85 | /** @brief Obsolete synonym */ |
86 | #ifndef IPV6_JOIN_GROUP | |
87 | # define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP | |
88 | #endif | |
89 | ||
0b75463f | 90 | /** @brief RTP socket */ |
e83d0967 RK |
91 | static int rtpfd; |
92 | ||
345ebe66 RK |
93 | /** @brief Log output */ |
94 | static FILE *logfp; | |
95 | ||
0b75463f | 96 | /** @brief Output device */ |
8e3fe3d8 | 97 | const char *device; |
0b75463f | 98 | |
9086a105 | 99 | /** @brief Minimum low watermark |
0b75463f | 100 | * |
101 | * We'll stop playing if there's only this many samples in the buffer. */ | |
c593cf7c | 102 | unsigned minbuffer = 2 * 44100 / 10; /* 0.2 seconds */ |
0b75463f | 103 | |
9086a105 | 104 | /** @brief Buffer high watermark |
1153fd23 | 105 | * |
106 | * We'll only start playing when this many samples are available. */ | |
8d0c14d7 | 107 | static unsigned readahead = 2 * 2 * 44100; |
0b75463f | 108 | |
9086a105 RK |
109 | /** @brief Maximum buffer size |
110 | * | |
111 | * We'll stop reading from the network if we have this many samples. */ | |
112 | static unsigned maxbuffer; | |
113 | ||
189e9830 RK |
114 | /** @brief Received packets |
115 | * Protected by @ref receive_lock | |
116 | * | |
117 | * Received packets are added to this list, and queue_thread() picks them off | |
118 | * it and adds them to @ref packets. Whenever a packet is added to it, @ref | |
119 | * receive_cond is signalled. | |
120 | */ | |
8e3fe3d8 | 121 | struct packet *received_packets; |
189e9830 RK |
122 | |
123 | /** @brief Tail of @ref received_packets | |
124 | * Protected by @ref receive_lock | |
125 | */ | |
8e3fe3d8 | 126 | struct packet **received_tail = &received_packets; |
189e9830 RK |
127 | |
128 | /** @brief Lock protecting @ref received_packets | |
129 | * | |
130 | * Only listen_thread() and queue_thread() ever hold this lock. It is vital | |
131 | * that queue_thread() not hold it any longer than it strictly has to. */ | |
8e3fe3d8 | 132 | pthread_mutex_t receive_lock = PTHREAD_MUTEX_INITIALIZER; |
189e9830 RK |
133 | |
134 | /** @brief Condition variable signalled when @ref received_packets is updated | |
135 | * | |
136 | * Used by listen_thread() to notify queue_thread() that it has added another | |
137 | * packet to @ref received_packets. */ | |
8e3fe3d8 | 138 | pthread_cond_t receive_cond = PTHREAD_COND_INITIALIZER; |
189e9830 RK |
139 | |
140 | /** @brief Length of @ref received_packets */ | |
8e3fe3d8 | 141 | uint32_t nreceived; |
28bacdc0 RK |
142 | |
143 | /** @brief Binary heap of received packets */ | |
8e3fe3d8 | 144 | struct pheap packets; |
28bacdc0 | 145 | |
189e9830 RK |
146 | /** @brief Total number of samples available |
147 | * | |
148 | * We make this volatile because we inspect it without a protecting lock, | |
149 | * so the usual pthread_* guarantees aren't available. | |
150 | */ | |
8e3fe3d8 | 151 | volatile uint32_t nsamples; |
0b75463f | 152 | |
153 | /** @brief Timestamp of next packet to play. | |
154 | * | |
155 | * This is set to the timestamp of the last packet, plus the number of | |
09ee2f0d | 156 | * samples it contained. Only valid if @ref active is nonzero. |
0b75463f | 157 | */ |
8e3fe3d8 | 158 | uint32_t next_timestamp; |
e83d0967 | 159 | |
09ee2f0d | 160 | /** @brief True if actively playing |
161 | * | |
162 | * This is true when playing and false when just buffering. */ | |
8e3fe3d8 | 163 | int active; |
09ee2f0d | 164 | |
189e9830 | 165 | /** @brief Lock protecting @ref packets */ |
8e3fe3d8 | 166 | pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; |
189e9830 RK |
167 | |
168 | /** @brief Condition variable signalled whenever @ref packets is changed */ | |
8e3fe3d8 | 169 | pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
2c7c9eae | 170 | |
146e86fb | 171 | #if HAVE_ALSA_ASOUNDLIB_H |
c593cf7c | 172 | # define DEFAULT_BACKEND playrtp_alsa |
a9f0ad12 | 173 | #elif HAVE_SYS_SOUNDCARD_H || EMPEG_HOST |
c593cf7c | 174 | # define DEFAULT_BACKEND playrtp_oss |
175 | #elif HAVE_COREAUDIO_AUDIOHARDWARE_H | |
176 | # define DEFAULT_BACKEND playrtp_coreaudio | |
177 | #else | |
178 | # error No known backend | |
179 | #endif | |
180 | ||
181 | /** @brief Backend to play with */ | |
182 | static void (*backend)(void) = &DEFAULT_BACKEND; | |
183 | ||
8e3fe3d8 | 184 | HEAP_DEFINE(pheap, struct packet *, lt_packet); |
e83d0967 RK |
185 | |
186 | static const struct option options[] = { | |
187 | { "help", no_argument, 0, 'h' }, | |
188 | { "version", no_argument, 0, 'V' }, | |
189 | { "debug", no_argument, 0, 'd' }, | |
0b75463f | 190 | { "device", required_argument, 0, 'D' }, |
1153fd23 | 191 | { "min", required_argument, 0, 'm' }, |
9086a105 | 192 | { "max", required_argument, 0, 'x' }, |
1153fd23 | 193 | { "buffer", required_argument, 0, 'b' }, |
1f10f780 | 194 | { "rcvbuf", required_argument, 0, 'R' }, |
23205f9c | 195 | { "multicast", required_argument, 0, 'M' }, |
a9f0ad12 | 196 | #if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST |
c593cf7c | 197 | { "oss", no_argument, 0, 'o' }, |
198 | #endif | |
146e86fb | 199 | #if HAVE_ALSA_ASOUNDLIB_H |
c593cf7c | 200 | { "alsa", no_argument, 0, 'a' }, |
201 | #endif | |
202 | #if HAVE_COREAUDIO_AUDIOHARDWARE_H | |
203 | { "core-audio", no_argument, 0, 'c' }, | |
204 | #endif | |
a7e9570a | 205 | { "config", required_argument, 0, 'C' }, |
e83d0967 RK |
206 | { 0, 0, 0, 0 } |
207 | }; | |
208 | ||
28bacdc0 RK |
209 | /** @brief Drop the first packet |
210 | * | |
211 | * Assumes that @ref lock is held. | |
212 | */ | |
213 | static void drop_first_packet(void) { | |
214 | if(pheap_count(&packets)) { | |
215 | struct packet *const p = pheap_remove(&packets); | |
216 | nsamples -= p->nsamples; | |
c593cf7c | 217 | playrtp_free_packet(p); |
2c7c9eae | 218 | pthread_cond_broadcast(&cond); |
2c7c9eae | 219 | } |
9086a105 RK |
220 | } |
221 | ||
189e9830 RK |
222 | /** @brief Background thread adding packets to heap |
223 | * | |
224 | * This just transfers packets from @ref received_packets to @ref packets. It | |
225 | * is important that it holds @ref receive_lock for as little time as possible, | |
226 | * in order to minimize the interval between calls to read() in | |
227 | * listen_thread(). | |
228 | */ | |
229 | static void *queue_thread(void attribute((unused)) *arg) { | |
230 | struct packet *p; | |
231 | ||
232 | for(;;) { | |
233 | /* Get the next packet */ | |
234 | pthread_mutex_lock(&receive_lock); | |
235 | while(!received_packets) | |
236 | pthread_cond_wait(&receive_cond, &receive_lock); | |
237 | p = received_packets; | |
238 | received_packets = p->next; | |
239 | if(!received_packets) | |
240 | received_tail = &received_packets; | |
241 | --nreceived; | |
242 | pthread_mutex_unlock(&receive_lock); | |
243 | /* Add it to the heap */ | |
244 | pthread_mutex_lock(&lock); | |
245 | pheap_insert(&packets, p); | |
246 | nsamples += p->nsamples; | |
247 | pthread_cond_broadcast(&cond); | |
248 | pthread_mutex_unlock(&lock); | |
249 | } | |
250 | } | |
251 | ||
09ee2f0d | 252 | /** @brief Background thread collecting samples |
0b75463f | 253 | * |
254 | * This function collects samples, perhaps converts them to the target format, | |
b0fdc63d | 255 | * and adds them to the packet list. |
256 | * | |
257 | * It is crucial that the gap between successive calls to read() is as small as | |
258 | * possible: otherwise packets will be dropped. | |
259 | * | |
260 | * We use a binary heap to ensure that the unavoidable effort is at worst | |
261 | * logarithmic in the total number of packets - in fact if packets are mostly | |
262 | * received in order then we will largely do constant work per packet since the | |
263 | * newest packet will always be last. | |
264 | * | |
265 | * Of more concern is that we must acquire the lock on the heap to add a packet | |
266 | * to it. If this proves a problem in practice then the answer would be | |
267 | * (probably doubly) linked list with new packets added the end and a second | |
268 | * thread which reads packets off the list and adds them to the heap. | |
269 | * | |
270 | * We keep memory allocation (mostly) very fast by keeping pre-allocated | |
c593cf7c | 271 | * packets around; see @ref playrtp_new_packet(). |
b0fdc63d | 272 | */ |
0b75463f | 273 | static void *listen_thread(void attribute((unused)) *arg) { |
2c7c9eae | 274 | struct packet *p = 0; |
0b75463f | 275 | int n; |
2c7c9eae RK |
276 | struct rtp_header header; |
277 | uint16_t seq; | |
278 | uint32_t timestamp; | |
279 | struct iovec iov[2]; | |
e83d0967 RK |
280 | |
281 | for(;;) { | |
189e9830 | 282 | if(!p) |
c593cf7c | 283 | p = playrtp_new_packet(); |
2c7c9eae RK |
284 | iov[0].iov_base = &header; |
285 | iov[0].iov_len = sizeof header; | |
286 | iov[1].iov_base = p->samples_raw; | |
b64efe7e | 287 | iov[1].iov_len = sizeof p->samples_raw / sizeof *p->samples_raw; |
2c7c9eae | 288 | n = readv(rtpfd, iov, 2); |
e83d0967 RK |
289 | if(n < 0) { |
290 | switch(errno) { | |
291 | case EINTR: | |
292 | continue; | |
293 | default: | |
294 | fatal(errno, "error reading from socket"); | |
295 | } | |
296 | } | |
0b75463f | 297 | /* Ignore too-short packets */ |
345ebe66 RK |
298 | if((size_t)n <= sizeof (struct rtp_header)) { |
299 | info("ignored a short packet"); | |
0b75463f | 300 | continue; |
345ebe66 | 301 | } |
2c7c9eae RK |
302 | timestamp = htonl(header.timestamp); |
303 | seq = htons(header.seq); | |
09ee2f0d | 304 | /* Ignore packets in the past */ |
2c7c9eae | 305 | if(active && lt(timestamp, next_timestamp)) { |
c0e41690 | 306 | info("dropping old packet, timestamp=%"PRIx32" < %"PRIx32, |
2c7c9eae | 307 | timestamp, next_timestamp); |
09ee2f0d | 308 | continue; |
c0e41690 | 309 | } |
189e9830 | 310 | p->next = 0; |
58b5a68f | 311 | p->flags = 0; |
2c7c9eae | 312 | p->timestamp = timestamp; |
e83d0967 | 313 | /* Convert to target format */ |
58b5a68f RK |
314 | if(header.mpt & 0x80) |
315 | p->flags |= IDLE; | |
2c7c9eae | 316 | switch(header.mpt & 0x7F) { |
e83d0967 | 317 | case 10: |
2c7c9eae | 318 | p->nsamples = (n - sizeof header) / sizeof(uint16_t); |
e83d0967 RK |
319 | break; |
320 | /* TODO support other RFC3551 media types (when the speaker does) */ | |
321 | default: | |
0b75463f | 322 | fatal(0, "unsupported RTP payload type %d", |
2c7c9eae | 323 | header.mpt & 0x7F); |
e83d0967 | 324 | } |
345ebe66 RK |
325 | if(logfp) |
326 | fprintf(logfp, "sequence %u timestamp %"PRIx32" length %"PRIx32" end %"PRIx32"\n", | |
2c7c9eae | 327 | seq, timestamp, p->nsamples, timestamp + p->nsamples); |
0b75463f | 328 | /* Stop reading if we've reached the maximum. |
329 | * | |
330 | * This is rather unsatisfactory: it means that if packets get heavily | |
331 | * out of order then we guarantee dropouts. But for now... */ | |
345ebe66 | 332 | if(nsamples >= maxbuffer) { |
189e9830 | 333 | pthread_mutex_lock(&lock); |
345ebe66 RK |
334 | while(nsamples >= maxbuffer) |
335 | pthread_cond_wait(&cond, &lock); | |
189e9830 | 336 | pthread_mutex_unlock(&lock); |
345ebe66 | 337 | } |
189e9830 RK |
338 | /* Add the packet to the receive queue */ |
339 | pthread_mutex_lock(&receive_lock); | |
340 | *received_tail = p; | |
341 | received_tail = &p->next; | |
342 | ++nreceived; | |
343 | pthread_cond_signal(&receive_cond); | |
344 | pthread_mutex_unlock(&receive_lock); | |
58b5a68f RK |
345 | /* We'll need a new packet */ |
346 | p = 0; | |
e83d0967 RK |
347 | } |
348 | } | |
349 | ||
5626f6d2 RK |
350 | /** @brief Wait until the buffer is adequately full |
351 | * | |
352 | * Must be called with @ref lock held. | |
353 | */ | |
c593cf7c | 354 | void playrtp_fill_buffer(void) { |
bfd27c14 RK |
355 | while(nsamples) |
356 | drop_first_packet(); | |
5626f6d2 RK |
357 | info("Buffering..."); |
358 | while(nsamples < readahead) | |
359 | pthread_cond_wait(&cond, &lock); | |
360 | next_timestamp = pheap_first(&packets)->timestamp; | |
361 | active = 1; | |
362 | } | |
363 | ||
364 | /** @brief Find next packet | |
365 | * @return Packet to play or NULL if none found | |
366 | * | |
367 | * The return packet is merely guaranteed not to be in the past: it might be | |
368 | * the first packet in the future rather than one that is actually suitable to | |
369 | * play. | |
370 | * | |
371 | * Must be called with @ref lock held. | |
372 | */ | |
c593cf7c | 373 | struct packet *playrtp_next_packet(void) { |
5626f6d2 RK |
374 | while(pheap_count(&packets)) { |
375 | struct packet *const p = pheap_first(&packets); | |
376 | if(le(p->timestamp + p->nsamples, next_timestamp)) { | |
377 | /* This packet is in the past. Drop it and try another one. */ | |
378 | drop_first_packet(); | |
379 | } else | |
380 | /* This packet is NOT in the past. (It might be in the future | |
381 | * however.) */ | |
382 | return p; | |
383 | } | |
384 | return 0; | |
385 | } | |
386 | ||
09ee2f0d | 387 | /** @brief Play an RTP stream |
388 | * | |
389 | * This is the guts of the program. It is responsible for: | |
390 | * - starting the listening thread | |
391 | * - opening the audio device | |
392 | * - reading ahead to build up a buffer | |
393 | * - arranging for audio to be played | |
394 | * - detecting when the buffer has got too small and re-buffering | |
395 | */ | |
0b75463f | 396 | static void play_rtp(void) { |
397 | pthread_t ltid; | |
e83d0967 RK |
398 | |
399 | /* We receive and convert audio data in a background thread */ | |
0b75463f | 400 | pthread_create(<id, 0, listen_thread, 0); |
189e9830 RK |
401 | /* We have a second thread to add received packets to the queue */ |
402 | pthread_create(<id, 0, queue_thread, 0); | |
c593cf7c | 403 | /* The rest of the work is backend-specific */ |
404 | backend(); | |
e83d0967 RK |
405 | } |
406 | ||
407 | /* display usage message and terminate */ | |
408 | static void help(void) { | |
409 | xprintf("Usage:\n" | |
410 | " disorder-playrtp [OPTIONS] ADDRESS [PORT]\n" | |
411 | "Options:\n" | |
1153fd23 | 412 | " --device, -D DEVICE Output device\n" |
413 | " --min, -m FRAMES Buffer low water mark\n" | |
9086a105 RK |
414 | " --buffer, -b FRAMES Buffer high water mark\n" |
415 | " --max, -x FRAMES Buffer maximum size\n" | |
1f10f780 | 416 | " --rcvbuf, -R BYTES Socket receive buffer size\n" |
23205f9c | 417 | " --multicast, -M GROUP Join multicast group\n" |
a7e9570a | 418 | " --config, -C PATH Set configuration file\n" |
146e86fb | 419 | #if HAVE_ALSA_ASOUNDLIB_H |
c593cf7c | 420 | " --alsa, -a Use ALSA to play audio\n" |
421 | #endif | |
a9f0ad12 | 422 | #if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST |
c593cf7c | 423 | " --oss, -o Use OSS to play audio\n" |
424 | #endif | |
425 | #if HAVE_COREAUDIO_AUDIOHARDWARE_H | |
426 | " --core-audio, -c Use Core Audio to play audio\n" | |
427 | #endif | |
9086a105 RK |
428 | " --help, -h Display usage message\n" |
429 | " --version, -V Display version number\n" | |
430 | ); | |
e83d0967 RK |
431 | xfclose(stdout); |
432 | exit(0); | |
433 | } | |
434 | ||
435 | /* display version number and terminate */ | |
436 | static void version(void) { | |
437 | xprintf("disorder-playrtp version %s\n", disorder_version_string); | |
438 | xfclose(stdout); | |
439 | exit(0); | |
440 | } | |
441 | ||
442 | int main(int argc, char **argv) { | |
443 | int n; | |
444 | struct addrinfo *res; | |
445 | struct stringlist sl; | |
0b75463f | 446 | char *sockname; |
1f10f780 RK |
447 | int rcvbuf, target_rcvbuf = 131072; |
448 | socklen_t len; | |
23205f9c RK |
449 | char *multicast_group = 0; |
450 | struct ip_mreq mreq; | |
451 | struct ipv6_mreq mreq6; | |
a7e9570a RK |
452 | disorder_client *c; |
453 | char *address, *port; | |
e83d0967 | 454 | |
0b75463f | 455 | static const struct addrinfo prefs = { |
e83d0967 RK |
456 | AI_PASSIVE, |
457 | PF_INET, | |
458 | SOCK_DGRAM, | |
459 | IPPROTO_UDP, | |
460 | 0, | |
461 | 0, | |
462 | 0, | |
463 | 0 | |
464 | }; | |
465 | ||
466 | mem_init(); | |
467 | if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale"); | |
a7e9570a | 468 | while((n = getopt_long(argc, argv, "hVdD:m:b:x:L:R:M:aocC:", options, 0)) >= 0) { |
e83d0967 RK |
469 | switch(n) { |
470 | case 'h': help(); | |
471 | case 'V': version(); | |
472 | case 'd': debugging = 1; break; | |
0b75463f | 473 | case 'D': device = optarg; break; |
1153fd23 | 474 | case 'm': minbuffer = 2 * atol(optarg); break; |
475 | case 'b': readahead = 2 * atol(optarg); break; | |
9086a105 | 476 | case 'x': maxbuffer = 2 * atol(optarg); break; |
345ebe66 | 477 | case 'L': logfp = fopen(optarg, "w"); break; |
1f10f780 | 478 | case 'R': target_rcvbuf = atoi(optarg); break; |
23205f9c | 479 | case 'M': multicast_group = optarg; break; |
146e86fb | 480 | #if HAVE_ALSA_ASOUNDLIB_H |
c593cf7c | 481 | case 'a': backend = playrtp_alsa; break; |
482 | #endif | |
a9f0ad12 | 483 | #if HAVE_SYS_SOUNDCARD_H || EMPEG_HOST |
c593cf7c | 484 | case 'o': backend = playrtp_oss; break; |
485 | #endif | |
486 | #if HAVE_COREAUDIO_AUDIOHARDWARE_H | |
487 | case 'c': backend = playrtp_coreaudio; break; | |
c593cf7c | 488 | #endif |
a7e9570a | 489 | case 'C': configfile = optarg; break; |
e83d0967 RK |
490 | default: fatal(0, "invalid option"); |
491 | } | |
492 | } | |
a7e9570a | 493 | if(config_read(0)) fatal(0, "cannot read configuration"); |
9086a105 RK |
494 | if(!maxbuffer) |
495 | maxbuffer = 4 * readahead; | |
e83d0967 RK |
496 | argc -= optind; |
497 | argv += optind; | |
a7e9570a RK |
498 | switch(argc) { |
499 | case 0: | |
500 | case 1: | |
501 | if(!(c = disorder_new(1))) exit(EXIT_FAILURE); | |
502 | if(disorder_connect(c)) exit(EXIT_FAILURE); | |
503 | if(disorder_rtp_address(c, &address, &port)) exit(EXIT_FAILURE); | |
504 | sl.n = 1; | |
505 | sl.s = &port; | |
506 | /* set multicast_group if address is a multicast address */ | |
507 | break; | |
508 | case 2: | |
509 | sl.n = argc; | |
510 | sl.s = argv; | |
511 | break; | |
512 | default: | |
513 | fatal(0, "usage: disorder-playrtp [OPTIONS] [ADDRESS [PORT]]"); | |
514 | } | |
e83d0967 | 515 | /* Listen for inbound audio data */ |
0b75463f | 516 | if(!(res = get_address(&sl, &prefs, &sockname))) |
e83d0967 | 517 | exit(1); |
a7e9570a | 518 | info("listening on %s", sockname); |
e83d0967 RK |
519 | if((rtpfd = socket(res->ai_family, |
520 | res->ai_socktype, | |
521 | res->ai_protocol)) < 0) | |
522 | fatal(errno, "error creating socket"); | |
523 | if(bind(rtpfd, res->ai_addr, res->ai_addrlen) < 0) | |
524 | fatal(errno, "error binding socket to %s", sockname); | |
23205f9c RK |
525 | if(multicast_group) { |
526 | if((n = getaddrinfo(multicast_group, 0, &prefs, &res))) | |
527 | fatal(0, "getaddrinfo %s: %s", multicast_group, gai_strerror(n)); | |
528 | switch(res->ai_family) { | |
529 | case PF_INET: | |
530 | mreq.imr_multiaddr = ((struct sockaddr_in *)res->ai_addr)->sin_addr; | |
531 | mreq.imr_interface.s_addr = 0; /* use primary interface */ | |
532 | if(setsockopt(rtpfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, | |
533 | &mreq, sizeof mreq) < 0) | |
534 | fatal(errno, "error calling setsockopt IP_ADD_MEMBERSHIP"); | |
535 | break; | |
536 | case PF_INET6: | |
537 | mreq6.ipv6mr_multiaddr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; | |
538 | memset(&mreq6.ipv6mr_interface, 0, sizeof mreq6.ipv6mr_interface); | |
539 | if(setsockopt(rtpfd, IPPROTO_IPV6, IPV6_JOIN_GROUP, | |
540 | &mreq6, sizeof mreq6) < 0) | |
541 | fatal(errno, "error calling setsockopt IPV6_JOIN_GROUP"); | |
542 | break; | |
543 | default: | |
544 | fatal(0, "unsupported address family %d", res->ai_family); | |
545 | } | |
546 | } | |
1f10f780 RK |
547 | len = sizeof rcvbuf; |
548 | if(getsockopt(rtpfd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &len) < 0) | |
549 | fatal(errno, "error calling getsockopt SO_RCVBUF"); | |
f0bae611 | 550 | if(target_rcvbuf > rcvbuf) { |
1f10f780 RK |
551 | if(setsockopt(rtpfd, SOL_SOCKET, SO_RCVBUF, |
552 | &target_rcvbuf, sizeof target_rcvbuf) < 0) | |
553 | error(errno, "error calling setsockopt SO_RCVBUF %d", | |
554 | target_rcvbuf); | |
555 | /* We try to carry on anyway */ | |
556 | else | |
557 | info("changed socket receive buffer from %d to %d", | |
558 | rcvbuf, target_rcvbuf); | |
559 | } else | |
560 | info("default socket receive buffer %d", rcvbuf); | |
561 | if(logfp) | |
562 | info("WARNING: -L option can impact performance"); | |
e83d0967 RK |
563 | play_rtp(); |
564 | return 0; | |
565 | } | |
566 | ||
567 | /* | |
568 | Local Variables: | |
569 | c-basic-offset:2 | |
570 | comment-column:40 | |
571 | fill-column:79 | |
572 | indent-tabs-mode:nil | |
573 | End: | |
574 | */ |