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

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: avi@ comments. 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
« no previous file with comments | « content/browser/device_monitor_mac.h ('k') | media/media.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 kUnknown
22 };
23
24 DeviceInfo(std::string unique_id, DeviceType type)
25 : unique_id_(unique_id), type_(type) {}
26
27 // Operator== is needed here to use this class in a std::find.
28 bool operator==(const DeviceInfo& device) const {
29 return unique_id_ == device.unique_id_;
Avi (use Gerrit) 2013/10/23 16:01:01 Can you put your explanation as to why you don't c
30 }
31
32 const std::string& unique_id() const { return unique_id_; }
33 DeviceType type() const { return type_; }
20 34
21 private: 35 private:
22 void OnDeviceChanged(); 36 std::string unique_id_;
23 37 DeviceType type_;
24 DeviceMonitorMac* monitor_; 38 // Allow generated copy constructor and assignment.
25 int number_audio_devices_; 39 };
26 int number_video_devices_; 40
41 // Base abstract class used by DeviceMonitorMac to interact with either a QTKit
42 // or an AVFoundation implementation of events and notifications.
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 private:
31 }; 72 DISALLOW_COPY_AND_ASSIGN(DeviceMonitorMacImpl);
32 73 };
33 DeviceMonitorMac::QTMonitorImpl::QTMonitorImpl(DeviceMonitorMac* monitor) 74
34 : monitor_(monitor), 75 void DeviceMonitorMacImpl::ConsolidateDevicesListAndNotify(
35 number_audio_devices_(0), 76 const std::vector<DeviceInfo>& snapshot_devices) {
36 number_video_devices_(0), 77 bool video_device_added = false;
37 device_arrival_(nil), 78 bool audio_device_added = false;
38 device_removal_(nil) { 79 bool video_device_removed = false;
39 DCHECK(monitor); 80 bool audio_device_removed = false;
40 } 81
41 82 // Compare the current system devices snapshot with the ones cached to detect
42 void DeviceMonitorMac::QTMonitorImpl::Start() { 83 // additions, present in the former but not in the latter. If we find a device
84 // in snapshot_devices entry also present in cached_devices, we remove it from
85 // the latter vector.
86 std::vector<DeviceInfo>::const_iterator it;
87 for (it = snapshot_devices.begin(); it != snapshot_devices.end(); ++it) {
88 std::vector<DeviceInfo>::iterator cached_devices_iterator =
89 std::find(cached_devices_.begin(), cached_devices_.end(), *it);
90 if (cached_devices_iterator == cached_devices_.end()) {
91 video_device_added |= ((it->type() == DeviceInfo::kVideo) ||
92 (it->type() == DeviceInfo::kMuxed));
93 audio_device_added |= ((it->type() == DeviceInfo::kAudio) ||
94 (it->type() == DeviceInfo::kMuxed));
95 DVLOG(1) << "Device has been added, id: " << it->unique_id();
96 } else {
97 cached_devices_.erase(cached_devices_iterator);
98 }
99 }
100 // All the remaining entries in cached_devices are removed devices.
101 for (it = cached_devices_.begin(); it != cached_devices_.end(); ++it) {
102 video_device_removed |= ((it->type() == DeviceInfo::kVideo) ||
103 (it->type() == DeviceInfo::kMuxed));
104 audio_device_removed |= ((it->type() == DeviceInfo::kAudio) ||
105 (it->type() == DeviceInfo::kMuxed));
106 DVLOG(1) << "Device has been removed, id: " << it->unique_id();
107 }
108 // Update the cached devices with the current system snapshot.
109 cached_devices_ = snapshot_devices;
110
111 if (video_device_added || video_device_removed)
112 monitor_->NotifyDeviceChanged(base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE);
113 if (audio_device_added || video_device_removed)
114 monitor_->NotifyDeviceChanged(base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE);
115 }
116
117 class QTKitMonitorImpl : public DeviceMonitorMacImpl {
118 public:
119 explicit QTKitMonitorImpl(content::DeviceMonitorMac* monitor);
120 virtual ~QTKitMonitorImpl();
121
122 virtual void OnDeviceChanged() OVERRIDE;
123 };
124
125 QTKitMonitorImpl::QTKitMonitorImpl(content::DeviceMonitorMac* monitor)
126 : DeviceMonitorMacImpl(monitor) {
43 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; 127 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
44 device_arrival_ = 128 device_arrival_ =
45 [nc addObserverForName:QTCaptureDeviceWasConnectedNotification 129 [nc addObserverForName:QTCaptureDeviceWasConnectedNotification
46 object:nil 130 object:nil
47 queue:nil 131 queue:nil
48 usingBlock:^(NSNotification* notification) { 132 usingBlock:^(NSNotification* notification) {
49 OnDeviceChanged();}]; 133 OnDeviceChanged();
134 }];
50 135
51 device_removal_ = 136 device_removal_ =
52 [nc addObserverForName:QTCaptureDeviceWasDisconnectedNotification 137 [nc addObserverForName:QTCaptureDeviceWasDisconnectedNotification
53 object:nil 138 object:nil
54 queue:nil 139 queue:nil
55 usingBlock:^(NSNotification* notification) { 140 usingBlock:^(NSNotification* notification) {
56 OnDeviceChanged();}]; 141 OnDeviceChanged();
57 } 142 }];
58 143 }
59 void DeviceMonitorMac::QTMonitorImpl::Stop() { 144
60 if (!monitor_) 145 QTKitMonitorImpl::~QTKitMonitorImpl() {
61 return;
62
63 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; 146 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
64 [nc removeObserver:device_arrival_]; 147 [nc removeObserver:device_arrival_];
65 [nc removeObserver:device_removal_]; 148 [nc removeObserver:device_removal_];
66 } 149 }
67 150
68 void DeviceMonitorMac::QTMonitorImpl::OnDeviceChanged() { 151 void QTKitMonitorImpl::OnDeviceChanged() {
152 std::vector<DeviceInfo> snapshot_devices;
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 DeviceInfo::DeviceType device_type = DeviceInfo::kUnknown;
74 [device hasMediaType:QTMediaTypeMuxed]) 157 if ([device hasMediaType:QTMediaTypeVideo])
75 ++number_video_devices; 158 device_type = DeviceInfo::kVideo;
76 159 else if ([device hasMediaType:QTMediaTypeMuxed])
77 if ([device hasMediaType:QTMediaTypeSound] || 160 device_type = DeviceInfo::kMuxed;
78 [device hasMediaType:QTMediaTypeMuxed]) 161 else if ([device hasMediaType:QTMediaTypeSound])
79 ++number_audio_devices; 162 device_type = DeviceInfo::kAudio;
80 } 163
81 164 snapshot_devices.push_back(
82 if (number_video_devices_ != number_video_devices) { 165 DeviceInfo([[device uniqueID] UTF8String], device_type));
83 number_video_devices_ = number_video_devices; 166 }
84 monitor_->NotifyDeviceChanged(base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE); 167
85 } 168 ConsolidateDevicesListAndNotify(snapshot_devices);
86 169 }
87 if (number_audio_devices_ != number_audio_devices) { 170
88 number_audio_devices_ = number_audio_devices; 171 class AVFoundationMonitorImpl : public DeviceMonitorMacImpl {
89 monitor_->NotifyDeviceChanged(base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE); 172 public:
90 } 173 explicit AVFoundationMonitorImpl(content::DeviceMonitorMac* monitor);
91 } 174 virtual ~AVFoundationMonitorImpl();
175
176 virtual void OnDeviceChanged() OVERRIDE;
177 };
178
179 AVFoundationMonitorImpl::AVFoundationMonitorImpl(
180 content::DeviceMonitorMac* monitor)
181 : DeviceMonitorMacImpl(monitor) {
182 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
183
184 device_arrival_ =
185 [nc addObserverForName:AVFoundationGlue::
186 AVCaptureDeviceWasConnectedNotification()
187 object:nil
188 queue:nil
189 usingBlock:^(NSNotification* notification) {
190 OnDeviceChanged();
191 }];
192 device_removal_ =
193 [nc addObserverForName:AVFoundationGlue::
194 AVCaptureDeviceWasDisconnectedNotification()
195 object:nil
196 queue:nil
197 usingBlock:^(NSNotification* notification) {
198 OnDeviceChanged();
199 }];
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
211 NSArray* devices = [AVCaptureDeviceGlue devices];
212 for (CrAVCaptureDevice* device in devices) {
213 DeviceInfo::DeviceType device_type = DeviceInfo::kUnknown;
214 if ([AVCaptureDeviceGlue hasMediaType:AVFoundationGlue::AVMediaTypeVideo()
215 forCaptureDevice:device]) {
216 device_type = DeviceInfo::kVideo;
217 } else if ([AVCaptureDeviceGlue
218 hasMediaType:AVFoundationGlue::AVMediaTypeMuxed()
219 forCaptureDevice:device]) {
220 device_type = DeviceInfo::kMuxed;
221 } else if ([AVCaptureDeviceGlue
222 hasMediaType:AVFoundationGlue::AVMediaTypeAudio()
223 forCaptureDevice:device]) {
224 device_type = DeviceInfo::kAudio;
225 }
226 snapshot_devices.push_back(DeviceInfo(
227 [[AVCaptureDeviceGlue uniqueID:device] UTF8String], device_type));
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
« no previous file with comments | « content/browser/device_monitor_mac.h ('k') | media/media.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698