chiark / gitweb /
Mildly more vigorous uninstall rules
[disorder] / server / speaker-coreaudio.c
CommitLineData
937be4c0
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/** @file server/speaker-coreaudio.c
21 * @brief Support for @ref BACKEND_COREAUDIO
22 *
23 * Core Audio likes to make callbacks from a separate player thread
24 * which then fill in the required number of bytes of audio. We fit
25 * this into the existing architecture by means of a pipe between the
26 * threads.
27 *
28 * We currently only support 16-bit 44100Hz stereo (and enforce this
29 * in @ref lib/configuration.c.) There are some nasty bodges in this
30 * code which depend on this and on further assumptions still...
31 *
32 * @todo support @ref config::device
33 */
34
05b75f8d 35#include "common.h"
937be4c0
RK
36
37#if HAVE_COREAUDIO_AUDIOHARDWARE_H
38
937be4c0
RK
39#include <poll.h>
40#include <sys/socket.h>
41#include <unistd.h>
42#include <CoreAudio/AudioHardware.h>
43
44#include "configuration.h"
45#include "syscalls.h"
46#include "log.h"
47#include "speaker-protocol.h"
48#include "speaker.h"
49
50/** @brief Core Audio Device ID */
51static AudioDeviceID adid;
52
53/** @brief Pipe between main and player threads
54 *
55 * We'll write samples to pfd[1] and read them from pfd[0].
56 */
57static int pfd[2];
58
59/** @brief Slot number in poll array */
60static int pfd_slot;
61
62/** @brief Callback from Core Audio */
63static OSStatus adioproc
64 (AudioDeviceID attribute((unused)) inDevice,
65 const AudioTimeStamp attribute((unused)) *inNow,
66 const AudioBufferList attribute((unused)) *inInputData,
67 const AudioTimeStamp attribute((unused)) *inInputTime,
68 AudioBufferList *outOutputData,
69 const AudioTimeStamp attribute((unused)) *inOutputTime,
70 void attribute((unused)) *inClientData) {
71 UInt32 nbuffers = outOutputData->mNumberBuffers;
72 AudioBuffer *ab = outOutputData->mBuffers;
73
74 while(nbuffers > 0) {
75 float *samplesOut = ab->mData;
76 size_t samplesOutLeft = ab->mDataByteSize / sizeof (float);
77 int16_t input[1024], *ptr;
78 size_t bytes;
79 ssize_t bytes_read;
80 size_t samples;
81
82 while(samplesOutLeft > 0) {
83 /* Read some more data */
84 bytes = samplesOutLeft * sizeof (int16_t);
85 if(bytes > sizeof input)
86 bytes = sizeof input;
87
88 bytes_read = read(pfd[0], input, bytes);
89 if(bytes_read < 0)
90 switch(errno) {
91 case EINTR:
92 continue; /* just try again */
93 case EAGAIN:
94 return 0; /* underrun - just play 0s */
95 default:
96 fatal(errno, "read error in core audio thread");
97 }
98 assert(bytes_read % 4 == 0); /* TODO horrible bodge! */
99 samples = bytes_read / sizeof (int16_t);
100 assert(samples <= samplesOutLeft);
101 ptr = input;
102 samplesOutLeft -= samples;
103 while(samples-- > 0)
104 *samplesOut++ = *ptr++ * (0.5 / 32767);
105 }
106 ++ab;
107 --nbuffers;
108 }
109 return 0;
110}
111
112/** @brief Core Audio backend initialization */
113static void coreaudio_init(void) {
114 OSStatus status;
115 UInt32 propertySize;
116 AudioStreamBasicDescription asbd;
117
118 propertySize = sizeof adid;
119 status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice,
120 &propertySize, &adid);
121 if(status)
122 fatal(0, "AudioHardwareGetProperty: %d", (int)status);
123 if(adid == kAudioDeviceUnknown)
124 fatal(0, "no output device");
125 propertySize = sizeof asbd;
126 status = AudioDeviceGetProperty(adid, 0, false,
127 kAudioDevicePropertyStreamFormat,
128 &propertySize, &asbd);
129 if(status)
130 fatal(0, "AudioHardwareGetProperty: %d", (int)status);
131 D(("mSampleRate %f", asbd.mSampleRate));
132 D(("mFormatID %08lx", asbd.mFormatID));
133 D(("mFormatFlags %08lx", asbd.mFormatFlags));
134 D(("mBytesPerPacket %08lx", asbd.mBytesPerPacket));
135 D(("mFramesPerPacket %08lx", asbd.mFramesPerPacket));
136 D(("mBytesPerFrame %08lx", asbd.mBytesPerFrame));
137 D(("mChannelsPerFrame %08lx", asbd.mChannelsPerFrame));
138 D(("mBitsPerChannel %08lx", asbd.mBitsPerChannel));
139 D(("mReserved %08lx", asbd.mReserved));
140 if(asbd.mFormatID != kAudioFormatLinearPCM)
141 fatal(0, "audio device does not support kAudioFormatLinearPCM");
142 status = AudioDeviceAddIOProc(adid, adioproc, 0);
143 if(status)
144 fatal(0, "AudioDeviceAddIOProc: %d", (int)status);
145 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfd) < 0)
146 fatal(errno, "error calling socketpair");
147 nonblock(pfd[0]);
148 nonblock(pfd[1]);
149 info("selected Core Audio backend");
150}
151
152/** @brief Core Audio deactivation */
153static void coreaudio_deactivate(void) {
154 const OSStatus status = AudioDeviceStop(adid, adioproc);
155 if(status) {
156 error(0, "AudioDeviceStop: %d", (int)status);
157 device_state = device_error;
158 } else
159 device_state = device_closed;
160}
161
162/** @brief Core Audio backend activation */
163static void coreaudio_activate(void) {
164 const OSStatus status = AudioDeviceStart(adid, adioproc);
165
166 if(status) {
167 error(0, "AudioDeviceStart: %d", (int)status);
168 device_state = device_error;
169 }
170 device_state = device_open;
171}
172
173/** @brief Play via Core Audio */
174static size_t coreaudio_play(size_t frames) {
175 static size_t leftover;
176
177 size_t bytes = frames * bpf + leftover;
178 ssize_t bytes_written;
179
180 if(leftover)
181 /* There is a partial frame left over from an earlier write. Try
182 * and finish that off before doing anything else. */
183 bytes = leftover;
184 bytes_written = write(pfd[1], playing->buffer + playing->start, bytes);
185 if(bytes_written < 0)
186 switch(errno) {
187 case EINTR: /* interrupted */
188 case EAGAIN: /* buffer full */
189 return 0; /* try later */
190 default:
191 fatal(errno, "error writing to core audio player thread");
192 }
193 if(leftover) {
194 /* We were dealing the leftover bytes of a partial frame */
195 leftover -= bytes_written;
196 return !leftover;
197 }
198 leftover = bytes_written % bpf;
199 return bytes_written / bpf;
200}
201
202/** @brief Fill in poll fd array for Core Audio */
e84fb5f0 203static void coreaudio_beforepoll(int attribute((unused)) *timeoutp) {
937be4c0
RK
204 pfd_slot = addfd(pfd[1], POLLOUT);
205}
206
207/** @brief Process poll() results for Core Audio */
208static int coreaudio_ready(void) {
209 return !!(fds[pfd_slot].revents & (POLLOUT|POLLERR));
210}
211
212/** @brief Backend definition for Core Audio */
213const struct speaker_backend coreaudio_backend = {
214 BACKEND_COREAUDIO,
215 0,
216 coreaudio_init,
217 coreaudio_activate,
218 coreaudio_play,
219 coreaudio_deactivate,
220 coreaudio_beforepoll,
221 coreaudio_ready
222};
223
224#endif
225
226/*
227Local Variables:
228c-basic-offset:2
229comment-column:40
230fill-column:79
231indent-tabs-mode:nil
232End:
233*/