Bug 1439997 - Fix ondevicechange event on OS X. r=jib, a=RyanVM
authorDan Minor <dminor@mozilla.com>
Tue, 08 Jan 2019 14:39:15 +0000
changeset 506646 b295af2ecf44a671a7802d6de183a7a5a03c259d
parent 506645 9104a4e2e3a4670bb75334e3ab1994f7728362b9
child 506647 c0445e5ce38871a960e29fa97a6992df24b0bd50
push id10497
push userryanvm@gmail.com
push dateSat, 12 Jan 2019 18:08:31 +0000
treeherdermozilla-beta@c0445e5ce388 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjib, RyanVM
bugs1439997, 1286429
milestone65.0
Bug 1439997 - Fix ondevicechange event on OS X. r=jib, a=RyanVM This brings the changes from Bug 1286429 that were made to the older mac/avfoundation video capture code to the newer objc video capture code. Depends on D15196 Differential Revision: https://phabricator.services.mozilla.com/D15197
media/webrtc/trunk/webrtc/modules/video_capture/objc/device_info.h
media/webrtc/trunk/webrtc/modules/video_capture/objc/device_info.mm
media/webrtc/trunk/webrtc/modules/video_capture/objc/device_info_objc.h
media/webrtc/trunk/webrtc/modules/video_capture/objc/device_info_objc.mm
--- a/media/webrtc/trunk/webrtc/modules/video_capture/objc/device_info.h
+++ b/media/webrtc/trunk/webrtc/modules/video_capture/objc/device_info.h
@@ -11,16 +11,18 @@
 #ifndef MODULES_VIDEO_CAPTURE_OBJC_DEVICE_INFO_H_
 #define MODULES_VIDEO_CAPTURE_OBJC_DEVICE_INFO_H_
 
 #include "modules/video_capture/device_info_impl.h"
 
 #include <map>
 #include <string>
 
+@class DeviceInfoIosObjC;
+
 namespace webrtc {
 namespace videocapturemodule {
 class DeviceInfoIos : public DeviceInfoImpl {
  public:
   DeviceInfoIos();
   virtual ~DeviceInfoIos();
 
   // Implementation of DeviceInfoImpl.
@@ -49,14 +51,15 @@ class DeviceInfoIos : public DeviceInfoI
 
   int32_t GetOrientation(const char* deviceUniqueIdUTF8,
                          VideoRotation& orientation) override;
 
   int32_t CreateCapabilityMap(const char* device_unique_id_utf8) override;
 
  private:
   std::map<std::string, VideoCaptureCapabilities> _capabilitiesMap;
+  DeviceInfoIosObjC* _captureInfo;
 };
 
 }  // namespace videocapturemodule
 }  // namespace webrtc
 
 #endif  // MODULES_VIDEO_CAPTURE_OBJC_DEVICE_INFO_H_
--- a/media/webrtc/trunk/webrtc/modules/video_capture/objc/device_info.mm
+++ b/media/webrtc/trunk/webrtc/modules/video_capture/objc/device_info.mm
@@ -40,18 +40,20 @@ VideoCaptureModule::DeviceInfo* VideoCap
 
 DeviceInfoIos::DeviceInfoIos() {
   this->Init();
 }
 
 DeviceInfoIos::~DeviceInfoIos() {}
 
 int32_t DeviceInfoIos::Init() {
+  _captureInfo = [[DeviceInfoIosObjC alloc] init];
+  [_captureInfo registerOwner:this];
+
   // Fill in all device capabilities.
-
   int deviceCount = [DeviceInfoIosObjC captureDeviceCount];
 
   for (int i = 0; i < deviceCount; i++) {
     AVCaptureDevice* avDevice = [DeviceInfoIosObjC captureDeviceForIndex:i];
     VideoCaptureCapabilities capabilityVector;
 
     for (NSString* preset in camera_presets) {
       BOOL support = [avDevice supportsAVCaptureSessionPreset:preset];
--- a/media/webrtc/trunk/webrtc/modules/video_capture/objc/device_info_objc.h
+++ b/media/webrtc/trunk/webrtc/modules/video_capture/objc/device_info_objc.h
@@ -10,20 +10,28 @@
 
 #ifndef MODULES_VIDEO_CAPTURE_OBJC_DEVICE_INFO_OBJC_H_
 #define MODULES_VIDEO_CAPTURE_OBJC_DEVICE_INFO_OBJC_H_
 
 #import <AVFoundation/AVFoundation.h>
 
 #include "modules/video_capture/video_capture_defines.h"
 
-@interface DeviceInfoIosObjC : NSObject
+@interface DeviceInfoIosObjC : NSObject {
+  NSArray* _observers;
+  NSLock* _lock;
+  webrtc::videocapturemodule::DeviceInfoIos* _owner;
+}
+
 + (int)captureDeviceCount;
 + (AVCaptureDevice*)captureDeviceForIndex:(int)index;
 + (AVCaptureDevice*)captureDeviceForUniqueId:(NSString*)uniqueId;
 + (NSString*)deviceNameForIndex:(int)index;
 + (NSString*)deviceUniqueIdForIndex:(int)index;
 + (NSString*)deviceNameForUniqueId:(NSString*)uniqueId;
 + (webrtc::VideoCaptureCapability)capabilityForPreset:(NSString*)preset;
 
+- (void)registerOwner:(webrtc::videocapturemodule::DeviceInfoIos*)owner;
+- (void)configureObservers;
+
 @end
 
 #endif  // MODULES_VIDEO_CAPTURE_OBJC_DEVICE_INFO_OBJC_H_
--- a/media/webrtc/trunk/webrtc/modules/video_capture/objc/device_info_objc.mm
+++ b/media/webrtc/trunk/webrtc/modules/video_capture/objc/device_info_objc.mm
@@ -14,16 +14,39 @@
 
 #import <AVFoundation/AVFoundation.h>
 
 #import "modules/video_capture/objc/device_info_objc.h"
 #include "modules/video_capture/video_capture_config.h"
 
 @implementation DeviceInfoIosObjC
 
+- (id)init {
+  self = [super init];
+
+  if (nil != self) {
+    [self configureObservers];
+  }
+
+  return self;
+}
+
+
+- (void)dealloc {
+   NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
+   for (id observer in _observers)
+       [notificationCenter removeObserver:observer];
+}
+
+- (void)registerOwner:(DeviceInfoIos*)owner {
+  [_lock lock];
+  _owner = owner;
+  [_lock unlock];
+}
+
 + (int)captureDeviceCount {
   return [[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo] count];
 }
 
 + (AVCaptureDevice*)captureDeviceForIndex:(int)index {
   return [[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]
       objectAtIndex:index];
 }
@@ -74,9 +97,38 @@
     capability.maxFPS = 30;
     capability.videoType = webrtc::VideoType::kNV12;
     capability.interlaced = false;
   }
 
   return capability;
 }
 
+- (void)configureObservers {
+  //register device connected / disconnected event
+  _lock = [[NSLock alloc] init];
+
+  NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
+
+  id deviceWasConnectedObserver = [notificationCenter addObserverForName:AVCaptureDeviceWasConnectedNotification
+      object:nil
+      queue:[NSOperationQueue mainQueue]
+      usingBlock:^(NSNotification *note) {
+          [_lock lock];
+          if(_owner)
+              _owner->DeviceChange();
+          [_lock unlock];
+      }];
+
+  id deviceWasDisconnectedObserver = [notificationCenter addObserverForName:AVCaptureDeviceWasDisconnectedNotification
+      object:nil
+      queue:[NSOperationQueue mainQueue]
+      usingBlock:^(NSNotification *note) {
+          [_lock lock];
+          if(_owner)
+              _owner->DeviceChange();
+          [_lock unlock];
+      }];
+
+  _observers = [[NSArray alloc] initWithObjects:deviceWasConnectedObserver, deviceWasDisconnectedObserver, nil];
+}
+
 @end