OLD | NEW |
---|---|
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 <set> | 9 #include <set> |
10 | 10 |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/mac/bind_objc_block.h" | |
13 #include "base/mac/scoped_nsobject.h" | 14 #include "base/mac/scoped_nsobject.h" |
14 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
15 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
16 #import "media/video/capture/mac/avfoundation_glue.h" | 17 #import "media/video/capture/mac/avfoundation_glue.h" |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 // This class is used to keep track of system devices names and their types. | 21 // This class is used to keep track of system devices names and their types. |
21 class DeviceInfo { | 22 class DeviceInfo { |
22 public: | 23 public: |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
209 ConsolidateDevicesListAndNotify(snapshot_devices); | 210 ConsolidateDevicesListAndNotify(snapshot_devices); |
210 } | 211 } |
211 | 212 |
212 // Forward declaration for use by CrAVFoundationDeviceObserver. | 213 // Forward declaration for use by CrAVFoundationDeviceObserver. |
213 class SuspendObserverDelegate; | 214 class SuspendObserverDelegate; |
214 | 215 |
215 } // namespace | 216 } // namespace |
216 | 217 |
217 // This class is a Key-Value Observer (KVO) shim. It is needed because C++ | 218 // This class is a Key-Value Observer (KVO) shim. It is needed because C++ |
218 // classes cannot observe Key-Values directly. Created, manipulated, and | 219 // classes cannot observe Key-Values directly. Created, manipulated, and |
219 // destroyed on the Device Thread by SuspendedObserverDelegate. | 220 // destroyed on the UI Thread by SuspendObserverDelegate. |
220 @interface CrAVFoundationDeviceObserver : NSObject { | 221 @interface CrAVFoundationDeviceObserver : NSObject { |
221 @private | 222 @private |
222 SuspendObserverDelegate* receiver_; // weak | 223 SuspendObserverDelegate* receiver_; // weak |
223 // Member to keep track of the devices we are already monitoring. | 224 // Member to keep track of the devices we are already monitoring. |
224 std::set<CrAVCaptureDevice*> monitoredDevices_; | 225 std::set<CrAVCaptureDevice*> monitoredDevices_; |
225 } | 226 } |
226 | 227 |
227 - (id)initWithChangeReceiver:(SuspendObserverDelegate*)receiver; | 228 - (id)initWithChangeReceiver:(SuspendObserverDelegate*)receiver; |
228 - (void)startObserving:(CrAVCaptureDevice*)device; | 229 - (void)startObserving:(CrAVCaptureDevice*)device; |
229 - (void)stopObserving:(CrAVCaptureDevice*)device; | 230 - (void)stopObserving:(CrAVCaptureDevice*)device; |
230 | 231 |
231 @end | 232 @end |
232 | 233 |
233 namespace { | 234 namespace { |
234 | 235 |
235 // This class owns and manages the lifetime of a CrAVFoundationDeviceObserver. | 236 // This class owns and manages the lifetime of a CrAVFoundationDeviceObserver. |
236 // Provides a callback for this device observer to indicate that there has been | 237 // AVFoundationMonitorImpl creates and destroys it in UI thread, but otherwise |
237 // a device change of some kind. Created by AVFoundationMonitorImpl in UI thread | 238 // operates in Device Thread. Notably, its |suspend_observer_| lives in UI |
238 // but living in Device Thread. | 239 // thread as well; this observer calls back to indicate that there has been a |
240 // device change of some kind. | |
239 class SuspendObserverDelegate : | 241 class SuspendObserverDelegate : |
240 public base::RefCountedThreadSafe<SuspendObserverDelegate> { | 242 public base::RefCountedThreadSafe<SuspendObserverDelegate> { |
241 public: | 243 public: |
242 explicit SuspendObserverDelegate(DeviceMonitorMacImpl* monitor) | 244 explicit SuspendObserverDelegate(DeviceMonitorMacImpl* monitor) |
243 : avfoundation_monitor_impl_(monitor) { | 245 : avfoundation_monitor_impl_(monitor) { |
244 device_thread_checker_.DetachFromThread(); | 246 device_thread_checker_.DetachFromThread(); |
245 } | 247 } |
246 | 248 |
247 void OnDeviceChanged(); | 249 void OnDeviceChanged(); |
248 void StartObserver(); | 250 void StartObserver(); |
249 void ResetDeviceMonitorOnUIThread(); | 251 void ResetDeviceMonitorOnUIThread(); |
250 | 252 |
251 private: | 253 private: |
252 friend class base::RefCountedThreadSafe<SuspendObserverDelegate>; | 254 friend class base::RefCountedThreadSafe<SuspendObserverDelegate>; |
253 | 255 |
254 virtual ~SuspendObserverDelegate() {} | 256 virtual ~SuspendObserverDelegate() { |
257 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
258 } | |
255 | 259 |
256 void OnDeviceChangedOnUIThread( | 260 void OnDeviceChangedOnUIThread( |
257 const std::vector<DeviceInfo>& snapshot_devices); | 261 const std::vector<DeviceInfo>& snapshot_devices); |
258 | 262 |
259 base::ThreadChecker device_thread_checker_; | 263 base::ThreadChecker device_thread_checker_; |
264 // Created, used and released in UI thread. | |
260 base::scoped_nsobject<CrAVFoundationDeviceObserver> suspend_observer_; | 265 base::scoped_nsobject<CrAVFoundationDeviceObserver> suspend_observer_; |
261 DeviceMonitorMacImpl* avfoundation_monitor_impl_; | 266 DeviceMonitorMacImpl* avfoundation_monitor_impl_; |
262 }; | 267 }; |
263 | 268 |
264 void SuspendObserverDelegate::OnDeviceChanged() { | 269 void SuspendObserverDelegate::OnDeviceChanged() { |
265 DCHECK(device_thread_checker_.CalledOnValidThread()); | 270 DCHECK(device_thread_checker_.CalledOnValidThread()); |
266 NSArray* devices = [AVCaptureDeviceGlue devices]; | 271 NSArray* devices = [AVCaptureDeviceGlue devices]; |
267 std::vector<DeviceInfo> snapshot_devices; | 272 std::vector<DeviceInfo> snapshot_devices; |
268 for (CrAVCaptureDevice* device in devices) { | 273 for (CrAVCaptureDevice* device in devices) { |
269 [suspend_observer_ startObserving:device]; | 274 //[suspend_observer_ startObserving:device]; |
275 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
276 base::BindBlock(^{ [suspend_observer_ startObserving:device]; })); | |
270 BOOL suspended = [device respondsToSelector:@selector(isSuspended)] && | 277 BOOL suspended = [device respondsToSelector:@selector(isSuspended)] && |
271 [device isSuspended]; | 278 [device isSuspended]; |
272 DeviceInfo::DeviceType device_type = DeviceInfo::kUnknown; | 279 DeviceInfo::DeviceType device_type = DeviceInfo::kUnknown; |
273 if ([device hasMediaType:AVFoundationGlue::AVMediaTypeVideo()]) { | 280 if ([device hasMediaType:AVFoundationGlue::AVMediaTypeVideo()]) { |
274 if (suspended) | 281 if (suspended) |
275 continue; | 282 continue; |
276 device_type = DeviceInfo::kVideo; | 283 device_type = DeviceInfo::kVideo; |
277 } else if ([device hasMediaType:AVFoundationGlue::AVMediaTypeMuxed()]) { | 284 } else if ([device hasMediaType:AVFoundationGlue::AVMediaTypeMuxed()]) { |
278 device_type = suspended ? DeviceInfo::kAudio : DeviceInfo::kMuxed; | 285 device_type = suspended ? DeviceInfo::kAudio : DeviceInfo::kMuxed; |
279 } else if ([device hasMediaType:AVFoundationGlue::AVMediaTypeAudio()]) { | 286 } else if ([device hasMediaType:AVFoundationGlue::AVMediaTypeAudio()]) { |
280 device_type = DeviceInfo::kAudio; | 287 device_type = DeviceInfo::kAudio; |
281 } | 288 } |
282 snapshot_devices.push_back(DeviceInfo([[device uniqueID] UTF8String], | 289 snapshot_devices.push_back(DeviceInfo([[device uniqueID] UTF8String], |
283 device_type)); | 290 device_type)); |
284 } | 291 } |
285 // Post the consolidation of enumerated devices to be done on UI thread. | 292 // Post the consolidation of enumerated devices to be done on UI thread. |
286 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 293 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
287 base::Bind(&SuspendObserverDelegate::OnDeviceChangedOnUIThread, | 294 base::Bind(&SuspendObserverDelegate::OnDeviceChangedOnUIThread, |
288 this, snapshot_devices)); | 295 this, snapshot_devices)); |
289 } | 296 } |
290 | 297 |
291 void SuspendObserverDelegate::StartObserver() { | 298 void SuspendObserverDelegate::StartObserver() { |
292 DCHECK(device_thread_checker_.CalledOnValidThread()); | 299 DCHECK(device_thread_checker_.CalledOnValidThread()); |
293 suspend_observer_.reset([[CrAVFoundationDeviceObserver alloc] | 300 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
294 initWithChangeReceiver:this]); | 301 base::BindBlock(^{ |
Robert Sesek
2014/07/02 19:44:45
nit: indent 2 more spaces
mcasas
2014/07/03 12:03:25
Done.
| |
295 for (CrAVCaptureDevice* device in [AVCaptureDeviceGlue devices]) | 302 suspend_observer_.reset([[CrAVFoundationDeviceObserver alloc] |
Robert Sesek
2014/07/02 19:44:45
This seems like questionably unsafe data access.
mcasas
2014/07/03 12:03:25
I'm not sure I understand. Isn't it the same as wh
| |
296 [suspend_observer_ startObserving:device]; | 303 initWithChangeReceiver:this]); })); |
Robert Sesek
2014/07/02 19:44:45
Closing brace for the block should go on the next
mcasas
2014/07/03 12:03:25
Done.
| |
304 for (CrAVCaptureDevice* device in [AVCaptureDeviceGlue devices]) { | |
305 //[suspend_observer_ startObserving:device]; | |
306 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
307 base::BindBlock(^{ [suspend_observer_ startObserving:device]; })); | |
308 } | |
297 } | 309 } |
298 | 310 |
299 void SuspendObserverDelegate::ResetDeviceMonitorOnUIThread() { | 311 void SuspendObserverDelegate::ResetDeviceMonitorOnUIThread() { |
300 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 312 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
301 avfoundation_monitor_impl_ = NULL; | 313 avfoundation_monitor_impl_ = NULL; |
302 } | 314 } |
303 | 315 |
304 void SuspendObserverDelegate::OnDeviceChangedOnUIThread( | 316 void SuspendObserverDelegate::OnDeviceChangedOnUIThread( |
305 const std::vector<DeviceInfo>& snapshot_devices) { | 317 const std::vector<DeviceInfo>& snapshot_devices) { |
306 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 318 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
378 device_task_runner_->PostTask(FROM_HERE, | 390 device_task_runner_->PostTask(FROM_HERE, |
379 base::Bind(&SuspendObserverDelegate::OnDeviceChanged, | 391 base::Bind(&SuspendObserverDelegate::OnDeviceChanged, |
380 suspend_observer_delegate_)); | 392 suspend_observer_delegate_)); |
381 } | 393 } |
382 | 394 |
383 } // namespace | 395 } // namespace |
384 | 396 |
385 @implementation CrAVFoundationDeviceObserver | 397 @implementation CrAVFoundationDeviceObserver |
386 | 398 |
387 - (id)initWithChangeReceiver:(SuspendObserverDelegate*)receiver { | 399 - (id)initWithChangeReceiver:(SuspendObserverDelegate*)receiver { |
400 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
388 if ((self = [super init])) { | 401 if ((self = [super init])) { |
389 DCHECK(receiver != NULL); | 402 DCHECK(receiver != NULL); |
390 receiver_ = receiver; | 403 receiver_ = receiver; |
391 } | 404 } |
392 return self; | 405 return self; |
393 } | 406 } |
394 | 407 |
395 - (void)dealloc { | 408 - (void)dealloc { |
409 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
396 std::set<CrAVCaptureDevice*>::iterator it = monitoredDevices_.begin(); | 410 std::set<CrAVCaptureDevice*>::iterator it = monitoredDevices_.begin(); |
397 while (it != monitoredDevices_.end()) | 411 while (it != monitoredDevices_.end()) { |
398 [self stopObserving:*it++]; | 412 [self removeObservers:*it]; |
413 monitoredDevices_.erase(it++); | |
414 } | |
399 [super dealloc]; | 415 [super dealloc]; |
400 } | 416 } |
401 | 417 |
402 - (void)startObserving:(CrAVCaptureDevice*)device { | 418 - (void)startObserving:(CrAVCaptureDevice*)device { |
419 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
403 DCHECK(device != nil); | 420 DCHECK(device != nil); |
404 // Skip this device if there are already observers connected to it. | 421 // Skip this device if there are already observers connected to it. |
405 if (std::find(monitoredDevices_.begin(), monitoredDevices_.end(), device) != | 422 if (std::find(monitoredDevices_.begin(), monitoredDevices_.end(), device) != |
406 monitoredDevices_.end()) { | 423 monitoredDevices_.end()) { |
407 return; | 424 return; |
408 } | 425 } |
409 [device addObserver:self | 426 [device addObserver:self |
410 forKeyPath:@"suspended" | 427 forKeyPath:@"suspended" |
411 options:0 | 428 options:0 |
412 context:device]; | 429 context:device]; |
413 [device addObserver:self | 430 [device addObserver:self |
414 forKeyPath:@"connected" | 431 forKeyPath:@"connected" |
415 options:0 | 432 options:0 |
416 context:device]; | 433 context:device]; |
417 monitoredDevices_.insert(device); | 434 monitoredDevices_.insert(device); |
418 } | 435 } |
419 | 436 |
420 - (void)stopObserving:(CrAVCaptureDevice*)device { | 437 - (void)stopObserving:(CrAVCaptureDevice*)device { |
438 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
421 DCHECK(device != nil); | 439 DCHECK(device != nil); |
422 std::set<CrAVCaptureDevice*>::iterator found = | 440 std::set<CrAVCaptureDevice*>::iterator found = |
423 std::find(monitoredDevices_.begin(), monitoredDevices_.end(), device); | 441 std::find(monitoredDevices_.begin(), monitoredDevices_.end(), device); |
424 DCHECK(found != monitoredDevices_.end()); | 442 DCHECK(found != monitoredDevices_.end()); |
425 // Every so seldom, |device| might be gone when getting here, in that case | 443 [self removeObservers:*found]; |
426 // removing the observer causes a crash. Try to avoid it by checking sanity of | 444 monitoredDevices_.erase(found); |
427 // the |device| via its -observationInfo. http://crbug.com/371271. | 445 } |
446 | |
447 - (void)removeObservers:(CrAVCaptureDevice*)device { | |
448 // Check sanity of |device| via its -observationInfo. http://crbug.com/371271. | |
428 if ([device observationInfo]) { | 449 if ([device observationInfo]) { |
429 [device removeObserver:self | 450 [device removeObserver:self |
430 forKeyPath:@"suspended"]; | 451 forKeyPath:@"suspended"]; |
431 [device removeObserver:self | 452 [device removeObserver:self |
432 forKeyPath:@"connected"]; | 453 forKeyPath:@"connected"]; |
433 } | 454 } |
434 monitoredDevices_.erase(found); | |
435 } | 455 } |
436 | 456 |
437 - (void)observeValueForKeyPath:(NSString*)keyPath | 457 - (void)observeValueForKeyPath:(NSString*)keyPath |
438 ofObject:(id)object | 458 ofObject:(id)object |
439 change:(NSDictionary*)change | 459 change:(NSDictionary*)change |
440 context:(void*)context { | 460 context:(void*)context { |
461 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
441 if ([keyPath isEqual:@"suspended"]) | 462 if ([keyPath isEqual:@"suspended"]) |
442 receiver_->OnDeviceChanged(); | 463 receiver_->OnDeviceChanged(); |
443 if ([keyPath isEqual:@"connected"]) | 464 if ([keyPath isEqual:@"connected"]) |
444 [self stopObserving:static_cast<CrAVCaptureDevice*>(context)]; | 465 [self stopObserving:static_cast<CrAVCaptureDevice*>(context)]; |
445 } | 466 } |
446 | 467 |
447 @end // @implementation CrAVFoundationDeviceObserver | 468 @end // @implementation CrAVFoundationDeviceObserver |
448 | 469 |
449 namespace content { | 470 namespace content { |
450 | 471 |
(...skipping 20 matching lines...) Expand all Loading... | |
471 } | 492 } |
472 | 493 |
473 void DeviceMonitorMac::NotifyDeviceChanged( | 494 void DeviceMonitorMac::NotifyDeviceChanged( |
474 base::SystemMonitor::DeviceType type) { | 495 base::SystemMonitor::DeviceType type) { |
475 DCHECK(thread_checker_.CalledOnValidThread()); | 496 DCHECK(thread_checker_.CalledOnValidThread()); |
476 // TODO(xians): Remove the global variable for SystemMonitor. | 497 // TODO(xians): Remove the global variable for SystemMonitor. |
477 base::SystemMonitor::Get()->ProcessDevicesChanged(type); | 498 base::SystemMonitor::Get()->ProcessDevicesChanged(type); |
478 } | 499 } |
479 | 500 |
480 } // namespace content | 501 } // namespace content |
OLD | NEW |