Mercurial > mplayer.hg
comparison libao2/ao_macosx.c @ 24734:09f9d0de17f1
Add support for AC-3/DTS passthrough.
patch by Ulion, ulion2002 gmail com
author | nplourde |
---|---|
date | Thu, 11 Oct 2007 02:00:05 +0000 |
parents | 300e9b7c499f |
children | 5ce7bc88eb0c |
comparison
equal
deleted
inserted
replaced
24733:8eb925e5e3b7 | 24734:09f9d0de17f1 |
---|---|
43 #include <stdio.h> | 43 #include <stdio.h> |
44 #include <string.h> | 44 #include <string.h> |
45 #include <stdlib.h> | 45 #include <stdlib.h> |
46 #include <inttypes.h> | 46 #include <inttypes.h> |
47 #include <pthread.h> | 47 #include <pthread.h> |
48 #include <sys/types.h> | |
49 #include <unistd.h> | |
48 | 50 |
49 #include "config.h" | 51 #include "config.h" |
50 #include "mp_msg.h" | 52 #include "mp_msg.h" |
51 | 53 |
52 #include "audio_out.h" | 54 #include "audio_out.h" |
53 #include "audio_out_internal.h" | 55 #include "audio_out_internal.h" |
54 #include "libaf/af_format.h" | 56 #include "libaf/af_format.h" |
57 #include "osdep/timer.h" | |
55 | 58 |
56 static ao_info_t info = | 59 static ao_info_t info = |
57 { | 60 { |
58 "Darwin/Mac OS X native audio output", | 61 "Darwin/Mac OS X native audio output", |
59 "macosx", | 62 "macosx", |
70 * CoreAudio supposedly has an internal latency in the order of 2ms */ | 73 * CoreAudio supposedly has an internal latency in the order of 2ms */ |
71 #define NUM_BUFS 32 | 74 #define NUM_BUFS 32 |
72 | 75 |
73 typedef struct ao_macosx_s | 76 typedef struct ao_macosx_s |
74 { | 77 { |
78 AudioDeviceID i_selected_dev; /* Keeps DeviceID of the selected device. */ | |
79 int b_supports_digital; /* Does the currently selected device support digital mode? */ | |
80 int b_digital; /* Are we running in digital mode? */ | |
81 | |
75 /* AudioUnit */ | 82 /* AudioUnit */ |
76 AudioUnit theOutputUnit; | 83 AudioUnit theOutputUnit; |
84 | |
85 /* CoreAudio SPDIF mode specific */ | |
86 pid_t i_hog_pid; /* Keeps the pid of our hog status. */ | |
87 AudioStreamID i_stream_id; /* The StreamID that has a cac3 streamformat */ | |
88 int i_stream_index; /* The index of i_stream_id in an AudioBufferList */ | |
89 AudioStreamBasicDescription stream_format;/* The format we changed the stream to */ | |
90 AudioStreamBasicDescription sfmt_revert; /* The original format of the stream */ | |
91 int b_revert; /* Whether we need to revert the stream format */ | |
92 int b_changed_mixing; /* Whether we need to set the mixing mode back */ | |
93 int b_stream_format_changed; /* Flag for main thread to reset stream's format to digital and reset buffer */ | |
94 | |
95 /* Original common part */ | |
77 int packetSize; | 96 int packetSize; |
78 int paused; | 97 int paused; |
79 | 98 |
80 /* Ring-buffer */ | 99 /* Ring-buffer */ |
81 /* does not need explicit synchronization, but needs to allocate | 100 /* does not need explicit synchronization, but needs to allocate |
175 OSStatus err; | 194 OSStatus err; |
176 Float32 vol; | 195 Float32 vol; |
177 switch (cmd) { | 196 switch (cmd) { |
178 case AOCONTROL_GET_VOLUME: | 197 case AOCONTROL_GET_VOLUME: |
179 control_vol = (ao_control_vol_t*)arg; | 198 control_vol = (ao_control_vol_t*)arg; |
199 if (ao->b_digital) { | |
200 // Digital output has no volume adjust. | |
201 return CONTROL_FALSE; | |
202 } | |
180 err = AudioUnitGetParameter(ao->theOutputUnit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, &vol); | 203 err = AudioUnitGetParameter(ao->theOutputUnit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, &vol); |
181 | 204 |
182 if(err==0) { | 205 if(err==0) { |
183 // printf("GET VOL=%f\n", vol); | 206 // printf("GET VOL=%f\n", vol); |
184 control_vol->left=control_vol->right=vol*100.0/4.0; | 207 control_vol->left=control_vol->right=vol*100.0/4.0; |
185 return CONTROL_TRUE; | 208 return CONTROL_TRUE; |
186 } | 209 } |
187 else { | 210 else { |
211 ao_msg(MSGT_AO, MSGL_WARN, "could not get HAL output volume: [%4.4s]\n", (char *)&err); | |
188 return CONTROL_FALSE; | 212 return CONTROL_FALSE; |
189 } | 213 } |
190 | 214 |
191 case AOCONTROL_SET_VOLUME: | 215 case AOCONTROL_SET_VOLUME: |
216 if (ao->b_digital) | |
217 // Digital output can not set volume. Here we have to return true | |
218 // to make mixer forget it. Else mixer will add a soft filter, | |
219 // that's not we expected and the filter not support ac3 stream | |
220 // will cause mplayer die. | |
221 return CONTROL_TRUE; | |
192 control_vol = (ao_control_vol_t*)arg; | 222 control_vol = (ao_control_vol_t*)arg; |
193 | 223 |
194 vol=(control_vol->left+control_vol->right)*4.0/200.0; | 224 vol=(control_vol->left+control_vol->right)*4.0/200.0; |
195 err = AudioUnitSetParameter(ao->theOutputUnit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, vol, 0); | 225 err = AudioUnitSetParameter(ao->theOutputUnit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, vol, 0); |
196 if(err==0) { | 226 if(err==0) { |
197 // printf("SET VOL=%f\n", vol); | 227 // printf("SET VOL=%f\n", vol); |
198 return CONTROL_TRUE; | 228 return CONTROL_TRUE; |
199 } | 229 } |
200 else { | 230 else { |
231 ao_msg(MSGT_AO, MSGL_WARN, "could not set HAL output volume: [%4.4s]\n", (char *)&err); | |
201 return CONTROL_FALSE; | 232 return CONTROL_FALSE; |
202 } | 233 } |
203 /* Everything is currently unimplemented */ | 234 /* Everything is currently unimplemented */ |
204 default: | 235 default: |
205 return CONTROL_FALSE; | 236 return CONTROL_FALSE; |
206 } | 237 } |
207 | 238 |
208 } | 239 } |
209 | 240 |
210 | 241 |
211 static void print_format(const char* str,AudioStreamBasicDescription *f){ | 242 static void print_format(int lev, const char* str, const AudioStreamBasicDescription *f){ |
212 uint32_t flags=(uint32_t) f->mFormatFlags; | 243 uint32_t flags=(uint32_t) f->mFormatFlags; |
213 ao_msg(MSGT_AO,MSGL_V, "%s %7.1fHz %dbit [%c%c%c%c] %s %s %s%s%s%s\n", | 244 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", |
214 str, f->mSampleRate, f->mBitsPerChannel, | 245 str, f->mSampleRate, f->mBitsPerChannel, |
215 (int)(f->mFormatID & 0xff000000) >> 24, | 246 (int)(f->mFormatID & 0xff000000) >> 24, |
216 (int)(f->mFormatID & 0x00ff0000) >> 16, | 247 (int)(f->mFormatID & 0x00ff0000) >> 16, |
217 (int)(f->mFormatID & 0x0000ff00) >> 8, | 248 (int)(f->mFormatID & 0x0000ff00) >> 8, |
218 (int)(f->mFormatID & 0x000000ff) >> 0, | 249 (int)(f->mFormatID & 0x000000ff) >> 0, |
250 f->mFormatFlags, f->mBytesPerPacket, | |
251 f->mFramesPerPacket, f->mBytesPerFrame, | |
252 f->mChannelsPerFrame, | |
219 (flags&kAudioFormatFlagIsFloat) ? "float" : "int", | 253 (flags&kAudioFormatFlagIsFloat) ? "float" : "int", |
220 (flags&kAudioFormatFlagIsBigEndian) ? "BE" : "LE", | 254 (flags&kAudioFormatFlagIsBigEndian) ? "BE" : "LE", |
221 (flags&kAudioFormatFlagIsSignedInteger) ? "S" : "U", | 255 (flags&kAudioFormatFlagIsSignedInteger) ? "S" : "U", |
222 (flags&kAudioFormatFlagIsPacked) ? " packed" : "", | 256 (flags&kAudioFormatFlagIsPacked) ? " packed" : "", |
223 (flags&kAudioFormatFlagIsAlignedHigh) ? " aligned" : "", | 257 (flags&kAudioFormatFlagIsAlignedHigh) ? " aligned" : "", |
224 (flags&kAudioFormatFlagIsNonInterleaved) ? " ni" : "" ); | 258 (flags&kAudioFormatFlagIsNonInterleaved) ? " ni" : "" ); |
225 | 259 } |
226 ao_msg(MSGT_AO,MSGL_DBG2, "%5d mBytesPerPacket\n", | 260 |
227 (int)f->mBytesPerPacket); | 261 |
228 ao_msg(MSGT_AO,MSGL_DBG2, "%5d mFramesPerPacket\n", | 262 static int AudioDeviceSupportsDigital( AudioDeviceID i_dev_id ); |
229 (int)f->mFramesPerPacket); | 263 static int AudioStreamSupportsDigital( AudioStreamID i_stream_id ); |
230 ao_msg(MSGT_AO,MSGL_DBG2, "%5d mBytesPerFrame\n", | 264 static int OpenSPDIF(); |
231 (int)f->mBytesPerFrame); | 265 static int AudioStreamChangeFormat( AudioStreamID i_stream_id, AudioStreamBasicDescription change_format ); |
232 ao_msg(MSGT_AO,MSGL_DBG2, "%5d mChannelsPerFrame\n", | 266 static OSStatus RenderCallbackSPDIF( AudioDeviceID inDevice, |
233 (int)f->mChannelsPerFrame); | 267 const AudioTimeStamp * inNow, |
234 | 268 const void * inInputData, |
235 } | 269 const AudioTimeStamp * inInputTime, |
236 | 270 AudioBufferList * outOutputData, |
271 const AudioTimeStamp * inOutputTime, | |
272 void * threadGlobals ); | |
273 static OSStatus StreamListener( AudioStreamID inStream, | |
274 UInt32 inChannel, | |
275 AudioDevicePropertyID inPropertyID, | |
276 void * inClientData ); | |
277 static OSStatus DeviceListener( AudioDeviceID inDevice, | |
278 UInt32 inChannel, | |
279 Boolean isInput, | |
280 AudioDevicePropertyID inPropertyID, | |
281 void* inClientData ); | |
237 | 282 |
238 static int init(int rate,int channels,int format,int flags) | 283 static int init(int rate,int channels,int format,int flags) |
239 { | 284 { |
240 AudioStreamBasicDescription inDesc; | 285 AudioStreamBasicDescription inDesc; |
241 ComponentDescription desc; | 286 ComponentDescription desc; |
242 Component comp; | 287 Component comp; |
243 AURenderCallbackStruct renderCallback; | 288 AURenderCallbackStruct renderCallback; |
244 OSStatus err; | 289 OSStatus err; |
245 UInt32 size, maxFrames; | 290 UInt32 size, maxFrames, i_param_size; |
291 char *psz_name; | |
246 int aoIsCreated = ao != NULL; | 292 int aoIsCreated = ao != NULL; |
247 | 293 AudioDeviceID devid_def = 0; |
248 if (!aoIsCreated) ao = malloc(sizeof(ao_macosx_t)); | 294 int b_alive; |
295 | |
296 ao_msg(MSGT_AO,MSGL_V, "init([%dHz][%dch][%s][%d])\n", rate, channels, af_fmt2str_short(format), flags); | |
297 | |
298 if (!aoIsCreated) { ao = malloc(sizeof(ao_macosx_t)); ao->buffer = NULL;} | |
299 | |
300 ao->i_selected_dev = 0; | |
301 ao->b_supports_digital = 0; | |
302 ao->b_digital = 0; | |
303 ao->b_stream_format_changed = 0; | |
304 ao->i_hog_pid = -1; | |
305 ao->i_stream_id = 0; | |
306 ao->i_stream_index = -1; | |
307 ao->b_revert = 0; | |
308 ao->b_changed_mixing = 0; | |
309 | |
310 /* Probe whether device support S/PDIF stream output if input is AC3 stream. */ | |
311 if ((format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_AC3) | |
312 { | |
313 /* Find the ID of the default Device. */ | |
314 i_param_size = sizeof(AudioDeviceID); | |
315 err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, | |
316 &i_param_size, &devid_def); | |
317 if (err != noErr) | |
318 { | |
319 ao_msg(MSGT_AO, MSGL_WARN, "could not get default audio device: [%4.4s]\n", (char *)&err); | |
320 return CONTROL_FALSE; | |
321 } | |
322 | |
323 /* Retrieve the length of the device name. */ | |
324 i_param_size = 0; | |
325 err = AudioDeviceGetPropertyInfo(devid_def, 0, 0, | |
326 kAudioDevicePropertyDeviceName, | |
327 &i_param_size, NULL); | |
328 if (err != noErr) | |
329 { | |
330 ao_msg(MSGT_AO, MSGL_WARN, "could not get default audio device name length: [%4.4s]\n", (char *)&err); | |
331 return CONTROL_FALSE; | |
332 } | |
333 | |
334 /* Retrieve the name of the device. */ | |
335 psz_name = (char *)malloc(i_param_size); | |
336 err = AudioDeviceGetProperty(devid_def, 0, 0, | |
337 kAudioDevicePropertyDeviceName, | |
338 &i_param_size, psz_name); | |
339 if (err != noErr) | |
340 { | |
341 ao_msg(MSGT_AO, MSGL_WARN, "could not get default audio device name: [%4.4s]\n", (char *)&err); | |
342 return CONTROL_FALSE; | |
343 } | |
344 | |
345 ao_msg(MSGT_AO,MSGL_V, "got default audio output device ID: %#lx Name: %s\n", devid_def, psz_name ); | |
346 | |
347 if (AudioDeviceSupportsDigital(devid_def)) | |
348 { | |
349 ao->b_supports_digital = 1; | |
350 ao->i_selected_dev = devid_def; | |
351 } | |
352 ao_msg(MSGT_AO,MSGL_V, "probe default audio output device whether support digital s/pdif output:%d\n", ao->b_supports_digital ); | |
353 | |
354 free( psz_name); | |
355 } | |
249 | 356 |
250 // Build Description for the input format | 357 // Build Description for the input format |
251 inDesc.mSampleRate=rate; | 358 inDesc.mSampleRate=rate; |
252 inDesc.mFormatID=kAudioFormatLinearPCM; | 359 inDesc.mFormatID=ao->b_supports_digital ? kAudioFormat60958AC3 : kAudioFormatLinearPCM; |
253 inDesc.mChannelsPerFrame=channels; | 360 inDesc.mChannelsPerFrame=channels; |
254 switch(format&AF_FORMAT_BITS_MASK){ | 361 switch(format&AF_FORMAT_BITS_MASK){ |
255 case AF_FORMAT_8BIT: | 362 case AF_FORMAT_8BIT: |
256 inDesc.mBitsPerChannel=8; | 363 inDesc.mBitsPerChannel=8; |
257 break; | 364 break; |
280 } | 387 } |
281 else { | 388 else { |
282 // unsigned int | 389 // unsigned int |
283 inDesc.mFormatFlags = kAudioFormatFlagIsPacked; | 390 inDesc.mFormatFlags = kAudioFormatFlagIsPacked; |
284 } | 391 } |
285 | 392 if ((format & AF_FORMAT_SPECIAL_MASK) == AF_FORMAT_AC3) { |
286 if((format&AF_FORMAT_END_MASK)==AF_FORMAT_BE) | 393 // Currently ac3 input (comes from hwac3) is always in native byte-order. |
287 inDesc.mFormatFlags |= kAudioFormatFlagIsBigEndian; | 394 #ifdef WORDS_BIGENDIAN |
395 inDesc.mFormatFlags |= kAudioFormatFlagIsBigEndian; | |
396 #endif | |
397 } | |
398 else if ((format & AF_FORMAT_END_MASK) == AF_FORMAT_BE) | |
399 inDesc.mFormatFlags |= kAudioFormatFlagIsBigEndian; | |
288 | 400 |
289 inDesc.mFramesPerPacket = 1; | 401 inDesc.mFramesPerPacket = 1; |
290 ao->packetSize = inDesc.mBytesPerPacket = inDesc.mBytesPerFrame = inDesc.mFramesPerPacket*channels*(inDesc.mBitsPerChannel/8); | 402 ao->packetSize = inDesc.mBytesPerPacket = inDesc.mBytesPerFrame = inDesc.mFramesPerPacket*channels*(inDesc.mBitsPerChannel/8); |
291 print_format("source: ",&inDesc); | 403 print_format(MSGL_V, "source:",&inDesc); |
292 | 404 |
405 if (ao->b_supports_digital) | |
406 { | |
407 b_alive = 1; | |
408 i_param_size = sizeof(b_alive); | |
409 err = AudioDeviceGetProperty(ao->i_selected_dev, 0, FALSE, | |
410 kAudioDevicePropertyDeviceIsAlive, | |
411 &i_param_size, &b_alive); | |
412 if (err != noErr) | |
413 ao_msg(MSGT_AO, MSGL_WARN, "could not check whether device is alive: [%4.4s]\n", (char *)&err); | |
414 if (!b_alive) | |
415 ao_msg(MSGT_AO, MSGL_WARN, "device is not alive\n" ); | |
416 /* S/PDIF output need device in HogMode. */ | |
417 i_param_size = sizeof(ao->i_hog_pid); | |
418 err = AudioDeviceGetProperty(ao->i_selected_dev, 0, FALSE, | |
419 kAudioDevicePropertyHogMode, | |
420 &i_param_size, &ao->i_hog_pid); | |
421 | |
422 if (err != noErr) | |
423 { | |
424 /* This is not a fatal error. Some drivers simply don't support this property. */ | |
425 ao_msg(MSGT_AO, MSGL_WARN, "could not check whether device is hogged: [%4.4s]\n", | |
426 (char *)&err); | |
427 ao->i_hog_pid = -1; | |
428 } | |
429 | |
430 if (ao->i_hog_pid != -1 && ao->i_hog_pid != getpid()) | |
431 { | |
432 ao_msg(MSGT_AO, MSGL_WARN, "Selected audio device is exclusively in use by another program.\n" ); | |
433 return CONTROL_FALSE; | |
434 } | |
435 ao->stream_format = inDesc; | |
436 return OpenSPDIF(); | |
437 } | |
438 | |
439 /* original analog output code */ | |
293 if (!aoIsCreated) { | 440 if (!aoIsCreated) { |
294 desc.componentType = kAudioUnitType_Output; | 441 desc.componentType = kAudioUnitType_Output; |
295 desc.componentSubType = kAudioUnitSubType_DefaultOutput; | 442 desc.componentSubType = kAudioUnitSubType_DefaultOutput; |
296 desc.componentManufacturer = kAudioUnitManufacturer_Apple; | 443 desc.componentManufacturer = kAudioUnitManufacturer_Apple; |
297 desc.componentFlags = 0; | 444 desc.componentFlags = 0; |
303 return CONTROL_FALSE; | 450 return CONTROL_FALSE; |
304 } | 451 } |
305 | 452 |
306 err = OpenAComponent(comp, &(ao->theOutputUnit)); //gains access to the services provided by the component | 453 err = OpenAComponent(comp, &(ao->theOutputUnit)); //gains access to the services provided by the component |
307 if (err) { | 454 if (err) { |
308 ao_msg(MSGT_AO, MSGL_WARN, "Unable to open Output Unit component (err=%d)\n", err); | 455 ao_msg(MSGT_AO, MSGL_WARN, "Unable to open Output Unit component: [%4.4s]\n", (char *)&err); |
309 return CONTROL_FALSE; | 456 return CONTROL_FALSE; |
310 } | 457 } |
311 | 458 |
312 // Initialize AudioUnit | 459 // Initialize AudioUnit |
313 err = AudioUnitInitialize(ao->theOutputUnit); | 460 err = AudioUnitInitialize(ao->theOutputUnit); |
314 if (err) { | 461 if (err) { |
315 ao_msg(MSGT_AO, MSGL_WARN, "Unable to initialize Output Unit component (err=%d)\n", err); | 462 ao_msg(MSGT_AO, MSGL_WARN, "Unable to initialize Output Unit component: [%4.4s]\n", (char *)&err); |
316 return CONTROL_FALSE; | 463 return CONTROL_FALSE; |
317 } | 464 } |
318 } | 465 } |
319 | 466 |
320 size = sizeof(AudioStreamBasicDescription); | 467 size = sizeof(AudioStreamBasicDescription); |
321 err = AudioUnitSetProperty(ao->theOutputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &inDesc, size); | 468 err = AudioUnitSetProperty(ao->theOutputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &inDesc, size); |
322 | 469 |
323 if (err) { | 470 if (err) { |
324 ao_msg(MSGT_AO, MSGL_WARN, "Unable to set the input format (err=%d)\n", err); | 471 ao_msg(MSGT_AO, MSGL_WARN, "Unable to set the input format: [%4.4s]\n", (char *)&err); |
325 return CONTROL_FALSE; | 472 return CONTROL_FALSE; |
326 } | 473 } |
327 | 474 |
328 size = sizeof(UInt32); | 475 size = sizeof(UInt32); |
329 err = AudioUnitGetProperty(ao->theOutputUnit, kAudioDevicePropertyBufferSize, kAudioUnitScope_Input, 0, &maxFrames, &size); | 476 err = AudioUnitGetProperty(ao->theOutputUnit, kAudioDevicePropertyBufferSize, kAudioUnitScope_Input, 0, &maxFrames, &size); |
330 | 477 |
331 if (err) | 478 if (err) |
332 { | 479 { |
333 ao_msg(MSGT_AO,MSGL_WARN, "AudioUnitGetProperty returned %d when getting kAudioDevicePropertyBufferSize\n", (int)err); | 480 ao_msg(MSGT_AO,MSGL_WARN, "AudioUnitGetProperty returned [%4.4s] when getting kAudioDevicePropertyBufferSize\n", (char *)&err); |
334 return CONTROL_FALSE; | 481 return CONTROL_FALSE; |
335 } | 482 } |
336 | 483 |
337 ao->chunk_size = maxFrames;//*inDesc.mBytesPerFrame; | 484 ao->chunk_size = maxFrames;//*inDesc.mBytesPerFrame; |
338 | 485 |
351 | 498 |
352 renderCallback.inputProc = theRenderProc; | 499 renderCallback.inputProc = theRenderProc; |
353 renderCallback.inputProcRefCon = 0; | 500 renderCallback.inputProcRefCon = 0; |
354 err = AudioUnitSetProperty(ao->theOutputUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &renderCallback, sizeof(AURenderCallbackStruct)); | 501 err = AudioUnitSetProperty(ao->theOutputUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &renderCallback, sizeof(AURenderCallbackStruct)); |
355 if (err) { | 502 if (err) { |
356 ao_msg(MSGT_AO, MSGL_WARN, "Unable to set the render callback (err=%d)\n", err); | 503 ao_msg(MSGT_AO, MSGL_WARN, "Unable to set the render callback: [%4.4s]\n", (char *)&err); |
357 return CONTROL_FALSE; | 504 return CONTROL_FALSE; |
358 } | 505 } |
359 | 506 |
360 reset(); | 507 reset(); |
361 | 508 |
362 return CONTROL_OK; | 509 return CONTROL_OK; |
363 } | 510 } |
364 | 511 |
512 /***************************************************************************** | |
513 * Setup a encoded digital stream (SPDIF) | |
514 *****************************************************************************/ | |
515 static int OpenSPDIF() | |
516 { | |
517 OSStatus err = noErr; | |
518 UInt32 i_param_size, b_mix = 0; | |
519 Boolean b_writeable = 0; | |
520 AudioStreamID *p_streams = NULL; | |
521 int i, i_streams = 0; | |
522 | |
523 /* Start doing the SPDIF setup process. */ | |
524 ao->b_digital = 1; | |
525 | |
526 /* Hog the device. */ | |
527 i_param_size = sizeof(ao->i_hog_pid); | |
528 ao->i_hog_pid = getpid() ; | |
529 | |
530 err = AudioDeviceSetProperty(ao->i_selected_dev, 0, 0, FALSE, | |
531 kAudioDevicePropertyHogMode, i_param_size, &ao->i_hog_pid); | |
532 | |
533 if (err != noErr) | |
534 { | |
535 ao_msg(MSGT_AO, MSGL_WARN, "failed to set hogmode: [%4.4s]\n", (char *)&err); | |
536 return CONTROL_FALSE; | |
537 } | |
538 | |
539 /* Set mixable to false if we are allowed to. */ | |
540 err = AudioDeviceGetPropertyInfo(ao->i_selected_dev, 0, FALSE, | |
541 kAudioDevicePropertySupportsMixing, | |
542 &i_param_size, &b_writeable); | |
543 err = AudioDeviceGetProperty(ao->i_selected_dev, 0, FALSE, | |
544 kAudioDevicePropertySupportsMixing, | |
545 &i_param_size, &b_mix); | |
546 if (err != noErr && b_writeable) | |
547 { | |
548 b_mix = 0; | |
549 err = AudioDeviceSetProperty(ao->i_selected_dev, 0, 0, FALSE, | |
550 kAudioDevicePropertySupportsMixing, | |
551 i_param_size, &b_mix); | |
552 ao->b_changed_mixing = 1; | |
553 } | |
554 if (err != noErr) | |
555 { | |
556 ao_msg(MSGT_AO, MSGL_WARN, "failed to set mixmode: [%4.4s]\n", (char *)&err); | |
557 return CONTROL_FALSE; | |
558 } | |
559 | |
560 /* Get a list of all the streams on this device. */ | |
561 err = AudioDeviceGetPropertyInfo(ao->i_selected_dev, 0, FALSE, | |
562 kAudioDevicePropertyStreams, | |
563 &i_param_size, NULL); | |
564 if (err != noErr) | |
565 { | |
566 ao_msg(MSGT_AO, MSGL_WARN, "could not get number of streams: [%4.4s]\n", (char *)&err); | |
567 return CONTROL_FALSE; | |
568 } | |
569 | |
570 i_streams = i_param_size / sizeof(AudioStreamID); | |
571 p_streams = (AudioStreamID *)malloc(i_param_size); | |
572 if (p_streams == NULL) | |
573 { | |
574 ao_msg(MSGT_AO, MSGL_WARN, "out of memory\n" ); | |
575 return CONTROL_FALSE; | |
576 } | |
577 | |
578 err = AudioDeviceGetProperty(ao->i_selected_dev, 0, FALSE, | |
579 kAudioDevicePropertyStreams, | |
580 &i_param_size, p_streams); | |
581 if (err != noErr) | |
582 { | |
583 ao_msg(MSGT_AO, MSGL_WARN, "could not get number of streams: [%4.4s]\n", (char *)&err); | |
584 if (p_streams) free(p_streams); | |
585 return CONTROL_FALSE; | |
586 } | |
587 | |
588 ao_msg(MSGT_AO, MSGL_V, "current device stream number: %d\n", i_streams); | |
589 | |
590 for (i = 0; i < i_streams && ao->i_stream_index < 0; ++i) | |
591 { | |
592 /* Find a stream with a cac3 stream. */ | |
593 AudioStreamBasicDescription *p_format_list = NULL; | |
594 int i_formats = 0, j = 0, b_digital = 0; | |
595 | |
596 /* Retrieve all the stream formats supported by each output stream. */ | |
597 err = AudioStreamGetPropertyInfo(p_streams[i], 0, | |
598 kAudioStreamPropertyPhysicalFormats, | |
599 &i_param_size, NULL); | |
600 if (err != noErr) | |
601 { | |
602 ao_msg(MSGT_AO, MSGL_WARN, "could not get number of streamformats: [%4.4s]\n", (char *)&err); | |
603 continue; | |
604 } | |
605 | |
606 i_formats = i_param_size / sizeof(AudioStreamBasicDescription); | |
607 p_format_list = (AudioStreamBasicDescription *)malloc(i_param_size); | |
608 if (p_format_list == NULL) | |
609 { | |
610 ao_msg(MSGT_AO, MSGL_WARN, "could not malloc the memory\n" ); | |
611 continue; | |
612 } | |
613 | |
614 err = AudioStreamGetProperty(p_streams[i], 0, | |
615 kAudioStreamPropertyPhysicalFormats, | |
616 &i_param_size, p_format_list); | |
617 if (err != noErr) | |
618 { | |
619 ao_msg(MSGT_AO, MSGL_WARN, "could not get the list of streamformats: [%4.4s]\n", (char *)&err); | |
620 if (p_format_list) free(p_format_list); | |
621 continue; | |
622 } | |
623 | |
624 /* Check if one of the supported formats is a digital format. */ | |
625 for (j = 0; j < i_formats; ++j) | |
626 { | |
627 if (p_format_list[j].mFormatID == 'IAC3' || | |
628 p_format_list[j].mFormatID == kAudioFormat60958AC3) | |
629 { | |
630 b_digital = 1; | |
631 break; | |
632 } | |
633 } | |
634 | |
635 if (b_digital) | |
636 { | |
637 /* If this stream supports a digital (cac3) format, then set it. */ | |
638 int i_requested_rate_format = -1; | |
639 int i_current_rate_format = -1; | |
640 int i_backup_rate_format = -1; | |
641 | |
642 ao->i_stream_id = p_streams[i]; | |
643 ao->i_stream_index = i; | |
644 | |
645 if (ao->b_revert == 0) | |
646 { | |
647 /* Retrieve the original format of this stream first if not done so already. */ | |
648 i_param_size = sizeof(ao->sfmt_revert); | |
649 err = AudioStreamGetProperty(ao->i_stream_id, 0, | |
650 kAudioStreamPropertyPhysicalFormat, | |
651 &i_param_size, | |
652 &ao->sfmt_revert); | |
653 if (err != noErr) | |
654 { | |
655 ao_msg(MSGT_AO, MSGL_WARN, "could not retrieve the original streamformat: [%4.4s]\n", (char *)&err); | |
656 if (p_format_list) free(p_format_list); | |
657 continue; | |
658 } | |
659 ao->b_revert = 1; | |
660 } | |
661 | |
662 for (j = 0; j < i_formats; ++j) | |
663 if (p_format_list[j].mFormatID == 'IAC3' || | |
664 p_format_list[j].mFormatID == kAudioFormat60958AC3) | |
665 { | |
666 if (p_format_list[j].mSampleRate == ao->stream_format.mSampleRate) | |
667 { | |
668 i_requested_rate_format = j; | |
669 break; | |
670 } | |
671 if (p_format_list[j].mSampleRate == ao->sfmt_revert.mSampleRate) | |
672 i_current_rate_format = j; | |
673 else if (i_backup_rate_format < 0 || p_format_list[j].mSampleRate > p_format_list[i_backup_rate_format].mSampleRate) | |
674 i_backup_rate_format = j; | |
675 } | |
676 | |
677 if (i_requested_rate_format >= 0) /* We prefer to output at the samplerate of the original audio. */ | |
678 ao->stream_format = p_format_list[i_requested_rate_format]; | |
679 else if (i_current_rate_format >= 0) /* If not possible, we will try to use the current samplerate of the device. */ | |
680 ao->stream_format = p_format_list[i_current_rate_format]; | |
681 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). */ | |
682 } | |
683 if (p_format_list) free(p_format_list); | |
684 } | |
685 if (p_streams) free(p_streams); | |
686 | |
687 if (ao->i_stream_index < 0) | |
688 { | |
689 ao_msg(MSGT_AO, MSGL_WARN, "can not find any digital output stream format when OpenSPDIF().\n"); | |
690 return CONTROL_FALSE; | |
691 } | |
692 | |
693 print_format(MSGL_V, "original stream format:", &ao->sfmt_revert); | |
694 | |
695 if (!AudioStreamChangeFormat(ao->i_stream_id, ao->stream_format)) | |
696 return CONTROL_FALSE; | |
697 | |
698 err = AudioDeviceAddPropertyListener(ao->i_selected_dev, | |
699 kAudioPropertyWildcardChannel, | |
700 0, | |
701 kAudioDevicePropertyDeviceHasChanged, | |
702 DeviceListener, | |
703 NULL); | |
704 if (err != noErr) | |
705 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceAddPropertyListener for kAudioDevicePropertyDeviceHasChanged failed: [%4.4s]\n", (char *)&err); | |
706 | |
707 | |
708 /* FIXME: If output stream is not native byte-order, we need change endian somewhere. */ | |
709 /* Although there's no such case reported. */ | |
710 #ifdef WORDS_BIGENDIAN | |
711 if (!(ao->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian)) | |
712 #else | |
713 if (ao->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian) | |
714 #endif | |
715 ao_msg(MSGT_AO, MSGL_WARN, "output stream has a no-native byte-order, digital output may failed.\n", (char *)&err); | |
716 | |
717 /* For ac3/dts, just use packet size 6144 bytes as chunk size. */ | |
718 ao->chunk_size = ao->stream_format.mBytesPerPacket; | |
719 | |
720 ao_data.samplerate = ao->stream_format.mSampleRate; | |
721 ao_data.channels = ao->stream_format.mChannelsPerFrame; | |
722 ao_data.bps = ao_data.samplerate * (ao->stream_format.mBytesPerPacket/ao->stream_format.mFramesPerPacket); | |
723 ao_data.outburst = ao->chunk_size; | |
724 ao_data.buffersize = ao_data.bps; | |
725 | |
726 ao->num_chunks = (ao_data.bps+ao->chunk_size-1)/ao->chunk_size; | |
727 ao->buffer_len = (ao->num_chunks + 1) * ao->chunk_size; | |
728 ao->buffer = NULL!=ao->buffer ? realloc(ao->buffer,(ao->num_chunks + 1)*ao->chunk_size) | |
729 : calloc(ao->num_chunks + 1, ao->chunk_size); | |
730 | |
731 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); | |
732 | |
733 | |
734 /* Add IOProc callback. */ | |
735 err = AudioDeviceAddIOProc(ao->i_selected_dev, | |
736 (AudioDeviceIOProc)RenderCallbackSPDIF, | |
737 (void *)ao); | |
738 if (err != noErr) | |
739 { | |
740 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceAddIOProc failed: [%4.4s]\n", (char *)&err); | |
741 return CONTROL_FALSE; | |
742 } | |
743 | |
744 reset(); | |
745 | |
746 return CONTROL_TRUE; | |
747 } | |
748 | |
749 /***************************************************************************** | |
750 * AudioDeviceSupportsDigital: Check i_dev_id for digital stream support. | |
751 *****************************************************************************/ | |
752 static int AudioDeviceSupportsDigital( AudioDeviceID i_dev_id ) | |
753 { | |
754 OSStatus err = noErr; | |
755 UInt32 i_param_size = 0; | |
756 AudioStreamID *p_streams = NULL; | |
757 int i = 0, i_streams = 0; | |
758 int b_return = CONTROL_FALSE; | |
759 | |
760 /* Retrieve all the output streams. */ | |
761 err = AudioDeviceGetPropertyInfo(i_dev_id, 0, FALSE, | |
762 kAudioDevicePropertyStreams, | |
763 &i_param_size, NULL); | |
764 if (err != noErr) | |
765 { | |
766 ao_msg(MSGT_AO,MSGL_V, "could not get number of streams: [%4.4s]\n", (char *)&err); | |
767 return CONTROL_FALSE; | |
768 } | |
769 | |
770 i_streams = i_param_size / sizeof(AudioStreamID); | |
771 p_streams = (AudioStreamID *)malloc(i_param_size); | |
772 if (p_streams == NULL) | |
773 { | |
774 ao_msg(MSGT_AO,MSGL_V, "out of memory\n"); | |
775 return CONTROL_FALSE; | |
776 } | |
777 | |
778 err = AudioDeviceGetProperty(i_dev_id, 0, FALSE, | |
779 kAudioDevicePropertyStreams, | |
780 &i_param_size, p_streams); | |
781 | |
782 if (err != noErr) | |
783 { | |
784 ao_msg(MSGT_AO,MSGL_V, "could not get number of streams: [%4.4s]\n", (char *)&err); | |
785 free(p_streams); | |
786 return CONTROL_FALSE; | |
787 } | |
788 | |
789 for (i = 0; i < i_streams; ++i) | |
790 { | |
791 if (AudioStreamSupportsDigital(p_streams[i])) | |
792 b_return = CONTROL_OK; | |
793 } | |
794 | |
795 free(p_streams); | |
796 return b_return; | |
797 } | |
798 | |
799 /***************************************************************************** | |
800 * AudioStreamSupportsDigital: Check i_stream_id for digital stream support. | |
801 *****************************************************************************/ | |
802 static int AudioStreamSupportsDigital( AudioStreamID i_stream_id ) | |
803 { | |
804 OSStatus err = noErr; | |
805 UInt32 i_param_size; | |
806 AudioStreamBasicDescription *p_format_list = NULL; | |
807 int i, i_formats, b_return = CONTROL_FALSE; | |
808 | |
809 /* Retrieve all the stream formats supported by each output stream. */ | |
810 err = AudioStreamGetPropertyInfo(i_stream_id, 0, | |
811 kAudioStreamPropertyPhysicalFormats, | |
812 &i_param_size, NULL); | |
813 if (err != noErr) | |
814 { | |
815 ao_msg(MSGT_AO,MSGL_V, "could not get number of streamformats: [%4.4s]\n", (char *)&err); | |
816 return CONTROL_FALSE; | |
817 } | |
818 | |
819 i_formats = i_param_size / sizeof(AudioStreamBasicDescription); | |
820 p_format_list = (AudioStreamBasicDescription *)malloc(i_param_size); | |
821 if (p_format_list == NULL) | |
822 { | |
823 ao_msg(MSGT_AO,MSGL_V, "could not malloc the memory\n" ); | |
824 return CONTROL_FALSE; | |
825 } | |
826 | |
827 err = AudioStreamGetProperty(i_stream_id, 0, | |
828 kAudioStreamPropertyPhysicalFormats, | |
829 &i_param_size, p_format_list); | |
830 if (err != noErr) | |
831 { | |
832 ao_msg(MSGT_AO,MSGL_V, "could not get the list of streamformats: [%4.4s]\n", (char *)&err); | |
833 free(p_format_list); | |
834 return CONTROL_FALSE; | |
835 } | |
836 | |
837 for (i = 0; i < i_formats; ++i) | |
838 { | |
839 print_format(MSGL_V, "supported format:", &p_format_list[i]); | |
840 | |
841 if (p_format_list[i].mFormatID == 'IAC3' || | |
842 p_format_list[i].mFormatID == kAudioFormat60958AC3) | |
843 b_return = CONTROL_OK; | |
844 } | |
845 | |
846 free(p_format_list); | |
847 return b_return; | |
848 } | |
849 | |
850 /***************************************************************************** | |
851 * AudioStreamChangeFormat: Change i_stream_id to change_format | |
852 *****************************************************************************/ | |
853 static int AudioStreamChangeFormat( AudioStreamID i_stream_id, AudioStreamBasicDescription change_format ) | |
854 { | |
855 OSStatus err = noErr; | |
856 UInt32 i_param_size = 0; | |
857 int i; | |
858 | |
859 struct timeval now; | |
860 struct timespec timeout; | |
861 struct { pthread_mutex_t lock; pthread_cond_t cond; } w; | |
862 | |
863 print_format(MSGL_V, "setting stream format:", &change_format); | |
864 | |
865 /* Condition because SetProperty is asynchronious. */ | |
866 pthread_cond_init(&w.cond, NULL); | |
867 pthread_mutex_init(&w.lock, NULL); | |
868 pthread_mutex_lock(&w.lock); | |
869 | |
870 /* Install the callback. */ | |
871 err = AudioStreamAddPropertyListener(i_stream_id, 0, | |
872 kAudioStreamPropertyPhysicalFormat, | |
873 StreamListener, (void *)&w); | |
874 if (err != noErr) | |
875 { | |
876 ao_msg(MSGT_AO, MSGL_WARN, "AudioStreamAddPropertyListener failed: [%4.4s]\n", (char *)&err); | |
877 return CONTROL_FALSE; | |
878 } | |
879 | |
880 /* Change the format. */ | |
881 err = AudioStreamSetProperty(i_stream_id, 0, 0, | |
882 kAudioStreamPropertyPhysicalFormat, | |
883 sizeof(AudioStreamBasicDescription), | |
884 &change_format); | |
885 if (err != noErr) | |
886 { | |
887 ao_msg(MSGT_AO, MSGL_WARN, "could not set the stream format: [%4.4s]\n", (char *)&err); | |
888 return CONTROL_FALSE; | |
889 } | |
890 | |
891 /* The AudioStreamSetProperty is not only asynchronious (requiring the locks), | |
892 * it is also not Atomic, in its behaviour. | |
893 * Therefore we check 5 times before we really give up. | |
894 * FIXME: failing isn't actually implemented yet. */ | |
895 for (i = 0; i < 5; ++i) | |
896 { | |
897 AudioStreamBasicDescription actual_format; | |
898 | |
899 gettimeofday(&now, NULL); | |
900 timeout.tv_sec = now.tv_sec; | |
901 timeout.tv_nsec = (now.tv_usec + 500000) * 1000; | |
902 | |
903 if (pthread_cond_timedwait(&w.cond, &w.lock, &timeout)) | |
904 ao_msg(MSGT_AO, MSGL_V, "reached timeout\n" ); | |
905 | |
906 i_param_size = sizeof(AudioStreamBasicDescription); | |
907 err = AudioStreamGetProperty(i_stream_id, 0, | |
908 kAudioStreamPropertyPhysicalFormat, | |
909 &i_param_size, | |
910 &actual_format); | |
911 | |
912 print_format(MSGL_V, "actual format in use:", &actual_format); | |
913 if (actual_format.mSampleRate == change_format.mSampleRate && | |
914 actual_format.mFormatID == change_format.mFormatID && | |
915 actual_format.mFramesPerPacket == change_format.mFramesPerPacket) | |
916 { | |
917 /* The right format is now active. */ | |
918 break; | |
919 } | |
920 /* We need to check again. */ | |
921 } | |
922 | |
923 /* Removing the property listener. */ | |
924 err = AudioStreamRemovePropertyListener(i_stream_id, 0, | |
925 kAudioStreamPropertyPhysicalFormat, | |
926 StreamListener); | |
927 if (err != noErr) | |
928 { | |
929 ao_msg(MSGT_AO, MSGL_WARN, "AudioStreamRemovePropertyListener failed: [%4.4s]\n", (char *)&err); | |
930 return CONTROL_FALSE; | |
931 } | |
932 | |
933 /* Destroy the lock and condition. */ | |
934 pthread_mutex_unlock(&w.lock); | |
935 pthread_mutex_destroy(&w.lock); | |
936 pthread_cond_destroy(&w.cond); | |
937 | |
938 return CONTROL_TRUE; | |
939 } | |
940 | |
941 /***************************************************************************** | |
942 * RenderCallbackSPDIF: callback for SPDIF audio output | |
943 *****************************************************************************/ | |
944 static OSStatus RenderCallbackSPDIF( AudioDeviceID inDevice, | |
945 const AudioTimeStamp * inNow, | |
946 const void * inInputData, | |
947 const AudioTimeStamp * inInputTime, | |
948 AudioBufferList * outOutputData, | |
949 const AudioTimeStamp * inOutputTime, | |
950 void * threadGlobals ) | |
951 { | |
952 int amt = buf_used(); | |
953 int req = outOutputData->mBuffers[ao->i_stream_index].mDataByteSize; | |
954 | |
955 if (amt > req) | |
956 amt = req; | |
957 if (amt) | |
958 read_buffer((unsigned char *)outOutputData->mBuffers[ao->i_stream_index].mData, amt); | |
959 | |
960 return noErr; | |
961 } | |
962 | |
365 | 963 |
366 static int play(void* output_samples,int num_bytes,int flags) | 964 static int play(void* output_samples,int num_bytes,int flags) |
367 { | 965 { |
368 int wrote=write_buffer(output_samples, num_bytes); | 966 int wrote, b_digital; |
369 | 967 |
370 audio_resume(); | 968 // Check whether we need to reset the digital output stream. |
371 return wrote; | 969 if (ao->b_digital && ao->b_stream_format_changed) |
970 { | |
971 ao->b_stream_format_changed = 0; | |
972 b_digital = AudioStreamSupportsDigital(ao->i_stream_id); | |
973 if (b_digital) | |
974 { | |
975 /* Current stream support digital format output, let's set it. */ | |
976 ao_msg(MSGT_AO, MSGL_V, "detected current stream support digital, try to restore digital output...\n"); | |
977 | |
978 if (!AudioStreamChangeFormat(ao->i_stream_id, ao->stream_format)) | |
979 { | |
980 ao_msg(MSGT_AO, MSGL_WARN, "restore digital output failed.\n"); | |
981 } | |
982 else | |
983 { | |
984 ao_msg(MSGT_AO, MSGL_WARN, "restore digital output succeed.\n"); | |
985 reset(); | |
986 } | |
987 } | |
988 else | |
989 ao_msg(MSGT_AO, MSGL_V, "detected current stream do not support digital.\n"); | |
990 } | |
991 | |
992 wrote=write_buffer(output_samples, num_bytes); | |
993 audio_resume(); | |
994 return wrote; | |
372 } | 995 } |
373 | 996 |
374 /* set variables and buffer to initial state */ | 997 /* set variables and buffer to initial state */ |
375 static void reset(void) | 998 static void reset(void) |
376 { | 999 { |
400 | 1023 |
401 | 1024 |
402 /* unload plugin and deregister from coreaudio */ | 1025 /* unload plugin and deregister from coreaudio */ |
403 static void uninit(int immed) | 1026 static void uninit(int immed) |
404 { | 1027 { |
1028 OSStatus err = noErr; | |
1029 UInt32 i_param_size = 0; | |
405 | 1030 |
406 if (!immed) { | 1031 if (!immed) { |
407 long long timeleft=(1000000LL*buf_used())/ao_data.bps; | 1032 long long timeleft=(1000000LL*buf_used())/ao_data.bps; |
408 ao_msg(MSGT_AO,MSGL_DBG2, "%d bytes left @%d bps (%ld usec)\n", buf_used(), ao_data.bps, (int)timeleft); | 1033 ao_msg(MSGT_AO,MSGL_DBG2, "%d bytes left @%d bps (%d usec)\n", buf_used(), ao_data.bps, (int)timeleft); |
409 usec_sleep((int)timeleft); | 1034 usec_sleep((int)timeleft); |
410 } | 1035 } |
411 | 1036 |
412 AudioOutputUnitStop(ao->theOutputUnit); | 1037 if (!ao->b_digital) { |
413 AudioUnitUninitialize(ao->theOutputUnit); | 1038 AudioOutputUnitStop(ao->theOutputUnit); |
414 CloseComponent(ao->theOutputUnit); | 1039 AudioUnitUninitialize(ao->theOutputUnit); |
1040 CloseComponent(ao->theOutputUnit); | |
1041 } | |
1042 else { | |
1043 /* Stop device. */ | |
1044 err = AudioDeviceStop(ao->i_selected_dev, | |
1045 (AudioDeviceIOProc)RenderCallbackSPDIF); | |
1046 if (err != noErr) | |
1047 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceStop failed: [%4.4s]\n", (char *)&err); | |
1048 | |
1049 /* Remove IOProc callback. */ | |
1050 err = AudioDeviceRemoveIOProc(ao->i_selected_dev, | |
1051 (AudioDeviceIOProc)RenderCallbackSPDIF); | |
1052 if (err != noErr) | |
1053 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceRemoveIOProc failed: [%4.4s]\n", (char *)&err); | |
1054 | |
1055 if (ao->b_revert) | |
1056 AudioStreamChangeFormat(ao->i_stream_id, ao->sfmt_revert); | |
1057 | |
1058 if (ao->b_changed_mixing && ao->sfmt_revert.mFormatID != kAudioFormat60958AC3) | |
1059 { | |
1060 int b_mix; | |
1061 Boolean b_writeable; | |
1062 /* Revert mixable to true if we are allowed to. */ | |
1063 err = AudioDeviceGetPropertyInfo(ao->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing, | |
1064 &i_param_size, &b_writeable); | |
1065 err = AudioDeviceGetProperty(ao->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing, | |
1066 &i_param_size, &b_mix); | |
1067 if (err != noErr && b_writeable) | |
1068 { | |
1069 b_mix = 1; | |
1070 err = AudioDeviceSetProperty(ao->i_selected_dev, 0, 0, FALSE, | |
1071 kAudioDevicePropertySupportsMixing, i_param_size, &b_mix); | |
1072 } | |
1073 if (err != noErr) | |
1074 ao_msg(MSGT_AO, MSGL_WARN, "failed to set mixmode: [%4.4s]\n", (char *)&err); | |
1075 } | |
1076 if (ao->i_hog_pid == getpid()) | |
1077 { | |
1078 ao->i_hog_pid = -1; | |
1079 i_param_size = sizeof(ao->i_hog_pid); | |
1080 err = AudioDeviceSetProperty(ao->i_selected_dev, 0, 0, FALSE, | |
1081 kAudioDevicePropertyHogMode, i_param_size, &ao->i_hog_pid); | |
1082 if (err != noErr) ao_msg(MSGT_AO, MSGL_WARN, "Could not release hogmode: [%4.4s]\n", (char *)&err); | |
1083 } | |
1084 } | |
415 | 1085 |
416 free(ao->buffer); | 1086 free(ao->buffer); |
417 free(ao); | 1087 free(ao); |
418 ao = NULL; | 1088 ao = NULL; |
419 } | 1089 } |
420 | 1090 |
421 | 1091 |
422 /* stop playing, keep buffers (for pause) */ | 1092 /* stop playing, keep buffers (for pause) */ |
423 static void audio_pause(void) | 1093 static void audio_pause(void) |
424 { | 1094 { |
425 OSErr status=noErr; | 1095 OSErr err=noErr; |
426 | 1096 |
427 /* stop callback */ | 1097 /* Stop callback. */ |
428 status=AudioOutputUnitStop(ao->theOutputUnit); | 1098 if (!ao->b_digital) |
429 if (status) | 1099 { |
430 ao_msg(MSGT_AO,MSGL_WARN, "AudioOutputUnitStop returned %d\n", | 1100 err=AudioOutputUnitStop(ao->theOutputUnit); |
431 (int)status); | 1101 if (err != noErr) |
432 ao->paused=1; | 1102 ao_msg(MSGT_AO,MSGL_WARN, "AudioOutputUnitStop returned [%4.4s]\n", (char *)&err); |
1103 } | |
1104 else | |
1105 { | |
1106 err = AudioDeviceStop(ao->i_selected_dev, (AudioDeviceIOProc)RenderCallbackSPDIF); | |
1107 if (err != noErr) | |
1108 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceStop failed: [%4.4s]\n", (char *)&err); | |
1109 } | |
1110 ao->paused = 1; | |
433 } | 1111 } |
434 | 1112 |
435 | 1113 |
436 /* resume playing, after audio_pause() */ | 1114 /* resume playing, after audio_pause() */ |
437 static void audio_resume(void) | 1115 static void audio_resume(void) |
438 { | 1116 { |
439 if(ao->paused) { | 1117 OSErr err=noErr; |
440 OSErr status=noErr; | 1118 |
441 /* start callback */ | 1119 if (!ao->paused) |
442 status=AudioOutputUnitStart(ao->theOutputUnit); | 1120 return; |
443 if (status) | 1121 |
444 ao_msg(MSGT_AO,MSGL_WARN, "AudioOutputUnitStart returned %d\n", | 1122 /* Start callback. */ |
445 (int)status); | 1123 if (!ao->b_digital) |
446 ao->paused=0; | 1124 { |
447 } | 1125 err = AudioOutputUnitStart(ao->theOutputUnit); |
448 } | 1126 if (err != noErr) |
1127 ao_msg(MSGT_AO,MSGL_WARN, "AudioOutputUnitStart returned [%4.4s]\n", (char *)&err); | |
1128 } | |
1129 else | |
1130 { | |
1131 err = AudioDeviceStart(ao->i_selected_dev, (AudioDeviceIOProc)RenderCallbackSPDIF); | |
1132 if (err != noErr) | |
1133 ao_msg(MSGT_AO, MSGL_WARN, "AudioDeviceStart failed: [%4.4s]\n", (char *)&err); | |
1134 } | |
1135 ao->paused = 0; | |
1136 } | |
1137 | |
1138 /***************************************************************************** | |
1139 * StreamListener | |
1140 *****************************************************************************/ | |
1141 static OSStatus StreamListener( AudioStreamID inStream, | |
1142 UInt32 inChannel, | |
1143 AudioDevicePropertyID inPropertyID, | |
1144 void * inClientData ) | |
1145 { | |
1146 struct { pthread_mutex_t lock; pthread_cond_t cond; } * w = inClientData; | |
1147 | |
1148 switch (inPropertyID) | |
1149 { | |
1150 case kAudioStreamPropertyPhysicalFormat: | |
1151 if (NULL!=w) | |
1152 { | |
1153 pthread_mutex_lock(&w->lock); | |
1154 pthread_cond_signal(&w->cond); | |
1155 pthread_mutex_unlock(&w->lock); | |
1156 } | |
1157 default: | |
1158 break; | |
1159 } | |
1160 return noErr; | |
1161 } | |
1162 | |
1163 static OSStatus DeviceListener( AudioDeviceID inDevice, | |
1164 UInt32 inChannel, | |
1165 Boolean isInput, | |
1166 AudioDevicePropertyID inPropertyID, | |
1167 void* inClientData ) | |
1168 { | |
1169 switch (inPropertyID) | |
1170 { | |
1171 case kAudioDevicePropertyDeviceHasChanged: | |
1172 ao_msg(MSGT_AO, MSGL_WARN, "got notify kAudioDevicePropertyDeviceHasChanged.\n"); | |
1173 ao->b_stream_format_changed = 1; | |
1174 default: | |
1175 break; | |
1176 } | |
1177 return noErr; | |
1178 } |