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

Side by Side Diff: content/browser/device_monitor_mac.mm

Issue 24615005: Added AVFoundation Glue and Device Monitoring for Mac. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: MacMonitor->DeviceMonitorMacImpl and nits. Created 7 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/device_monitor_mac.h" 5 #include "content/browser/device_monitor_mac.h"
6 6
7 #import <QTKit/QTKit.h> 7 #import <QTKit/QTKit.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 10 #import "media/video/capture/mac/avfoundation_glue.h"
11 namespace content { 11
12 12 namespace {
13 class DeviceMonitorMac::QTMonitorImpl { 13
14 public: 14 // This class is used to keep track of system devices names and their types.
15 explicit QTMonitorImpl(DeviceMonitorMac* monitor); 15 class DeviceInfo {
16 virtual ~QTMonitorImpl() {} 16 public:
17 17 enum DeviceType {
18 void Start(); 18 kAudio,
19 void Stop(); 19 kVideo,
20 kMuxed
21 };
22
23 DeviceInfo(std::string unique_id, DeviceType type)
24 : unique_id_(unique_id), type_(type) {}
25
26 // Operator== is needed here to use this class in a std::vector.
27 bool operator==(const DeviceInfo& device) const {
28 return unique_id_ == device.unique_id_;
29 }
30
31 const std::string& unique_id() const { return unique_id_; }
32 DeviceType type() const { return type_; }
20 33
21 private: 34 private:
22 void OnDeviceChanged(); 35 std::string unique_id_;
23 36 DeviceType type_;
24 DeviceMonitorMac* monitor_; 37 // Allow generated copy constructor and assignment.
25 int number_audio_devices_; 38 };
26 int number_video_devices_; 39
40 // Base class used by DeviceMonitorMac to interact with either a QTKit or an
41 // AVFoundation implementation of events and notifications. This class should
42 // never be instantiated on its own.
43 class DeviceMonitorMacImpl {
44 public:
45 explicit DeviceMonitorMacImpl(content::DeviceMonitorMac* monitor)
46 : monitor_(monitor),
47 cached_devices_(),
48 device_arrival_(nil),
49 device_removal_(nil) {
50 DCHECK(monitor);
51 }
52 virtual ~DeviceMonitorMacImpl() {}
53
54 virtual void OnDeviceChanged() = 0;
55
56 // Method called by the default notification center when a device is removed
57 // or added to the system. It will compare the |cached_devices_| with the
58 // current situation, update it, and, if there's an update, signal to
59 // |monitor_| with the appropriate device type.
60 void ConsolidateDevicesListAndNotify(
61 const std::vector<DeviceInfo>& snapshot_devices);
62
63 protected:
64 content::DeviceMonitorMac* monitor_;
65 std::vector<DeviceInfo> cached_devices_;
66
67 // Handles to NSNotificationCenter block observers.
27 id device_arrival_; 68 id device_arrival_;
28 id device_removal_; 69 id device_removal_;
29 70
30 DISALLOW_COPY_AND_ASSIGN(QTMonitorImpl); 71 DISALLOW_COPY_AND_ASSIGN(DeviceMonitorMacImpl);
31 }; 72 };
32 73
33 DeviceMonitorMac::QTMonitorImpl::QTMonitorImpl(DeviceMonitorMac* monitor) 74 void DeviceMonitorMacImpl::ConsolidateDevicesListAndNotify(
34 : monitor_(monitor), 75 const std::vector<DeviceInfo>& snapshot_devices) {
35 number_audio_devices_(0), 76 bool video_device_added = false;
36 number_video_devices_(0), 77 bool audio_device_added = false;
37 device_arrival_(nil), 78 bool video_device_removed = false;
38 device_removal_(nil) { 79 bool audio_device_removed = false;
39 DCHECK(monitor); 80
40 } 81 // Compare the current system devices snapshot with the ones cached to detect
41 82 // additions, present in the former but not in the latter. If we find a device
42 void DeviceMonitorMac::QTMonitorImpl::Start() { 83 // in snapshot_devices entry also present in cached_devices, we remove it from
84 // the latter vector.
85 std::vector<DeviceInfo>::const_iterator it;
86 for (it = snapshot_devices.begin(); it != snapshot_devices.end(); ++it) {
87 std::vector<DeviceInfo>::iterator cached_devices_iterator =
88 std::find(cached_devices_.begin(), cached_devices_.end(), *it);
89 if (cached_devices_iterator == cached_devices_.end()) {
90 video_device_added |= ((it->type() == DeviceInfo::kVideo) ||
91 (it->type() == DeviceInfo::kMuxed));
92 audio_device_added |= ((it->type() == DeviceInfo::kAudio) ||
93 (it->type() == DeviceInfo::kMuxed));
94 DVLOG(1) << "Device has been added, id: " << it->unique_id();
95 } else {
96 cached_devices_.erase(cached_devices_iterator);
97 }
98 }
99 // All the remaining entries in cached_devices are removed devices.
100 for (it = cached_devices_.begin(); it != cached_devices_.end(); ++it) {
101 video_device_removed |= ((it->type() == DeviceInfo::kVideo) ||
102 (it->type() == DeviceInfo::kMuxed));
103 audio_device_removed |= ((it->type() == DeviceInfo::kAudio) ||
104 (it->type() == DeviceInfo::kMuxed));
105 DVLOG(1) << "Device has been removed, id: " << it->unique_id();
106 }
107 // Update the cached devices with the current system snapshot.
108 cached_devices_ = snapshot_devices;
109
110 if (video_device_added || video_device_removed)
111 monitor_->NotifyDeviceChanged(base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE);
112 if (audio_device_added || video_device_removed)
113 monitor_->NotifyDeviceChanged(base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE);
114 }
115
116 class QTKitMonitorImpl : public DeviceMonitorMacImpl {
117 public:
118 explicit QTKitMonitorImpl(content::DeviceMonitorMac* monitor);
119 virtual ~QTKitMonitorImpl();
120
121 virtual void OnDeviceChanged() OVERRIDE;
122 };
123
124 QTKitMonitorImpl::QTKitMonitorImpl(content::DeviceMonitorMac* monitor)
125 : DeviceMonitorMacImpl(monitor) {
43 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; 126 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
44 device_arrival_ = 127 device_arrival_ =
45 [nc addObserverForName:QTCaptureDeviceWasConnectedNotification 128 [nc addObserverForName:QTCaptureDeviceWasConnectedNotification
46 object:nil 129 object:nil
47 queue:nil 130 queue:nil
48 usingBlock:^(NSNotification* notification) { 131 usingBlock:^(NSNotification* notification) {
49 OnDeviceChanged();}]; 132 OnDeviceChanged();
133 }];
50 134
51 device_removal_ = 135 device_removal_ =
52 [nc addObserverForName:QTCaptureDeviceWasDisconnectedNotification 136 [nc addObserverForName:QTCaptureDeviceWasDisconnectedNotification
53 object:nil 137 object:nil
54 queue:nil 138 queue:nil
55 usingBlock:^(NSNotification* notification) { 139 usingBlock:^(NSNotification* notification) {
56 OnDeviceChanged();}]; 140 OnDeviceChanged();
57 } 141 }];
58 142 }
59 void DeviceMonitorMac::QTMonitorImpl::Stop() { 143
60 if (!monitor_) 144 QTKitMonitorImpl::~QTKitMonitorImpl() {
61 return;
62
63 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; 145 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
64 [nc removeObserver:device_arrival_]; 146 [nc removeObserver:device_arrival_];
65 [nc removeObserver:device_removal_]; 147 [nc removeObserver:device_removal_];
66 } 148 }
67 149
68 void DeviceMonitorMac::QTMonitorImpl::OnDeviceChanged() { 150 void QTKitMonitorImpl::OnDeviceChanged() {
151 std::vector<DeviceInfo> snapshot_devices;
152 DeviceInfo::DeviceType device_info;
Robert Sesek 2013/10/22 14:03:50 naming: device_info -> device_type. Also maybe mov
mcasas 2013/10/22 18:37:42 Done.
153
69 NSArray* devices = [QTCaptureDevice inputDevices]; 154 NSArray* devices = [QTCaptureDevice inputDevices];
70 int number_video_devices = 0;
71 int number_audio_devices = 0;
72 for (QTCaptureDevice* device in devices) { 155 for (QTCaptureDevice* device in devices) {
73 if ([device hasMediaType:QTMediaTypeVideo] || 156 if ([device hasMediaType:QTMediaTypeVideo])
74 [device hasMediaType:QTMediaTypeMuxed]) 157 device_info = DeviceInfo::kVideo;
75 ++number_video_devices; 158 else if ([device hasMediaType:QTMediaTypeMuxed])
76 159 device_info = DeviceInfo::kMuxed;
77 if ([device hasMediaType:QTMediaTypeSound] || 160 else if ([device hasMediaType:QTMediaTypeSound])
78 [device hasMediaType:QTMediaTypeMuxed]) 161 device_info = DeviceInfo::kAudio;
79 ++number_audio_devices; 162
80 } 163 snapshot_devices.push_back(
81 164 DeviceInfo([[device uniqueID] UTF8String], device_info));
82 if (number_video_devices_ != number_video_devices) { 165 }
83 number_video_devices_ = number_video_devices; 166
84 monitor_->NotifyDeviceChanged(base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE); 167 ConsolidateDevicesListAndNotify(snapshot_devices);
85 } 168 }
86 169
87 if (number_audio_devices_ != number_audio_devices) { 170 class AVFoundationMonitorImpl : public DeviceMonitorMacImpl {
88 number_audio_devices_ = number_audio_devices; 171 public:
89 monitor_->NotifyDeviceChanged(base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE); 172 explicit AVFoundationMonitorImpl(content::DeviceMonitorMac* monitor);
90 } 173 virtual ~AVFoundationMonitorImpl();
91 } 174
175 virtual void OnDeviceChanged() OVERRIDE;
176 };
177
178 AVFoundationMonitorImpl::AVFoundationMonitorImpl(
179 content::DeviceMonitorMac* monitor)
180 : DeviceMonitorMacImpl(monitor) {
181 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
182
183 device_arrival_ =
184 [nc addObserverForName:AVFoundationGlue::
185 AVCaptureDeviceWasConnectedNotification()
186 object:nil
187 queue:nil
188 usingBlock:^(NSNotification* notification) {
189 OnDeviceChanged();
190 }];
191 device_removal_ =
192 [nc addObserverForName:AVFoundationGlue::
193 AVCaptureDeviceWasDisconnectedNotification()
194 object:nil
195 queue:nil
196 usingBlock:^(NSNotification* notification) {
197 OnDeviceChanged();
198 }];
199 }
200
201 AVFoundationMonitorImpl::~AVFoundationMonitorImpl() {
202 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
203 [nc removeObserver:device_arrival_];
204 [nc removeObserver:device_removal_];
205 }
206
207 void AVFoundationMonitorImpl::OnDeviceChanged() {
208 std::vector<DeviceInfo> snapshot_devices;
209 DeviceInfo::DeviceType device_info;
Robert Sesek 2013/10/22 14:03:50 Same.
mcasas 2013/10/22 18:37:42 Done.
210
211 NSArray* devices = [AVCaptureDeviceGlue devices];
212 for (CrAVCaptureDevice* device in devices) {
213 if ([AVCaptureDeviceGlue hasMediaType:AVFoundationGlue::AVMediaTypeVideo()
214 forCaptureDevice:device]) {
215 device_info = DeviceInfo::kVideo;
216 } else if ([AVCaptureDeviceGlue
217 hasMediaType:AVFoundationGlue::AVMediaTypeMuxed()
218 forCaptureDevice:device]) {
219 device_info = DeviceInfo::kMuxed;
220 } else if ([AVCaptureDeviceGlue
221 hasMediaType:AVFoundationGlue::AVMediaTypeAudio()
222 forCaptureDevice:device]) {
223 device_info = DeviceInfo::kAudio;
224 }
225 snapshot_devices.push_back(DeviceInfo(
226 [[AVCaptureDeviceGlue uniqueID:device] UTF8String], device_info));
227 }
228
229 ConsolidateDevicesListAndNotify(snapshot_devices);
230 }
231
232 } // namespace
233
234 namespace content {
92 235
93 DeviceMonitorMac::DeviceMonitorMac() { 236 DeviceMonitorMac::DeviceMonitorMac() {
94 qt_monitor_.reset(new QTMonitorImpl(this)); 237 if (AVFoundationGlue::IsAVFoundationSupported()) {
95 qt_monitor_->Start(); 238 DVLOG(1) << "Monitoring via AVFoundation";
96 } 239 device_monitor_impl_.reset(new AVFoundationMonitorImpl(this));
97 240 } else {
98 DeviceMonitorMac::~DeviceMonitorMac() { 241 DVLOG(1) << "Monitoring via QTKit";
99 qt_monitor_->Stop(); 242 device_monitor_impl_.reset(new QTKitMonitorImpl(this));
100 } 243 }
244 // Force device enumeration to correctly list those already in the system.
245 device_monitor_impl_->OnDeviceChanged();
246 }
247
248 DeviceMonitorMac::~DeviceMonitorMac() {}
101 249
102 void DeviceMonitorMac::NotifyDeviceChanged( 250 void DeviceMonitorMac::NotifyDeviceChanged(
103 base::SystemMonitor::DeviceType type) { 251 base::SystemMonitor::DeviceType type) {
104 // TODO(xians): Remove the global variable for SystemMonitor. 252 // TODO(xians): Remove the global variable for SystemMonitor.
105 base::SystemMonitor::Get()->ProcessDevicesChanged(type); 253 base::SystemMonitor::Get()->ProcessDevicesChanged(type);
106 } 254 }
107 255
108 } // namespace content 256 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698