1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "content/browser/device_monitor_mac.h" 6 7 #import <QTKit/QTKit.h> 8 9 #include "base/logging.h" 10 11 namespace content { 12 13 class DeviceMonitorMac::QTMonitorImpl { 14 public: 15 explicit QTMonitorImpl(DeviceMonitorMac* monitor); 16 virtual ~QTMonitorImpl() {} 17 18 void Start(); 19 void Stop(); 20 21 private: 22 void OnDeviceChanged(); 23 24 DeviceMonitorMac* monitor_; 25 int number_audio_devices_; 26 int number_video_devices_; 27 id device_arrival_; 28 id device_removal_; 29 30 DISALLOW_COPY_AND_ASSIGN(QTMonitorImpl); 31 }; 32 33 DeviceMonitorMac::QTMonitorImpl::QTMonitorImpl(DeviceMonitorMac* monitor) 34 : monitor_(monitor), 35 number_audio_devices_(0), 36 number_video_devices_(0), 37 device_arrival_(nil), 38 device_removal_(nil) { 39 DCHECK(monitor); 40 } 41 42 void DeviceMonitorMac::QTMonitorImpl::Start() { 43 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; 44 device_arrival_ = 45 [nc addObserverForName:QTCaptureDeviceWasConnectedNotification 46 object:nil 47 queue:nil 48 usingBlock:^(NSNotification* notification) { 49 OnDeviceChanged();}]; 50 51 device_removal_ = 52 [nc addObserverForName:QTCaptureDeviceWasDisconnectedNotification 53 object:nil 54 queue:nil 55 usingBlock:^(NSNotification* notification) { 56 OnDeviceChanged();}]; 57 } 58 59 void DeviceMonitorMac::QTMonitorImpl::Stop() { 60 if (!monitor_) 61 return; 62 63 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; 64 [nc removeObserver:device_arrival_]; 65 [nc removeObserver:device_removal_]; 66 } 67 68 void DeviceMonitorMac::QTMonitorImpl::OnDeviceChanged() { 69 NSArray* devices = [QTCaptureDevice inputDevices]; 70 int number_video_devices = 0; 71 int number_audio_devices = 0; 72 for (QTCaptureDevice* device in devices) { 73 if ([device hasMediaType:QTMediaTypeVideo] || 74 [device hasMediaType:QTMediaTypeMuxed]) 75 ++number_video_devices; 76 77 if ([device hasMediaType:QTMediaTypeSound] || 78 [device hasMediaType:QTMediaTypeMuxed]) 79 ++number_audio_devices; 80 } 81 82 if (number_video_devices_ != number_video_devices) { 83 number_video_devices_ = number_video_devices; 84 monitor_->NotifyDeviceChanged(base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE); 85 } 86 87 if (number_audio_devices_ != number_audio_devices) { 88 number_audio_devices_ = number_audio_devices; 89 monitor_->NotifyDeviceChanged(base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE); 90 } 91 } 92 93 DeviceMonitorMac::DeviceMonitorMac() { 94 qt_monitor_.reset(new QTMonitorImpl(this)); 95 qt_monitor_->Start(); 96 } 97 98 DeviceMonitorMac::~DeviceMonitorMac() { 99 qt_monitor_->Stop(); 100 } 101 102 void DeviceMonitorMac::NotifyDeviceChanged( 103 base::SystemMonitor::DeviceType type) { 104 // TODO(xians): Remove the global variable for SystemMonitor. 105 base::SystemMonitor::Get()->ProcessDevicesChanged(type); 106 } 107 108 } // namespace content 109