chiark / gitweb /
a bit more doxygen
[disorder] / clients / playrtp.c
CommitLineData
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 */
20
21#include <config.h>
22#include "types.h"
23
24#include <getopt.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <sys/socket.h>
28#include <sys/types.h>
29#include <sys/socket.h>
30#include <netdb.h>
31#include <pthread.h>
0b75463f 32#include <locale.h>
e83d0967
RK
33
34#include "log.h"
35#include "mem.h"
36#include "configuration.h"
37#include "addr.h"
38#include "syscalls.h"
39#include "rtp.h"
0b75463f 40#include "defs.h"
e83d0967
RK
41
42#if HAVE_COREAUDIO_AUDIOHARDWARE_H
43# include <CoreAudio/AudioHardware.h>
44#endif
0b75463f 45#if API_ALSA
46#include <alsa/asoundlib.h>
47#endif
e83d0967 48
1153fd23 49#define readahead linux_headers_are_borked
50
0b75463f 51/** @brief RTP socket */
e83d0967
RK
52static int rtpfd;
53
0b75463f 54/** @brief Output device */
55static const char *device;
56
57/** @brief Maximum samples per packet we'll support
58 *
59 * NB that two channels = two samples in this program.
60 */
61#define MAXSAMPLES 2048
62
63/** @brief Minimum buffer size
64 *
65 * We'll stop playing if there's only this many samples in the buffer. */
1153fd23 66static unsigned minbuffer = 2 * 44100 / 10; /* 0.2 seconds */
0b75463f 67
68/** @brief Maximum sample size
69 *
70 * The maximum supported size (in bytes) of one sample. */
71#define MAXSAMPLESIZE 2
72
1153fd23 73/** @brief Buffer size
74 *
75 * We'll only start playing when this many samples are available. */
76static unsigned readahead = 4 * 2 * 44100; /* 4 seconds */
0b75463f 77
e83d0967
RK
78#define MAXBUFFER (3 * 88200) /* maximum buffer contents */
79
0b75463f 80/** @brief Received packet
81 *
82 * Packets are recorded in an ordered linked list. */
83struct packet {
84 /** @brief Pointer to next packet
85 * The next packet might not be immediately next: if packets are dropped
86 * or mis-ordered there may be gaps at any given moment. */
87 struct packet *next;
88 /** @brief Number of samples in this packet */
89 int nsamples;
90 /** @brief Number of samples used from this packet */
91 int nused;
92 /** @brief Timestamp from RTP packet
93 *
94 * NB that "timestamps" are really sample counters.*/
95 uint32_t timestamp;
e83d0967 96#if HAVE_COREAUDIO_AUDIOHARDWARE_H
0b75463f 97 /** @brief Converted sample data */
98 float samples_float[MAXSAMPLES];
99#else
100 /** @brief Raw sample data */
101 unsigned char samples_raw[MAXSAMPLES * MAXSAMPLESIZE];
e83d0967
RK
102#endif
103};
104
0b75463f 105/** @brief Total number of samples available */
106static unsigned long nsamples;
107
108/** @brief Linked list of packets
109 *
110 * In ascending order of timestamp. */
111static struct packet *packets;
112
113/** @brief Timestamp of next packet to play.
114 *
115 * This is set to the timestamp of the last packet, plus the number of
09ee2f0d 116 * samples it contained. Only valid if @ref active is nonzero.
0b75463f 117 */
118static uint32_t next_timestamp;
e83d0967 119
09ee2f0d 120/** @brief True if actively playing
121 *
122 * This is true when playing and false when just buffering. */
123static int active;
124
0b75463f 125/** @brief Lock protecting @ref packets */
e83d0967 126static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
e83d0967 127
0b75463f 128/** @brief Condition variable signalled whenever @ref packets is changed */
129static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
e83d0967
RK
130
131static const struct option options[] = {
132 { "help", no_argument, 0, 'h' },
133 { "version", no_argument, 0, 'V' },
134 { "debug", no_argument, 0, 'd' },
0b75463f 135 { "device", required_argument, 0, 'D' },
1153fd23 136 { "min", required_argument, 0, 'm' },
137 { "buffer", required_argument, 0, 'b' },
e83d0967
RK
138 { 0, 0, 0, 0 }
139};
140
0b75463f 141/** @brief Return true iff a < b in sequence-space arithmetic */
09ee2f0d 142static inline int lt(uint32_t a, uint32_t b) {
143 return (uint32_t)(a - b) & 0x80000000;
e83d0967
RK
144}
145
09ee2f0d 146/** @brief Background thread collecting samples
0b75463f 147 *
148 * This function collects samples, perhaps converts them to the target format,
149 * and adds them to the packet list. */
150static void *listen_thread(void attribute((unused)) *arg) {
09ee2f0d 151 struct packet *p = 0, **pp;
0b75463f 152 int n;
e83d0967
RK
153 union {
154 struct rtp_header header;
155 uint8_t bytes[sizeof(uint16_t) * MAXSAMPLES + sizeof (struct rtp_header)];
156 } packet;
157 const uint16_t *const samples = (uint16_t *)(packet.bytes
158 + sizeof (struct rtp_header));
159
160 for(;;) {
09ee2f0d 161 if(!p)
162 p = xmalloc(sizeof *p);
e83d0967
RK
163 n = read(rtpfd, packet.bytes, sizeof packet.bytes);
164 if(n < 0) {
165 switch(errno) {
166 case EINTR:
167 continue;
168 default:
169 fatal(errno, "error reading from socket");
170 }
171 }
0b75463f 172 /* Ignore too-short packets */
173 if((size_t)n <= sizeof (struct rtp_header))
174 continue;
09ee2f0d 175 p->nused = 0;
176 p->timestamp = ntohl(packet.header.timestamp);
177 /* Ignore packets in the past */
178 if(active && lt(p->timestamp, next_timestamp))
179 continue;
e83d0967 180 /* Convert to target format */
0b75463f 181 switch(packet.header.mpt & 0x7F) {
e83d0967 182 case 10:
09ee2f0d 183 p->nsamples = (n - sizeof (struct rtp_header)) / sizeof(uint16_t);
0b75463f 184#if HAVE_COREAUDIO_AUDIOHARDWARE_H
185 /* Convert to what Core Audio expects */
09ee2f0d 186 for(n = 0; n < p->nsamples; ++n)
187 p->samples_float[n] = (int16_t)ntohs(samples[n]) * (0.5f / 32767);
0b75463f 188#else
189 /* ALSA can do any necessary conversion itself (though it might be better
190 * to do any necessary conversion in the background) */
09ee2f0d 191 memcpy(p->samples_raw, samples, n - sizeof (struct rtp_header));
0b75463f 192#endif
e83d0967
RK
193 break;
194 /* TODO support other RFC3551 media types (when the speaker does) */
195 default:
0b75463f 196 fatal(0, "unsupported RTP payload type %d",
e83d0967
RK
197 packet.header.mpt & 0x7F);
198 }
e83d0967 199 pthread_mutex_lock(&lock);
0b75463f 200 /* Stop reading if we've reached the maximum.
201 *
202 * This is rather unsatisfactory: it means that if packets get heavily
203 * out of order then we guarantee dropouts. But for now... */
e83d0967
RK
204 while(nsamples >= MAXBUFFER)
205 pthread_cond_wait(&cond, &lock);
09ee2f0d 206 for(pp = &packets;
207 *pp && lt((*pp)->timestamp, p->timestamp);
208 pp = &(*pp)->next)
e83d0967 209 ;
09ee2f0d 210 /* So now either !*pp or *pp >= p */
211 if(*pp && p->timestamp == (*pp)->timestamp) {
212 /* *pp == p; a duplicate. Ideally we avoid the translation step here,
0b75463f 213 * but we'll worry about that another time. */
0b75463f 214 } else {
09ee2f0d 215 p->next = *pp;
216 *pp = p;
217 nsamples += p->nsamples;
0b75463f 218 pthread_cond_broadcast(&cond);
09ee2f0d 219 p = 0; /* we've consumed this packet */
0b75463f 220 }
e83d0967 221 pthread_mutex_unlock(&lock);
e83d0967
RK
222 }
223}
224
225#if HAVE_COREAUDIO_AUDIOHARDWARE_H
09ee2f0d 226/** @brief Callback from Core Audio */
e83d0967
RK
227static OSStatus adioproc(AudioDeviceID inDevice,
228 const AudioTimeStamp *inNow,
229 const AudioBufferList *inInputData,
230 const AudioTimeStamp *inInputTime,
231 AudioBufferList *outOutputData,
232 const AudioTimeStamp *inOutputTime,
233 void *inClientData) {
234 UInt32 nbuffers = outOutputData->mNumberBuffers;
235 AudioBuffer *ab = outOutputData->mBuffers;
236 float *samplesOut; /* where to write samples to */
237 size_t samplesOutLeft; /* space left */
238 size_t samplesInLeft;
239 size_t samplesToCopy;
240
0b75463f 241 pthread_mutex_lock(&lock);
e83d0967
RK
242 samplesOut = ab->data;
243 samplesOutLeft = ab->mDataByteSize / sizeof (float);
0b75463f 244 while(packets && nbuffers > 0) {
245 if(packets->used == packets->nsamples) {
e83d0967 246 /* TODO if we dropped a packet then we should introduce a gap here */
09ee2f0d 247 struct packet *const p = packets;
248 packets = p->next;
249 free(p);
e83d0967
RK
250 pthread_cond_broadcast(&cond);
251 continue;
252 }
253 if(samplesOutLeft == 0) {
254 --nbuffers;
255 ++ab;
256 samplesOut = ab->data;
257 samplesOutLeft = ab->mDataByteSize / sizeof (float);
258 continue;
259 }
260 /* Now: (1) there is some data left to read
261 * (2) there is some space to put it */
0b75463f 262 samplesInLeft = packets->nsamples - packets->used;
e83d0967
RK
263 samplesToCopy = (samplesInLeft < samplesOutLeft
264 ? samplesInLeft : samplesOutLeft);
0b75463f 265 memcpy(samplesOut, packet->samples + packets->used, samplesToCopy);
266 packets->used += samplesToCopy;
e83d0967
RK
267 samplesOut += samplesToCopy;
268 samesOutLeft -= samplesToCopy;
269 }
270 pthread_mutex_unlock(&lock);
271 return 0;
272}
273#endif
274
09ee2f0d 275/** @brief Play an RTP stream
276 *
277 * This is the guts of the program. It is responsible for:
278 * - starting the listening thread
279 * - opening the audio device
280 * - reading ahead to build up a buffer
281 * - arranging for audio to be played
282 * - detecting when the buffer has got too small and re-buffering
283 */
0b75463f 284static void play_rtp(void) {
285 pthread_t ltid;
e83d0967
RK
286
287 /* We receive and convert audio data in a background thread */
0b75463f 288 pthread_create(&ltid, 0, listen_thread, 0);
e83d0967 289#if API_ALSA
0b75463f 290 {
291 snd_pcm_t *pcm;
292 snd_pcm_hw_params_t *hwparams;
293 snd_pcm_sw_params_t *swparams;
294 /* Only support one format for now */
295 const int sample_format = SND_PCM_FORMAT_S16_BE;
296 unsigned rate = 44100;
297 const int channels = 2;
298 const int samplesize = channels * sizeof(uint16_t);
299 snd_pcm_uframes_t pcm_bufsize = MAXSAMPLES * samplesize * 3;
300 /* If we can write more than this many samples we'll get a wakeup */
301 const int avail_min = 256;
302 snd_pcm_sframes_t frames_written;
303 size_t samples_written;
304 int prepared = 1;
305 int err;
ed13cbc8 306 int infilling = 0;
0b75463f 307
308 /* Open ALSA */
309 if((err = snd_pcm_open(&pcm,
310 device ? device : "default",
311 SND_PCM_STREAM_PLAYBACK,
312 SND_PCM_NONBLOCK)))
313 fatal(0, "error from snd_pcm_open: %d", err);
314 /* Set up 'hardware' parameters */
315 snd_pcm_hw_params_alloca(&hwparams);
316 if((err = snd_pcm_hw_params_any(pcm, hwparams)) < 0)
317 fatal(0, "error from snd_pcm_hw_params_any: %d", err);
318 if((err = snd_pcm_hw_params_set_access(pcm, hwparams,
319 SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
320 fatal(0, "error from snd_pcm_hw_params_set_access: %d", err);
321 if((err = snd_pcm_hw_params_set_format(pcm, hwparams,
322 sample_format)) < 0)
323 fatal(0, "error from snd_pcm_hw_params_set_format (%d): %d",
324 sample_format, err);
325 if((err = snd_pcm_hw_params_set_rate_near(pcm, hwparams, &rate, 0)) < 0)
326 fatal(0, "error from snd_pcm_hw_params_set_rate (%d): %d",
327 rate, err);
328 if((err = snd_pcm_hw_params_set_channels(pcm, hwparams,
329 channels)) < 0)
330 fatal(0, "error from snd_pcm_hw_params_set_channels (%d): %d",
331 channels, err);
332 if((err = snd_pcm_hw_params_set_buffer_size_near(pcm, hwparams,
333 &pcm_bufsize)) < 0)
334 fatal(0, "error from snd_pcm_hw_params_set_buffer_size (%d): %d",
335 MAXSAMPLES * samplesize * 3, err);
336 if((err = snd_pcm_hw_params(pcm, hwparams)) < 0)
337 fatal(0, "error calling snd_pcm_hw_params: %d", err);
338 /* Set up 'software' parameters */
339 snd_pcm_sw_params_alloca(&swparams);
340 if((err = snd_pcm_sw_params_current(pcm, swparams)) < 0)
341 fatal(0, "error calling snd_pcm_sw_params_current: %d", err);
342 if((err = snd_pcm_sw_params_set_avail_min(pcm, swparams, avail_min)) < 0)
343 fatal(0, "error calling snd_pcm_sw_params_set_avail_min %d: %d",
344 avail_min, err);
345 if((err = snd_pcm_sw_params(pcm, swparams)) < 0)
346 fatal(0, "error calling snd_pcm_sw_params: %d", err);
347
348 /* Ready to go */
349
350 pthread_mutex_lock(&lock);
351 for(;;) {
352 /* Wait for the buffer to fill up a bit */
ed13cbc8 353 info("Buffering...");
1153fd23 354 while(nsamples < readahead)
0b75463f 355 pthread_cond_wait(&cond, &lock);
356 if(!prepared) {
357 if((err = snd_pcm_prepare(pcm)))
358 fatal(0, "error calling snd_pcm_prepare: %d", err);
359 prepared = 1;
360 }
09ee2f0d 361 /* Start at the first available packet */
362 next_timestamp = packets->timestamp;
363 active = 1;
ed13cbc8 364 infilling = 0;
365 info("Playing...");
0b75463f 366 /* Wait until the buffer empties out */
1153fd23 367 while(nsamples >= minbuffer) {
0b75463f 368 /* Wait for ALSA to ask us for more data */
369 pthread_mutex_unlock(&lock);
370 snd_pcm_wait(pcm, -1);
371 pthread_mutex_lock(&lock);
09ee2f0d 372 /* ALSA is ready for more data */
0b75463f 373 if(packets && packets->timestamp + packets->nused == next_timestamp) {
374 /* Hooray, we have a packet we can play */
375 const size_t samples_available = packets->nsamples - packets->nused;
376 const size_t frames_available = samples_available / 2;
377
378 frames_written = snd_pcm_writei(pcm,
379 packets->samples_raw + packets->nused,
380 frames_available);
1153fd23 381 if(frames_written < 0) {
382 if(frames_written != -EAGAIN)
383 fatal(0, "error calling snd_pcm_writei: %ld",
384 (long)frames_written);
385 } else {
386 samples_written = frames_written * 2;
387 packets->nused += samples_written;
388 next_timestamp += samples_written;
389 if(packets->nused == packets->nsamples) {
390 /* We're done with this packet */
391 struct packet *p = packets;
392
393 packets = p->next;
394 nsamples -= p->nsamples;
395 free(p);
396 pthread_cond_broadcast(&cond);
397 }
398 infilling = 0;
0b75463f 399 }
400 } else {
401 /* We don't have anything to play! We'd better play some 0s. */
402 static const uint16_t zeros[1024];
403 size_t samples_available = 1024, frames_available;
ed13cbc8 404
405 if(!infilling) {
406 info("Infilling...");
407 infilling = 1;
408 }
0b75463f 409 if(packets && next_timestamp + samples_available > packets->timestamp)
410 samples_available = packets->timestamp - next_timestamp;
411 frames_available = samples_available / 2;
412 frames_written = snd_pcm_writei(pcm,
413 zeros,
414 frames_available);
1153fd23 415 if(frames_written < 0) {
416 if(frames_written != -EAGAIN)
417 fatal(0, "error calling snd_pcm_writei: %ld",
418 (long)frames_written);
74a94bd0 419 } else {
420 samples_written = frames_written * 2;
1153fd23 421 next_timestamp += samples_written;
74a94bd0 422 }
0b75463f 423 }
424 }
09ee2f0d 425 active = 0;
0b75463f 426 /* We stop playing for a bit until the buffer re-fills */
427 pthread_mutex_unlock(&lock);
ed13cbc8 428 if((err = snd_pcm_nonblock(pcm, 0)))
429 fatal(0, "error calling snd_pcm_nonblock: %d", err);
0b75463f 430 if((err = snd_pcm_drain(pcm)))
431 fatal(0, "error calling snd_pcm_drain: %d", err);
ed13cbc8 432 if((err = snd_pcm_nonblock(pcm, 1)))
433 fatal(0, "error calling snd_pcm_nonblock: %d", err);
0b75463f 434 prepared = 0;
435 pthread_mutex_lock(&lock);
436 }
437
438 }
e83d0967
RK
439#elif HAVE_COREAUDIO_AUDIOHARDWARE_H
440 {
441 OSStatus status;
442 UInt32 propertySize;
443 AudioDeviceID adid;
444 AudioStreamBasicDescription asbd;
445
446 /* If this looks suspiciously like libao's macosx driver there's an
447 * excellent reason for that... */
448
449 /* TODO report errors as strings not numbers */
450 propertySize = sizeof adid;
451 status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice,
452 &propertySize, &adid);
453 if(status)
454 fatal(0, "AudioHardwareGetProperty: %d", (int)status);
455 if(adid == kAudioDeviceUnknown)
456 fatal(0, "no output device");
457 propertySize = sizeof asbd;
458 status = AudioDeviceGetProperty(adid, 0, false,
459 kAudioDevicePropertyStreamFormat,
460 &propertySize, &asbd);
461 if(status)
462 fatal(0, "AudioHardwareGetProperty: %d", (int)status);
463 D(("mSampleRate %f", asbd.mSampleRate));
464 D(("mFormatID %08"PRIx32, asbd.mFormatID));
465 D(("mFormatFlags %08"PRIx32, asbd.mFormatFlags));
466 D(("mBytesPerPacket %08"PRIx32, asbd.mBytesPerPacket));
467 D(("mFramesPerPacket %08"PRIx32, asbd.mFramesPerPacket));
468 D(("mBytesPerFrame %08"PRIx32, asbd.mBytesPerFrame));
469 D(("mChannelsPerFrame %08"PRIx32, asbd.mChannelsPerFrame));
470 D(("mBitsPerChannel %08"PRIx32, asbd.mBitsPerChannel));
471 D(("mReserved %08"PRIx32, asbd.mReserved));
472 if(asbd.mFormatID != kAudioFormatLinearPCM)
473 fatal(0, "audio device does not support kAudioFormatLinearPCM");
474 status = AudioDeviceAddIOProc(adid, adioproc, 0);
475 if(status)
476 fatal(0, "AudioDeviceAddIOProc: %d", (int)status);
477 pthread_mutex_lock(&lock);
478 for(;;) {
479 /* Wait for the buffer to fill up a bit */
1153fd23 480 while(nsamples < readahead)
e83d0967
RK
481 pthread_cond_wait(&cond, &lock);
482 /* Start playing now */
483 status = AudioDeviceStart(adid, adioproc);
484 if(status)
485 fatal(0, "AudioDeviceStart: %d", (int)status);
486 /* Wait until the buffer empties out */
1153fd23 487 while(nsamples >= minbuffer)
e83d0967
RK
488 pthread_cond_wait(&cond, &lock);
489 /* Stop playing for a bit until the buffer re-fills */
490 status = AudioDeviceStop(adid, adioproc);
491 if(status)
492 fatal(0, "AudioDeviceStop: %d", (int)status);
493 /* Go back round */
494 }
495 }
496#else
497# error No known audio API
498#endif
499}
500
501/* display usage message and terminate */
502static void help(void) {
503 xprintf("Usage:\n"
504 " disorder-playrtp [OPTIONS] ADDRESS [PORT]\n"
505 "Options:\n"
506 " --help, -h Display usage message\n"
507 " --version, -V Display version number\n"
0b75463f 508 " --debug, -d Turn on debugging\n"
1153fd23 509 " --device, -D DEVICE Output device\n"
510 " --min, -m FRAMES Buffer low water mark\n"
511 " --buffer, -b FRAMES Buffer high water mark\n");
e83d0967
RK
512 xfclose(stdout);
513 exit(0);
514}
515
516/* display version number and terminate */
517static void version(void) {
518 xprintf("disorder-playrtp version %s\n", disorder_version_string);
519 xfclose(stdout);
520 exit(0);
521}
522
523int main(int argc, char **argv) {
524 int n;
525 struct addrinfo *res;
526 struct stringlist sl;
0b75463f 527 char *sockname;
e83d0967 528
0b75463f 529 static const struct addrinfo prefs = {
e83d0967
RK
530 AI_PASSIVE,
531 PF_INET,
532 SOCK_DGRAM,
533 IPPROTO_UDP,
534 0,
535 0,
536 0,
537 0
538 };
539
540 mem_init();
541 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
1153fd23 542 while((n = getopt_long(argc, argv, "hVdD:m:b:", options, 0)) >= 0) {
e83d0967
RK
543 switch(n) {
544 case 'h': help();
545 case 'V': version();
546 case 'd': debugging = 1; break;
0b75463f 547 case 'D': device = optarg; break;
1153fd23 548 case 'm': minbuffer = 2 * atol(optarg); break;
549 case 'b': readahead = 2 * atol(optarg); break;
e83d0967
RK
550 default: fatal(0, "invalid option");
551 }
552 }
553 argc -= optind;
554 argv += optind;
555 if(argc < 1 || argc > 2)
556 fatal(0, "usage: disorder-playrtp [OPTIONS] ADDRESS [PORT]");
557 sl.n = argc;
558 sl.s = argv;
559 /* Listen for inbound audio data */
0b75463f 560 if(!(res = get_address(&sl, &prefs, &sockname)))
e83d0967
RK
561 exit(1);
562 if((rtpfd = socket(res->ai_family,
563 res->ai_socktype,
564 res->ai_protocol)) < 0)
565 fatal(errno, "error creating socket");
566 if(bind(rtpfd, res->ai_addr, res->ai_addrlen) < 0)
567 fatal(errno, "error binding socket to %s", sockname);
568 play_rtp();
569 return 0;
570}
571
572/*
573Local Variables:
574c-basic-offset:2
575comment-column:40
576fill-column:79
577indent-tabs-mode:nil
578End:
579*/