From d92e0b5923f50ec14124d82bb162c7334d0621b8 Mon Sep 17 00:00:00 2001 From: Peter Hanspers Date: Mon, 23 Oct 2017 14:30:25 +0200 Subject: [PATCH] Fixing crash in Mac client when no cameras are available. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: webrtc:8348 Change-Id: Ibf84ca76812d8c002fae9bd7bcf616abc53c78b1 Reviewed-on: https://webrtc-review.googlesource.com/7340 Reviewed-by: Kári Helgason Reviewed-by: Daniela Jovanoska Petrenko Commit-Queue: Peter Hanspers Cr-Commit-Position: refs/heads/master@{#20392} --- .../objc/AppRTCMobile/ARDCaptureController.m | 10 +++++++++- examples/objc/AppRTCMobile/ARDSettingsModel.m | 16 +++++++++++++--- examples/objc/AppRTCMobile/ARDSettingsStore.m | 11 ++++++++--- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/examples/objc/AppRTCMobile/ARDCaptureController.m b/examples/objc/AppRTCMobile/ARDCaptureController.m index da6a37a8ff..25aba8ce6b 100644 --- a/examples/objc/AppRTCMobile/ARDCaptureController.m +++ b/examples/objc/AppRTCMobile/ARDCaptureController.m @@ -11,6 +11,7 @@ #import "ARDCaptureController.h" #import "ARDSettingsModel.h" +#import "WebRTC/RTCLogging.h" @implementation ARDCaptureController { RTCCameraVideoCapturer *_capturer; @@ -34,6 +35,14 @@ _usingFrontCamera ? AVCaptureDevicePositionFront : AVCaptureDevicePositionBack; AVCaptureDevice *device = [self findDeviceForPosition:position]; AVCaptureDeviceFormat *format = [self selectFormatForDevice:device]; + + if (format == nil) { + RTCLogError(@"No valid formats for device %@", device); + NSAssert(NO, @""); + + return; + } + NSInteger fps = [self selectFpsForFormat:format]; [_capturer startCaptureWithDevice:device format:format fps:fps]; @@ -77,7 +86,6 @@ } } - NSAssert(selectedFormat != nil, @"No suitable capture format found."); return selectedFormat; } diff --git a/examples/objc/AppRTCMobile/ARDSettingsModel.m b/examples/objc/AppRTCMobile/ARDSettingsModel.m index cdaead774a..ac265c8296 100644 --- a/examples/objc/AppRTCMobile/ARDSettingsModel.m +++ b/examples/objc/AppRTCMobile/ARDSettingsModel.m @@ -151,11 +151,11 @@ NS_ASSUME_NONNULL_BEGIN #pragma mark - - (NSString *)defaultVideoResolutionSetting { - return [self availableVideoResolutions][0]; + return [self availableVideoResolutions].firstObject; } - (RTCVideoCodecInfo *)defaultVideoCodecSetting { - return [self availableVideoCodecs][0]; + return [self availableVideoCodecs].firstObject; } - (int)videoResolutionComponentAtIndex:(int)index inString:(NSString *)resolution { @@ -170,11 +170,21 @@ NS_ASSUME_NONNULL_BEGIN } - (void)registerStoreDefaults { + NSString *defaultVideoResolutionSetting = [self defaultVideoResolutionSetting]; + BOOL audioOnly = (defaultVideoResolutionSetting.length == 0); + +// The iOS simulator doesn't provide any sort of camera capture +// support or emulation (http://goo.gl/rHAnC1) so don't bother +// trying to open a local stream. +#if TARGET_IPHONE_SIMULATOR + audioOnly = YES; +#endif + NSData *codecData = [NSKeyedArchiver archivedDataWithRootObject:[self defaultVideoCodecSetting]]; [ARDSettingsStore setDefaultsForVideoResolution:[self defaultVideoResolutionSetting] videoCodec:codecData bitrate:nil - audioOnly:NO + audioOnly:audioOnly createAecDump:NO useLevelController:NO useManualAudioConfig:YES]; diff --git a/examples/objc/AppRTCMobile/ARDSettingsStore.m b/examples/objc/AppRTCMobile/ARDSettingsStore.m index 28a5ab7a24..86bdaaf54c 100644 --- a/examples/objc/AppRTCMobile/ARDSettingsStore.m +++ b/examples/objc/AppRTCMobile/ARDSettingsStore.m @@ -35,15 +35,20 @@ NS_ASSUME_NONNULL_BEGIN useLevelController:(BOOL)useLevelController useManualAudioConfig:(BOOL)useManualAudioConfig { NSMutableDictionary *defaultsDictionary = [@{ - kVideoResolutionKey : videoResolution, - kVideoCodecKey : videoCodec, kAudioOnlyKey : @(audioOnly), kCreateAecDumpKey : @(createAecDump), kUseLevelControllerKey : @(useLevelController), kUseManualAudioConfigKey : @(useManualAudioConfig) } mutableCopy]; + + if (videoResolution) { + defaultsDictionary[kVideoResolutionKey] = videoResolution; + } + if (videoCodec) { + defaultsDictionary[kVideoCodecKey] = videoCodec; + } if (bitrate) { - [defaultsDictionary setObject:bitrate forKey:kBitrateKey]; + defaultsDictionary[kBitrateKey] = bitrate; } [[NSUserDefaults standardUserDefaults] registerDefaults:defaultsDictionary]; }