Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Unified Diff: media/audio/mac/audio_device_listener_mac.cc

Issue 11344005: Handle audio device changes on Mac (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: locking Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/audio/mac/audio_device_listener_mac.cc
diff --git a/media/audio/mac/audio_device_listener_mac.cc b/media/audio/mac/audio_device_listener_mac.cc
new file mode 100644
index 0000000000000000000000000000000000000000..5db665d59fc355f1f2042a982939e54a8f28b2c7
--- /dev/null
+++ b/media/audio/mac/audio_device_listener_mac.cc
@@ -0,0 +1,80 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "media/audio/mac/audio_device_listener_mac.h"
+
+#include <CoreAudio/CoreAudio.h>
+
+#include "base/bind.h"
+#include "base/location.h"
+#include "base/logging.h"
+#include "base/mac/mac_logging.h"
+
+namespace {
+
+AudioObjectPropertySelector g_observed_properties[] = {
DaleCurtis 2012/10/29 21:11:32 static const. Then the name should be styled kObse
sail 2012/10/29 21:38:28 Changed to const. This doesn't need to be static s
+ kAudioHardwarePropertyDefaultOutputDevice,
+ kAudioDevicePropertyNominalSampleRate,
+};
+
+// Callback from system when the default device changes. This can be called
+// either on the main thread or on a audio thread managed by the system
+// (depending on what kAudioHardwarePropertyRunLoop is set to).
+OSStatus OnDefaultDeviceChangedCallback(
+ AudioObjectID object,
+ UInt32 size,
+ const AudioObjectPropertyAddress addresses[],
+ void* context) {
+ media::AudioDeviceListenerMac* listener =
+ static_cast<media::AudioDeviceListenerMac*>(context);
+ listener->OnDefaultDeviceChanged();
+ return noErr;
+}
+
+} // namespace
+
+namespace media {
+
+AudioDeviceListenerMac::AudioDeviceListenerMac(const base::Closure& listener_cb)
+ : listener_cb_(listener_cb) {
+ for (size_t i = 0; i < arraysize(g_observed_properties); ++i) {
+ AudioObjectPropertyAddress property = {
+ g_observed_properties[i],
+ kAudioObjectPropertyScopeGlobal,
+ kAudioObjectPropertyElementMaster
+ };
+ OSStatus result = AudioObjectAddPropertyListener(
+ kAudioObjectSystemObject,
+ &property,
+ &OnDefaultDeviceChangedCallback,
+ this);
+ OSSTATUS_DCHECK(result == noErr, result);
+ }
+}
+
+AudioDeviceListenerMac::~AudioDeviceListenerMac() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ base::AutoLock auto_lock(lock_);
Scott Hess - ex-Googler 2012/10/29 21:04:02 AFAICT, you have this lock to prevent races WRT On
DaleCurtis 2012/10/29 21:11:32 Does this cause races during destruction? I assume
sail 2012/10/29 21:38:28 Done.
DaleCurtis 2012/10/29 21:57:14 You should double check my logic here to ensure th
Scott Hess - ex-Googler 2012/10/29 22:19:22 I'm finding myself nervous, here. I'm wondering i
+
+ for (size_t i = 0; i < arraysize(g_observed_properties); ++i) {
+ AudioObjectPropertyAddress property = {
+ g_observed_properties[i],
+ kAudioObjectPropertyScopeGlobal,
+ kAudioObjectPropertyElementMaster
+ };
+ OSStatus result = AudioObjectRemovePropertyListener(
+ kAudioObjectSystemObject,
+ &property,
+ &OnDefaultDeviceChangedCallback,
+ this);
+ OSSTATUS_DCHECK(result == noErr, result);
+ }
+}
+
+void AudioDeviceListenerMac::OnDefaultDeviceChanged() {
+ base::AutoLock auto_lock(lock_);
+ listener_cb_.Run();
+}
+
+} // namespace media

Powered by Google App Engine
This is Rietveld 408576698