Mercurial > mplayer.hg
comparison libao2/ao_macosx.c @ 10147:f2725d6717bd
Native MacOSX audio output by Dan Christiansen <danchr@daimi.au.dk>
author | alex |
---|---|
date | Wed, 21 May 2003 21:15:46 +0000 |
parents | |
children | b252d1b6829e |
comparison
equal
deleted
inserted
replaced
10146:d72aabc36ca1 | 10147:f2725d6717bd |
---|---|
1 /* | |
2 * | |
3 * ao_macosx.c | |
4 * | |
5 * Original Copyright (C) Timothy J. Wood - Aug 2000 | |
6 * | |
7 * This file is part of libao, a cross-platform library. See | |
8 * README for a history of this source code. | |
9 * | |
10 * libao is free software; you can redistribute it and/or modify | |
11 * it under the terms of the GNU General Public License as published by | |
12 * the Free Software Foundation; either version 2, or (at your option) | |
13 * any later version. | |
14 * | |
15 * libao is distributed in the hope that it will be useful, | |
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 * GNU General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU General Public License | |
21 * along with GNU Make; see the file COPYING. If not, write to | |
22 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 * | |
24 * $Id$ | |
25 */ | |
26 | |
27 /* | |
28 * The MacOS X CoreAudio framework doesn't mesh as simply as some | |
29 * simpler frameworks do. This is due to the fact that CoreAudio pulls | |
30 * audio samples rather than having them pushed at it (which is nice | |
31 * when you are wanting to do good buffering of audio). | |
32 */ | |
33 | |
34 /* Change log: | |
35 * | |
36 * 14/5-2003: Ported to MPlayer libao2 by Dan Christiansen | |
37 * | |
38 * AC-3 and MPEG audio passthrough is possible, but I don't have | |
39 * access to a sound card that supports it. | |
40 */ | |
41 | |
42 | |
43 */ | |
44 | |
45 #include <CoreAudio/AudioHardware.h> | |
46 #include <stdio.h> | |
47 #include <string.h> | |
48 #include <inttypes.h> | |
49 #include <pthread.h> | |
50 | |
51 #include "../mp_msg.h" | |
52 | |
53 #include "audio_out.h" | |
54 #include "audio_out_internal.h" | |
55 #include "afmt.h" | |
56 | |
57 static ao_info_t info = | |
58 { | |
59 "Darwin/Mac OS X native audio output", | |
60 "macosx", | |
61 "Timothy J. Wood & Dan Christiansen", | |
62 "" | |
63 }; | |
64 | |
65 LIBAO_EXTERN(macosx) | |
66 | |
67 /* This is large, but best (maybe it should be even larger). | |
68 * CoreAudio supposedly has an internal latency in the order of 2ms */ | |
69 #define NUM_BUFS 128 | |
70 | |
71 typedef struct ao_macosx_s | |
72 { | |
73 /* CoreAudio */ | |
74 AudioDeviceID outputDeviceID; | |
75 AudioStreamBasicDescription outputStreamBasicDescription; | |
76 | |
77 /* Ring-buffer */ | |
78 pthread_mutex_t buffer_mutex; /* mutex covering buffer variables */ | |
79 | |
80 unsigned char *buffer[NUM_BUFS]; | |
81 unsigned int buffer_len; | |
82 | |
83 unsigned int buf_read; | |
84 unsigned int buf_write; | |
85 unsigned int buf_read_pos; | |
86 unsigned int buf_write_pos; | |
87 int full_buffers; | |
88 int buffered_bytes; | |
89 } ao_macosx_t; | |
90 | |
91 static ao_macosx_t *ao; | |
92 | |
93 /* General purpose Ring-buffering routines */ | |
94 static int write_buffer(unsigned char* data,int len){ | |
95 int len2=0; | |
96 int x; | |
97 | |
98 while(len>0){ | |
99 if(ao->full_buffers==NUM_BUFS) { | |
100 mp_msg(MSGT_AO,MSGL_V, "ao_macosx: Buffer overrun\n"); | |
101 break; | |
102 } | |
103 | |
104 x=ao->buffer_len-ao->buf_write_pos; | |
105 if(x>len) x=len; | |
106 memcpy(ao->buffer[ao->buf_write]+ao->buf_write_pos,data+len2,x); | |
107 | |
108 /* accessing common variables, locking mutex */ | |
109 pthread_mutex_lock(&ao->buffer_mutex); | |
110 len2+=x; len-=x; | |
111 ao->buffered_bytes+=x; ao->buf_write_pos+=x; | |
112 if(ao->buf_write_pos>=ao->buffer_len) { | |
113 /* block is full, find next! */ | |
114 ao->buf_write=(ao->buf_write+1)%NUM_BUFS; | |
115 ++ao->full_buffers; | |
116 ao->buf_write_pos=0; | |
117 } | |
118 pthread_mutex_unlock(&ao->buffer_mutex); | |
119 } | |
120 | |
121 return len2; | |
122 } | |
123 | |
124 static int read_buffer(unsigned char* data,int len){ | |
125 int len2=0; | |
126 int x; | |
127 | |
128 while(len>0){ | |
129 if(ao->full_buffers==0) { | |
130 mp_msg(MSGT_AO,MSGL_V, "ao_macosx: Buffer underrun\n"); | |
131 break; | |
132 } | |
133 | |
134 x=ao->buffer_len-ao->buf_read_pos; | |
135 if(x>len) x=len; | |
136 memcpy(data+len2,ao->buffer[ao->buf_read]+ao->buf_read_pos,x); | |
137 len2+=x; len-=x; | |
138 | |
139 /* accessing common variables, locking mutex */ | |
140 pthread_mutex_lock(&ao->buffer_mutex); | |
141 ao->buffered_bytes-=x; ao->buf_read_pos+=x; | |
142 if(ao->buf_read_pos>=ao->buffer_len){ | |
143 /* block is empty, find next! */ | |
144 ao->buf_read=(ao->buf_read+1)%NUM_BUFS; | |
145 --ao->full_buffers; | |
146 ao->buf_read_pos=0; | |
147 } | |
148 pthread_mutex_unlock(&ao->buffer_mutex); | |
149 } | |
150 | |
151 | |
152 return len2; | |
153 } | |
154 | |
155 /* end ring buffer stuff */ | |
156 | |
157 /* The function that the CoreAudio thread calls when it wants more data */ | |
158 static OSStatus audioDeviceIOProc(AudioDeviceID inDevice, const AudioTimeStamp *inNow, const AudioBufferList *inInputData, const AudioTimeStamp *inInputTime, AudioBufferList *outOutputData, const AudioTimeStamp *inOutputTime, void *inClientData) | |
159 { | |
160 outOutputData->mBuffers[0].mDataByteSize = | |
161 read_buffer((char *)outOutputData->mBuffers[0].mData, ao->buffer_len); | |
162 | |
163 return 0; | |
164 } | |
165 | |
166 | |
167 static int control(int cmd,void *arg){ | |
168 switch (cmd) { | |
169 case AOCONTROL_SET_DEVICE: | |
170 case AOCONTROL_GET_DEVICE: | |
171 case AOCONTROL_GET_VOLUME: | |
172 case AOCONTROL_SET_VOLUME: | |
173 /* unimplemented/meaningless */ | |
174 return CONTROL_NA; | |
175 case AOCONTROL_QUERY_FORMAT: | |
176 /* stick with what CoreAudio requests */ | |
177 return CONTROL_NA; | |
178 default: | |
179 return CONTROL_UNKNOWN; | |
180 } | |
181 | |
182 } | |
183 | |
184 | |
185 static int init(int rate,int channels,int format,int flags) | |
186 { | |
187 OSStatus status; | |
188 UInt32 propertySize; | |
189 int rc; | |
190 int i; | |
191 | |
192 ao = (ao_macosx_t *)malloc(sizeof(ao_macosx_t)); | |
193 | |
194 /* initialise mutex */ | |
195 pthread_mutex_init(&ao->buffer_mutex, NULL); | |
196 pthread_mutex_unlock(&ao->buffer_mutex); | |
197 | |
198 /* get default output device */ | |
199 propertySize = sizeof(ao->outputDeviceID); | |
200 status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &propertySize, &(ao->outputDeviceID)); | |
201 if (status) { | |
202 mp_msg(MSGT_AO,MSGL_WARN, | |
203 "ao_coreaudio: AudioHardwareGetProperty returned %d\n", | |
204 (int)status); | |
205 return CONTROL_ERROR; | |
206 } | |
207 | |
208 if (ao->outputDeviceID == kAudioDeviceUnknown) { | |
209 mp_msg(MSGT_AO,MSGL_WARN, "ao_coreaudio: AudioHardwareGetProperty: ao->outputDeviceID is kAudioDeviceUnknown\n"); | |
210 return CONTROL_ERROR; | |
211 } | |
212 | |
213 /* get default output format | |
214 * TODO: get all support formats and iterate through them | |
215 */ | |
216 propertySize = sizeof(ao->outputStreamBasicDescription); | |
217 status = AudioDeviceGetProperty(ao->outputDeviceID, 0, false, kAudioDevicePropertyStreamFormat, &propertySize, &ao->outputStreamBasicDescription); | |
218 if (status) { | |
219 mp_msg(MSGT_AO,MSGL_WARN, "ao_coreaudio: AudioDeviceGetProperty returned %d when getting kAudioDevicePropertyStreamFormat\n", (int)status); | |
220 return CONTROL_ERROR; | |
221 } | |
222 | |
223 mp_msg(MSGT_AO,MSGL_V, "hardware format...\n"); | |
224 mp_msg(MSGT_AO,MSGL_V, "%f mSampleRate\n", ao->outputStreamBasicDescription.mSampleRate); | |
225 mp_msg(MSGT_AO,MSGL_V, " %c%c%c%c mFormatID\n", | |
226 (int)(ao->outputStreamBasicDescription.mFormatID & 0xff000000) >> 24, | |
227 (int)(ao->outputStreamBasicDescription.mFormatID & 0x00ff0000) >> 16, | |
228 (int)(ao->outputStreamBasicDescription.mFormatID & 0x0000ff00) >> 8, | |
229 (int)(ao->outputStreamBasicDescription.mFormatID & 0x000000ff) >> 0); | |
230 mp_msg(MSGT_AO,MSGL_V, "%5d mBytesPerPacket\n", | |
231 (int)ao->outputStreamBasicDescription.mBytesPerPacket); | |
232 mp_msg(MSGT_AO,MSGL_V, "%5d mFramesPerPacket\n", | |
233 (int)ao->outputStreamBasicDescription.mFramesPerPacket); | |
234 mp_msg(MSGT_AO,MSGL_V, "%5d mBytesPerFrame\n", | |
235 (int)ao->outputStreamBasicDescription.mBytesPerFrame); | |
236 mp_msg(MSGT_AO,MSGL_V, "%5d mChannelsPerFrame\n", | |
237 (int)ao->outputStreamBasicDescription.mChannelsPerFrame); | |
238 | |
239 /* get requested buffer length */ | |
240 propertySize = sizeof(ao->buffer_len); | |
241 status = AudioDeviceGetProperty(ao->outputDeviceID, 0, false, kAudioDevicePropertyBufferSize, &propertySize, &ao->buffer_len); | |
242 if (status) { | |
243 mp_msg(MSGT_AO,MSGL_WARN, "ao_coreaudio: AudioDeviceGetProperty returned %d when getting kAudioDevicePropertyBufferSize\n", (int)status); | |
244 return CONTROL_ERROR; | |
245 } | |
246 mp_msg(MSGT_AO,MSGL_V, "%5d ao->buffer_len\n", (int)ao->buffer_len); | |
247 | |
248 ao_data.samplerate = (int)ao->outputStreamBasicDescription.mSampleRate; | |
249 ao_data.channels = ao->outputStreamBasicDescription.mChannelsPerFrame; | |
250 ao_data.outburst = ao_data.buffersize = ao->buffer_len; | |
251 ao_data.bps = | |
252 ao_data.samplerate * ao->outputStreamBasicDescription.mBytesPerFrame; | |
253 | |
254 if (ao->outputStreamBasicDescription.mFormatID == kAudioFormatLinearPCM) { | |
255 uint32_t flags = ao->outputStreamBasicDescription.mFormatFlags; | |
256 if (flags & kAudioFormatFlagIsFloat) { | |
257 ao_data.format = AFMT_FLOAT; | |
258 } else { | |
259 mp_msg(MSGT_AO,MSGL_WARN, "ao_coreaudio: Unsupported audio output " | |
260 "format. Please report this to the developers\n", | |
261 (int)status); | |
262 } | |
263 | |
264 } else { | |
265 /* TODO: handle AFMT_AC3, AFMT_MPEG & friends */ | |
266 mp_msg(MSGT_AO,MSGL_WARN, "ao_coreaudio: Default Audio Device doesn't " | |
267 "support Linear PCM!\n"); | |
268 return CONTROL_ERROR; | |
269 } | |
270 | |
271 /* Allocate ring-buffer memory */ | |
272 for(i=0;i<NUM_BUFS;i++) | |
273 ao->buffer[i]=(unsigned char *) malloc(ao->buffer_len); | |
274 | |
275 | |
276 /* Prepare for playback */ | |
277 | |
278 reset(); | |
279 | |
280 /* Set the IO proc that CoreAudio will call when it needs data */ | |
281 status = AudioDeviceAddIOProc(ao->outputDeviceID, audioDeviceIOProc, NULL); | |
282 if (status) { | |
283 mp_msg(MSGT_AO,MSGL_WARN, "ao_coreaudio: AudioDeviceAddIOProc returned %d\n", (int)status); | |
284 return CONTROL_ERROR; | |
285 } | |
286 | |
287 /* Start callback */ | |
288 status = AudioDeviceStart(ao->outputDeviceID, audioDeviceIOProc); | |
289 if (status) { | |
290 mp_msg(MSGT_AO,MSGL_WARN, "ao_coreaudio: AudioDeviceStart returned %d\n", | |
291 (int)status); | |
292 return CONTROL_ERROR; | |
293 } | |
294 | |
295 return CONTROL_OK; | |
296 } | |
297 | |
298 | |
299 static int play(void* output_samples,int num_bytes,int flags) | |
300 { | |
301 return write_buffer(output_samples, num_bytes); | |
302 } | |
303 | |
304 /* set variables and buffer to initial state */ | |
305 static void reset() | |
306 { | |
307 int i; | |
308 | |
309 pthread_mutex_lock(&ao->buffer_mutex); | |
310 | |
311 /* reset ring-buffer state */ | |
312 ao->buf_read=0; | |
313 ao->buf_write=0; | |
314 ao->buf_read_pos=0; | |
315 ao->buf_write_pos=0; | |
316 | |
317 ao->full_buffers=0; | |
318 ao->buffered_bytes=0; | |
319 | |
320 for (i = 0; i < NUM_BUFS; i++) | |
321 bzero(ao->buffer[i], ao->buffer_len); | |
322 | |
323 pthread_mutex_unlock(&ao->buffer_mutex); | |
324 | |
325 return; | |
326 } | |
327 | |
328 | |
329 /* return available space */ | |
330 static int get_space() | |
331 { | |
332 return (NUM_BUFS-ao->full_buffers)*ao_data.buffersize - ao->buf_write_pos; | |
333 } | |
334 | |
335 | |
336 /* return delay until audio is played | |
337 * FIXME */ | |
338 static float get_delay() | |
339 { | |
340 return (float)(ao->buffered_bytes)/(float)ao_data.bps; | |
341 } | |
342 | |
343 | |
344 /* unload plugin and deregister from coreaudio */ | |
345 static void uninit() | |
346 { | |
347 int i; | |
348 OSErr status; | |
349 | |
350 reset(); | |
351 | |
352 status = AudioDeviceRemoveIOProc(ao->outputDeviceID, audioDeviceIOProc); | |
353 if (status) | |
354 mp_msg(MSGT_AO,MSGL_WARN, "ao_coreaudio: AudioDeviceRemoveIOProc " | |
355 "returned %d\n", (int)status); | |
356 | |
357 for(i=0;i<NUM_BUFS;i++) free(ao->buffer[i]); | |
358 free(ao); | |
359 } | |
360 | |
361 | |
362 /* stop playing, keep buffers (for pause) */ | |
363 static void audio_pause() | |
364 { | |
365 OSErr status; | |
366 | |
367 /* stop callback */ | |
368 status = AudioDeviceStop(ao->outputDeviceID, audioDeviceIOProc); | |
369 if (status) | |
370 mp_msg(MSGT_AO,MSGL_WARN, "ao_coreaudio: AudioDeviceStop returned %d\n", | |
371 (int)status); | |
372 } | |
373 | |
374 | |
375 /* resume playing, after audio_pause() */ | |
376 static void audio_resume() | |
377 { | |
378 OSErr status = AudioDeviceStart(ao->outputDeviceID, audioDeviceIOProc); | |
379 if (status) | |
380 mp_msg(MSGT_AO,MSGL_WARN, "ao_coreaudio: AudioDeviceStart returned %d\n", | |
381 (int)status); | |
382 } | |
383 | |
384 |