| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
#ifdef HAVE_CONFIG_H |
|---|
| 28 |
# include "config.h" |
|---|
| 29 |
#endif |
|---|
| 30 |
|
|---|
| 31 |
#include <unistd.h> |
|---|
| 32 |
|
|---|
| 33 |
#include <vlc_common.h> |
|---|
| 34 |
#include <vlc_plugin.h> |
|---|
| 35 |
#include <vlc_interface.h> |
|---|
| 36 |
#include <vlc_aout.h> |
|---|
| 37 |
|
|---|
| 38 |
#include <CoreAudio/CoreAudio.h> |
|---|
| 39 |
#include <AudioUnit/AudioUnitProperties.h> |
|---|
| 40 |
#include <AudioUnit/AudioUnitParameters.h> |
|---|
| 41 |
#include <AudioUnit/AudioOutputUnit.h> |
|---|
| 42 |
#include <AudioToolbox/AudioFormat.h> |
|---|
| 43 |
|
|---|
| 44 |
#define STREAM_FORMAT_MSG( pre, sfm ) \ |
|---|
| 45 |
pre "[%ld][%4.4s][%ld][%ld][%ld][%ld][%ld][%ld]", \ |
|---|
| 46 |
(UInt32)sfm.mSampleRate, (char *)&sfm.mFormatID, \ |
|---|
| 47 |
sfm.mFormatFlags, sfm.mBytesPerPacket, \ |
|---|
| 48 |
sfm.mFramesPerPacket, sfm.mBytesPerFrame, \ |
|---|
| 49 |
sfm.mChannelsPerFrame, sfm.mBitsPerChannel |
|---|
| 50 |
|
|---|
| 51 |
#define STREAM_FORMAT_MSG_FULL( pre, sfm ) \ |
|---|
| 52 |
pre ":\nsamplerate: [%ld]\nFormatID: [%4.4s]\nFormatFlags: [%ld]\nBypesPerPacket: [%ld]\nFramesPerPacket: [%ld]\nBytesPerFrame: [%ld]\nChannelsPerFrame: [%ld]\nBitsPerChannel[%ld]", \ |
|---|
| 53 |
(UInt32)sfm.mSampleRate, (char *)&sfm.mFormatID, \ |
|---|
| 54 |
sfm.mFormatFlags, sfm.mBytesPerPacket, \ |
|---|
| 55 |
sfm.mFramesPerPacket, sfm.mBytesPerFrame, \ |
|---|
| 56 |
sfm.mChannelsPerFrame, sfm.mBitsPerChannel |
|---|
| 57 |
|
|---|
| 58 |
#define BUFSIZE 0xffffff |
|---|
| 59 |
#define AOUT_VAR_SPDIF_FLAG 0xf00000 |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
struct aout_sys_t |
|---|
| 76 |
{ |
|---|
| 77 |
AudioDeviceID i_default_dev; |
|---|
| 78 |
AudioDeviceID i_selected_dev; |
|---|
| 79 |
UInt32 i_devices; |
|---|
| 80 |
bool b_supports_digital; |
|---|
| 81 |
bool b_digital; |
|---|
| 82 |
mtime_t clock_diff; |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
Component au_component; |
|---|
| 86 |
AudioUnit au_unit; |
|---|
| 87 |
uint8_t p_remainder_buffer[BUFSIZE]; |
|---|
| 88 |
uint32_t i_read_bytes; |
|---|
| 89 |
uint32_t i_total_bytes; |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
pid_t i_hog_pid; |
|---|
| 93 |
AudioStreamID i_stream_id; |
|---|
| 94 |
int i_stream_index; |
|---|
| 95 |
AudioStreamBasicDescription stream_format; |
|---|
| 96 |
AudioStreamBasicDescription sfmt_revert; |
|---|
| 97 |
bool b_revert; |
|---|
| 98 |
bool b_changed_mixing; |
|---|
| 99 |
}; |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
static int Open ( vlc_object_t * ); |
|---|
| 105 |
static int OpenAnalog ( aout_instance_t * ); |
|---|
| 106 |
static int OpenSPDIF ( aout_instance_t * ); |
|---|
| 107 |
static void Close ( vlc_object_t * ); |
|---|
| 108 |
|
|---|
| 109 |
static void Play ( aout_instance_t * ); |
|---|
| 110 |
static void Probe ( aout_instance_t * ); |
|---|
| 111 |
|
|---|
| 112 |
static int AudioDeviceHasOutput ( AudioDeviceID ); |
|---|
| 113 |
static int AudioDeviceSupportsDigital( aout_instance_t *, AudioDeviceID ); |
|---|
| 114 |
static int AudioStreamSupportsDigital( aout_instance_t *, AudioStreamID ); |
|---|
| 115 |
static int AudioStreamChangeFormat ( aout_instance_t *, AudioStreamID, AudioStreamBasicDescription ); |
|---|
| 116 |
|
|---|
| 117 |
static OSStatus RenderCallbackAnalog ( vlc_object_t *, AudioUnitRenderActionFlags *, const AudioTimeStamp *, |
|---|
| 118 |
unsigned int, unsigned int, AudioBufferList *); |
|---|
| 119 |
static OSStatus RenderCallbackSPDIF ( AudioDeviceID, const AudioTimeStamp *, const void *, const AudioTimeStamp *, |
|---|
| 120 |
AudioBufferList *, const AudioTimeStamp *, void * ); |
|---|
| 121 |
static OSStatus HardwareListener ( AudioHardwarePropertyID, void *); |
|---|
| 122 |
static OSStatus StreamListener ( AudioStreamID, UInt32, |
|---|
| 123 |
AudioDevicePropertyID, void * ); |
|---|
| 124 |
static int AudioDeviceCallback ( vlc_object_t *, const char *, |
|---|
| 125 |
vlc_value_t, vlc_value_t, void * ); |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
#define ADEV_TEXT N_("Audio Device") |
|---|
| 132 |
#define ADEV_LONGTEXT N_("Choose a number corresponding to the number of an " \ |
|---|
| 133 |
"audio device, as listed in your 'Audio Device' menu. This device will " \ |
|---|
| 134 |
"then be used by default for audio playback.") |
|---|
| 135 |
|
|---|
| 136 |
vlc_module_begin(); |
|---|
| 137 |
set_shortname( "auhal" ); |
|---|
| 138 |
set_description( N_("HAL AudioUnit output") ); |
|---|
| 139 |
set_capability( "audio output", 101 ); |
|---|
| 140 |
set_category( CAT_AUDIO ); |
|---|
| 141 |
set_subcategory( SUBCAT_AUDIO_AOUT ); |
|---|
| 142 |
set_callbacks( Open, Close ); |
|---|
| 143 |
add_integer( "macosx-audio-device", 0, NULL, ADEV_TEXT, ADEV_LONGTEXT, false ); |
|---|
| 144 |
vlc_module_end(); |
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
static int Open( vlc_object_t * p_this ) |
|---|
| 150 |
{ |
|---|
| 151 |
OSStatus err = noErr; |
|---|
| 152 |
UInt32 i_param_size = 0; |
|---|
| 153 |
struct aout_sys_t *p_sys = NULL; |
|---|
| 154 |
vlc_value_t val; |
|---|
| 155 |
aout_instance_t *p_aout = (aout_instance_t *)p_this; |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
int b_alive = false; |
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) ); |
|---|
| 163 |
if( p_aout->output.p_sys == NULL ) |
|---|
| 164 |
return VLC_ENOMEM; |
|---|
| 165 |
|
|---|
| 166 |
p_sys = p_aout->output.p_sys; |
|---|
| 167 |
p_sys->i_default_dev = 0; |
|---|
| 168 |
p_sys->i_selected_dev = 0; |
|---|
| 169 |
p_sys->i_devices = 0; |
|---|
| 170 |
p_sys->b_supports_digital = false; |
|---|
| 171 |
p_sys->b_digital = false; |
|---|
| 172 |
p_sys->au_component = NULL; |
|---|
| 173 |
p_sys->au_unit = NULL; |
|---|
| 174 |
p_sys->clock_diff = (mtime_t) 0; |
|---|
| 175 |
p_sys->i_read_bytes = 0; |
|---|
| 176 |
p_sys->i_total_bytes = 0; |
|---|
| 177 |
p_sys->i_hog_pid = -1; |
|---|
| 178 |
p_sys->i_stream_id = 0; |
|---|
| 179 |
p_sys->i_stream_index = -1; |
|---|
| 180 |
p_sys->b_revert = false; |
|---|
| 181 |
p_sys->b_changed_mixing = false; |
|---|
| 182 |
memset( p_sys->p_remainder_buffer, 0, sizeof(uint8_t) * BUFSIZE ); |
|---|
| 183 |
|
|---|
| 184 |
p_aout->output.pf_play = Play; |
|---|
| 185 |
|
|---|
| 186 |
aout_FormatPrint( p_aout, "VLC is looking for:", (audio_sample_format_t *)&p_aout->output.output ); |
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
if( var_Type( p_aout->p_libvlc, "macosx-audio-device" ) == 0 ) |
|---|
| 190 |
{ |
|---|
| 191 |
var_Create( p_aout->p_libvlc, "macosx-audio-device", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT ); |
|---|
| 192 |
} |
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
if( var_Type( p_aout, "audio-device" ) == 0 ) |
|---|
| 196 |
{ |
|---|
| 197 |
Probe( p_aout ); |
|---|
| 198 |
} |
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
if( var_Get( p_aout, "audio-device", &val ) < 0 ) |
|---|
| 202 |
{ |
|---|
| 203 |
msg_Err( p_aout, "audio-device var does not exist. device probe failed." ); |
|---|
| 204 |
goto error; |
|---|
| 205 |
} |
|---|
| 206 |
|
|---|
| 207 |
p_sys->i_selected_dev = val.i_int & ~AOUT_VAR_SPDIF_FLAG; |
|---|
| 208 |
p_sys->b_supports_digital = ( val.i_int & AOUT_VAR_SPDIF_FLAG ) ? true : false; |
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
i_param_size = sizeof( b_alive ); |
|---|
| 213 |
err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE, |
|---|
| 214 |
kAudioDevicePropertyDeviceIsAlive, |
|---|
| 215 |
&i_param_size, &b_alive ); |
|---|
| 216 |
|
|---|
| 217 |
if( err != noErr ) |
|---|
| 218 |
{ |
|---|
| 219 |
|
|---|
| 220 |
msg_Warn( p_aout, "could not check whether device [0x%x] is alive: %4.4s", (unsigned int)p_sys->i_selected_dev, (char *)&err ); |
|---|
| 221 |
b_alive = false; |
|---|
| 222 |
} |
|---|
| 223 |
|
|---|
| 224 |
if( b_alive == false ) |
|---|
| 225 |
{ |
|---|
| 226 |
msg_Warn( p_aout, "selected audio device is not alive, switching to default device" ); |
|---|
| 227 |
p_sys->i_selected_dev = p_sys->i_default_dev; |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
i_param_size = sizeof( p_sys->i_hog_pid ); |
|---|
| 231 |
err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE, |
|---|
| 232 |
kAudioDevicePropertyHogMode, |
|---|
| 233 |
&i_param_size, &p_sys->i_hog_pid ); |
|---|
| 234 |
|
|---|
| 235 |
if( err != noErr ) |
|---|
| 236 |
{ |
|---|
| 237 |
|
|---|
| 238 |
msg_Warn( p_aout, "could not check whether device is hogged: %4.4s", |
|---|
| 239 |
(char *)&err ); |
|---|
| 240 |
p_sys->i_hog_pid = -1; |
|---|
| 241 |
} |
|---|
| 242 |
|
|---|
| 243 |
if( p_sys->i_hog_pid != -1 && p_sys->i_hog_pid != getpid() ) |
|---|
| 244 |
{ |
|---|
| 245 |
msg_Err( p_aout, "Selected audio device is exclusively in use by another program." ); |
|---|
| 246 |
intf_UserFatal( p_aout, false, _("Audio output failed"), |
|---|
| 247 |
_("The selected audio output device is exclusively in " |
|---|
| 248 |
"use by another program.") ); |
|---|
| 249 |
goto error; |
|---|
| 250 |
} |
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
if( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) && p_sys->b_supports_digital ) |
|---|
| 254 |
{ |
|---|
| 255 |
if( OpenSPDIF( p_aout ) ) |
|---|
| 256 |
return VLC_SUCCESS; |
|---|
| 257 |
} |
|---|
| 258 |
else |
|---|
| 259 |
{ |
|---|
| 260 |
if( OpenAnalog( p_aout ) ) |
|---|
| 261 |
return VLC_SUCCESS; |
|---|
| 262 |
} |
|---|
| 263 |
|
|---|
| 264 |
error: |
|---|
| 265 |
|
|---|
| 266 |
var_Destroy( p_aout, "audio-device" ); |
|---|
| 267 |
free( p_sys ); |
|---|
| 268 |
return VLC_EGENERIC; |
|---|
| 269 |
} |
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
static int OpenAnalog( aout_instance_t *p_aout ) |
|---|
| 275 |
{ |
|---|
| 276 |
struct aout_sys_t *p_sys = p_aout->output.p_sys; |
|---|
| 277 |
OSStatus err = noErr; |
|---|
| 278 |
UInt32 i_param_size = 0, i = 0; |
|---|
| 279 |
int i_original; |
|---|
| 280 |
ComponentDescription desc; |
|---|
| 281 |
AudioStreamBasicDescription DeviceFormat; |
|---|
| 282 |
AudioChannelLayout *layout; |
|---|
| 283 |
AudioChannelLayout new_layout; |
|---|
| 284 |
AURenderCallbackStruct input; |
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
desc.componentType = kAudioUnitType_Output; |
|---|
| 288 |
desc.componentSubType = kAudioUnitSubType_HALOutput; |
|---|
| 289 |
desc.componentManufacturer = kAudioUnitManufacturer_Apple; |
|---|
| 290 |
desc.componentFlags = 0; |
|---|
| 291 |
desc.componentFlagsMask = 0; |
|---|
| 292 |
|
|---|
| 293 |
p_sys->au_component = FindNextComponent( NULL, &desc ); |
|---|
| 294 |
if( p_sys->au_component == NULL ) |
|---|
| 295 |
{ |
|---|
| 296 |
msg_Warn( p_aout, "we cannot find our HAL component" ); |
|---|
| 297 |
return false; |
|---|
| 298 |
} |
|---|
| 299 |
|
|---|
| 300 |
err = OpenAComponent( p_sys->au_component, &p_sys->au_unit ); |
|---|
| 301 |
if( err != noErr ) |
|---|
| 302 |
{ |
|---|
| 303 |
msg_Warn( p_aout, "we cannot open our HAL component" ); |
|---|
| 304 |
return false; |
|---|
| 305 |
} |
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 |
err = AudioUnitSetProperty( p_sys->au_unit, |
|---|
| 309 |
kAudioOutputUnitProperty_CurrentDevice, |
|---|
| 310 |
kAudioUnitScope_Global, |
|---|
| 311 |
0, |
|---|
| 312 |
&p_sys->i_selected_dev, |
|---|
| 313 |
sizeof( AudioDeviceID )); |
|---|
| 314 |
|
|---|
| 315 |
if( err != noErr ) |
|---|
| 316 |
{ |
|---|
| 317 |
msg_Warn( p_aout, "we cannot select the audio device" ); |
|---|
| 318 |
return false; |
|---|
| 319 |
} |
|---|
| 320 |
|
|---|
| 321 |
|
|---|
| 322 |
i_param_size = sizeof(AudioStreamBasicDescription); |
|---|
| 323 |
|
|---|
| 324 |
err = AudioUnitGetProperty( p_sys->au_unit, |
|---|
| 325 |
kAudioUnitProperty_StreamFormat, |
|---|
| 326 |
kAudioUnitScope_Input, |
|---|
| 327 |
0, |
|---|
| 328 |
&DeviceFormat, |
|---|
| 329 |
&i_param_size ); |
|---|
| 330 |
|
|---|
| 331 |
if( err != noErr ) return false; |
|---|
| 332 |
else msg_Dbg( p_aout, STREAM_FORMAT_MSG( "current format is: ", DeviceFormat ) ); |
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
err = AudioUnitGetPropertyInfo( p_sys->au_unit, |
|---|
| 336 |
kAudioDevicePropertyPreferredChannelLayout, |
|---|
| 337 |
kAudioUnitScope_Output, |
|---|
| 338 |
0, |
|---|
| 339 |
&i_param_size, |
|---|
| 340 |
NULL ); |
|---|
| 341 |
|
|---|
| 342 |
if( err == noErr ) |
|---|
| 343 |
{ |
|---|
| 344 |
layout = (AudioChannelLayout *)malloc( i_param_size); |
|---|
| 345 |
|
|---|
| 346 |
verify_noerr( AudioUnitGetProperty( p_sys->au_unit, |
|---|
| 347 |
kAudioDevicePropertyPreferredChannelLayout, |
|---|
| 348 |
kAudioUnitScope_Output, |
|---|
| 349 |
0, |
|---|
| 350 |
layout, |
|---|
| 351 |
&i_param_size )); |
|---|
| 352 |
|
|---|
| 353 |
|
|---|
| 354 |
if( layout->mChannelLayoutTag == kAudioChannelLayoutTag_UseChannelBitmap) |
|---|
| 355 |
{ |
|---|
| 356 |
|
|---|
| 357 |
verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForBitmap, |
|---|
| 358 |
sizeof( UInt32), &layout->mChannelBitmap, |
|---|
| 359 |
&i_param_size, |
|---|
| 360 |
layout )); |
|---|
| 361 |
} |
|---|
| 362 |
else if( layout->mChannelLayoutTag != kAudioChannelLayoutTag_UseChannelDescriptions ) |
|---|
| 363 |
{ |
|---|
| 364 |
|
|---|
| 365 |
verify_noerr( AudioFormatGetProperty( kAudioFormatProperty_ChannelLayoutForTag, |
|---|
| 366 |
sizeof( AudioChannelLayoutTag ), &layout->mChannelLayoutTag, |
|---|
| 367 |
&i_param_size, |
|---|
| 368 |
layout )); |
|---|
| 369 |
} |
|---|
| 370 |
|
|---|
| 371 |
msg_Dbg( p_aout, "layout of AUHAL has %d channels" , (int)layout->mNumberChannelDescriptions ); |
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 |
p_aout->output.output.i_physical_channels = 0; |
|---|
| 375 |
i_original = p_aout->output.output.i_original_channels & AOUT_CHAN_PHYSMASK; |
|---|
| 376 |
|
|---|
| 377 |
if( i_original == AOUT_CHAN_CENTER || layout->mNumberChannelDescriptions < 2 ) |
|---|
| 378 |
{ |
|---|
| 379 |
|
|---|
| 380 |
p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER; |
|---|
| 381 |
} |
|---|
| 382 |
else if( i_original == (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) || layout->mNumberChannelDescriptions < 3 ) |
|---|
| 383 |
{ |
|---|
| 384 |
|
|---|
| 385 |
p_aout->output.output.i_physical_channels = AOUT_CHAN_RIGHT | AOUT_CHAN_LEFT; |
|---|
| 386 |
} |
|---|
| 387 |
else |
|---|
| 388 |
{ |
|---|
| 389 |
|
|---|
| 390 |
for( i = 0; i < layout->mNumberChannelDescriptions; i++ ) |
|---|
| 391 |
{ |
|---|
| 392 |
msg_Dbg( p_aout, "this is channel: %d", (int)layout->mChannelDescriptions[i].mChannelLabel ); |
|---|
| 393 |
|
|---|
| 394 |
switch( layout->mChannelDescriptions[i].mChannelLabel ) |
|---|
| 395 |
{ |
|---|
| 396 |
case kAudioChannelLabel_Left: |
|---|
| 397 |
p_aout->output.output.i_physical_channels |= AOUT_CHAN_LEFT; |
|---|
| 398 |
continue; |
|---|
| 399 |
case kAudioChannelLabel_Right: |
|---|
| 400 |
p_aout->output.output.i_physical_channels |= AOUT_CHAN_RIGHT; |
|---|
| 401 |
continue; |
|---|
| 402 |
case kAudioChannelLabel_Center: |
|---|
| 403 |
p_aout->output.output.i_physical_channels |= AOUT_CHAN_CENTER; |
|---|
| 404 |
continue; |
|---|
| 405 |
case kAudioChannelLabel_LFEScreen: |
|---|
| 406 |
p_aout->output.output.i_physical_channels |= AOUT_CHAN_LFE; |
|---|
| 407 |
continue; |
|---|
| 408 |
case kAudioChannelLabel_LeftSurround: |
|---|
| 409 |
p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARLEFT; |
|---|
| 410 |
continue; |
|---|
| 411 |
case kAudioChannelLabel_RightSurround: |
|---|
| 412 |
p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARRIGHT; |
|---|
| 413 |
continue; |
|---|
| 414 |
case kAudioChannelLabel_RearSurroundLeft: |
|---|
| 415 |
p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLELEFT; |
|---|
| 416 |
continue; |
|---|
| 417 |
case kAudioChannelLabel_RearSurroundRight: |
|---|
| 418 |
p_aout->output.output.i_physical_channels |= AOUT_CHAN_MIDDLERIGHT; |
|---|
| 419 |
continue; |
|---|
| 420 |
case kAudioChannelLabel_CenterSurround: |
|---|
| 421 |
p_aout->output.output.i_physical_channels |= AOUT_CHAN_REARCENTER; |
|---|
| 422 |
continue; |
|---|
| 423 |
default: |
|---|
| 424 |
msg_Warn( p_aout, "unrecognized channel form provided by driver: %d", (int)layout->mChannelDescriptions[i].mChannelLabel ); |
|---|
| 425 |
} |
|---|
| 426 |
} |
|---|
| 427 |
if( p_aout->output.output.i_physical_channels == 0 ) |
|---|
| 428 |
{ |
|---|
| 429 |
p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT; |
|---|
| 430 |
msg_Err( p_aout, "You should configure your speaker layout with Audio Midi Setup Utility in /Applications/Utilities. Now using Stereo mode." ); |
|---|
| 431 |
intf_UserFatal( p_aout, false, _("Audio device is not configured"), |
|---|
| 432 |
_("You should configure your speaker layout with " |
|---|
| 433 |
"the \"Audio Midi Setup\" utility in /Applications/" |
|---|
| 434 |
"Utilities. Stereo mode is being used now.") ); |
|---|
| 435 |
} |
|---|
| 436 |
} |
|---|
| 437 |
free( layout ); |
|---|
| 438 |
} |
|---|
| 439 |
else |
|---|
| 440 |
{ |
|---|
| 441 |
msg_Warn( p_aout, "this driver does not support kAudioDevicePropertyPreferredChannelLayout. BAD DRIVER AUTHOR !!!" ); |
|---|
| 442 |
p_aout->output.output.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT; |
|---|
| 443 |
} |
|---|
| 444 |
|
|---|
| 445 |
msg_Dbg( p_aout, "selected %d physical channels for device output", aout_FormatNbChannels( &p_aout->output.output ) ); |
|---|
| 446 |
msg_Dbg( p_aout, "VLC will output: %s", aout_FormatPrintChannels( &p_aout->output.output )); |
|---|
| 447 |
|
|---|
| 448 |
memset (&new_layout, 0, sizeof(new_layout)); |
|---|
| 449 |
switch( aout_FormatNbChannels( &p_aout->output.output ) ) |
|---|
| 450 |
{ |
|---|
| 451 |
case 1: |
|---|
| 452 |
new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Mono; |
|---|
| 453 |
break; |
|---|
| 454 |
case 2: |
|---|
| 455 |
new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo; |
|---|
| 456 |
break; |
|---|
| 457 |
case 3: |
|---|
| 458 |
if( p_aout->output.output.i_physical_channels & AOUT_CHAN_CENTER ) |
|---|
| 459 |
{ |
|---|
| 460 |
new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_7; |
|---|
| 461 |
} |
|---|
| 462 |
else if( p_aout->output.output.i_physical_channels & AOUT_CHAN_LFE ) |
|---|
| 463 |
{ |
|---|
| 464 |
new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_4; |
|---|
| 465 |
} |
|---|
| 466 |
break; |
|---|
| 467 |
case 4: |
|---|
| 468 |
if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_LFE ) ) |
|---|
| 469 |
{ |
|---|
| 470 |
new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_10; |
|---|
| 471 |
} |
|---|
| 472 |
else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT ) ) |
|---|
| 473 |
{ |
|---|
| 474 |
new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; |
|---|
| 475 |
} |
|---|
| 476 |
else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER ) ) |
|---|
| 477 |
{ |
|---|
| 478 |
new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_3; |
|---|
| 479 |
} |
|---|
| 480 |
break; |
|---|
| 481 |
case 5: |
|---|
| 482 |
if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_CENTER ) ) |
|---|
| 483 |
{ |
|---|
| 484 |
new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_19; |
|---|
| 485 |
} |
|---|
| 486 |
else if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_LFE ) ) |
|---|
| 487 |
{ |
|---|
| 488 |
new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_18; |
|---|
| 489 |
} |
|---|
| 490 |
break; |
|---|
| 491 |
case 6: |
|---|
| 492 |
if( p_aout->output.output.i_physical_channels & ( AOUT_CHAN_LFE ) ) |
|---|
| 493 |
{ |
|---|
| 494 |
new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_DVD_20; |
|---|
| 495 |
} |
|---|
| 496 |
else |
|---|
| 497 |
{ |
|---|
| 498 |
new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_AudioUnit_6_0; |
|---|
| 499 |
} |
|---|
| 500 |
break; |
|---|
| 501 |
case 7: |
|---|
| 502 |
|
|---|
| 503 |
new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_6_1_A; |
|---|
| 504 |
break; |
|---|
| 505 |
case 8: |
|---|
| 506 |
|
|---|
| 507 |
new_layout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_7_1_A; |
|---|
| 508 |
break; |
|---|
| 509 |
} |
|---|
| 510 |
|
|---|
| 511 |
|
|---|
| 512 |
DeviceFormat.mSampleRate = p_aout->output.output.i_rate; |
|---|
| 513 |
DeviceFormat.mFormatID = kAudioFormatLinearPCM; |
|---|
| 514 |
|
|---|
| 515 |
|
|---|
| 516 |
p_aout->output.output.i_format = VLC_FOURCC( 'f','l','3','2'); |
|---|
| 517 |
DeviceFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked; |
|---|
| 518 |
DeviceFormat.mBitsPerChannel = 32; |
|---|
| 519 |
DeviceFormat.mChannelsPerFrame = aout_FormatNbChannels( &p_aout->output.output ); |
|---|
| 520 |
|
|---|
| 521 |
|
|---|
| 522 |
DeviceFormat.mFramesPerPacket = 1; |
|---|
| 523 |
DeviceFormat.mBytesPerFrame = DeviceFormat.mBitsPerChannel * DeviceFormat.mChannelsPerFrame / 8; |
|---|
| 524 |
DeviceFormat.mBytesPerPacket = DeviceFormat.mBytesPerFrame * DeviceFormat.mFramesPerPacket; |
|---|
| 525 |
|
|---|
| 526 |
|
|---|
| 527 |
i_param_size = sizeof(AudioStreamBasicDescription); |
|---|
| 528 |
verify_noerr( AudioUnitSetProperty( p_sys->au_unit, |
|---|
| 529 |
kAudioUnitProperty_StreamFormat, |
|---|
| 530 |
kAudioUnitScope_Input, |
|---|
| 531 |
0, |
|---|
| 532 |
&DeviceFormat, |
|---|
| 533 |
i_param_size )); |
|---|
| 534 |
|
|---|
| 535 |
msg_Dbg( p_aout, STREAM_FORMAT_MSG( "we set the AU format: " , DeviceFormat ) ); |
|---|
| 536 |
|
|---|
| 537 |
|
|---|
| 538 |
verify_noerr( AudioUnitGetProperty( p_sys->au_unit, |
|---|
| 539 |
kAudioUnitProperty_StreamFormat, |
|---|
| 540 |
kAudioUnitScope_Input, |
|---|
| 541 |
0, |
|---|
| 542 |
&DeviceFormat, |
|---|
| 543 |
&i_param_size )); |
|---|
| 544 |
|
|---|
| 545 |
msg_Dbg( p_aout, STREAM_FORMAT_MSG( "the actual set AU format is " , DeviceFormat ) ); |
|---|
| 546 |
|
|---|
| 547 |
|
|---|
| 548 |
aout_FormatPrepare( &p_aout->output.output ); |
|---|
| 549 |
p_aout->output.i_nb_samples = 2048; |
|---|
| 550 |
aout_VolumeSoftInit( p_aout ); |
|---|
| 551 |
|
|---|
| 552 |
|
|---|
| 553 |
input.inputProc = (AURenderCallback) RenderCallbackAnalog; |
|---|
| 554 |
input.inputProcRefCon = p_aout; |
|---|
| 555 |
|
|---|
| 556 |
verify_noerr( AudioUnitSetProperty( p_sys->au_unit, |
|---|
| 557 |
kAudioUnitProperty_SetRenderCallback, |
|---|
| 558 |
kAudioUnitScope_Input, |
|---|
| 559 |
0, &input, sizeof( input ) ) ); |
|---|
| 560 |
|
|---|
| 561 |
input.inputProc = (AURenderCallback) RenderCallbackAnalog; |
|---|
| 562 |
input.inputProcRefCon = p_aout; |
|---|
| 563 |
|
|---|
| 564 |
|
|---|
| 565 |
verify_noerr( AudioUnitSetProperty( p_sys->au_unit, |
|---|
| 566 |
kAudioUnitProperty_AudioChannelLayout, |
|---|
| 567 |
kAudioUnitScope_Input, |
|---|
| 568 |
0, &new_layout, sizeof(new_layout) ) ); |
|---|
| 569 |
|
|---|
| 570 |
if( new_layout.mNumberChannelDescriptions > 0 ) |
|---|
| 571 |
free( new_layout.mChannelDescriptions ); |
|---|
| 572 |
|
|---|
| 573 |
|
|---|
| 574 |
verify_noerr( AudioUnitInitialize(p_sys->au_unit) ); |
|---|
| 575 |
|
|---|
| 576 |
|
|---|
| 577 |
p_sys->clock_diff = - (mtime_t) |
|---|
| 578 |
AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000; |
|---|
| 579 |
p_sys->clock_diff += mdate(); |
|---|
| 580 |
|
|---|
| 581 |
|
|---|
| 582 |
verify_noerr( AudioOutputUnitStart(p_sys->au_unit) ); |
|---|
| 583 |
|
|---|
| 584 |
return true; |
|---|
| 585 |
} |
|---|
| 586 |
|
|---|
| 587 |
|
|---|
| 588 |
|
|---|
| 589 |
|
|---|
| 590 |
static int OpenSPDIF( aout_instance_t * p_aout ) |
|---|
| 591 |
{ |
|---|
| 592 |
struct aout_sys_t *p_sys = p_aout->output.p_sys; |
|---|
| 593 |
OSStatus err = noErr; |
|---|
| 594 |
UInt32 i_param_size = 0, b_mix = 0; |
|---|
| 595 |
Boolean b_writeable = false; |
|---|
| 596 |
AudioStreamID *p_streams = NULL; |
|---|
| 597 |
int i = 0, i_streams = 0; |
|---|
| 598 |
|
|---|
| 599 |
|
|---|
| 600 |
p_sys->b_digital = true; |
|---|
| 601 |
|
|---|
| 602 |
|
|---|
| 603 |
i_param_size = sizeof( p_sys->i_hog_pid ); |
|---|
| 604 |
p_sys->i_hog_pid = getpid() ; |
|---|
| 605 |
|
|---|
| 606 |
err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE, |
|---|
| 607 |
kAudioDevicePropertyHogMode, i_param_size, &p_sys->i_hog_pid ); |
|---|
| 608 |
|
|---|
| 609 |
if( err != noErr ) |
|---|
| 610 |
{ |
|---|
| 611 |
msg_Err( p_aout, "failed to set hogmode: [%4.4s]", (char *)&err ); |
|---|
| 612 |
return false; |
|---|
| 613 |
} |
|---|
| 614 |
|
|---|
| 615 |
|
|---|
| 616 |
err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing, |
|---|
| 617 |
&i_param_size, &b_writeable ); |
|---|
| 618 |
|
|---|
| 619 |
err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE, kAudioDevicePropertySupportsMixing, |
|---|
| 620 |
&i_param_size, &b_mix ); |
|---|
| 621 |
|
|---|
| 622 |
if( !err && b_writeable ) |
|---|
| 623 |
{ |
|---|
| 624 |
b_mix = 0; |
|---|
| 625 |
err = AudioDeviceSetProperty( p_sys->i_selected_dev, 0, 0, FALSE, |
|---|
| 626 |
kAudioDevicePropertySupportsMixing, i_param_size, &b_mix ); |
|---|
| 627 |
p_sys->b_changed_mixing = true; |
|---|
| 628 |
} |
|---|
| 629 |
|
|---|
| 630 |
if( err != noErr ) |
|---|
| 631 |
{ |
|---|
| 632 |
msg_Err( p_aout, "failed to set mixmode: [%4.4s]", (char *)&err ); |
|---|
| 633 |
return false; |
|---|
| 634 |
} |
|---|
| 635 |
|
|---|
| 636 |
|
|---|
| 637 |
err = AudioDeviceGetPropertyInfo( p_sys->i_selected_dev, 0, FALSE, |
|---|
| 638 |
kAudioDevicePropertyStreams, |
|---|
| 639 |
&i_param_size, NULL ); |
|---|
| 640 |
if( err != noErr ) |
|---|
| 641 |
{ |
|---|
| 642 |
msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err ); |
|---|
| 643 |
return false; |
|---|
| 644 |
} |
|---|
| 645 |
|
|---|
| 646 |
i_streams = i_param_size / sizeof( AudioStreamID ); |
|---|
| 647 |
p_streams = (AudioStreamID *)malloc( i_param_size ); |
|---|
| 648 |
if( p_streams == NULL ) |
|---|
| 649 |
return false; |
|---|
| 650 |
|
|---|
| 651 |
err = AudioDeviceGetProperty( p_sys->i_selected_dev, 0, FALSE, |
|---|
| 652 |
kAudioDevicePropertyStreams, |
|---|
| 653 |
&i_param_size, p_streams ); |
|---|
| 654 |
|
|---|
| 655 |
if( err != noErr ) |
|---|
| 656 |
{ |
|---|
| 657 |
msg_Err( p_aout, "could not get number of streams: [%4.4s]", (char *)&err ); |
|---|
| 658 |
free( p_streams ); |
|---|
| 659 |
return false; |
|---|
| 660 |
} |
|---|
| 661 |
|
|---|
| 662 |
for( i = 0; i < i_streams && p_sys->i_stream_index < 0 ; i++ ) |
|---|
| 663 |
{ |
|---|
| 664 |
|
|---|
| 665 |
AudioStreamBasicDescription *p_format_list = NULL; |
|---|
| 666 |
int i_formats = 0, j = 0; |
|---|
| 667 |
bool b_digital = false; |
|---|
| 668 |
|
|---|
| 669 |
|
|---|
| 670 |
err = AudioStreamGetPropertyInfo( p_streams[i], 0, |
|---|
| 671 |
kAudioStreamPropertyPhysicalFormats, |
|---|
| 672 |
&i_param_size, NULL ); |
|---|
| 673 |
if( err != noErr ) |
|---|
| 674 |
{ |
|---|
| 675 |
msg_Err( p_aout, "could not get number of streamformats: [%4.4s]", (char *)&err ); |
|---|
| 676 |
continue; |
|---|
| 677 |
} |
|---|
| 678 |
|
|---|
| 679 |
i_formats = i_param_size / sizeof( AudioStreamBasicDescription ); |
|---|
| 680 |
p_format_list = (AudioStreamBasicDescription *)malloc( i_param_size ); |
|---|
| 681 |
if( p_format_list == NULL ) |
|---|
| 682 |
continue; |
|---|
| 683 |
|
|---|
| 684 |
err = AudioStreamGetProperty( p_streams[i], 0, |
|---|
| 685 |
kAudioStreamPropertyPhysicalFormats, |
|---|
| 686 |
&i_param_size, p_format_list ); |
|---|
| 687 |
if( err != noErr ) |
|---|
| 688 |
{ |
|---|
| 689 |
msg_Err( p_aout, "could not get the list of streamformats: [%4.4s]", (char *)&err ); |
|---|
| 690 |
free( p_format_list ); |
|---|
| 691 |
continue; |
|---|
| 692 |
} |
|---|
| 693 |
|
|---|
| 694 |
|
|---|
| 695 |
for( j = 0; j < i_formats; j++ ) |
|---|
| 696 |
{ |
|---|
| 697 |
if( p_format_list[j].mFormatID == 'IAC3' || |
|---|
| 698 |
p_format_list[j].mFormatID == kAudioFormat60958AC3 ) |
|---|
| 699 |
{ |
|---|
| 700 |
b_digital = true; |
|---|
| 701 |
break; |
|---|
| 702 |
} |
|---|
| 703 |
} |
|---|
| 704 |
|
|---|
| 705 |
if( b_digital ) |
|---|
| 706 |
{ |
|---|
| 707 |
|
|---|
| 708 |
int i_requested_rate_format = -1; |
|---|
| 709 |
int i_current_rate_format = -1; |
|---|
| 710 |
int i_backup_rate_format = -1; |
|---|
| 711 |
|
|---|
| 712 |
p_sys->i_stream_id = p_streams[i]; |
|---|
| 713 |
p_sys->i_stream_index = i; |
|---|
| 714 |
|
|---|
| 715 |
if( p_sys->b_revert == false ) |
|---|
| 716 |
{ |
|---|
| 717 |
|
|---|
| 718 |
i_param_size = sizeof( p_sys->sfmt_revert ); |
|---|
| 719 |
err = AudioStreamGetProperty( p_sys->i_stream_id, 0, |
|---|
| 720 |
kAudioStreamPropertyPhysicalFormat, |
|---|
| 721 |
&i_param_size, |
|---|
| 722 |
&p_sys->sfmt_revert ); |
|---|
| 723 |
if( err != noErr ) |
|---|
| 724 |
{ |
|---|
| 725 |
msg_Err( p_aout, "could not retrieve the original streamformat: [%4.4s]", (char *)&err ); |
|---|
| 726 |
continue; |
|---|
| 727 |
} |
|---|
| 728 |
p_sys->b_revert = true; |
|---|
| 729 |
} |
|---|
| 730 |
|
|---|
| 731 |
for( j = 0; j < i_formats; j++ ) |
|---|
| 732 |
{ |
|---|
| 733 |
if( p_format_list[j].mFormatID == 'IAC3' || |
|---|
| 734 |
p_format_list[j].mFormatID == kAudioFormat60958AC3 ) |
|---|
| 735 |
{ |
|---|
| 736 |
if( p_format_list[j].mSampleRate == p_aout->output.output.i_rate ) |
|---|
| 737 |
{ |
|---|
| 738 |
i_requested_rate_format = j; |
|---|
| 739 |
break; |
|---|
| 740 |
} |
|---|
| 741 |
else if( p_format_list[j].mSampleRate == p_sys->sfmt_revert.mSampleRate ) |
|---|
| 742 |
{ |
|---|
| 743 |
i_current_rate_format = j; |
|---|
| 744 |
} |
|---|
| 745 |
else |
|---|
| 746 |
{ |
|---|
| 747 |
if( i_backup_rate_format < 0 || p_format_list[j].mSampleRate > p_format_list[i_backup_rate_format].mSampleRate ) |
|---|
| 748 |
i_backup_rate_format = j; |
|---|
| 749 |
} |
|---|
| 750 |
} |
|---|
| 751 |
|
|---|
| 752 |
} |
|---|
| 753 |
|
|---|
| 754 |
if( i_requested_rate_format >= 0 ) |
|---|
| 755 |
p_sys->stream_format = p_format_list[i_requested_rate_format]; |
|---|
| 756 |
else if( i_current_rate_format >= 0 ) |
|---|
| 757 |
p_sys->stream_format = p_format_list[i_current_rate_format]; |
|---|
| 758 |
else p_sys->stream_format = p_format_list[i_backup_rate_format]; |
|---|
| 759 |
} |
|---|
| 760 |
free( p_format_list ); |
|---|
| 761 |
} |
|---|
| 762 |
free( p_streams ); |
|---|
| 763 |
|
|---|
| 764 |
msg_Dbg( p_aout, STREAM_FORMAT_MSG( "original stream format: ", p_sys->sfmt_revert ) ); |
|---|
| 765 |
|
|---|
| 766 |
if( !AudioStreamChangeFormat( p_aout, p_sys->i_stream_id, p_sys->stream_format ) ) |
|---|
| 767 |
return false; |
|---|
| 768 |
|
|---|
| 769 |
|
|---|
| 770 |
if( p_sys->stream_format.mFormatFlags & kAudioFormatFlagIsBigEndian ) |
|---|
| 771 |
p_aout->output.output.i_format = VLC_FOURCC('s','p','d','b'); |
|---|
| 772 |
else |
|---|
| 773 |
p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i'); |
|---|
| 774 |
p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE; |
|---|
| 775 |
p_aout->output.output.i_frame_length = A52_FRAME_NB; |
|---|
| 776 |
p_aout->output.i_nb_samples = p_aout->output.output.i_frame_length; |
|---|
| 777 |
p_aout->output.output.i_rate = (unsigned int)p_sys->stream_format.mSampleRate; |
|---|
| 778 |
aout_FormatPrepare( &p_aout->output.output ); |
|---|
| 779 |
aout_VolumeNoneInit( p_aout ); |
|---|
| 780 |
|
|---|
| 781 |
|
|---|
| 782 |
err = AudioDeviceAddIOProc( p_sys->i_selected_dev, |
|---|
| 783 |
(AudioDeviceIOProc)RenderCallbackSPDIF, |
|---|
| 784 |
(void *)p_aout ); |
|---|
| 785 |
|
|---|
| 786 |
if( err != noErr ) |
|---|
| 787 |
{ |
|---|
| 788 |
msg_Err( p_aout, "AudioDeviceAddIOProc failed: [%4.4s]", (char *)&err ); |
|---|
| 789 |
return false; |
|---|
| 790 |
} |
|---|
| 791 |
|
|---|
| 792 |
|
|---|
| 793 |
p_sys->clock_diff = - (mtime_t) |
|---|
| 794 |
AudioConvertHostTimeToNanos( AudioGetCurrentHostTime() ) / 1000; |
|---|
| 795 |
p_sys->clock_diff += mdate(); |
|---|
| 796 |
|
|---|
|
|---|