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 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU 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, see <http://www.gnu.org/licenses/>.
18 /** @file server/speaker-coreaudio.c
19 * @brief Support for @ref BACKEND_COREAUDIO
21 * Core Audio likes to make callbacks from a separate player thread
22 * which then fill in the required number of bytes of audio. We fit
23 * this into the existing architecture by means of a pipe between the
26 * We currently only support 16-bit 44100Hz stereo (and enforce this
27 * in @ref lib/configuration.c.) There are some nasty bodges in this
28 * code which depend on this and on further assumptions still...
30 * @todo support @ref config::device
35 #if HAVE_COREAUDIO_AUDIOHARDWARE_H
38 #include <sys/socket.h>
40 #include <CoreAudio/AudioHardware.h>
42 #include "configuration.h"
45 #include "speaker-protocol.h"
48 /** @brief Core Audio Device ID */
49 static AudioDeviceID adid;
51 /** @brief Pipe between main and player threads
53 * We'll write samples to pfd[1] and read them from pfd[0].
57 /** @brief Slot number in poll array */
60 /** @brief Callback from Core Audio */
61 static OSStatus adioproc
62 (AudioDeviceID attribute((unused)) inDevice,
63 const AudioTimeStamp attribute((unused)) *inNow,
64 const AudioBufferList attribute((unused)) *inInputData,
65 const AudioTimeStamp attribute((unused)) *inInputTime,
66 AudioBufferList *outOutputData,
67 const AudioTimeStamp attribute((unused)) *inOutputTime,
68 void attribute((unused)) *inClientData) {
69 UInt32 nbuffers = outOutputData->mNumberBuffers;
70 AudioBuffer *ab = outOutputData->mBuffers;
73 float *samplesOut = ab->mData;
74 size_t samplesOutLeft = ab->mDataByteSize / sizeof (float);
75 int16_t input[1024], *ptr;
80 while(samplesOutLeft > 0) {
81 /* Read some more data */
82 bytes = samplesOutLeft * sizeof (int16_t);
83 if(bytes > sizeof input)
86 bytes_read = read(pfd[0], input, bytes);
90 continue; /* just try again */
92 return 0; /* underrun - just play 0s */
94 fatal(errno, "read error in core audio thread");
96 assert(bytes_read % 4 == 0); /* TODO horrible bodge! */
97 samples = bytes_read / sizeof (int16_t);
98 assert(samples <= samplesOutLeft);
100 samplesOutLeft -= samples;
102 *samplesOut++ = *ptr++ * (0.5 / 32767);
110 /** @brief Core Audio backend initialization */
111 static void coreaudio_init(void) {
114 AudioStreamBasicDescription asbd;
116 propertySize = sizeof adid;
117 status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice,
118 &propertySize, &adid);
120 fatal(0, "AudioHardwareGetProperty: %d", (int)status);
121 if(adid == kAudioDeviceUnknown)
122 fatal(0, "no output device");
123 propertySize = sizeof asbd;
124 status = AudioDeviceGetProperty(adid, 0, false,
125 kAudioDevicePropertyStreamFormat,
126 &propertySize, &asbd);
128 fatal(0, "AudioHardwareGetProperty: %d", (int)status);
129 D(("mSampleRate %f", asbd.mSampleRate));
130 D(("mFormatID %08lx", asbd.mFormatID));
131 D(("mFormatFlags %08lx", asbd.mFormatFlags));
132 D(("mBytesPerPacket %08lx", asbd.mBytesPerPacket));
133 D(("mFramesPerPacket %08lx", asbd.mFramesPerPacket));
134 D(("mBytesPerFrame %08lx", asbd.mBytesPerFrame));
135 D(("mChannelsPerFrame %08lx", asbd.mChannelsPerFrame));
136 D(("mBitsPerChannel %08lx", asbd.mBitsPerChannel));
137 D(("mReserved %08lx", asbd.mReserved));
138 if(asbd.mFormatID != kAudioFormatLinearPCM)
139 fatal(0, "audio device does not support kAudioFormatLinearPCM");
140 status = AudioDeviceAddIOProc(adid, adioproc, 0);
142 fatal(0, "AudioDeviceAddIOProc: %d", (int)status);
143 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfd) < 0)
144 fatal(errno, "error calling socketpair");
147 info("selected Core Audio backend");
150 /** @brief Core Audio deactivation */
151 static void coreaudio_deactivate(void) {
152 const OSStatus status = AudioDeviceStop(adid, adioproc);
154 error(0, "AudioDeviceStop: %d", (int)status);
155 device_state = device_error;
157 device_state = device_closed;
160 /** @brief Core Audio backend activation */
161 static void coreaudio_activate(void) {
162 const OSStatus status = AudioDeviceStart(adid, adioproc);
165 error(0, "AudioDeviceStart: %d", (int)status);
166 device_state = device_error;
168 device_state = device_open;
171 /** @brief Play via Core Audio */
172 static size_t coreaudio_play(size_t frames) {
173 static size_t leftover;
175 size_t bytes = frames * bpf + leftover;
176 ssize_t bytes_written;
179 /* There is a partial frame left over from an earlier write. Try
180 * and finish that off before doing anything else. */
182 bytes_written = write(pfd[1], playing->buffer + playing->start, bytes);
183 if(bytes_written < 0)
185 case EINTR: /* interrupted */
186 case EAGAIN: /* buffer full */
187 return 0; /* try later */
189 fatal(errno, "error writing to core audio player thread");
192 /* We were dealing the leftover bytes of a partial frame */
193 leftover -= bytes_written;
196 leftover = bytes_written % bpf;
197 return bytes_written / bpf;
200 /** @brief Fill in poll fd array for Core Audio */
201 static void coreaudio_beforepoll(int attribute((unused)) *timeoutp) {
202 pfd_slot = addfd(pfd[1], POLLOUT);
205 /** @brief Process poll() results for Core Audio */
206 static int coreaudio_ready(void) {
207 return !!(fds[pfd_slot].revents & (POLLOUT|POLLERR));
210 /** @brief Backend definition for Core Audio */
211 const struct speaker_backend coreaudio_backend = {
217 coreaudio_deactivate,
218 coreaudio_beforepoll,