2 * This file is part of DisOrder.
3 * Copyright (C) 2007 Richard Kettlewell
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.
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.
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
27 #include <sys/socket.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
36 #include "configuration.h"
42 #if HAVE_COREAUDIO_AUDIOHARDWARE_H
43 # include <CoreAudio/AudioHardware.h>
46 #include <alsa/asoundlib.h>
49 #define readahead linux_headers_are_borked
51 /** @brief RTP socket */
54 /** @brief Output device */
55 static const char *device;
57 /** @brief Maximum samples per packet we'll support
59 * NB that two channels = two samples in this program.
61 #define MAXSAMPLES 2048
63 /** @brief Minimum buffer size
65 * We'll stop playing if there's only this many samples in the buffer. */
66 static unsigned minbuffer = 2 * 44100 / 10; /* 0.2 seconds */
68 /** @brief Maximum sample size
70 * The maximum supported size (in bytes) of one sample. */
71 #define MAXSAMPLESIZE 2
73 /** @brief Buffer size
75 * We'll only start playing when this many samples are available. */
76 static unsigned readahead = 4 * 2 * 44100; /* 4 seconds */
78 #define MAXBUFFER (3 * 88200) /* maximum buffer contents */
80 /** @brief Received packet
82 * Packets are recorded in an ordered linked list. */
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. */
88 /** @brief Number of samples in this packet */
90 /** @brief Number of samples used from this packet */
92 /** @brief Timestamp from RTP packet
94 * NB that "timestamps" are really sample counters.*/
96 #if HAVE_COREAUDIO_AUDIOHARDWARE_H
97 /** @brief Converted sample data */
98 float samples_float[MAXSAMPLES];
100 /** @brief Raw sample data */
101 unsigned char samples_raw[MAXSAMPLES * MAXSAMPLESIZE];
105 /** @brief Total number of samples available */
106 static unsigned long nsamples;
108 /** @brief Linked list of packets
110 * In ascending order of timestamp. */
111 static struct packet *packets;
113 /** @brief Timestamp of next packet to play.
115 * This is set to the timestamp of the last packet, plus the number of
116 * samples it contained. Only valid if @ref active is nonzero.
118 static uint32_t next_timestamp;
120 /** @brief True if actively playing
122 * This is true when playing and false when just buffering. */
125 /** @brief Lock protecting @ref packets */
126 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
128 /** @brief Condition variable signalled whenever @ref packets is changed */
129 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
131 static const struct option options[] = {
132 { "help", no_argument, 0, 'h' },
133 { "version", no_argument, 0, 'V' },
134 { "debug", no_argument, 0, 'd' },
135 { "device", required_argument, 0, 'D' },
136 { "min", required_argument, 0, 'm' },
137 { "buffer", required_argument, 0, 'b' },
141 /** @brief Return true iff a < b in sequence-space arithmetic */
142 static inline int lt(uint32_t a, uint32_t b) {
143 return (uint32_t)(a - b) & 0x80000000;
146 /** @brief Background thread collecting samples
148 * This function collects samples, perhaps converts them to the target format,
149 * and adds them to the packet list. */
150 static void *listen_thread(void attribute((unused)) *arg) {
151 struct packet *p = 0, **pp;
154 struct rtp_header header;
155 uint8_t bytes[sizeof(uint16_t) * MAXSAMPLES + sizeof (struct rtp_header)];
157 const uint16_t *const samples = (uint16_t *)(packet.bytes
158 + sizeof (struct rtp_header));
162 p = xmalloc(sizeof *p);
163 n = read(rtpfd, packet.bytes, sizeof packet.bytes);
169 fatal(errno, "error reading from socket");
172 /* Ignore too-short packets */
173 if((size_t)n <= sizeof (struct rtp_header))
176 p->timestamp = ntohl(packet.header.timestamp);
177 /* Ignore packets in the past */
178 if(active && lt(p->timestamp, next_timestamp))
180 /* Convert to target format */
181 switch(packet.header.mpt & 0x7F) {
183 p->nsamples = (n - sizeof (struct rtp_header)) / sizeof(uint16_t);
184 #if HAVE_COREAUDIO_AUDIOHARDWARE_H
185 /* Convert to what Core Audio expects */
186 for(n = 0; n < p->nsamples; ++n)
187 p->samples_float[n] = (int16_t)ntohs(samples[n]) * (0.5f / 32767);
189 /* ALSA can do any necessary conversion itself (though it might be better
190 * to do any necessary conversion in the background) */
191 memcpy(p->samples_raw, samples, n - sizeof (struct rtp_header));
194 /* TODO support other RFC3551 media types (when the speaker does) */
196 fatal(0, "unsupported RTP payload type %d",
197 packet.header.mpt & 0x7F);
199 pthread_mutex_lock(&lock);
200 /* Stop reading if we've reached the maximum.
202 * This is rather unsatisfactory: it means that if packets get heavily
203 * out of order then we guarantee dropouts. But for now... */
204 while(nsamples >= MAXBUFFER)
205 pthread_cond_wait(&cond, &lock);
207 *pp && lt((*pp)->timestamp, p->timestamp);
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,
213 * but we'll worry about that another time. */
217 nsamples += p->nsamples;
218 pthread_cond_broadcast(&cond);
219 p = 0; /* we've consumed this packet */
221 pthread_mutex_unlock(&lock);
225 #if HAVE_COREAUDIO_AUDIOHARDWARE_H
226 /** @brief Callback from Core Audio */
227 static 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;
241 pthread_mutex_lock(&lock);
242 samplesOut = ab->data;
243 samplesOutLeft = ab->mDataByteSize / sizeof (float);
244 while(packets && nbuffers > 0) {
245 if(packets->used == packets->nsamples) {
246 /* TODO if we dropped a packet then we should introduce a gap here */
247 struct packet *const p = packets;
250 pthread_cond_broadcast(&cond);
253 if(samplesOutLeft == 0) {
256 samplesOut = ab->data;
257 samplesOutLeft = ab->mDataByteSize / sizeof (float);
260 /* Now: (1) there is some data left to read
261 * (2) there is some space to put it */
262 samplesInLeft = packets->nsamples - packets->used;
263 samplesToCopy = (samplesInLeft < samplesOutLeft
264 ? samplesInLeft : samplesOutLeft);
265 memcpy(samplesOut, packet->samples + packets->used, samplesToCopy);
266 packets->used += samplesToCopy;
267 samplesOut += samplesToCopy;
268 samesOutLeft -= samplesToCopy;
270 pthread_mutex_unlock(&lock);
275 /** @brief Play an RTP stream
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
284 static void play_rtp(void) {
287 /* We receive and convert audio data in a background thread */
288 pthread_create(<id, 0, listen_thread, 0);
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;
309 if((err = snd_pcm_open(&pcm,
310 device ? device : "default",
311 SND_PCM_STREAM_PLAYBACK,
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,
323 fatal(0, "error from snd_pcm_hw_params_set_format (%d): %d",
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",
328 if((err = snd_pcm_hw_params_set_channels(pcm, hwparams,
330 fatal(0, "error from snd_pcm_hw_params_set_channels (%d): %d",
332 if((err = snd_pcm_hw_params_set_buffer_size_near(pcm, hwparams,
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",
345 if((err = snd_pcm_sw_params(pcm, swparams)) < 0)
346 fatal(0, "error calling snd_pcm_sw_params: %d", err);
350 pthread_mutex_lock(&lock);
352 /* Wait for the buffer to fill up a bit */
353 info("Buffering...");
354 while(nsamples < readahead)
355 pthread_cond_wait(&cond, &lock);
357 if((err = snd_pcm_prepare(pcm)))
358 fatal(0, "error calling snd_pcm_prepare: %d", err);
361 /* Start at the first available packet */
362 next_timestamp = packets->timestamp;
366 /* Wait until the buffer empties out */
367 while(nsamples >= minbuffer) {
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);
372 /* ALSA is ready for more data */
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;
378 frames_written = snd_pcm_writei(pcm,
379 packets->samples_raw + packets->nused,
381 if(frames_written < 0) {
382 if(frames_written != -EAGAIN)
383 fatal(0, "error calling snd_pcm_writei: %ld",
384 (long)frames_written);
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;
394 nsamples -= p->nsamples;
396 pthread_cond_broadcast(&cond);
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;
406 info("Infilling...");
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,
415 if(frames_written < 0) {
416 if(frames_written != -EAGAIN)
417 fatal(0, "error calling snd_pcm_writei: %ld",
418 (long)frames_written);
420 samples_written = frames_written * 2;
421 next_timestamp += samples_written;
426 /* We stop playing for a bit until the buffer re-fills */
427 pthread_mutex_unlock(&lock);
428 if((err = snd_pcm_nonblock(pcm, 0)))
429 fatal(0, "error calling snd_pcm_nonblock: %d", err);
430 if((err = snd_pcm_drain(pcm)))
431 fatal(0, "error calling snd_pcm_drain: %d", err);
432 if((err = snd_pcm_nonblock(pcm, 1)))
433 fatal(0, "error calling snd_pcm_nonblock: %d", err);
435 pthread_mutex_lock(&lock);
439 #elif HAVE_COREAUDIO_AUDIOHARDWARE_H
444 AudioStreamBasicDescription asbd;
446 /* If this looks suspiciously like libao's macosx driver there's an
447 * excellent reason for that... */
449 /* TODO report errors as strings not numbers */
450 propertySize = sizeof adid;
451 status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice,
452 &propertySize, &adid);
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);
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);
476 fatal(0, "AudioDeviceAddIOProc: %d", (int)status);
477 pthread_mutex_lock(&lock);
479 /* Wait for the buffer to fill up a bit */
480 while(nsamples < readahead)
481 pthread_cond_wait(&cond, &lock);
482 /* Start playing now */
483 status = AudioDeviceStart(adid, adioproc);
485 fatal(0, "AudioDeviceStart: %d", (int)status);
486 /* Wait until the buffer empties out */
487 while(nsamples >= minbuffer)
488 pthread_cond_wait(&cond, &lock);
489 /* Stop playing for a bit until the buffer re-fills */
490 status = AudioDeviceStop(adid, adioproc);
492 fatal(0, "AudioDeviceStop: %d", (int)status);
497 # error No known audio API
501 /* display usage message and terminate */
502 static void help(void) {
504 " disorder-playrtp [OPTIONS] ADDRESS [PORT]\n"
506 " --help, -h Display usage message\n"
507 " --version, -V Display version number\n"
508 " --debug, -d Turn on debugging\n"
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");
516 /* display version number and terminate */
517 static void version(void) {
518 xprintf("disorder-playrtp version %s\n", disorder_version_string);
523 int main(int argc, char **argv) {
525 struct addrinfo *res;
526 struct stringlist sl;
529 static const struct addrinfo prefs = {
541 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
542 while((n = getopt_long(argc, argv, "hVdD:m:b:", options, 0)) >= 0) {
546 case 'd': debugging = 1; break;
547 case 'D': device = optarg; break;
548 case 'm': minbuffer = 2 * atol(optarg); break;
549 case 'b': readahead = 2 * atol(optarg); break;
550 default: fatal(0, "invalid option");
555 if(argc < 1 || argc > 2)
556 fatal(0, "usage: disorder-playrtp [OPTIONS] ADDRESS [PORT]");
559 /* Listen for inbound audio data */
560 if(!(res = get_address(&sl, &prefs, &sockname)))
562 if((rtpfd = socket(res->ai_family,
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);