Mercurial > mplayer.hg
annotate libao2/ao_coreaudio.c @ 30239:7227245da803
Use AF_FORMAT_IS_AC3 in ao_oss.c
author | reimar |
---|---|
date | Mon, 11 Jan 2010 19:55:07 +0000 |
parents | 53c43f683cd7 |
children | 03c1ad03f29d |
rev | line source |
---|---|
29209 | 1 /* |
2 * CoreAudio audio output driver for Mac OS X | |
3 * | |
4 * original copyright (C) Timothy J. Wood - Aug 2000 | |
5 * ported to MPlayer libao2 by Dan Christiansen | |
6 * | |
7 * The S/PDIF part of the code is based on the auhal audio output | |
8 * module from VideoLAN: | |
9 * Copyright (c) 2006 Derk-Jan Hartman <hartman at videolan dot org> | |
10 * | |
11 * This file is part of MPlayer. | |
12 * | |
13 * MPlayer is free software; you can redistribute it and/or modify | |
14 * it under the terms of the GNU General Public License as published by | |
15 * the Free Software Foundation; either version 2 of the License, or | |
16 * (at your option) any later version. | |
17 * | |
18 * MPlayer is distributed in the hope that it will be useful, | |
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 * GNU General Public License for more details. | |
22 * | |
23 * You should have received a copy of the GNU General Public License along | |
24 * along with MPlayer; if not, write to the Free Software | |
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
26 */ | |
27 | |
28 /* | |
29 * The MacOS X CoreAudio framework doesn't mesh as simply as some | |
30 * simpler frameworks do. This is due to the fact that CoreAudio pulls | |
31 * audio samples rather than having them pushed at it (which is nice | |
32 * when you are wanting to do good buffering of audio). | |
33 * | |
34 * AC-3 and MPEG audio passthrough is possible, but has never been tested | |
35 * due to lack of a soundcard that supports it. | |
36 */ | |
37 | |
38 #include <CoreServices/CoreServices.h> | |
39 #include <AudioUnit/AudioUnit.h> | |
40 #include <AudioToolbox/AudioToolbox.h> | |
41 #include <stdio.h> | |
42 #include <string.h> | |
43 #include <stdlib.h> | |
44 #include <inttypes.h> | |
45 #include <sys/types.h> | |
46 #include <unistd.h> | |
47 | |
48 #include "config.h" | |
49 #include "mp_msg.h" | |
50 | |
51 #include "audio_out.h" | |
52 #include "audio_out_internal.h" | |
53 #include "libaf/af_format.h" | |
54 #include "osdep/timer.h" | |
55 #include "libavutil/fifo.h" | |
56 | |
57 static const ao_info_t info = | |
58 { | |
59 "Darwin/Mac OS X native audio output", | |
60 "coreaudio", | |
61 "Timothy J. Wood & Dan Christiansen & Chris Roccati", | |
62 "" | |
63 }; | |
64 | |
65 LIBAO_EXTERN(coreaudio) | |
66 | |
67 /* Prefix for all mp_msg() calls */ | |
68 #define ao_msg(a, b, c...) mp_msg(a, b, "AO: [coreaudio] " c) | |
69 | |
70 typedef struct ao_coreaudio_s | |
71 { | |
72 AudioDeviceID i_selected_dev; /* Keeps DeviceID of the selected device. */ | |
73 int b_supports_digital; /* Does the currently selected device support digital mode? */ | |
74 int b_digital; /* Are we running in digital mode? */ | |
75 int b_muted; /* Are we muted in digital mode? */ | |
76 | |
77 /* AudioUnit */ | |
78 AudioUnit theOutputUnit; | |
79 | |
80 /* CoreAudio SPDIF mode specific */ | |
81 pid_t i_hog_pid; /* Keeps the pid of our hog status. */ | |
82 AudioStreamID i_stream_id; /* The StreamID that has a cac3 streamformat */ | |
83 int i_stream_index; /* The index of i_stream_id in an AudioBufferList */ | |
84 AudioStreamBasicDescription stream_format;/* The format we changed the stream to */ | |
85 AudioStreamBasicDescription sfmt_revert; /* The original format of the stream */ | |
86 int b_revert; /* Whether we need to revert the stream format */ | |
87 int b_changed_mixing; /* Whether we need to set the mixing mode back */ | |
88 int b_stream_format_changed; /* Flag for main thread to reset stream's format to digital and reset buffer */ | |
89 | |
90 /* Original common part */ | |
91 int packetSize; | |
92 int paused; | |
93 | |
94 /* Ring-buffer */ | |
95 AVFifoBuffer *buffer; | |
96 unsigned int buffer_len; ///< must always be num_chunks * chunk_size | |
97 unsigned int num_chunks; | |
98 unsigned int chunk_size; | |
99 } ao_coreaudio_t; | |
100 | |
101 static ao_coreaudio_t *ao = NULL; | |
102 | |
103 /** | |
104 * \brief add data to ringbuffer | |
105 */ | |
106 static int write_buffer(unsigned char* data, int len){ | |
107 int free = ao->buffer_len - av_fifo_size(ao->buffer); | |
108 if (len > free) len = free; | |
109 return av_fifo_generic_write(ao->buffer, data, len, NULL); | |
110 } | |
111 | |
112 /** | |
113 * \brief remove data from ringbuffer | |
114 */ | |
115 static int read_buffer(unsigned char* data,int len){ | |
116 int buffered = av_fifo_size(ao->buffer); | |
117 if (len > buffered) len = buffered; | |
29439
02dec439f717
100l, av_fifo_generic_read does not return anything useful, so ignore its
reimar
parents:
29401
diff
changeset
|
118 av_fifo_generic_read(ao->buffer, data, len, NULL); |
02dec439f717
100l, av_fifo_generic_read does not return anything useful, so ignore its
reimar
parents:
29401
diff
changeset
|
119 return len; |
29209 | 120 } |
121 | |
122 OSStatus theRenderProc(void *inRefCon, AudioUnitRenderActionFlags *inActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumFrames, AudioBufferList *ioData) | |
123 { | |
124 int amt=av_fifo_size(ao->buffer); | |
125 int req=(inNumFrames)*ao->packetSize; | |
126 | |
127 if(amt>req) | |
128 amt=req; | |
129 | |
130 if(amt) | |
131 read_buffer((unsigned char *)ioData->mBuffers[0].mData, amt); | |
132 else audio_pause(); | |
133 ioData->mBuffers[0].mDataByteSize = amt; | |
134 | |
135 return noErr; | |
136 } | |
137 | |
138 static int control(int cmd,void *arg){ | |
139 ao_control_vol_t *control_vol; | |
140 OSStatus err; | |
141 Float32 vol; | |
142 switch (cmd) { | |
143 case AOCONTROL_GET_VOLUME: | |
144 control_vol = (ao_control_vol_t*)arg; | |
145 if (ao->b_digital) { | |
146 // Digital output has no volume adjust. | |
147 return CONTROL_FALSE; | |
148 } | |
149 err = AudioUnitGetParameter(ao->theOutputUnit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, &vol); | |
150 | |
151 if(err==0) { | |
152 // printf("GET VOL=%f\n", vol); | |
153 control_vol->left=control_vol->right=vol*100.0/4.0; | |
154 return CONTROL_TRUE; | |
155 } | |
156 else { | |
157 ao_msg(MSGT_AO, MSGL_WARN, "could not get HAL output volume: [%4.4s]\n", (char *)&err); | |
158 return CONTROL_FALSE; | |
159 } | |
160 | |
161 case AOCONTROL_SET_VOLUME: | |
162 control_vol = (ao_control_vol_t*)arg; | |
163 | |
164 if (ao->b_digital) { | |
165 // Digital output can not set volume. Here we have to return true | |
166 // to make mixer forget it. Else mixer will add a soft filter, | |
167 // that's not we expected and the filter not support ac3 stream | |
168 // will cause mplayer die. | |
169 | |
170 // Although not support set volume, but at least we support mute. | |
171 // MPlayer set mute by set volume to zero, we handle it. | |
172 if (control_vol->left == 0 && control_vol->right == 0) | |
173 ao->b_muted = 1; | |
174 else | |
175 ao->b_muted = 0; | |
176 return CONTROL_TRUE; | |
177 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29212
diff
changeset
|
178 |
29209 | 179 vol=(control_vol->left+control_vol->right)*4.0/200.0; |
180 err = AudioUnitSetParameter(ao->theOutputUnit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, vol, 0); | |
181 if(err==0) { | |
182 // printf("SET VOL=%f\n", vol); | |
183 return CONTROL_TRUE; | |
184 } | |
185 else { | |
186 ao_msg(MSGT_AO, MSGL_WARN, "could not set HAL output volume: [%4.4s]\n", (char *)&err); | |
187 return CONTROL_FALSE; | |
188 } | |
189 /* Everything is currently unimplemented */ | |
190 default: | |
191 return CONTROL_FALSE; | |
192 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29212
diff
changeset
|
193 |
29209 | 194 } |
195 | |
196 | |
197 static void print_format(int lev, const char* str, const AudioStreamBasicDescription *f){ | |
198 uint32_t flags=(uint32_t) f->mFormatFlags; | |
199 ao_msg(MSGT_AO,lev, "%s %7.1fHz %lubit [%c%c%c%c][%lu][%lu][%lu][%lu][%lu] %s %s %s%s%s%s\n", | |
200 str, f->mSampleRate, f->mBitsPerChannel, | |
201 (int)(f->mFormatID & 0xff000000) >> 24, | |
202 (int)(f->mFormatID & 0x00ff0000) >> 16, | |
203 (int)(f->mFormatID & 0x0000ff00) >> 8, | |
204 (int)(f->mFormatID & 0x000000ff) >> 0, | |
205 f->mFormatFlags, f->mBytesPerPacket, | |
206 f->mFramesPerPacket, f->mBytesPerFrame, | |
207 f->mChannelsPerFrame, | |
208 (flags&kAudioFormatFlagIsFloat) ? "float" : "int", | |
209 (flags&kAudioFormatFlagIsBigEndian) ? "BE" : "LE", | |
210 (flags&kAudioFormatFlagIsSignedInteger) ? "S" : "U", | |
211 (flags&kAudioFormatFlagIsPacked) ? " packed" : "", | |
212 (flags&kAudioFormatFlagIsAlignedHigh) ? " aligned" : "", | |
213 (flags&kAudioFormatFlagIsNonInterleaved) ? " ni" : "" ); | |
214 } | |
215 | |
216 | |
217 static int AudioDeviceSupportsDigital( AudioDeviceID i_dev_id ); | |
218 static int AudioStreamSupportsDigital( AudioStreamID i_stream_id ); | |
29212
eda346733b8c
Add missing 'void' to parameterless function declarations.
diego
parents:
29209
diff
changeset
|
219 static int OpenSPDIF(void); |
29209 | 220 static int AudioStreamChangeFormat( AudioStreamID i_stream_id, AudioStreamBasicDescription change_format ); |
221 static OSStatus RenderCallbackSPDIF( AudioDeviceID inDevice, | |
222 const AudioTimeStamp * inNow, | |
223 const void * inInputData, | |
224 const AudioTimeStamp * inInputTime, | |
225 AudioBufferList * outOutputData, | |
226 const AudioTimeStamp * inOutputTime, | |
227 void * threadGlobals ); | |
228 static OSStatus StreamListener( AudioStreamID inStream, | |
229 UInt32 inChannel, | |
230 AudioDevicePropertyID inPropertyID, | |
231 void * inClientData ); | |
232 static OSStatus DeviceListener( AudioDeviceID inDevice, | |
233 UInt32 inChannel, | |
234 Boolean isInput, | |
235 AudioDevicePropertyID inPropertyID, | |
236 void* inClientData ); | |
237 | |
238 static int init(int rate,int channels,int format,int flags) | |
239 { | |
240 AudioStreamBasicDescription inDesc; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29212
diff
changeset
|
241 ComponentDescription desc; |
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29212
diff
changeset
|
242 Component comp; |
29209 | 243 AURenderCallbackStruct renderCallback; |
244 OSStatus err; | |
245 UInt32 size, maxFrames, i_param_size; | |
246 char *psz_name; | |
247 AudioDeviceID devid_def = 0; | |
248 int b_alive; | |
249 | |
250 ao_msg(MSGT_AO,MSGL_V, "init([%dHz][%dch][%s][%d])\n", rate, channels, af_fmt2str_short(format), flags); | |
251 | |
252 ao = calloc(1, sizeof(ao_coreaudio_t)); | |
253 | |
254 ao->i_selected_dev = 0; | |
255 ao->b_supports_digital = 0; | |
256 ao->b_digital = 0; | |
257 ao->b_muted = 0; | |
258 ao->b_stream_format_changed = 0; | |
259 ao->i_hog_pid = -1; | |
260 ao->i_stream_id = 0; | |
261 ao->i_stream_index = -1; | |
262 ao->b_revert = 0; | |
263 ao->b_changed_mixing = 0; | |
264 | |
265 /* Probe whether device support S/PDIF stream output if input is AC3 stream. */ | |
30234 | 266 if (AF_FORMAT_IS_AC3(format)) |
29209 | 267 { |
268 /* Find the ID of the default Device. */ | |
269 i_param_size = sizeof(AudioDeviceID); | |
270 err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, | |
271 &i_param_size, &devid_def); | |
272 if (err != noErr) | |
273 { | |
274 ao_msg(MSGT_AO, MSGL_WARN, "could not get default audio device: [%4.4s]\n", (char *)&err); | |
275 goto err_out; | |
276 } | |
277 | |
278 /* Retrieve the length of the device name. */ | |
279 i_param_size = 0; | |
280 err = AudioDeviceGetPropertyInfo(devid_def, 0, 0, | |
281 kAudioDevicePropertyDeviceName, | |
282 &i_param_size, NULL); | |
283 if (err != noErr) | |
284 { | |
285 ao_msg(MSGT_AO, MSGL_WARN, "could not get default audio device name length: [%4.4s]\n", (char *)&err); | |
286 goto err_out; | |
287 } | |
288 | |
289 /* Retrieve the name of the device. */ | |
290 psz_name = (char *)malloc(i_param_size); | |
291 err = AudioDeviceGetProperty(devid_def, 0, 0, | |
292 kAudioDevicePropertyDeviceName, | |
293 &i_param_size, psz_name); | |
294 if (err != noErr) | |
295 { | |
296 ao_msg(MSGT_AO, MSGL_WARN, "could not get default audio device name: [%4.4s]\n", (char *)&err); | |
297 free( psz_name); | |
298 goto err_out; | |
299 } | |
300 | |
301 ao_msg(MSGT_AO,MSGL_V, "got default audio output device ID: %#lx Name: %s\n", devid_def, psz_name ); | |
302 | |
303 if (AudioDeviceSupportsDigital(devid_def)) | |
304 { | |
305 ao->b_supports_digital = 1; | |
306 ao->i_selected_dev = devid_def; | |
307 } | |
308 ao_msg(MSGT_AO,MSGL_V, "probe default audio output device whether support digital s/pdif output:%d\n", ao->b_supports_digital ); | |
309 | |
310 free( psz_name); | |
311 } | |
312 | |
313 // Build Description for the input format | |
314 inDesc.mSampleRate=rate; | |
315 inDesc.mFormatID=ao->b_supports_digital ? kAudioFormat60958AC3 : kAudioFormatLinearPCM; | |
316 inDesc.mChannelsPerFrame=channels; | |
30235 | 317 inDesc.mBitsPerChannel=af_fmt2bits(format); |
29209 | 318 |
319 if((format&AF_FORMAT_POINT_MASK)==AF_FORMAT_F) { | |
320 // float | |
321 inDesc.mFormatFlags = kAudioFormatFlagIsFloat|kAudioFormatFlagIsPacked; | |
322 } | |
323 else if((format&AF_FORMAT_SIGN_MASK)==AF_FORMAT_SI) { | |
324 // signed int | |
325 inDesc.mFormatFlags = kAudioFormatFlagIsSignedInteger|kAudioFormatFlagIsPacked; | |
326 } | |
327 else { | |
328 // unsigned int | |
329 inDesc.mFormatFlags = kAudioFormatFlagIsPacked; | |
330 } | |
30234 | 331 if (AF_FORMAT_IS_AC3(format)) { |
29209 | 332 // Currently ac3 input (comes from hwac3) is always in native byte-order. |
29401
f01023c524c3
Replace WORDS_BIGENDIAN by HAVE_BIGENDIAN in all internal code.
diego
parents:
29263
diff
changeset
|
333 #if HAVE_BIGENDIAN |
29209 | 334 inDesc.mFormatFlags |= kAudioFormatFlagIsBigEndian; |
335 #endif | |
336 } | |
337 else if ((format & AF_FORMAT_END_MASK) == AF_FORMAT_BE) | |
338 inDesc.mFormatFlags |= kAudioFormatFlagIsBigEndian; | |
339 | |
340 inDesc.mFramesPerPacket = 1; | |
341 ao->packetSize = inDesc.mBytesPerPacket = inDesc.mBytesPerFrame = inDesc.mFramesPerPacket*channels*(inDesc.mBitsPerChannel/8); | |
342 print_format(MSGL_V, "source:",&inDesc); | |
343 | |
344 if (ao->b_supports_digital) | |
345 { | |
346 b_alive = 1; | |
347 i_param_size = sizeof(b_alive); | |
348 err = AudioDeviceGetProperty(ao->i_selected_dev, 0, FALSE, | |
349 kAudioDevicePropertyDeviceIsAlive, | |
350 &i_param_size, &b_alive); | |
351 if (err != noErr) | |
352 ao_msg(MSGT_AO, MSGL_WARN, "could not check whether device is alive: [%4.4s]\n", (char *)&err); | |
353 if (!b_alive) | |
354 ao_msg(MSGT_AO, MSGL_WARN, "device is not alive\n" ); | |
355 /* S/PDIF output need device in HogMode. */ | |
356 i_param_size = sizeof(ao->i_hog_pid); | |
357 err = AudioDeviceGetProperty(ao->i_selected_dev, 0, FALSE, | |
358 kAudioDevicePropertyHogMode, | |
359 &i_param_size, &ao->i_hog_pid); | |
360 | |
361 if (err != noErr) | |
362 { | |
363 /* This is not a fatal error. Some drivers simply don't support this property. */ | |
364 ao_msg(MSGT_AO, MSGL_WARN, "could not check whether device is hogged: [%4.4s]\n", | |
365 (char *)&err); | |
366 ao->i_hog_pid = -1; | |
367 } | |
368 | |
369 if (ao->i_hog_pid != -1 && ao->i_hog_pid != getpid()) | |
370 { | |
371 ao_msg(MSGT_AO, MSGL_WARN, "Selected audio device is exclusively in use by another program.\n" ); | |
372 goto err_out; | |
373 } | |
374 ao->stream_format = inDesc; | |
375 return OpenSPDIF(); | |
376 } | |
377 | |
378 /* original analog output code */ | |
379 desc.componentType = kAudioUnitType_Output; | |
380 desc.componentSubType = kAudioUnitSubType_DefaultOutput; | |
381 desc.componentManufacturer = kAudioUnitManufacturer_Apple; | |
382 desc.componentFlags = 0; | |
383 desc.componentFlagsMask = 0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29212
diff
changeset
|
384 |
29209 | 385 comp = FindNextComponent(NULL, &desc); //Finds an component that meets the desc spec's |
386 if (comp == NULL) { | |
387 ao_msg(MSGT_AO, MSGL_WARN, "Unable to find Output Unit component\n"); | |
388 goto err_out; | |
389 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29212
diff
changeset
|
390 |
29209 | 391 err = OpenAComponent(comp, &(ao->theOutputUnit)); //gains access to the services provided by the component |
392 if (err) { | |
393 ao_msg(MSGT_AO, MSGL_WARN, "Unable to open Output Unit component: [%4.4s]\n", (char *)&err); | |
394 goto err_out; | |
395 } | |
396 | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29212
diff
changeset
|
397 // Initialize AudioUnit |
29209 | 398 err = AudioUnitInitialize(ao->theOutputUnit); |
399 if (err) { | |
400 ao_msg(MSGT_AO, MSGL_WARN, "Unable to initialize Output Unit component: [%4.4s]\n", (char *)&err); | |
401 goto err_out1; | |
402 } | |
403 | |
404 size = sizeof(AudioStreamBasicDescription); | |
405 err = AudioUnitSetProperty(ao->theOutputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &inDesc, size); | |
406 | |
407 if (err) { | |
408 ao_msg(MSGT_AO, MSGL_WARN, "Unable to set the input format: [%4.4s]\n", (char *)&err); | |
409 goto err_out2; | |
410 } | |
411 | |
412 size = sizeof(UInt32); | |
413 err = AudioUnitGetProperty(ao->theOutputUnit, kAudioDevicePropertyBufferSize, kAudioUnitScope_Input, 0, &maxFrames, &size); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29212
diff
changeset
|
414 |
29209 | 415 if (err) |
416 { | |
417 ao_msg(MSGT_AO,MSGL_WARN, "AudioUnitGetProperty returned [%4.4s] when getting kAudioDevicePropertyBufferSize\n", (char *)&err); | |
418 goto err_out2; | |
419 } | |
420 | |
421 ao->chunk_size = maxFrames;//*inDesc.mBytesPerFrame; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29212
diff
changeset
|
422 |
29209 | 423 ao_data.samplerate = inDesc.mSampleRate; |
424 ao_data.channels = inDesc.mChannelsPerFrame; | |
425 ao_data.bps = ao_data.samplerate * inDesc.mBytesPerFrame; | |
426 ao_data.outburst = ao->chunk_size; | |
427 ao_data.buffersize = ao_data.bps; | |
428 | |
429 ao->num_chunks = (ao_data.bps+ao->chunk_size-1)/ao->chunk_size; | |
430 ao->buffer_len = ao->num_chunks * ao->chunk_size; | |
431 ao->buffer = av_fifo_alloc(ao->buffer_len); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29212
diff
changeset
|
432 |
29209 | 433 ao_msg(MSGT_AO,MSGL_V, "using %5d chunks of %d bytes (buffer len %d bytes)\n", (int)ao->num_chunks, (int)ao->chunk_size, (int)ao->buffer_len); |
434 | |
435 renderCallback.inputProc = theRenderProc; | |
436 renderCallback.inputProcRefCon = 0; | |
437 err = AudioUnitSetProperty(ao->theOutputUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &renderCallback, sizeof(AURenderCallbackStruct)); | |
438 if (err) { | |
439 ao_msg(MSGT_AO, MSGL_WARN, "Unable to set the render callback: [%4.4s]\n", (char *)&err); | |
440 goto err_out2; | |
441 } | |
442 | |
443 reset(); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29212
diff
changeset
|
444 |
29209 | 445 return CONTROL_OK; |
446 | |
447 err_out2: | |
448 AudioUnitUninitialize(ao->theOutputUnit); | |
449 err_out1: | |
450 CloseComponent(ao->theOutputUnit); | |
451 err_out: | |
452 av_fifo_free(ao->buffer); | |
453 free(ao); | |
454 ao = NULL; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29212
diff
changeset
|
455 return CONTROL_FALSE; |
29209 | 456 } |
457 | |
458 /***************************************************************************** | |
459 * Setup a encoded digital stream (SPDIF) | |
460 *****************************************************************************/ | |
29212
eda346733b8c
Add missing 'void' to parameterless function declarations.
diego
parents:
29209
diff
changeset
|
461 static int OpenSPDIF(void) |
29209 | 462 { |
463 OSStatus err = noErr; | |
464 UInt32 i_param_size, b_mix = 0; | |
465 Boolean b_writeable = 0; | |
466 AudioStreamID *p_streams = NULL; | |
467 int i, i_streams = 0; | |
468 | |
469 /* Start doing the SPDIF setup process. */ | |
470 ao->b_digital = 1; | |
471 | |
472 /* Hog the device. */ | |
473 i_param_size = sizeof(ao->i_hog_pid); | |
474 ao->i_hog_pid = getpid() ; | |
475 | |
476 err = AudioDeviceSetProperty(ao->i_selected_dev, 0, 0, FALSE, | |
477 kAudioDevicePropertyHogMode, i_param_size, &ao->i_hog_pid); | |
478 | |
479 if (err != noErr) | |
480 { | |
481 ao_msg(MSGT_AO, MSGL_WARN, "failed to set hogmode: [%4.4s]\n", (char *)&err); | |
482 ao->i_hog_pid = -1; | |
483 goto err_out; | |
484 } | |
485 | |
486 /* Set mixable to false if we are allowed to. */ | |
487 err = AudioDeviceGetPropertyInfo(ao->i_selected_dev, 0, FALSE, | |
488 kAudioDevicePropertySupportsMixing, | |
489 &i_param_size, &b_writeable); | |
490 err = AudioDeviceGetProperty(ao->i_selected_dev, 0, FALSE, | |
491 kAudioDevicePropertySupportsMixing, | |
492 &i_param_size, &b_mix); | |
493 if (err != noErr && b_writeable) | |
494 { | |
495 b_mix = 0; | |
496 err = AudioDeviceSetProperty(ao->i_selected_dev, 0, 0, FALSE, | |
497 kAudioDevicePropertySupportsMixing, | |
498 i_param_size, &b_mix); | |
499 ao->b_changed_mixing = 1; | |
500 } | |
501 if (err != noErr) | |
502 { | |
503 ao_msg(MSGT_AO, MSGL_WARN, "failed to set mixmode: [%4.4s]\n", (char *)&err); | |
504 goto err_out; | |
505 } | |
506 | |
507 /* Get a list of all the streams on this device. */ | |
508 err = AudioDeviceGetPropertyInfo(ao->i_selected_dev, 0, FALSE, | |
509 kAudioDevicePropertyStreams, | |
510 &i_param_size, NULL); | |
511 if (err != noErr) | |
512 { | |
513 ao_msg(MSGT_AO, MSGL_WARN, "could not get number of streams: [%4.4s]\n", (char *)&err); | |
514 goto err_out; | |
515 } | |
516 | |
517 i_streams = i_param_size / sizeof(AudioStreamID); | |
518 p_streams = (AudioStreamID *)malloc(i_param_size); | |
519 if (p_streams == NULL) | |
520 { | |
521 ao_msg(MSGT_AO, MSGL_WARN, "out of memory\n" ); | |
522 goto err_out; | |
523 } | |
524 | |
525 err = AudioDeviceGetProperty(ao->i_selected_dev, 0, FALSE, | |
526 kAudioDevicePropertyStreams, | |
527 &i_param_size, p_streams); | |
528 if (err != noErr) | |
529 { | |
530 ao_msg(MSGT_AO, MSGL_WARN, "could not get number of streams: [%4.4s]\n", (char *)&err); | |
531 if (p_streams) free(p_streams); | |
532 goto err_out; | |
533 } | |
534 | |
535 ao_msg(MSGT_AO, MSGL_V, "current device stream number: %d\n", i_streams); | |
536 | |
537 for (i = 0; i < i_streams && ao->i_stream_index < 0; ++i) | |
538 { | |
539 /* Find a stream with a cac3 stream. */ | |
540 AudioStreamBasicDescription *p_format_list = NULL; | |
541 int i_formats = 0, j = 0, b_digital = 0; | |
542 | |
543 /* Retrieve all the stream formats supported by each output stream. */ | |
544 err = AudioStreamGetPropertyInfo(p_streams[i], 0, | |
545 kAudioStreamPropertyPhysicalFormats, | |
546 &i_param_size, NULL); | |
547 if (err != noErr) | |
548 { | |
549 ao_msg(MSGT_AO, MSGL_WARN, "could not get number of streamformats: [%4.4s]\n", (char *)&err); | |
550 continue; | |
551 } | |
552 | |
553 i_formats = i_param_size / sizeof(AudioStreamBasicDescription); | |
554 p_format_list = (AudioStreamBasicDescription *)malloc(i_param_size); | |
555 if (p_format_list == NULL) | |
556 { | |
557 ao_msg(MSGT_AO, MSGL_WARN, "could not malloc the memory\n" ); | |
558 continue; | |
559 } | |
560 | |
561 err = AudioStreamGetProperty(p_streams[i], 0, | |
562 kAudioStreamPropertyPhysicalFormats, | |
563 &i_param_size, p_format_list); | |
564 if (err != noErr) | |
565 { | |
566 ao_msg(MSGT_AO, MSGL_WARN, "could not get the list of streamformats: [%4.4s]\n", (char *)&err); | |
567 if (p_format_list) free(p_format_list); | |
568 continue; | |
569 } | |
570 | |
571 /* Check if one of the supported formats is a digital format. */ | |
572 for (j = 0; j < i_formats; ++j) | |
573 { | |
574 if (p_format_list[j].mFormatID == 'IAC3' || | |
575 p_format_list[j].mFormatID == kAudioFormat60958AC3) | |
576 { | |
577 b_digital = 1; | |
578 break; | |
579 } | |
580 } | |
581 | |
582 if (b_digital) | |
583 { | |
584 /* If this stream supports a digital (cac3) format, then set it. */ | |
585 int i_requested_rate_format = -1; | |
586 int i_current_rate_format = -1; | |
587 int i_backup_rate_format = -1; | |
588 | |
589 ao->i_stream_id = p_streams[i]; | |
590 ao->i_stream_index = i; | |
591 | |
592 if (ao->b_revert == 0) | |
593 { | |
594 /* Retrieve the original format of this stream first if not done so already. */ | |
595 i_param_size = sizeof(ao->sfmt_revert); | |
596 err = AudioStreamGetProperty(ao->i_stream_id, 0, | |
597 kAudioStreamPropertyPhysicalFormat, | |
598 &i_param_size, | |
599 &ao->sfmt_revert); | |
600 if (err != noErr) | |
601 { | |
602 ao_msg(MSGT_AO, MSGL_WARN, "could not retrieve the original streamformat: [%4.4s]\n", (char *)&err); | |
603 if (p_format_list) free(p_format_list); | |
604 continue; | |
605 } | |
606 ao->b_revert = 1; | |
607 } | |
608 | |
609 for (j = 0; j < i_formats; ++j) | |
610 if (p_format_list[j].mFormatID == 'IAC3' || | |
611 p_format_list[j].mFormatID == kAudioFormat60958AC3) | |
612 { | |
613 if (p_format_list[j].mSampleRate == ao->stream_format.mSampleRate) | |
614 { | |
615 i_requested_rate_format = j; | |
616 break; | |
617 } | |
618 if (p_format_list[j].mSampleRate == ao->sfmt_revert.mSampleRate) | |
619 i_current_rate_format = j; | |
620 else if (i_backup_rate_format < 0 || p_format_list[j].mSampleRate > p_format_list[i_backup_rate_format].mSampleRate) | |
621 i_backup_rate_format = j; | |
622 } | |
623 | |
624 if (i_requested_rate_format >= 0) /* We prefer to output at the samplerate of the original audio. */ | |
625 ao->stream_format = p_format_list[i_requested_rate_format]; | |
626 else if (i_current_rate_format >= 0) /* If not possible, we will try to use the current samplerate of the device. */ | |
627 ao->stream_format = p_format_list[i_current_rate_format]; | |
628 else ao->stream_format = p_format_list[i_backup_rate_format]; /* And if we have to, any digital format will be just fine (highest rate possible). */ | |
629 } | |
630 if (p_format_list) free(p_format_list); | |
631 } | |
632 if (p_streams) free(p_streams); | |
633 | |
634 if (ao->i_stream_index < 0) | |
635 { | |
636 ao_msg(MSGT_AO, MSGL_WARN, "can not find any digital output stream format when OpenSPDIF().\n"); | |
637 goto err_out; | |
638 } | |
639 | |
640 print_format(MSGL_V, "original stream format:", &ao->sfmt_revert); | |
641 | |
642 if (!AudioStreamChangeFormat(ao->i_stream_id, ao->stream_format)) | |
643 goto err_out; | |
644 | |
645 err = AudioDeviceAddPropertyListener(ao->i_selected_dev, | |
646 kAudioPropertyWildcardChannel, | |
647 0, | |
648 kAudioDevicePropertyDeviceHasChanged, | |
649 DeviceListener, | |
650 NULL); | |
651 if (err != noErr) | |
652 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceAddPropertyListener for kAudioDevicePropertyDeviceHasChanged failed: [%4.4s]\n", (char *)&err); | |
653 | |
654 | |
655 /* FIXME: If output stream is not native byte-order, we need change endian somewhere. */ | |
656 /* Although there's no such case reported. */ | |
29401
f01023c524c3
Replace WORDS_BIGENDIAN by HAVE_BIGENDIAN in all internal code.
diego
parents:
29263
diff
changeset
|
657 #if HAVE_BIGENDIAN |
29209 | 658 if (!(ao->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian)) |
659 #else | |
660 if (ao->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian) | |
661 #endif | |
662 ao_msg(MSGT_AO, MSGL_WARN, "output stream has a no-native byte-order, digital output may failed.\n"); | |
663 | |
664 /* For ac3/dts, just use packet size 6144 bytes as chunk size. */ | |
665 ao->chunk_size = ao->stream_format.mBytesPerPacket; | |
666 | |
667 ao_data.samplerate = ao->stream_format.mSampleRate; | |
668 ao_data.channels = ao->stream_format.mChannelsPerFrame; | |
669 ao_data.bps = ao_data.samplerate * (ao->stream_format.mBytesPerPacket/ao->stream_format.mFramesPerPacket); | |
670 ao_data.outburst = ao->chunk_size; | |
671 ao_data.buffersize = ao_data.bps; | |
672 | |
673 ao->num_chunks = (ao_data.bps+ao->chunk_size-1)/ao->chunk_size; | |
674 ao->buffer_len = ao->num_chunks * ao->chunk_size; | |
675 ao->buffer = av_fifo_alloc(ao->buffer_len); | |
676 | |
677 ao_msg(MSGT_AO,MSGL_V, "using %5d chunks of %d bytes (buffer len %d bytes)\n", (int)ao->num_chunks, (int)ao->chunk_size, (int)ao->buffer_len); | |
678 | |
679 | |
680 /* Add IOProc callback. */ | |
681 err = AudioDeviceAddIOProc(ao->i_selected_dev, | |
682 (AudioDeviceIOProc)RenderCallbackSPDIF, | |
683 (void *)ao); | |
684 if (err != noErr) | |
685 { | |
686 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceAddIOProc failed: [%4.4s]\n", (char *)&err); | |
687 goto err_out1; | |
688 } | |
689 | |
690 reset(); | |
691 | |
692 return CONTROL_TRUE; | |
693 | |
694 err_out1: | |
695 if (ao->b_revert) | |
696 AudioStreamChangeFormat(ao->i_stream_id, ao->sfmt_revert); | |
697 err_out: | |
698 if (ao->b_changed_mixing && ao->sfmt_revert.mFormatID != kAudioFormat60958AC3) | |
699 { | |
700 int b_mix = 1; | |
701 err = AudioDeviceSetProperty(ao->i_selected_dev, 0, 0, FALSE, | |
702 kAudioDevicePropertySupportsMixing, | |
703 i_param_size, &b_mix); | |
704 if (err != noErr) | |
705 ao_msg(MSGT_AO, MSGL_WARN, "failed to set mixmode: [%4.4s]\n", | |
706 (char *)&err); | |
707 } | |
708 if (ao->i_hog_pid == getpid()) | |
709 { | |
710 ao->i_hog_pid = -1; | |
711 i_param_size = sizeof(ao->i_hog_pid); | |
712 err = AudioDeviceSetProperty(ao->i_selected_dev, 0, 0, FALSE, | |
713 kAudioDevicePropertyHogMode, | |
714 i_param_size, &ao->i_hog_pid); | |
715 if (err != noErr) | |
716 ao_msg(MSGT_AO, MSGL_WARN, "Could not release hogmode: [%4.4s]\n", | |
717 (char *)&err); | |
718 } | |
719 av_fifo_free(ao->buffer); | |
720 free(ao); | |
721 ao = NULL; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29212
diff
changeset
|
722 return CONTROL_FALSE; |
29209 | 723 } |
724 | |
725 /***************************************************************************** | |
726 * AudioDeviceSupportsDigital: Check i_dev_id for digital stream support. | |
727 *****************************************************************************/ | |
728 static int AudioDeviceSupportsDigital( AudioDeviceID i_dev_id ) | |
729 { | |
730 OSStatus err = noErr; | |
731 UInt32 i_param_size = 0; | |
732 AudioStreamID *p_streams = NULL; | |
733 int i = 0, i_streams = 0; | |
734 int b_return = CONTROL_FALSE; | |
735 | |
736 /* Retrieve all the output streams. */ | |
737 err = AudioDeviceGetPropertyInfo(i_dev_id, 0, FALSE, | |
738 kAudioDevicePropertyStreams, | |
739 &i_param_size, NULL); | |
740 if (err != noErr) | |
741 { | |
742 ao_msg(MSGT_AO,MSGL_V, "could not get number of streams: [%4.4s]\n", (char *)&err); | |
743 return CONTROL_FALSE; | |
744 } | |
745 | |
746 i_streams = i_param_size / sizeof(AudioStreamID); | |
747 p_streams = (AudioStreamID *)malloc(i_param_size); | |
748 if (p_streams == NULL) | |
749 { | |
750 ao_msg(MSGT_AO,MSGL_V, "out of memory\n"); | |
751 return CONTROL_FALSE; | |
752 } | |
753 | |
754 err = AudioDeviceGetProperty(i_dev_id, 0, FALSE, | |
755 kAudioDevicePropertyStreams, | |
756 &i_param_size, p_streams); | |
757 | |
758 if (err != noErr) | |
759 { | |
760 ao_msg(MSGT_AO,MSGL_V, "could not get number of streams: [%4.4s]\n", (char *)&err); | |
761 free(p_streams); | |
762 return CONTROL_FALSE; | |
763 } | |
764 | |
765 for (i = 0; i < i_streams; ++i) | |
766 { | |
767 if (AudioStreamSupportsDigital(p_streams[i])) | |
768 b_return = CONTROL_OK; | |
769 } | |
770 | |
771 free(p_streams); | |
772 return b_return; | |
773 } | |
774 | |
775 /***************************************************************************** | |
776 * AudioStreamSupportsDigital: Check i_stream_id for digital stream support. | |
777 *****************************************************************************/ | |
778 static int AudioStreamSupportsDigital( AudioStreamID i_stream_id ) | |
779 { | |
780 OSStatus err = noErr; | |
781 UInt32 i_param_size; | |
782 AudioStreamBasicDescription *p_format_list = NULL; | |
783 int i, i_formats, b_return = CONTROL_FALSE; | |
784 | |
785 /* Retrieve all the stream formats supported by each output stream. */ | |
786 err = AudioStreamGetPropertyInfo(i_stream_id, 0, | |
787 kAudioStreamPropertyPhysicalFormats, | |
788 &i_param_size, NULL); | |
789 if (err != noErr) | |
790 { | |
791 ao_msg(MSGT_AO,MSGL_V, "could not get number of streamformats: [%4.4s]\n", (char *)&err); | |
792 return CONTROL_FALSE; | |
793 } | |
794 | |
795 i_formats = i_param_size / sizeof(AudioStreamBasicDescription); | |
796 p_format_list = (AudioStreamBasicDescription *)malloc(i_param_size); | |
797 if (p_format_list == NULL) | |
798 { | |
799 ao_msg(MSGT_AO,MSGL_V, "could not malloc the memory\n" ); | |
800 return CONTROL_FALSE; | |
801 } | |
802 | |
803 err = AudioStreamGetProperty(i_stream_id, 0, | |
804 kAudioStreamPropertyPhysicalFormats, | |
805 &i_param_size, p_format_list); | |
806 if (err != noErr) | |
807 { | |
808 ao_msg(MSGT_AO,MSGL_V, "could not get the list of streamformats: [%4.4s]\n", (char *)&err); | |
809 free(p_format_list); | |
810 return CONTROL_FALSE; | |
811 } | |
812 | |
813 for (i = 0; i < i_formats; ++i) | |
814 { | |
815 print_format(MSGL_V, "supported format:", &p_format_list[i]); | |
816 | |
817 if (p_format_list[i].mFormatID == 'IAC3' || | |
818 p_format_list[i].mFormatID == kAudioFormat60958AC3) | |
819 b_return = CONTROL_OK; | |
820 } | |
821 | |
822 free(p_format_list); | |
823 return b_return; | |
824 } | |
825 | |
826 /***************************************************************************** | |
827 * AudioStreamChangeFormat: Change i_stream_id to change_format | |
828 *****************************************************************************/ | |
829 static int AudioStreamChangeFormat( AudioStreamID i_stream_id, AudioStreamBasicDescription change_format ) | |
830 { | |
831 OSStatus err = noErr; | |
832 UInt32 i_param_size = 0; | |
833 int i; | |
834 | |
835 static volatile int stream_format_changed; | |
836 stream_format_changed = 0; | |
837 | |
838 print_format(MSGL_V, "setting stream format:", &change_format); | |
839 | |
840 /* Install the callback. */ | |
841 err = AudioStreamAddPropertyListener(i_stream_id, 0, | |
842 kAudioStreamPropertyPhysicalFormat, | |
843 StreamListener, | |
844 (void *)&stream_format_changed); | |
845 if (err != noErr) | |
846 { | |
847 ao_msg(MSGT_AO, MSGL_WARN, "AudioStreamAddPropertyListener failed: [%4.4s]\n", (char *)&err); | |
848 return CONTROL_FALSE; | |
849 } | |
850 | |
851 /* Change the format. */ | |
852 err = AudioStreamSetProperty(i_stream_id, 0, 0, | |
853 kAudioStreamPropertyPhysicalFormat, | |
854 sizeof(AudioStreamBasicDescription), | |
855 &change_format); | |
856 if (err != noErr) | |
857 { | |
858 ao_msg(MSGT_AO, MSGL_WARN, "could not set the stream format: [%4.4s]\n", (char *)&err); | |
859 return CONTROL_FALSE; | |
860 } | |
861 | |
862 /* The AudioStreamSetProperty is not only asynchronious, | |
863 * it is also not Atomic, in its behaviour. | |
864 * Therefore we check 5 times before we really give up. | |
865 * FIXME: failing isn't actually implemented yet. */ | |
866 for (i = 0; i < 5; ++i) | |
867 { | |
868 AudioStreamBasicDescription actual_format; | |
869 int j; | |
870 for (j = 0; !stream_format_changed && j < 50; ++j) | |
871 usec_sleep(10000); | |
872 if (stream_format_changed) | |
873 stream_format_changed = 0; | |
874 else | |
875 ao_msg(MSGT_AO, MSGL_V, "reached timeout\n" ); | |
876 | |
877 i_param_size = sizeof(AudioStreamBasicDescription); | |
878 err = AudioStreamGetProperty(i_stream_id, 0, | |
879 kAudioStreamPropertyPhysicalFormat, | |
880 &i_param_size, | |
881 &actual_format); | |
882 | |
883 print_format(MSGL_V, "actual format in use:", &actual_format); | |
884 if (actual_format.mSampleRate == change_format.mSampleRate && | |
885 actual_format.mFormatID == change_format.mFormatID && | |
886 actual_format.mFramesPerPacket == change_format.mFramesPerPacket) | |
887 { | |
888 /* The right format is now active. */ | |
889 break; | |
890 } | |
891 /* We need to check again. */ | |
892 } | |
893 | |
894 /* Removing the property listener. */ | |
895 err = AudioStreamRemovePropertyListener(i_stream_id, 0, | |
896 kAudioStreamPropertyPhysicalFormat, | |
897 StreamListener); | |
898 if (err != noErr) | |
899 { | |
900 ao_msg(MSGT_AO, MSGL_WARN, "AudioStreamRemovePropertyListener failed: [%4.4s]\n", (char *)&err); | |
901 return CONTROL_FALSE; | |
902 } | |
903 | |
904 return CONTROL_TRUE; | |
905 } | |
906 | |
907 /***************************************************************************** | |
908 * RenderCallbackSPDIF: callback for SPDIF audio output | |
909 *****************************************************************************/ | |
910 static OSStatus RenderCallbackSPDIF( AudioDeviceID inDevice, | |
911 const AudioTimeStamp * inNow, | |
912 const void * inInputData, | |
913 const AudioTimeStamp * inInputTime, | |
914 AudioBufferList * outOutputData, | |
915 const AudioTimeStamp * inOutputTime, | |
916 void * threadGlobals ) | |
917 { | |
918 int amt = av_fifo_size(ao->buffer); | |
919 int req = outOutputData->mBuffers[ao->i_stream_index].mDataByteSize; | |
920 | |
921 if (amt > req) | |
922 amt = req; | |
923 if (amt) | |
924 read_buffer(ao->b_muted ? NULL : (unsigned char *)outOutputData->mBuffers[ao->i_stream_index].mData, amt); | |
925 | |
926 return noErr; | |
927 } | |
928 | |
929 | |
930 static int play(void* output_samples,int num_bytes,int flags) | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29212
diff
changeset
|
931 { |
29209 | 932 int wrote, b_digital; |
933 | |
934 // Check whether we need to reset the digital output stream. | |
935 if (ao->b_digital && ao->b_stream_format_changed) | |
936 { | |
937 ao->b_stream_format_changed = 0; | |
938 b_digital = AudioStreamSupportsDigital(ao->i_stream_id); | |
939 if (b_digital) | |
940 { | |
941 /* Current stream support digital format output, let's set it. */ | |
942 ao_msg(MSGT_AO, MSGL_V, "detected current stream support digital, try to restore digital output...\n"); | |
943 | |
944 if (!AudioStreamChangeFormat(ao->i_stream_id, ao->stream_format)) | |
945 { | |
946 ao_msg(MSGT_AO, MSGL_WARN, "restore digital output failed.\n"); | |
947 } | |
948 else | |
949 { | |
950 ao_msg(MSGT_AO, MSGL_WARN, "restore digital output succeed.\n"); | |
951 reset(); | |
952 } | |
953 } | |
954 else | |
955 ao_msg(MSGT_AO, MSGL_V, "detected current stream do not support digital.\n"); | |
956 } | |
957 | |
958 wrote=write_buffer(output_samples, num_bytes); | |
959 audio_resume(); | |
960 return wrote; | |
961 } | |
962 | |
963 /* set variables and buffer to initial state */ | |
964 static void reset(void) | |
965 { | |
966 audio_pause(); | |
967 av_fifo_reset(ao->buffer); | |
968 } | |
969 | |
970 | |
971 /* return available space */ | |
972 static int get_space(void) | |
973 { | |
974 return ao->buffer_len - av_fifo_size(ao->buffer); | |
975 } | |
976 | |
977 | |
978 /* return delay until audio is played */ | |
979 static float get_delay(void) | |
980 { | |
981 // inaccurate, should also contain the data buffered e.g. by the OS | |
982 return (float)av_fifo_size(ao->buffer)/(float)ao_data.bps; | |
983 } | |
984 | |
985 | |
986 /* unload plugin and deregister from coreaudio */ | |
987 static void uninit(int immed) | |
988 { | |
989 OSStatus err = noErr; | |
990 UInt32 i_param_size = 0; | |
991 | |
992 if (!immed) { | |
993 long long timeleft=(1000000LL*av_fifo_size(ao->buffer))/ao_data.bps; | |
994 ao_msg(MSGT_AO,MSGL_DBG2, "%d bytes left @%d bps (%d usec)\n", av_fifo_size(ao->buffer), ao_data.bps, (int)timeleft); | |
995 usec_sleep((int)timeleft); | |
996 } | |
997 | |
998 if (!ao->b_digital) { | |
999 AudioOutputUnitStop(ao->theOutputUnit); | |
1000 AudioUnitUninitialize(ao->theOutputUnit); | |
1001 CloseComponent(ao->theOutputUnit); | |
1002 } | |
1003 else { | |
1004 /* Stop device. */ | |
1005 err = AudioDeviceStop(ao->i_selected_dev, | |
1006 (AudioDeviceIOProc)RenderCallbackSPDIF); | |
1007 if (err != noErr) | |
1008 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceStop failed: [%4.4s]\n", (char *)&err); | |
1009 | |
1010 /* Remove IOProc callback. */ | |
1011 err = AudioDeviceRemoveIOProc(ao->i_selected_dev, | |
1012 (AudioDeviceIOProc)RenderCallbackSPDIF); | |
1013 if (err != noErr) | |
1014 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceRemoveIOProc failed: [%4.4s]\n", (char *)&err); | |
1015 | |
1016 if (ao->b_revert) | |
1017 AudioStreamChangeFormat(ao->i_stream_id, ao->sfmt_revert); | |
1018 | |
1019 if (ao->b_changed_mixing && ao->sfmt_revert.mFormatID != kAudioFormat60958AC3) | |
1020 { | |
1021 int b_mix; | |
1022 Boolean b_writeable; | |
1023 /* Revert mixable to true if we are allowed to. */ | |
1024 err = AudioDeviceGetPropertyInfo(ao->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing, | |
1025 &i_param_size, &b_writeable); | |
1026 err = AudioDeviceGetProperty(ao->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing, | |
1027 &i_param_size, &b_mix); | |
1028 if (err != noErr && b_writeable) | |
1029 { | |
1030 b_mix = 1; | |
1031 err = AudioDeviceSetProperty(ao->i_selected_dev, 0, 0, FALSE, | |
1032 kAudioDevicePropertySupportsMixing, i_param_size, &b_mix); | |
1033 } | |
1034 if (err != noErr) | |
1035 ao_msg(MSGT_AO, MSGL_WARN, "failed to set mixmode: [%4.4s]\n", (char *)&err); | |
1036 } | |
1037 if (ao->i_hog_pid == getpid()) | |
1038 { | |
1039 ao->i_hog_pid = -1; | |
1040 i_param_size = sizeof(ao->i_hog_pid); | |
1041 err = AudioDeviceSetProperty(ao->i_selected_dev, 0, 0, FALSE, | |
1042 kAudioDevicePropertyHogMode, i_param_size, &ao->i_hog_pid); | |
1043 if (err != noErr) ao_msg(MSGT_AO, MSGL_WARN, "Could not release hogmode: [%4.4s]\n", (char *)&err); | |
1044 } | |
1045 } | |
1046 | |
1047 av_fifo_free(ao->buffer); | |
1048 free(ao); | |
1049 ao = NULL; | |
1050 } | |
1051 | |
1052 | |
1053 /* stop playing, keep buffers (for pause) */ | |
1054 static void audio_pause(void) | |
1055 { | |
1056 OSErr err=noErr; | |
1057 | |
1058 /* Stop callback. */ | |
1059 if (!ao->b_digital) | |
1060 { | |
1061 err=AudioOutputUnitStop(ao->theOutputUnit); | |
1062 if (err != noErr) | |
1063 ao_msg(MSGT_AO,MSGL_WARN, "AudioOutputUnitStop returned [%4.4s]\n", (char *)&err); | |
1064 } | |
1065 else | |
1066 { | |
1067 err = AudioDeviceStop(ao->i_selected_dev, (AudioDeviceIOProc)RenderCallbackSPDIF); | |
1068 if (err != noErr) | |
1069 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceStop failed: [%4.4s]\n", (char *)&err); | |
1070 } | |
1071 ao->paused = 1; | |
1072 } | |
1073 | |
1074 | |
1075 /* resume playing, after audio_pause() */ | |
1076 static void audio_resume(void) | |
1077 { | |
1078 OSErr err=noErr; | |
1079 | |
1080 if (!ao->paused) | |
1081 return; | |
1082 | |
1083 /* Start callback. */ | |
1084 if (!ao->b_digital) | |
1085 { | |
1086 err = AudioOutputUnitStart(ao->theOutputUnit); | |
1087 if (err != noErr) | |
1088 ao_msg(MSGT_AO,MSGL_WARN, "AudioOutputUnitStart returned [%4.4s]\n", (char *)&err); | |
1089 } | |
1090 else | |
1091 { | |
1092 err = AudioDeviceStart(ao->i_selected_dev, (AudioDeviceIOProc)RenderCallbackSPDIF); | |
1093 if (err != noErr) | |
1094 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceStart failed: [%4.4s]\n", (char *)&err); | |
1095 } | |
1096 ao->paused = 0; | |
1097 } | |
1098 | |
1099 /***************************************************************************** | |
1100 * StreamListener | |
1101 *****************************************************************************/ | |
1102 static OSStatus StreamListener( AudioStreamID inStream, | |
1103 UInt32 inChannel, | |
1104 AudioDevicePropertyID inPropertyID, | |
1105 void * inClientData ) | |
1106 { | |
1107 switch (inPropertyID) | |
1108 { | |
1109 case kAudioStreamPropertyPhysicalFormat: | |
1110 ao_msg(MSGT_AO, MSGL_V, "got notify kAudioStreamPropertyPhysicalFormat changed.\n"); | |
1111 if (inClientData) | |
1112 *(volatile int *)inClientData = 1; | |
1113 default: | |
1114 break; | |
1115 } | |
1116 return noErr; | |
1117 } | |
1118 | |
1119 static OSStatus DeviceListener( AudioDeviceID inDevice, | |
1120 UInt32 inChannel, | |
1121 Boolean isInput, | |
1122 AudioDevicePropertyID inPropertyID, | |
1123 void* inClientData ) | |
1124 { | |
1125 switch (inPropertyID) | |
1126 { | |
1127 case kAudioDevicePropertyDeviceHasChanged: | |
1128 ao_msg(MSGT_AO, MSGL_WARN, "got notify kAudioDevicePropertyDeviceHasChanged.\n"); | |
1129 ao->b_stream_format_changed = 1; | |
1130 default: | |
1131 break; | |
1132 } | |
1133 return noErr; | |
1134 } |