Skip to content

Commit

Permalink
[AE/CA] - on device change reinit coreaudio by calling opencoreaudio …
Browse files Browse the repository at this point in the history
…with the last engine parameters (this will fallback to default device when our current output device vanishes and on the other hand will go back to that device if it re-appears).
  • Loading branch information
Memphiz committed Jan 29, 2013
1 parent 9edb540 commit 6c00b75
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
56 changes: 56 additions & 0 deletions xbmc/cores/AudioEngine/Engines/CoreAudio/CoreAudioAE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,46 @@
#define DELAY_FRAME_TIME 20
#define BUFFERSIZE 16416

// on darwin when the devicelist changes
// reinit by calling opencoreaudio with the last engine parameters
// (this will fallback to default
// device when our current output device vanishes
// and on the other hand will go back to that device
// if it re-appears).
#if defined(TARGET_DARWIN_OSX)
OSStatus deviceChangedCB( AudioObjectID inObjectID,
UInt32 inNumberAddresses,
const AudioObjectPropertyAddress inAddresses[],
void* inClientData)
{
CCoreAudioAE *pEngine = (CCoreAudioAE *)inClientData;
pEngine->AudioDevicesChanged();
CLog::Log(LOGDEBUG, "CCoreAudioAE - audiodevicelist changed!");
return noErr;
}

void RegisterDeviceChangedCB(bool bRegister, void *ref)
{
OSStatus ret = noErr;
const AudioObjectPropertyAddress inAdr =
{
kAudioHardwarePropertyDevices,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};

if (bRegister)
ret = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &inAdr, deviceChangedCB, ref);
else
ret = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &inAdr, deviceChangedCB, ref);

if (ret != noErr)
CLog::Log(LOGERROR, "CCoreAudioAE::Deinitialize - error %s a listener callback for device changes!", bRegister?"attaching":"removing");
}
#else//ios
void RegisterDeviceChangedCB(bool bRegister, void *ref){}
#endif

CCoreAudioAE::CCoreAudioAE() :
m_Initialized (false ),
m_callbackRunning (false ),
Expand All @@ -58,10 +98,13 @@ CCoreAudioAE::CCoreAudioAE() :
m_currentAudioDevice (0 )
{
HAL = new CCoreAudioAEHAL;

RegisterDeviceChangedCB(true, this);
}

CCoreAudioAE::~CCoreAudioAE()
{
RegisterDeviceChangedCB(false, this);
Shutdown();
}

Expand Down Expand Up @@ -95,6 +138,17 @@ void CCoreAudioAE::Shutdown()
HAL = NULL;
}

void CCoreAudioAE::AudioDevicesChanged()
{
// give CA a bit time to realise that maybe the
// default device might have changed now - else
// OpenCoreAudio might open the old default device
// again (yeah that really is the case - duh)
Sleep(500);
CSingleLock engineLock(m_engineLock);
OpenCoreAudio(m_lastSampleRate, COREAUDIO_IS_RAW(m_lastStreamFormat), m_lastStreamFormat);
}

bool CCoreAudioAE::Initialize()
{
CSingleLock engineLock(m_engineLock);
Expand All @@ -104,6 +158,8 @@ bool CCoreAudioAE::Initialize()
Deinitialize();

bool ret = OpenCoreAudio(44100, false, AE_FMT_FLOAT);
m_lastSampleRate = 44100;
m_lastStreamFormat = AE_FMT_FLOAT;

Start();

Expand Down
2 changes: 2 additions & 0 deletions xbmc/cores/AudioEngine/Engines/CoreAudio/CoreAudioAE.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class CCoreAudioAE : public IAE, public ICoreAudioSource
virtual OSStatus Render(AudioUnitRenderActionFlags* actionFlags,
const AudioTimeStamp* pTimeStamp, UInt32 busNumber,
UInt32 frameCount, AudioBufferList* pBufList);

void AudioDevicesChanged();


private:
Expand Down

0 comments on commit 6c00b75

Please sign in to comment.