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

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: Rebase. 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 MacMonitor {
Robert Sesek 2013/10/21 17:31:36 Hm… maybe this should be more descriptively named?
mcasas 2013/10/21 23:55:25 Done.
44 public:
45 explicit MacMonitor(content::DeviceMonitorMac* monitor)
46 : monitor_(monitor),
47 cached_devices_(),
48 device_arrival_(nil),
49 device_removal_(nil) {
50 DCHECK(monitor);
51 }
52 virtual ~MacMonitor() {}
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_;
Robert Sesek 2013/10/21 17:31:36 nit: blank line after
mcasas 2013/10/21 23:55:25 Done.
29 70 DISALLOW_COPY_AND_ASSIGN(MacMonitor);
30 DISALLOW_COPY_AND_ASSIGN(QTMonitorImpl); 71 };
31 }; 72
32 73 void MacMonitor::ConsolidateDevicesListAndNotify(
33 DeviceMonitorMac::QTMonitorImpl::QTMonitorImpl(DeviceMonitorMac* monitor) 74 const std::vector<DeviceInfo>& snapshot_devices) {
34 : monitor_(monitor), 75 bool video_device_added = false;
35 number_audio_devices_(0), 76 bool audio_device_added = false;
36 number_video_devices_(0), 77 bool video_device_removed = false;
37 device_arrival_(nil), 78 bool audio_device_removed = false;
38 device_removal_(nil) { 79
39 DCHECK(monitor); 80 // Compare the current system devices snapshot with the ones cached to detect
40 } 81 // additions, present in the former but not in the latter. If we find a device
41 82 // in snapshot_devices entry also present in cached_devices, we remove it from
42 void DeviceMonitorMac::QTMonitorImpl::Start() { 83 // the latter vector.
84 std::vector<DeviceInfo>::const_iterator it;
85 for (it = snapshot_devices.begin(); it != snapshot_devices.end(); ++it) {
86 std::vector<DeviceInfo>::iterator cached_devices_iterator =
87 std::find(cached_devices_.begin(), cached_devices_.end(), *it);
88 if (cached_devices_iterator == cached_devices_.end()) {
89 video_device_added |= ((it->type() == DeviceInfo::kVideo) ||
90 (it->type() == DeviceInfo::kMuxed));
91 audio_device_added |= ((it->type() == DeviceInfo::kAudio) ||
92 (it->type() == DeviceInfo::kMuxed));
93 DVLOG(1) << "Device has been added, id: " << it->unique_id();
94 } else {
95 cached_devices_.erase(cached_devices_iterator);
96 }
97 }
98 // All the remaining entries in cached_devices are removed devices.
99 for (it = cached_devices_.begin(); it != cached_devices_.end(); ++it) {
100 video_device_removed |= ((it->type() == DeviceInfo::kVideo) ||
101 (it->type() == DeviceInfo::kMuxed));
102 audio_device_removed |= ((it->type() == DeviceInfo::kAudio) ||
103 (it->type() == DeviceInfo::kMuxed));
104 DVLOG(1) << "Device has been removed, id: " << it->unique_id();
105 }
106 // Update the cached devices with the current system snapshot.
107 cached_devices_ = snapshot_devices;
108
109 if (video_device_added || video_device_removed)
110 monitor_->NotifyDeviceChanged(base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE);
111 if (audio_device_added || video_device_removed)
112 monitor_->NotifyDeviceChanged(base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE);
113 }
114
115 class QTKitMonitorImpl : public MacMonitor {
116 public:
117 explicit QTKitMonitorImpl(content::DeviceMonitorMac* monitor);
118 virtual ~QTKitMonitorImpl();
119
120 virtual void OnDeviceChanged() OVERRIDE;
121 };
122
123 QTKitMonitorImpl::QTKitMonitorImpl(content::DeviceMonitorMac* monitor)
124 : MacMonitor(monitor) {
43 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; 125 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
44 device_arrival_ = 126 device_arrival_ =
45 [nc addObserverForName:QTCaptureDeviceWasConnectedNotification 127 [nc addObserverForName:QTCaptureDeviceWasConnectedNotification
46 object:nil 128 object:nil
47 queue:nil 129 queue:nil
48 usingBlock:^(NSNotification* notification) { 130 usingBlock:^(NSNotification* notification) {
49 OnDeviceChanged();}]; 131 OnDeviceChanged();
132 }];
50 133
51 device_removal_ = 134 device_removal_ =
52 [nc addObserverForName:QTCaptureDeviceWasDisconnectedNotification 135 [nc addObserverForName:QTCaptureDeviceWasDisconnectedNotification
53 object:nil 136 object:nil
54 queue:nil 137 queue:nil
55 usingBlock:^(NSNotification* notification) { 138 usingBlock:^(NSNotification* notification) {
56 OnDeviceChanged();}]; 139 OnDeviceChanged();
57 } 140 }];
58 141 }
59 void DeviceMonitorMac::QTMonitorImpl::Stop() { 142
60 if (!monitor_) 143 QTKitMonitorImpl::~QTKitMonitorImpl() {
61 return;
62
63 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; 144 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
64 [nc removeObserver:device_arrival_]; 145 [nc removeObserver:device_arrival_];
65 [nc removeObserver:device_removal_]; 146 [nc removeObserver:device_removal_];
66 } 147 }
67 148
68 void DeviceMonitorMac::QTMonitorImpl::OnDeviceChanged() { 149 void QTKitMonitorImpl::OnDeviceChanged() {
150 std::vector<DeviceInfo> snapshot_devices;
151 DeviceInfo::DeviceType device_info;
152
69 NSArray* devices = [QTCaptureDevice inputDevices]; 153 NSArray* devices = [QTCaptureDevice inputDevices];
70 int number_video_devices = 0;
71 int number_audio_devices = 0;
72 for (QTCaptureDevice* device in devices) { 154 for (QTCaptureDevice* device in devices) {
73 if ([device hasMediaType:QTMediaTypeVideo] || 155 if ([device hasMediaType:QTMediaTypeVideo])
74 [device hasMediaType:QTMediaTypeMuxed]) 156 device_info = DeviceInfo::kVideo;
75 ++number_video_devices; 157 else if ([device hasMediaType:QTMediaTypeMuxed])
76 158 device_info = DeviceInfo::kMuxed;
77 if ([device hasMediaType:QTMediaTypeSound] || 159 else if ([device hasMediaType:QTMediaTypeSound])
78 [device hasMediaType:QTMediaTypeMuxed]) 160 device_info = DeviceInfo::kAudio;
79 ++number_audio_devices; 161
80 } 162 snapshot_devices.push_back(
81 163 DeviceInfo([[device uniqueID] UTF8String], device_info));
82 if (number_video_devices_ != number_video_devices) { 164 }
83 number_video_devices_ = number_video_devices; 165
84 monitor_->NotifyDeviceChanged(base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE); 166 ConsolidateDevicesListAndNotify(snapshot_devices);
85 } 167 }
86 168
87 if (number_audio_devices_ != number_audio_devices) { 169 class AVFoundationMonitorImpl : public MacMonitor {
88 number_audio_devices_ = number_audio_devices; 170 public:
89 monitor_->NotifyDeviceChanged(base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE); 171 explicit AVFoundationMonitorImpl(content::DeviceMonitorMac* monitor);
90 } 172 virtual ~AVFoundationMonitorImpl();
91 } 173
174 virtual void OnDeviceChanged() OVERRIDE;
175 };
176
177 AVFoundationMonitorImpl::AVFoundationMonitorImpl(
178 content::DeviceMonitorMac* monitor)
179 : MacMonitor(monitor) {
180 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
181
182 device_arrival_ =
183 [nc addObserverForName:AVFoundationGlue::
184 AVCaptureDeviceWasConnectedNotification()
Robert Sesek 2013/10/21 17:31:36 nit: indent 4 more
mcasas 2013/10/21 23:55:25 Done.
185 object:nil
186 queue:nil
187 usingBlock:^(NSNotification* notification) {
188 OnDeviceChanged();
189 }];
190 DCHECK(device_arrival_);
Robert Sesek 2013/10/21 17:31:36 I don't think you need these DCHECKs.
mcasas 2013/10/21 23:55:25 Done.
191 device_removal_ =
192 [nc addObserverForName:AVFoundationGlue::
193 AVCaptureDeviceWasDisconnectedNotification()
Robert Sesek 2013/10/21 17:31:36 nit: indent 4 more
mcasas 2013/10/21 23:55:25 Done.
194 object:nil
195 queue:nil
196 usingBlock:^(NSNotification* notification) {
197 OnDeviceChanged();
198 }];
199 DCHECK(device_removal_);
200 }
201
202 AVFoundationMonitorImpl::~AVFoundationMonitorImpl() {
203 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
204 [nc removeObserver:device_arrival_];
205 [nc removeObserver:device_removal_];
206 }
207
208 void AVFoundationMonitorImpl::OnDeviceChanged() {
209 std::vector<DeviceInfo> snapshot_devices;
210 DeviceInfo::DeviceType device_info;
211
212 NSArray* devices = [AVCaptureDeviceGlue devices];
213 for (CrAVCaptureDevice* device in devices) {
214 if ([AVCaptureDeviceGlue hasMediaType:AVFoundationGlue::AVMediaTypeVideo()
215 forCaptureDevice:device]) {
216 device_info = DeviceInfo::kVideo;
217 } else if ([AVCaptureDeviceGlue
218 hasMediaType:AVFoundationGlue::AVMediaTypeMuxed()
219 forCaptureDevice:device]) {
220 device_info = DeviceInfo::kMuxed;
221 } else if ([AVCaptureDeviceGlue
222 hasMediaType:AVFoundationGlue::AVMediaTypeAudio()
223 forCaptureDevice:device]) {
224 device_info = DeviceInfo::kAudio;
225 }
226 snapshot_devices.push_back(DeviceInfo(
227 [[AVCaptureDeviceGlue uniqueID:device] UTF8String], device_info));
228 }
229
230 ConsolidateDevicesListAndNotify(snapshot_devices);
231 }
232
233 } // namespace
234
235 namespace content {
92 236
93 DeviceMonitorMac::DeviceMonitorMac() { 237 DeviceMonitorMac::DeviceMonitorMac() {
94 qt_monitor_.reset(new QTMonitorImpl(this)); 238 if (AVFoundationGlue::IsAVFoundationSupported()) {
95 qt_monitor_->Start(); 239 DVLOG(1) << "Monitoring via AVFoundation";
96 } 240 device_monitor_impl_.reset(new AVFoundationMonitorImpl(this));
97 241 } else {
98 DeviceMonitorMac::~DeviceMonitorMac() { 242 DVLOG(1) << "Monitoring via QTKit";
99 qt_monitor_->Stop(); 243 device_monitor_impl_.reset(new QTKitMonitorImpl(this));
100 } 244 }
245 // Force device enumeration to correctly list those already in the system.
246 device_monitor_impl_->OnDeviceChanged();
247 }
248
249 DeviceMonitorMac::~DeviceMonitorMac() {}
101 250
102 void DeviceMonitorMac::NotifyDeviceChanged( 251 void DeviceMonitorMac::NotifyDeviceChanged(
103 base::SystemMonitor::DeviceType type) { 252 base::SystemMonitor::DeviceType type) {
104 // TODO(xians): Remove the global variable for SystemMonitor. 253 // TODO(xians): Remove the global variable for SystemMonitor.
105 base::SystemMonitor::Get()->ProcessDevicesChanged(type); 254 base::SystemMonitor::Get()->ProcessDevicesChanged(type);
106 } 255 }
107 256
108 } // namespace content 257 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698