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
20 /** @file clients/playrtp-coreaudio.c
21 * @brief RTP player - Core Audio support
26 #if HAVE_COREAUDIO_AUDIOHARDWARE_H
31 #include <CoreAudio/AudioHardware.h>
39 /** @brief Callback from Core Audio */
40 static OSStatus adioproc
41 (AudioDeviceID attribute((unused)) inDevice,
42 const AudioTimeStamp attribute((unused)) *inNow,
43 const AudioBufferList attribute((unused)) *inInputData,
44 const AudioTimeStamp attribute((unused)) *inInputTime,
45 AudioBufferList *outOutputData,
46 const AudioTimeStamp attribute((unused)) *inOutputTime,
47 void attribute((unused)) *inClientData) {
48 UInt32 nbuffers = outOutputData->mNumberBuffers;
49 AudioBuffer *ab = outOutputData->mBuffers;
50 uint32_t samples_available;
52 pthread_mutex_lock(&lock);
54 float *samplesOut = ab->mData;
55 size_t samplesOutLeft = ab->mDataByteSize / sizeof (float);
57 while(samplesOutLeft > 0) {
58 const struct packet *p = playrtp_next_packet();
59 if(p && contains(p, next_timestamp)) {
60 /* This packet is ready to play */
61 const uint32_t packet_end = p->timestamp + p->nsamples;
62 const uint32_t offset = next_timestamp - p->timestamp;
63 const uint16_t *ptr = (void *)(p->samples_raw + offset);
65 samples_available = packet_end - next_timestamp;
66 if(samples_available > samplesOutLeft)
67 samples_available = samplesOutLeft;
68 next_timestamp += samples_available;
69 samplesOutLeft -= samples_available;
73 for(n = 0; n < samples_available; ++n) {
74 dump_buffer[dump_index++] = (int16_t)ntohs(ptr[n]);
75 dump_index %= dump_size;
78 while(samples_available-- > 0)
79 *samplesOut++ = (int16_t)ntohs(*ptr++) * (0.5 / 32767);
80 /* We don't bother junking the packet - that'll be dealt with next time
83 /* No packet is ready to play (and there might be no packet at all) */
84 samples_available = p ? p->timestamp - next_timestamp
86 if(samples_available > samplesOutLeft)
87 samples_available = samplesOutLeft;
88 //info("infill by %"PRIu32, samples_available);
89 /* Conveniently the buffer is 0 to start with */
90 next_timestamp += samples_available;
91 samplesOut += samples_available;
92 samplesOutLeft -= samples_available;
96 for(n = 0; n < samples_available; ++n) {
97 dump_buffer[dump_index++] = 0;
98 dump_index %= dump_size;
106 pthread_mutex_unlock(&lock);
110 void playrtp_coreaudio(void) {
114 AudioStreamBasicDescription asbd;
116 /* If this looks suspiciously like libao's macosx driver there's an
117 * excellent reason for that... */
119 /* TODO report errors as strings not numbers */
120 propertySize = sizeof adid;
121 status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice,
122 &propertySize, &adid);
124 fatal(0, "AudioHardwareGetProperty: %d", (int)status);
125 if(adid == kAudioDeviceUnknown)
126 fatal(0, "no output device");
127 propertySize = sizeof asbd;
128 status = AudioDeviceGetProperty(adid, 0, false,
129 kAudioDevicePropertyStreamFormat,
130 &propertySize, &asbd);
132 fatal(0, "AudioHardwareGetProperty: %d", (int)status);
133 D(("mSampleRate %f", asbd.mSampleRate));
134 D(("mFormatID %08lx", asbd.mFormatID));
135 D(("mFormatFlags %08lx", asbd.mFormatFlags));
136 D(("mBytesPerPacket %08lx", asbd.mBytesPerPacket));
137 D(("mFramesPerPacket %08lx", asbd.mFramesPerPacket));
138 D(("mBytesPerFrame %08lx", asbd.mBytesPerFrame));
139 D(("mChannelsPerFrame %08lx", asbd.mChannelsPerFrame));
140 D(("mBitsPerChannel %08lx", asbd.mBitsPerChannel));
141 D(("mReserved %08lx", asbd.mReserved));
142 if(asbd.mFormatID != kAudioFormatLinearPCM)
143 fatal(0, "audio device does not support kAudioFormatLinearPCM");
144 status = AudioDeviceAddIOProc(adid, adioproc, 0);
146 fatal(0, "AudioDeviceAddIOProc: %d", (int)status);
147 pthread_mutex_lock(&lock);
149 /* Wait for the buffer to fill up a bit */
150 playrtp_fill_buffer();
151 /* Start playing now */
153 next_timestamp = pheap_first(&packets)->timestamp;
155 status = AudioDeviceStart(adid, adioproc);
157 fatal(0, "AudioDeviceStart: %d", (int)status);
158 /* Wait until the buffer empties out */
159 while(nsamples >= minbuffer
161 && contains(pheap_first(&packets), next_timestamp)))
162 pthread_cond_wait(&cond, &lock);
163 /* Stop playing for a bit until the buffer re-fills */
164 status = AudioDeviceStop(adid, adioproc);
166 fatal(0, "AudioDeviceStop: %d", (int)status);