Bug 1439997 - Fix ondevicechange event on OS X; r=jib
authorDan Minor <dminor@mozilla.com>
Tue, 08 Jan 2019 14:39:15 +0000
changeset 452917 7924a0ccd7a35b7fd7cc2926738c84f76c2e058a
parent 452916 e79251fa381cfc9f3425f6a746ac8d8c22046d6b
child 452918 e017a870fd23cb395aee457421e984d45d21e83d
push id35334
push userrmaries@mozilla.com
push dateTue, 08 Jan 2019 21:58:40 +0000
treeherdermozilla-central@e08a0bb35c03 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjib
bugs1439997, 1286429
milestone66.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1439997 - Fix ondevicechange event on OS X; r=jib 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