Chromium Code Reviews| 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" |
| 17 #include "media/base/bind_to_current_loop.h" | |
| 16 #import "media/video/capture/mac/avfoundation_glue.h" | 18 #import "media/video/capture/mac/avfoundation_glue.h" |
| 17 | 19 |
| 18 namespace { | 20 namespace { |
| 19 | 21 |
| 20 // This class is used to keep track of system devices names and their types. | 22 // This class is used to keep track of system devices names and their types. |
| 21 class DeviceInfo { | 23 class DeviceInfo { |
| 22 public: | 24 public: |
| 23 enum DeviceType { | 25 enum DeviceType { |
| 24 kAudio, | 26 kAudio, |
| 25 kVideo, | 27 kVideo, |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 ConsolidateDevicesListAndNotify(snapshot_devices); | 211 ConsolidateDevicesListAndNotify(snapshot_devices); |
| 210 } | 212 } |
| 211 | 213 |
| 212 // Forward declaration for use by CrAVFoundationDeviceObserver. | 214 // Forward declaration for use by CrAVFoundationDeviceObserver. |
| 213 class SuspendObserverDelegate; | 215 class SuspendObserverDelegate; |
| 214 | 216 |
| 215 } // namespace | 217 } // namespace |
| 216 | 218 |
| 217 // This class is a Key-Value Observer (KVO) shim. It is needed because C++ | 219 // This class is a Key-Value Observer (KVO) shim. It is needed because C++ |
| 218 // classes cannot observe Key-Values directly. Created, manipulated, and | 220 // classes cannot observe Key-Values directly. Created, manipulated, and |
| 219 // destroyed on the Device Thread by SuspendedObserverDelegate. | 221 // destroyed on the UI Thread by SuspendObserverDelegate. |
| 220 @interface CrAVFoundationDeviceObserver : NSObject { | 222 @interface CrAVFoundationDeviceObserver : NSObject { |
| 221 @private | 223 @private |
| 222 SuspendObserverDelegate* receiver_; // weak | 224 base::Closure onDeviceChangedCallback_; |
| 225 SuspendObserverDelegate* receiver_; // Weak. | |
|
tommi (sloooow) - chröme
2014/07/07 07:41:07
you don't need this variable anymore
mcasas
2014/07/08 15:11:45
Done.
| |
| 223 // Member to keep track of the devices we are already monitoring. | 226 // Member to keep track of the devices we are already monitoring. |
| 224 std::set<CrAVCaptureDevice*> monitoredDevices_; | 227 std::set<CrAVCaptureDevice*> monitoredDevices_; |
| 225 } | 228 } |
| 226 | 229 |
| 227 - (id)initWithChangeReceiver:(SuspendObserverDelegate*)receiver; | 230 - (id)initWithChangeReceiver:(SuspendObserverDelegate*)receiver |
|
tommi (sloooow) - chröme
2014/07/07 07:41:07
Since this method doesn't need the receiver pointe
mcasas
2014/07/08 15:11:45
Changed, but still kept the initWithX as is part o
| |
| 231 onDeviceChangedCallback:(const base::Closure&)callback; | |
| 228 - (void)startObserving:(CrAVCaptureDevice*)device; | 232 - (void)startObserving:(CrAVCaptureDevice*)device; |
| 229 - (void)stopObserving:(CrAVCaptureDevice*)device; | 233 - (void)stopObserving:(CrAVCaptureDevice*)device; |
| 230 | 234 |
| 231 @end | 235 @end |
| 232 | 236 |
| 233 namespace { | 237 namespace { |
| 234 | 238 |
| 235 // This class owns and manages the lifetime of a CrAVFoundationDeviceObserver. | 239 // This class owns and manages the lifetime of a CrAVFoundationDeviceObserver. |
| 236 // Provides a callback for this device observer to indicate that there has been | 240 // AVFoundationMonitorImpl creates and destroys it in UI thread. Runs the |
| 237 // a device change of some kind. Created by AVFoundationMonitorImpl in UI thread | 241 // expensive device enumerations in OnDeviceChanged() and StartObserver() on |
| 238 // but living in Device Thread. | 242 // Device Thread. |
| 239 class SuspendObserverDelegate : | 243 class SuspendObserverDelegate : |
| 240 public base::RefCountedThreadSafe<SuspendObserverDelegate> { | 244 public base::RefCountedThreadSafe<SuspendObserverDelegate> { |
| 241 public: | 245 public: |
| 242 explicit SuspendObserverDelegate(DeviceMonitorMacImpl* monitor) | 246 explicit SuspendObserverDelegate(DeviceMonitorMacImpl* monitor) |
| 243 : avfoundation_monitor_impl_(monitor) { | 247 : avfoundation_monitor_impl_(monitor) { |
| 248 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 244 device_thread_checker_.DetachFromThread(); | 249 device_thread_checker_.DetachFromThread(); |
| 245 } | 250 } |
| 246 | 251 |
| 247 void OnDeviceChanged(); | 252 void OnDeviceChanged(); |
| 248 void StartObserver(); | 253 void StartObserver(); |
| 249 void ResetDeviceMonitorOnUIThread(); | 254 void ResetDeviceMonitorOnUIThread(); |
| 250 | 255 |
| 251 private: | 256 private: |
| 252 friend class base::RefCountedThreadSafe<SuspendObserverDelegate>; | 257 friend class base::RefCountedThreadSafe<SuspendObserverDelegate>; |
| 253 | 258 |
| 254 virtual ~SuspendObserverDelegate() {} | 259 virtual ~SuspendObserverDelegate() { |
| 260 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 261 } | |
| 255 | 262 |
| 256 void OnDeviceChangedOnUIThread( | 263 void OnDeviceChangedOnUIThread( |
| 257 const std::vector<DeviceInfo>& snapshot_devices); | 264 const std::vector<DeviceInfo>& snapshot_devices); |
| 258 | 265 |
| 259 base::ThreadChecker device_thread_checker_; | 266 base::ThreadChecker device_thread_checker_; |
| 267 // Created, used and released in UI thread. | |
| 260 base::scoped_nsobject<CrAVFoundationDeviceObserver> suspend_observer_; | 268 base::scoped_nsobject<CrAVFoundationDeviceObserver> suspend_observer_; |
| 261 DeviceMonitorMacImpl* avfoundation_monitor_impl_; | 269 DeviceMonitorMacImpl* avfoundation_monitor_impl_; |
| 262 }; | 270 }; |
| 263 | 271 |
| 264 void SuspendObserverDelegate::OnDeviceChanged() { | 272 void SuspendObserverDelegate::OnDeviceChanged() { |
| 265 DCHECK(device_thread_checker_.CalledOnValidThread()); | 273 DCHECK(device_thread_checker_.CalledOnValidThread()); |
| 266 NSArray* devices = [AVCaptureDeviceGlue devices]; | 274 NSArray* devices = [AVCaptureDeviceGlue devices]; |
| 267 std::vector<DeviceInfo> snapshot_devices; | 275 std::vector<DeviceInfo> snapshot_devices; |
| 268 for (CrAVCaptureDevice* device in devices) { | 276 for (CrAVCaptureDevice* device in devices) { |
| 269 [suspend_observer_ startObserving:device]; | 277 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 278 base::BindBlock(^{ [suspend_observer_ startObserving:device]; })); | |
| 270 BOOL suspended = [device respondsToSelector:@selector(isSuspended)] && | 279 BOOL suspended = [device respondsToSelector:@selector(isSuspended)] && |
| 271 [device isSuspended]; | 280 [device isSuspended]; |
| 272 DeviceInfo::DeviceType device_type = DeviceInfo::kUnknown; | 281 DeviceInfo::DeviceType device_type = DeviceInfo::kUnknown; |
| 273 if ([device hasMediaType:AVFoundationGlue::AVMediaTypeVideo()]) { | 282 if ([device hasMediaType:AVFoundationGlue::AVMediaTypeVideo()]) { |
| 274 if (suspended) | 283 if (suspended) |
| 275 continue; | 284 continue; |
| 276 device_type = DeviceInfo::kVideo; | 285 device_type = DeviceInfo::kVideo; |
| 277 } else if ([device hasMediaType:AVFoundationGlue::AVMediaTypeMuxed()]) { | 286 } else if ([device hasMediaType:AVFoundationGlue::AVMediaTypeMuxed()]) { |
| 278 device_type = suspended ? DeviceInfo::kAudio : DeviceInfo::kMuxed; | 287 device_type = suspended ? DeviceInfo::kAudio : DeviceInfo::kMuxed; |
| 279 } else if ([device hasMediaType:AVFoundationGlue::AVMediaTypeAudio()]) { | 288 } else if ([device hasMediaType:AVFoundationGlue::AVMediaTypeAudio()]) { |
| 280 device_type = DeviceInfo::kAudio; | 289 device_type = DeviceInfo::kAudio; |
| 281 } | 290 } |
| 282 snapshot_devices.push_back(DeviceInfo([[device uniqueID] UTF8String], | 291 snapshot_devices.push_back(DeviceInfo([[device uniqueID] UTF8String], |
| 283 device_type)); | 292 device_type)); |
| 284 } | 293 } |
| 285 // Post the consolidation of enumerated devices to be done on UI thread. | 294 // Post the consolidation of enumerated devices to be done on UI thread. |
| 286 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 295 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 287 base::Bind(&SuspendObserverDelegate::OnDeviceChangedOnUIThread, | 296 base::Bind(&SuspendObserverDelegate::OnDeviceChangedOnUIThread, |
| 288 this, snapshot_devices)); | 297 this, snapshot_devices)); |
| 289 } | 298 } |
| 290 | 299 |
| 291 void SuspendObserverDelegate::StartObserver() { | 300 void SuspendObserverDelegate::StartObserver() { |
| 292 DCHECK(device_thread_checker_.CalledOnValidThread()); | 301 DCHECK(device_thread_checker_.CalledOnValidThread()); |
| 293 suspend_observer_.reset([[CrAVFoundationDeviceObserver alloc] | 302 |
| 294 initWithChangeReceiver:this]); | 303 base::Closure on_device_changed_callback = media::BindToCurrentLoop( |
|
tommi (sloooow) - chröme
2014/07/07 07:41:07
is media::BindToCurrentLoop used elsewhere in cont
mcasas
2014/07/08 15:11:45
Yes, f.i. in browser/renderer_host/media/video_cap
| |
| 295 for (CrAVCaptureDevice* device in [AVCaptureDeviceGlue devices]) | 304 base::Bind(&SuspendObserverDelegate::OnDeviceChanged, this)); |
| 296 [suspend_observer_ startObserving:device]; | 305 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 306 base::BindBlock(^{ suspend_observer_.reset( | |
| 307 [[CrAVFoundationDeviceObserver alloc] | |
| 308 initWithChangeReceiver:this | |
| 309 onDeviceChangedCallback:on_device_changed_callback]); | |
| 310 })); | |
| 311 for (CrAVCaptureDevice* device in [AVCaptureDeviceGlue devices]) { | |
| 312 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
| 313 base::BindBlock(^{ [suspend_observer_ startObserving:device]; })); | |
| 314 } | |
| 297 } | 315 } |
| 298 | 316 |
| 299 void SuspendObserverDelegate::ResetDeviceMonitorOnUIThread() { | 317 void SuspendObserverDelegate::ResetDeviceMonitorOnUIThread() { |
| 300 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 318 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 301 avfoundation_monitor_impl_ = NULL; | 319 avfoundation_monitor_impl_ = NULL; |
| 302 } | 320 } |
| 303 | 321 |
| 304 void SuspendObserverDelegate::OnDeviceChangedOnUIThread( | 322 void SuspendObserverDelegate::OnDeviceChangedOnUIThread( |
| 305 const std::vector<DeviceInfo>& snapshot_devices) { | 323 const std::vector<DeviceInfo>& snapshot_devices) { |
| 306 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 324 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 377 DCHECK(thread_checker_.CalledOnValidThread()); | 395 DCHECK(thread_checker_.CalledOnValidThread()); |
| 378 device_task_runner_->PostTask(FROM_HERE, | 396 device_task_runner_->PostTask(FROM_HERE, |
| 379 base::Bind(&SuspendObserverDelegate::OnDeviceChanged, | 397 base::Bind(&SuspendObserverDelegate::OnDeviceChanged, |
| 380 suspend_observer_delegate_)); | 398 suspend_observer_delegate_)); |
| 381 } | 399 } |
| 382 | 400 |
| 383 } // namespace | 401 } // namespace |
| 384 | 402 |
| 385 @implementation CrAVFoundationDeviceObserver | 403 @implementation CrAVFoundationDeviceObserver |
| 386 | 404 |
| 387 - (id)initWithChangeReceiver:(SuspendObserverDelegate*)receiver { | 405 - (id)initWithChangeReceiver:(SuspendObserverDelegate*)receiver |
| 406 onDeviceChangedCallback:(const base::Closure&)callback { | |
| 407 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 388 if ((self = [super init])) { | 408 if ((self = [super init])) { |
| 389 DCHECK(receiver != NULL); | 409 DCHECK(receiver != NULL); |
| 390 receiver_ = receiver; | 410 receiver_ = receiver; |
| 411 onDeviceChangedCallback_ = callback; | |
| 391 } | 412 } |
| 392 return self; | 413 return self; |
| 393 } | 414 } |
| 394 | 415 |
| 395 - (void)dealloc { | 416 - (void)dealloc { |
| 417 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 396 std::set<CrAVCaptureDevice*>::iterator it = monitoredDevices_.begin(); | 418 std::set<CrAVCaptureDevice*>::iterator it = monitoredDevices_.begin(); |
| 397 while (it != monitoredDevices_.end()) | 419 while (it != monitoredDevices_.end()) { |
| 398 [self stopObserving:*it++]; | 420 [self removeObservers:*it]; |
| 421 monitoredDevices_.erase(it++); | |
| 422 } | |
| 399 [super dealloc]; | 423 [super dealloc]; |
| 400 } | 424 } |
| 401 | 425 |
| 402 - (void)startObserving:(CrAVCaptureDevice*)device { | 426 - (void)startObserving:(CrAVCaptureDevice*)device { |
| 427 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 403 DCHECK(device != nil); | 428 DCHECK(device != nil); |
| 404 // Skip this device if there are already observers connected to it. | 429 // Skip this device if there are already observers connected to it. |
| 405 if (std::find(monitoredDevices_.begin(), monitoredDevices_.end(), device) != | 430 if (std::find(monitoredDevices_.begin(), monitoredDevices_.end(), device) != |
| 406 monitoredDevices_.end()) { | 431 monitoredDevices_.end()) { |
| 407 return; | 432 return; |
| 408 } | 433 } |
| 409 [device addObserver:self | 434 [device addObserver:self |
| 410 forKeyPath:@"suspended" | 435 forKeyPath:@"suspended" |
| 411 options:0 | 436 options:0 |
| 412 context:device]; | 437 context:device]; |
| 413 [device addObserver:self | 438 [device addObserver:self |
| 414 forKeyPath:@"connected" | 439 forKeyPath:@"connected" |
| 415 options:0 | 440 options:0 |
| 416 context:device]; | 441 context:device]; |
| 417 monitoredDevices_.insert(device); | 442 monitoredDevices_.insert(device); |
| 418 } | 443 } |
| 419 | 444 |
| 420 - (void)stopObserving:(CrAVCaptureDevice*)device { | 445 - (void)stopObserving:(CrAVCaptureDevice*)device { |
| 446 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 421 DCHECK(device != nil); | 447 DCHECK(device != nil); |
| 422 std::set<CrAVCaptureDevice*>::iterator found = | 448 std::set<CrAVCaptureDevice*>::iterator found = |
| 423 std::find(monitoredDevices_.begin(), monitoredDevices_.end(), device); | 449 std::find(monitoredDevices_.begin(), monitoredDevices_.end(), device); |
| 424 DCHECK(found != monitoredDevices_.end()); | 450 DCHECK(found != monitoredDevices_.end()); |
| 425 // Every so seldom, |device| might be gone when getting here, in that case | 451 [self removeObservers:*found]; |
| 426 // removing the observer causes a crash. Try to avoid it by checking sanity of | 452 monitoredDevices_.erase(found); |
| 427 // the |device| via its -observationInfo. http://crbug.com/371271. | 453 } |
| 454 | |
| 455 - (void)removeObservers:(CrAVCaptureDevice*)device { | |
| 456 // Check sanity of |device| via its -observationInfo. http://crbug.com/371271. | |
| 428 if ([device observationInfo]) { | 457 if ([device observationInfo]) { |
| 429 [device removeObserver:self | 458 [device removeObserver:self |
| 430 forKeyPath:@"suspended"]; | 459 forKeyPath:@"suspended"]; |
| 431 [device removeObserver:self | 460 [device removeObserver:self |
| 432 forKeyPath:@"connected"]; | 461 forKeyPath:@"connected"]; |
| 433 } | 462 } |
| 434 monitoredDevices_.erase(found); | |
| 435 } | 463 } |
| 436 | 464 |
| 437 - (void)observeValueForKeyPath:(NSString*)keyPath | 465 - (void)observeValueForKeyPath:(NSString*)keyPath |
| 438 ofObject:(id)object | 466 ofObject:(id)object |
| 439 change:(NSDictionary*)change | 467 change:(NSDictionary*)change |
| 440 context:(void*)context { | 468 context:(void*)context { |
| 469 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 441 if ([keyPath isEqual:@"suspended"]) | 470 if ([keyPath isEqual:@"suspended"]) |
| 442 receiver_->OnDeviceChanged(); | 471 onDeviceChangedCallback_.Run(); |
| 443 if ([keyPath isEqual:@"connected"]) | 472 if ([keyPath isEqual:@"connected"]) |
| 444 [self stopObserving:static_cast<CrAVCaptureDevice*>(context)]; | 473 [self stopObserving:static_cast<CrAVCaptureDevice*>(context)]; |
| 445 } | 474 } |
| 446 | 475 |
| 447 @end // @implementation CrAVFoundationDeviceObserver | 476 @end // @implementation CrAVFoundationDeviceObserver |
| 448 | 477 |
| 449 namespace content { | 478 namespace content { |
| 450 | 479 |
| 451 DeviceMonitorMac::DeviceMonitorMac() { | 480 DeviceMonitorMac::DeviceMonitorMac() { |
| 452 // Both QTKit and AVFoundation do not need to be fired up until the user | 481 // Both QTKit and AVFoundation do not need to be fired up until the user |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 471 } | 500 } |
| 472 | 501 |
| 473 void DeviceMonitorMac::NotifyDeviceChanged( | 502 void DeviceMonitorMac::NotifyDeviceChanged( |
| 474 base::SystemMonitor::DeviceType type) { | 503 base::SystemMonitor::DeviceType type) { |
| 475 DCHECK(thread_checker_.CalledOnValidThread()); | 504 DCHECK(thread_checker_.CalledOnValidThread()); |
| 476 // TODO(xians): Remove the global variable for SystemMonitor. | 505 // TODO(xians): Remove the global variable for SystemMonitor. |
| 477 base::SystemMonitor::Get()->ProcessDevicesChanged(type); | 506 base::SystemMonitor::Get()->ProcessDevicesChanged(type); |
| 478 } | 507 } |
| 479 | 508 |
| 480 } // namespace content | 509 } // namespace content |
| OLD | NEW |