This source file includes following definitions.
- OnDefaultDeviceChanged
#include "media/audio/mac/audio_device_listener_mac.h"
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/mac/mac_logging.h"
#include "base/mac/mac_util.h"
#include "base/message_loop/message_loop.h"
#include "base/pending_task.h"
namespace media {
const AudioObjectPropertyAddress
AudioDeviceListenerMac::kDeviceChangePropertyAddress = {
kAudioHardwarePropertyDefaultOutputDevice,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
OSStatus AudioDeviceListenerMac::OnDefaultDeviceChanged(
AudioObjectID object, UInt32 num_addresses,
const AudioObjectPropertyAddress addresses[], void* context) {
if (object != kAudioObjectSystemObject)
return noErr;
for (UInt32 i = 0; i < num_addresses; ++i) {
if (addresses[i].mSelector == kDeviceChangePropertyAddress.mSelector &&
addresses[i].mScope == kDeviceChangePropertyAddress.mScope &&
addresses[i].mElement == kDeviceChangePropertyAddress.mElement &&
context) {
static_cast<AudioDeviceListenerMac*>(context)->listener_cb_.Run();
break;
}
}
return noErr;
}
AudioDeviceListenerMac::AudioDeviceListenerMac(
const base::Closure& listener_cb) {
OSStatus result = AudioObjectAddPropertyListener(
kAudioObjectSystemObject, &kDeviceChangePropertyAddress,
&AudioDeviceListenerMac::OnDefaultDeviceChanged, this);
if (result != noErr) {
OSSTATUS_DLOG(ERROR, result)
<< "AudioObjectAddPropertyListener() failed!";
return;
}
listener_cb_ = listener_cb;
}
AudioDeviceListenerMac::~AudioDeviceListenerMac() {
DCHECK(thread_checker_.CalledOnValidThread());
if (listener_cb_.is_null())
return;
OSStatus result = AudioObjectRemovePropertyListener(
kAudioObjectSystemObject, &kDeviceChangePropertyAddress,
&AudioDeviceListenerMac::OnDefaultDeviceChanged, this);
OSSTATUS_DLOG_IF(ERROR, result != noErr, result)
<< "AudioObjectRemovePropertyListener() failed!";
}
}