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

Unified Diff: content/browser/device_monitor_mac.mm

Issue 10824162: add device notification to Mac (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: addressed Tommi's comments Created 8 years, 4 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: content/browser/device_monitor_mac.mm
diff --git a/content/browser/device_monitor_mac.mm b/content/browser/device_monitor_mac.mm
new file mode 100644
index 0000000000000000000000000000000000000000..7cf724343aaccb40c4bee22d43eba7da0609d810
--- /dev/null
+++ b/content/browser/device_monitor_mac.mm
@@ -0,0 +1,176 @@
+// 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 "content/browser/device_monitor_mac.h"
+
+#include <CoreFoundation/CFNumber.h>
+#include <CoreFoundation/CoreFoundation.h>
+#include <IOKit/usb/IOUSBLib.h>
+
+#include "base/logging.h"
+#include "base/system_monitor/system_monitor.h"
+
+namespace content {
+
+namespace {
+
+DeviceMonitorMac* InstanceFromContext(void* context) {
+ return reinterpret_cast<DeviceMonitorMac*>(context);
+}
+
+void AddValueToDictionary(CFMutableDictionaryRef dictionary,
+ SInt32 code,
+ const void *key) {
+ CFNumberRef number_ref = CFNumberCreate(kCFAllocatorDefault,
+ kCFNumberSInt32Type,
+ &code);
+ if (!number_ref) {
+ NOTREACHED() << "failed to create CFNumberRef for " << code;
+ return;
+ }
+
+ CFDictionaryAddValue(dictionary, key, number_ref);
+ CFRelease(number_ref);
+
+ dictionary = (CFMutableDictionaryRef)(CFRetain(dictionary));
+}
+
+} // namespace
+
+DeviceMonitorMac::DeviceMonitorMac() {
+ CFRunLoopRef runloop = CFRunLoopGetCurrent();
+ CFRetain(runloop);
+
+ // Add the notification port to the run loop.
+ IONotificationPortRef notification_port =
+ IONotificationPortCreate(kIOMasterPortDefault);
+ CFRunLoopSourceRef notification_cfsource =
+ IONotificationPortGetRunLoopSource(notification_port);
+
+ RegisterVideoCallbacks(notification_port);
+ RegisterAudioCallbacks(notification_port);
+ CFRunLoopAddSource(runloop, notification_cfsource, kCFRunLoopCommonModes);
+}
+
+void DeviceMonitorMac::RegisterVideoCallbacks(IONotificationPortRef port) {
+ SInt32 interface_class_code = kUSBVideoInterfaceClass;
+ SInt32 interface_subclass_code = kUSBVideoControlSubClass;
+ CFMutableDictionaryRef matching_dictionary = IOServiceMatching(
+ kIOUSBInterfaceClassName);
+ AddValueToDictionary(matching_dictionary, interface_class_code,
+ CFSTR(kUSBInterfaceClass));
+ AddValueToDictionary(matching_dictionary, interface_subclass_code,
+ CFSTR(kUSBInterfaceSubClass));
+
+ // Add a callback which will be called when a video device is plugged in.
+ io_iterator_t new_devices_iterator = 0;
+ kern_return_t err = IOServiceAddMatchingNotification(
+ port,
+ kIOMatchedNotification,
+ matching_dictionary,
+ &VideoDeviceChangedCallback,
+ this,
+ &new_devices_iterator);
+ if (err) {
+ NOTREACHED() << "Failed to register the video IO mached notification";
+ return;
+ }
+ VideoDeviceChangedCallback(NULL, new_devices_iterator);
+
+ // Add a callback which will be called when a video device is terminated.
+ io_iterator_t lost_devices_iterator = 0;
+ err = IOServiceAddMatchingNotification(
+ port,
+ kIOTerminatedNotification,
+ matching_dictionary,
+ &VideoDeviceChangedCallback,
+ this,
+ &lost_devices_iterator);
+ if (err) {
+ NOTREACHED() << "Failed to register the video IO terminated notification";
+ return;
+ }
+ VideoDeviceChangedCallback(NULL, lost_devices_iterator);
+}
+
+void DeviceMonitorMac::RegisterAudioCallbacks(IONotificationPortRef port) {
wjia(left Chromium) 2012/08/03 13:50:08 It seems this function is very similar to Register
no longer working on chromium 2012/08/03 16:10:15 Done.
+ SInt32 interface_class_code = kUSBAudioInterfaceClass;
+ SInt32 interface_subclass_code = kUSBAudioControlSubClass;
+ CFMutableDictionaryRef matching_dictionary = IOServiceMatching(
+ kIOUSBInterfaceClassName);
+ AddValueToDictionary(matching_dictionary, interface_class_code,
+ CFSTR(kUSBInterfaceClass));
+ AddValueToDictionary(matching_dictionary, interface_subclass_code,
+ CFSTR(kUSBInterfaceSubClass));
+
+ // Add a callback which will be called when a audio device is plugged in.
+ io_iterator_t added_devices_iterator = 0;
+ kern_return_t err = IOServiceAddMatchingNotification(
+ port,
+ kIOMatchedNotification,
+ matching_dictionary,
+ &AudioDeviceChangedCallback,
+ this,
+ &added_devices_iterator);
+ if (err) {
+ NOTREACHED() << "Failed to register the audio IO mached notification";
+ return;
+ }
+ // Iterate over set of matching devices to access already-present devices
+ // and to arm the notification.
+ AudioDeviceChangedCallback(NULL, added_devices_iterator);
+
+ // Add a callback which will be called when a audio device is terminated.
+ io_iterator_t removed_devices_iterator = 0;
+ err = IOServiceAddMatchingNotification(
+ port,
+ kIOTerminatedNotification,
+ matching_dictionary,
+ &AudioDeviceChangedCallback,
+ this,
wjia(left Chromium) 2012/08/03 15:21:05 if the type, instead of |this|, is used here, you
no longer working on chromium 2012/08/03 16:10:15 In this case, we can't access to member functions.
+ &removed_devices_iterator);
+ if (err) {
+ NOTREACHED() << "Failed to register the audio IO terminated notification";
+ return;
+ }
+ // Iterate over set of matching devices to release each one and to
+ // arm the notification.
+ AudioDeviceChangedCallback(NULL, removed_devices_iterator);
+}
+
+void DeviceMonitorMac::VideoDeviceChangedCallback(void *context,
+ io_iterator_t devices) {
+ io_object_t thisObject;
+ while ((thisObject = IOIteratorNext(devices))) {
+ if (context)
+ InstanceFromContext(context)->NotifyVideoDeviceChanged();
wjia(left Chromium) 2012/08/03 13:50:08 At this point, the type is known. It seems you nee
no longer working on chromium 2012/08/03 16:10:15 Done.
+
+ IOObjectRelease(thisObject);
+ }
+}
+
+void DeviceMonitorMac::AudioDeviceChangedCallback(void *context,
+ io_iterator_t devices) {
+ io_object_t thisObject;
+ while ((thisObject = IOIteratorNext(devices))) {
+ if (context)
+ InstanceFromContext(context)->NotifyAudioDeviceChanged();
+
+ IOObjectRelease(thisObject);
+ }
+}
+
+void DeviceMonitorMac::NotifyAudioDeviceChanged() {
+ base::SystemMonitor* monitor = base::SystemMonitor::Get();
wjia(left Chromium) 2012/08/03 13:50:08 no need to have intermediate variable.
no longer working on chromium 2012/08/03 16:10:15 Done.
+ monitor->ProcessDevicesChanged(base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE);
+}
+
+void DeviceMonitorMac::NotifyVideoDeviceChanged() {
+ base::SystemMonitor* monitor = base::SystemMonitor::Get();
+ monitor->ProcessDevicesChanged(base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE);
+}
+
+DeviceMonitorMac::~DeviceMonitorMac() {}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698