Commit | Line | Data |
---|---|---|
937be4c0 RK |
1 | /* |
2 | * This file is part of DisOrder | |
3 | * Copyright (C) 2007 Richard Kettlewell | |
4 | * | |
e7eb3a27 | 5 | * This program is free software: you can redistribute it and/or modify |
937be4c0 | 6 | * it under the terms of the GNU General Public License as published by |
e7eb3a27 | 7 | * the Free Software Foundation, either version 3 of the License, or |
937be4c0 RK |
8 | * (at your option) any later version. |
9 | * | |
e7eb3a27 RK |
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. | |
14 | * | |
937be4c0 | 15 | * You should have received a copy of the GNU General Public License |
e7eb3a27 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
937be4c0 RK |
17 | */ |
18 | /** @file server/speaker-coreaudio.c | |
19 | * @brief Support for @ref BACKEND_COREAUDIO | |
20 | * | |
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 | |
24 | * threads. | |
25 | * | |
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... | |
29 | * | |
30 | * @todo support @ref config::device | |
31 | */ | |
32 | ||
05b75f8d | 33 | #include "common.h" |
937be4c0 RK |
34 | |
35 | #if HAVE_COREAUDIO_AUDIOHARDWARE_H | |
36 | ||
937be4c0 RK |
37 | #include <poll.h> |
38 | #include <sys/socket.h> | |
39 | #include <unistd.h> | |
40 | #include <CoreAudio/AudioHardware.h> | |
41 | ||
42 | #include "configuration.h" | |
43 | #include "syscalls.h" | |
44 | #include "log.h" | |
45 | #include "speaker-protocol.h" | |
46 | #include "speaker.h" | |
47 | ||
48 | /** @brief Core Audio Device ID */ | |
49 | static AudioDeviceID adid; | |
50 | ||
51 | /** @brief Pipe between main and player threads | |
52 | * | |
53 | * We'll write samples to pfd[1] and read them from pfd[0]. | |
54 | */ | |
55 | static int pfd[2]; | |
56 | ||
57 | /** @brief Slot number in poll array */ | |
58 | static int pfd_slot; | |
59 | ||
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; | |
71 | ||
72 | while(nbuffers > 0) { | |
73 | float *samplesOut = ab->mData; | |
74 | size_t samplesOutLeft = ab->mDataByteSize / sizeof (float); | |
75 | int16_t input[1024], *ptr; | |
76 | size_t bytes; | |
77 | ssize_t bytes_read; | |
78 | size_t samples; | |
79 | ||
80 | while(samplesOutLeft > 0) { | |
81 | /* Read some more data */ | |
82 | bytes = samplesOutLeft * sizeof (int16_t); | |
83 | if(bytes > sizeof input) | |
84 | bytes = sizeof input; | |
85 | ||
86 | bytes_read = read(pfd[0], input, bytes); | |
87 | if(bytes_read < 0) | |
88 | switch(errno) { | |
89 | case EINTR: | |
90 | continue; /* just try again */ | |
91 | case EAGAIN: | |
92 | return 0; /* underrun - just play 0s */ | |
93 | default: | |
94 | fatal(errno, "read error in core audio thread"); | |
95 | } | |
96 | assert(bytes_read % 4 == 0); /* TODO horrible bodge! */ | |
97 | samples = bytes_read / sizeof (int16_t); | |
98 | assert(samples <= samplesOutLeft); | |
99 | ptr = input; | |
100 | samplesOutLeft -= samples; | |
101 | while(samples-- > 0) | |
102 | *samplesOut++ = *ptr++ * (0.5 / 32767); | |
103 | } | |
104 | ++ab; | |
105 | --nbuffers; | |
106 | } | |
107 | return 0; | |
108 | } | |
109 | ||
110 | /** @brief Core Audio backend initialization */ | |
111 | static void coreaudio_init(void) { | |
112 | OSStatus status; | |
113 | UInt32 propertySize; | |
114 | AudioStreamBasicDescription asbd; | |
115 | ||
116 | propertySize = sizeof adid; | |
117 | status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, | |
118 | &propertySize, &adid); | |
119 | if(status) | |
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); | |
127 | if(status) | |
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); | |
141 | if(status) | |
142 | fatal(0, "AudioDeviceAddIOProc: %d", (int)status); | |
143 | if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfd) < 0) | |
144 | fatal(errno, "error calling socketpair"); | |
145 | nonblock(pfd[0]); | |
146 | nonblock(pfd[1]); | |
147 | info("selected Core Audio backend"); | |
148 | } | |
149 | ||
150 | /** @brief Core Audio deactivation */ | |
151 | static void coreaudio_deactivate(void) { | |
152 | const OSStatus status = AudioDeviceStop(adid, adioproc); | |
153 | if(status) { | |
154 | error(0, "AudioDeviceStop: %d", (int)status); | |
155 | device_state = device_error; | |
156 | } else | |
157 | device_state = device_closed; | |
158 | } | |
159 | ||
160 | /** @brief Core Audio backend activation */ | |
161 | static void coreaudio_activate(void) { | |
162 | const OSStatus status = AudioDeviceStart(adid, adioproc); | |
163 | ||
164 | if(status) { | |
165 | error(0, "AudioDeviceStart: %d", (int)status); | |
166 | device_state = device_error; | |
167 | } | |
168 | device_state = device_open; | |
169 | } | |
170 | ||
171 | /** @brief Play via Core Audio */ | |
172 | static size_t coreaudio_play(size_t frames) { | |
173 | static size_t leftover; | |
174 | ||
175 | size_t bytes = frames * bpf + leftover; | |
176 | ssize_t bytes_written; | |
177 | ||
178 | if(leftover) | |
179 | /* There is a partial frame left over from an earlier write. Try | |
180 | * and finish that off before doing anything else. */ | |
181 | bytes = leftover; | |
182 | bytes_written = write(pfd[1], playing->buffer + playing->start, bytes); | |
183 | if(bytes_written < 0) | |
184 | switch(errno) { | |
185 | case EINTR: /* interrupted */ | |
186 | case EAGAIN: /* buffer full */ | |
187 | return 0; /* try later */ | |
188 | default: | |
189 | fatal(errno, "error writing to core audio player thread"); | |
190 | } | |
191 | if(leftover) { | |
192 | /* We were dealing the leftover bytes of a partial frame */ | |
193 | leftover -= bytes_written; | |
194 | return !leftover; | |
195 | } | |
196 | leftover = bytes_written % bpf; | |
197 | return bytes_written / bpf; | |
198 | } | |
199 | ||
200 | /** @brief Fill in poll fd array for Core Audio */ | |
e84fb5f0 | 201 | static void coreaudio_beforepoll(int attribute((unused)) *timeoutp) { |
937be4c0 RK |
202 | pfd_slot = addfd(pfd[1], POLLOUT); |
203 | } | |
204 | ||
205 | /** @brief Process poll() results for Core Audio */ | |
206 | static int coreaudio_ready(void) { | |
207 | return !!(fds[pfd_slot].revents & (POLLOUT|POLLERR)); | |
208 | } | |
209 | ||
210 | /** @brief Backend definition for Core Audio */ | |
211 | const struct speaker_backend coreaudio_backend = { | |
212 | BACKEND_COREAUDIO, | |
213 | 0, | |
214 | coreaudio_init, | |
215 | coreaudio_activate, | |
216 | coreaudio_play, | |
217 | coreaudio_deactivate, | |
218 | coreaudio_beforepoll, | |
219 | coreaudio_ready | |
220 | }; | |
221 | ||
222 | #endif | |
223 | ||
224 | /* | |
225 | Local Variables: | |
226 | c-basic-offset:2 | |
227 | comment-column:40 | |
228 | fill-column:79 | |
229 | indent-tabs-mode:nil | |
230 | End: | |
231 | */ |