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

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: 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 #include "media/video/capture/mac/avfoundation_glue.h"
10 11
11 namespace content { 12 namespace content {
12 13
13 class DeviceMonitorMac::QTMonitorImpl { 14 // Interface used by DeviceMonitorMac to interact with either a QTKit or an
15 // AVFoundation implementation of events and notifications.
16 class DeviceMonitorMac::MacMonitorInterface {
14 public: 17 public:
15 explicit QTMonitorImpl(DeviceMonitorMac* monitor); 18 MacMonitorInterface() {};
16 virtual ~QTMonitorImpl() {} 19 MacMonitorInterface(DeviceMonitorMac* monitor)
Mark Mentovai 2013/09/26 17:09:10 This constructor should be explicit. http://google
mcasas 2013/09/30 17:53:50 Done.
20 : monitor_(monitor),
21 number_audio_devices_(0),
22 number_video_devices_(0),
23 device_arrival_(nil),
Mark Mentovai 2013/09/26 17:09:10 nil is an initializer for an Objective-C object po
mcasas 2013/09/30 17:53:50 Done.
24 device_removal_(nil) {};
25 virtual ~MacMonitorInterface() = 0;
17 26
18 void Start(); 27 protected:
19 void Stop(); 28 virtual void OnDeviceChanged() = 0;
Mark Mentovai 2013/09/26 17:09:10 Weird indentation in this entire protected section
mcasas 2013/09/30 17:53:50 Done.
20 29
21 private: 30 DeviceMonitorMac* monitor_;
Mark Mentovai 2013/09/26 17:09:10 Protected data is almost unseen, even in interface
mcasas 2013/09/30 17:53:50 Done.
22 void OnDeviceChanged(); 31 int number_audio_devices_;
32 int number_video_devices_;
33 int device_arrival_;
34 int device_removal_;
23 35
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 }; 36 };
32 37
33 DeviceMonitorMac::QTMonitorImpl::QTMonitorImpl(DeviceMonitorMac* monitor) 38 DeviceMonitorMac::MacMonitorInterface::~MacMonitorInterface() { }
Mark Mentovai 2013/09/26 17:09:10 In the class declaration above, you made the destr
mcasas 2013/09/30 17:53:50 Gone.
34 : monitor_(monitor), 39
35 number_audio_devices_(0), 40 class DeviceMonitorMac::QTKitMonitorImpl : public MacMonitorInterface {
36 number_video_devices_(0), 41 public:
37 device_arrival_(nil), 42 QTKitMonitorImpl(DeviceMonitorMac* monitor);
Mark Mentovai 2013/09/26 17:09:10 explicit.
mcasas 2013/09/30 17:53:50 Done.
38 device_removal_(nil) { 43 virtual ~QTKitMonitorImpl();
44
45 virtual void OnDeviceChanged() OVERRIDE;
46 };
47
48 DeviceMonitorMac::QTKitMonitorImpl::QTKitMonitorImpl(DeviceMonitorMac* monitor)
49 : MacMonitorInterface(monitor) {
39 DCHECK(monitor); 50 DCHECK(monitor);
40 }
41 51
42 void DeviceMonitorMac::QTMonitorImpl::Start() {
43 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; 52 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
44 device_arrival_ = 53 device_arrival_ = (int)
Mark Mentovai 2013/09/26 17:09:10 No, you definitely can’t cast id to int. I don’t k
mcasas 2013/09/30 17:53:50 Without wanting to escape my public defamation, I'
45 [nc addObserverForName:QTCaptureDeviceWasConnectedNotification 54 [nc addObserverForName:QTCaptureDeviceWasConnectedNotification
46 object:nil 55 object:nil
47 queue:nil 56 queue:nil
48 usingBlock:^(NSNotification* notification) { 57 usingBlock:^(NSNotification* notification) {
49 OnDeviceChanged();}]; 58 OnDeviceChanged();}];
50 59
51 device_removal_ = 60 device_removal_ = (int)
52 [nc addObserverForName:QTCaptureDeviceWasDisconnectedNotification 61 [nc addObserverForName:QTCaptureDeviceWasDisconnectedNotification
53 object:nil 62 object:nil
54 queue:nil 63 queue:nil
55 usingBlock:^(NSNotification* notification) { 64 usingBlock:^(NSNotification* notification) {
56 OnDeviceChanged();}]; 65 OnDeviceChanged();}];
57 } 66 }
58 67
59 void DeviceMonitorMac::QTMonitorImpl::Stop() { 68 DeviceMonitorMac::QTKitMonitorImpl::~QTKitMonitorImpl() {
60 if (!monitor_) 69 if (!monitor_)
61 return; 70 return;
62 71
63 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; 72 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
64 [nc removeObserver:device_arrival_]; 73 [nc removeObserver:(id)device_arrival_];
65 [nc removeObserver:device_removal_]; 74 [nc removeObserver:(id)device_removal_];
66 } 75 }
67 76
68 void DeviceMonitorMac::QTMonitorImpl::OnDeviceChanged() { 77 void DeviceMonitorMac::QTKitMonitorImpl::OnDeviceChanged() {
69 NSArray* devices = [QTCaptureDevice inputDevices]; 78 NSArray* devices = [QTCaptureDevice inputDevices];
70 int number_video_devices = 0; 79 int number_video_devices = 0;
71 int number_audio_devices = 0; 80 int number_audio_devices = 0;
72 for (QTCaptureDevice* device in devices) { 81 for (QTCaptureDevice* device in devices) {
73 if ([device hasMediaType:QTMediaTypeVideo] || 82 if ([device hasMediaType:QTMediaTypeVideo] ||
74 [device hasMediaType:QTMediaTypeMuxed]) 83 [device hasMediaType:QTMediaTypeMuxed])
75 ++number_video_devices; 84 ++number_video_devices;
76 85
77 if ([device hasMediaType:QTMediaTypeSound] || 86 if ([device hasMediaType:QTMediaTypeSound] ||
78 [device hasMediaType:QTMediaTypeMuxed]) 87 [device hasMediaType:QTMediaTypeMuxed])
79 ++number_audio_devices; 88 ++number_audio_devices;
80 } 89 }
81 90
82 if (number_video_devices_ != number_video_devices) { 91 if (number_video_devices_ != number_video_devices) {
83 number_video_devices_ = number_video_devices; 92 number_video_devices_ = number_video_devices;
84 monitor_->NotifyDeviceChanged(base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE); 93 monitor_->NotifyDeviceChanged(base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE);
85 } 94 }
86 95
87 if (number_audio_devices_ != number_audio_devices) { 96 if (number_audio_devices_ != number_audio_devices) {
88 number_audio_devices_ = number_audio_devices; 97 number_audio_devices_ = number_audio_devices;
89 monitor_->NotifyDeviceChanged(base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE); 98 monitor_->NotifyDeviceChanged(base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE);
90 } 99 }
91 } 100 }
92 101
102 class DeviceMonitorMac::AVFoundationMonitorImpl : public MacMonitorInterface {
103 public:
104 AVFoundationMonitorImpl(DeviceMonitorMac* monitor);
Mark Mentovai 2013/09/26 17:09:10 explicit
mcasas 2013/09/30 17:53:50 Done.
105 virtual ~AVFoundationMonitorImpl();
106
107 virtual void OnDeviceChanged() OVERRIDE;
108 };
109
110 DeviceMonitorMac::AVFoundationMonitorImpl::AVFoundationMonitorImpl(
111 DeviceMonitorMac* monitor)
Mark Mentovai 2013/09/26 17:09:10 This line would be a 4-space continuation line ind
mcasas 2013/09/30 17:53:50 Done.
112 : MacMonitorInterface(monitor) {
113 DCHECK(monitor);
114
115 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
116 device_arrival_ = (int)
117 [nc addObserverForName:AVCaptureDeviceWasConnectedNotification
118 object:nil
119 queue:nil
120 usingBlock:^(NSNotification* notification) {
121 OnDeviceChanged();}];
122
123 device_removal_ = (int)
124 [nc addObserverForName:AVCaptureDeviceWasDisconnectedNotification
125 object:nil
126 queue:nil
127 usingBlock:^(NSNotification* notification) {
128 OnDeviceChanged();}];
129 }
130
131 DeviceMonitorMac::AVFoundationMonitorImpl::~AVFoundationMonitorImpl() {
132 if (!monitor_)
133 return;
134
135 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
136 [nc removeObserver:(id)device_arrival_];
137 [nc removeObserver:(id)device_removal_];
138 }
139
140 void DeviceMonitorMac::AVFoundationMonitorImpl::OnDeviceChanged() {
141 NSArray* devices = [AVCaptureDeviceGlue devices];
142 int number_video_devices = 0;
143 int number_audio_devices = 0;
144 for (AVCaptureDeviceGlue* device in devices) {
Mark Mentovai 2013/09/26 17:09:10 Is it possible for the actual devices to change wi
mcasas 2013/09/30 17:53:50 I thought this is addressed by the "usingBlock" in
145 if ([device hasMediaType:AVMediaTypeVideo] ||
146 [device hasMediaType:AVMediaTypeMuxed])
147 ++number_video_devices;
148
149 if ([device hasMediaType:AVMediaTypeAudio] ||
150 [device hasMediaType:AVMediaTypeMuxed])
151 ++number_audio_devices;
152 }
153
154 if (number_video_devices_ != number_video_devices) {
155 number_video_devices_ = number_video_devices;
156 monitor_->NotifyDeviceChanged(base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE);
157 }
158
159 if (number_audio_devices_ != number_audio_devices) {
160 number_audio_devices_ = number_audio_devices;
161 monitor_->NotifyDeviceChanged(base::SystemMonitor::DEVTYPE_AUDIO_CAPTURE);
162 }
163 }
164
93 DeviceMonitorMac::DeviceMonitorMac() { 165 DeviceMonitorMac::DeviceMonitorMac() {
94 qt_monitor_.reset(new QTMonitorImpl(this)); 166 if ([AVFoundationGlue IsAVFoundationSupported]){
Mark Mentovai 2013/09/26 17:09:10 Method names (even class method names, what would
mcasas 2013/09/30 17:53:50 Happy to do so. I was following the style in: IsO
95 qt_monitor_->Start(); 167 device_monitor_impl_.reset(new AVFoundationMonitorImpl(this));
168 } else {
169 DVLOG(1) << "Monitoring via QTKit";
170 device_monitor_impl_.reset(new QTKitMonitorImpl(this));
171 }
96 } 172 }
97 173
98 DeviceMonitorMac::~DeviceMonitorMac() { 174 DeviceMonitorMac::~DeviceMonitorMac() {}
99 qt_monitor_->Stop();
100 }
101 175
102 void DeviceMonitorMac::NotifyDeviceChanged( 176 void DeviceMonitorMac::NotifyDeviceChanged(
103 base::SystemMonitor::DeviceType type) { 177 base::SystemMonitor::DeviceType type) {
104 // TODO(xians): Remove the global variable for SystemMonitor. 178 // TODO(xians): Remove the global variable for SystemMonitor.
105 base::SystemMonitor::Get()->ProcessDevicesChanged(type); 179 base::SystemMonitor::Get()->ProcessDevicesChanged(type);
106 } 180 }
107 181
108 } // namespace content 182 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698