forked from mirrors/gecko-dev
For some reason, its value in the Google style we use is 80... except for Objective-C, where it's 100, which led to things like: https://hg.mozilla.org/mozilla-central/rev/31bf68247e6e https://hg.mozilla.org/mozilla-central/rev/64ceb33533a4. There's probably a discussion to have about whether 80 is the right limit, but since it's what's used for everything except ObjC, let's roll with it. # ignore-this-changeset Differential Revision: https://phabricator.services.mozilla.com/D187409
172 lines
4.9 KiB
Text
172 lines
4.9 KiB
Text
/*
|
|
* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
|
|
#if !defined(__has_feature) || !__has_feature(objc_arc)
|
|
# error "This file requires ARC support."
|
|
#endif
|
|
|
|
#import <AVFoundation/AVFoundation.h>
|
|
|
|
#import "device_info_objc.h"
|
|
|
|
@implementation DeviceInfoIosObjC
|
|
|
|
- (id)init {
|
|
self = [super init];
|
|
if (nil != self) {
|
|
_lock = [[NSLock alloc] init];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)dealloc {
|
|
}
|
|
|
|
- (void)registerOwner:(webrtc::VideoCaptureModule::DeviceInfo*)owner {
|
|
[_lock lock];
|
|
if (!_owner && owner) {
|
|
[self configureObservers];
|
|
} else if (_owner && !owner) {
|
|
NSNotificationCenter* notificationCenter =
|
|
[NSNotificationCenter defaultCenter];
|
|
for (id observer in _observers) {
|
|
[notificationCenter removeObserver:observer];
|
|
}
|
|
_observers = nil;
|
|
}
|
|
_owner = owner;
|
|
[_lock unlock];
|
|
}
|
|
|
|
+ (int)captureDeviceCount {
|
|
int cnt = 0;
|
|
@try {
|
|
for (AVCaptureDevice* device in
|
|
[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
|
|
if ([device isSuspended]) {
|
|
continue;
|
|
}
|
|
cnt++;
|
|
}
|
|
} @catch (NSException* exception) {
|
|
cnt = 0;
|
|
}
|
|
return cnt;
|
|
}
|
|
|
|
+ (AVCaptureDevice*)captureDeviceForIndex:(int)index {
|
|
int cnt = 0;
|
|
@try {
|
|
for (AVCaptureDevice* device in
|
|
[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
|
|
if ([device isSuspended]) {
|
|
continue;
|
|
}
|
|
if (cnt == index) {
|
|
return device;
|
|
}
|
|
cnt++;
|
|
}
|
|
} @catch (NSException* exception) {
|
|
cnt = 0;
|
|
}
|
|
|
|
return nil;
|
|
}
|
|
|
|
+ (AVCaptureDevice*)captureDeviceForUniqueId:(NSString*)uniqueId {
|
|
for (AVCaptureDevice* device in
|
|
[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
|
|
if ([device isSuspended]) {
|
|
continue;
|
|
}
|
|
if ([uniqueId isEqual:device.uniqueID]) {
|
|
return device;
|
|
}
|
|
}
|
|
|
|
return nil;
|
|
}
|
|
|
|
+ (NSString*)deviceNameForIndex:(int)index {
|
|
return [DeviceInfoIosObjC captureDeviceForIndex:index].localizedName;
|
|
}
|
|
|
|
+ (NSString*)deviceUniqueIdForIndex:(int)index {
|
|
return [DeviceInfoIosObjC captureDeviceForIndex:index].uniqueID;
|
|
}
|
|
|
|
+ (NSString*)deviceNameForUniqueId:(NSString*)uniqueId {
|
|
return [[AVCaptureDevice deviceWithUniqueID:uniqueId] localizedName];
|
|
}
|
|
|
|
+ (webrtc::VideoCaptureCapability)capabilityForPreset:(NSString*)preset {
|
|
webrtc::VideoCaptureCapability capability;
|
|
|
|
// TODO(tkchin): Maybe query AVCaptureDevice for supported formats, and
|
|
// then get the dimensions / frame rate from each supported format
|
|
if ([preset isEqualToString:AVCaptureSessionPreset352x288]) {
|
|
capability.width = 352;
|
|
capability.height = 288;
|
|
capability.maxFPS = 30;
|
|
capability.videoType = webrtc::VideoType::kNV12;
|
|
capability.interlaced = false;
|
|
} else if ([preset isEqualToString:AVCaptureSessionPreset640x480]) {
|
|
capability.width = 640;
|
|
capability.height = 480;
|
|
capability.maxFPS = 30;
|
|
capability.videoType = webrtc::VideoType::kNV12;
|
|
capability.interlaced = false;
|
|
} else if ([preset isEqualToString:AVCaptureSessionPreset1280x720]) {
|
|
capability.width = 1280;
|
|
capability.height = 720;
|
|
capability.maxFPS = 30;
|
|
capability.videoType = webrtc::VideoType::kNV12;
|
|
capability.interlaced = false;
|
|
}
|
|
|
|
return capability;
|
|
}
|
|
|
|
- (void)configureObservers {
|
|
// register device connected / disconnected event
|
|
NSNotificationCenter* notificationCenter =
|
|
[NSNotificationCenter defaultCenter];
|
|
|
|
id deviceWasConnectedObserver = [notificationCenter
|
|
addObserverForName:AVCaptureDeviceWasConnectedNotification
|
|
object:nil
|
|
queue:[NSOperationQueue mainQueue]
|
|
usingBlock:^(NSNotification* note) {
|
|
[_lock lock];
|
|
AVCaptureDevice* device = [note object];
|
|
BOOL isVideoDevice = [device hasMediaType:AVMediaTypeVideo];
|
|
if (isVideoDevice && _owner) _owner->DeviceChange();
|
|
[_lock unlock];
|
|
}];
|
|
|
|
id deviceWasDisconnectedObserver = [notificationCenter
|
|
addObserverForName:AVCaptureDeviceWasDisconnectedNotification
|
|
object:nil
|
|
queue:[NSOperationQueue mainQueue]
|
|
usingBlock:^(NSNotification* note) {
|
|
[_lock lock];
|
|
AVCaptureDevice* device = [note object];
|
|
BOOL isVideoDevice = [device hasMediaType:AVMediaTypeVideo];
|
|
if (isVideoDevice && _owner) _owner->DeviceChange();
|
|
[_lock unlock];
|
|
}];
|
|
|
|
_observers =
|
|
[[NSArray alloc] initWithObjects:deviceWasConnectedObserver,
|
|
deviceWasDisconnectedObserver, nil];
|
|
}
|
|
|
|
@end
|