Skip to content

Commit

Permalink
[Camera] Return all possible cameras on iOS (flutter#5636)
Browse files Browse the repository at this point in the history
  • Loading branch information
martingeorgiu authored and yutaaraki-toydium committed Jun 4, 2022
1 parent 8d674f2 commit 88b276e
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/camera/camera/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.7

* Returns all the available cameras on iOS.

## 0.9.6

* Adds audio access permission handling logic on iOS to fix an issue with `prepareForVideoRecording` not awaiting for the audio permission request result.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
25C3919135C3D981E6F800D0 /* libPods-RunnerTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 1944D8072499F3B5E7653D44 /* libPods-RunnerTests.a */; };
334733EA2668111C00DCC49E /* CameraOrientationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 03BB767226653ABE00CE5A93 /* CameraOrientationTests.m */; };
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
43ED1537282570DE00EB00DE /* AvailableCamerasTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 43ED1536282570DE00EB00DE /* AvailableCamerasTest.m */; };
788A065A27B0E02900533D74 /* StreamingTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 788A065927B0E02900533D74 /* StreamingTest.m */; };
978B8F6F1D3862AE00F588F7 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 7AFFD8EE1D35381100E5BB4D /* AppDelegate.m */; };
97C146F31CF9000F007C117D /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 97C146F21CF9000F007C117D /* main.m */; };
Expand Down Expand Up @@ -71,6 +72,7 @@
14AE82C910C2A12F2ECB2094 /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = "<group>"; };
1944D8072499F3B5E7653D44 /* libPods-RunnerTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-RunnerTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
43ED1536282570DE00EB00DE /* AvailableCamerasTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AvailableCamerasTest.m; sourceTree = "<group>"; };
59848A7CA98C1FADF8840207 /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = "<group>"; };
788A065927B0E02900533D74 /* StreamingTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = StreamingTest.m; sourceTree = "<group>"; };
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = "<group>"; };
Expand Down Expand Up @@ -148,6 +150,7 @@
E032F24F279F5E94009E9028 /* CameraCaptureSessionQueueRaceConditionTests.m */,
E0F95E3C27A32AB900699390 /* CameraPropertiesTests.m */,
788A065927B0E02900533D74 /* StreamingTest.m */,
43ED1536282570DE00EB00DE /* AvailableCamerasTest.m */,
);
path = RunnerTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -418,6 +421,7 @@
E487C86026D686A10034AC92 /* CameraPreviewPauseTests.m in Sources */,
E071CF7427B31DE4006EF3BA /* FLTCamSampleBufferTests.m in Sources */,
E04F108627A87CA600573D0C /* FLTSavePhotoDelegateTests.m in Sources */,
43ED1537282570DE00EB00DE /* AvailableCamerasTest.m in Sources */,
F6EE622F2710A6FC00905E4A /* MockFLTThreadSafeFlutterResult.m in Sources */,
E0CDBAC227CD9729002561D9 /* CameraTestUtils.m in Sources */,
334733EA2668111C00DCC49E /* CameraOrientationTests.m in Sources */,
Expand Down
121 changes: 121 additions & 0 deletions packages/camera/camera/example/ios/RunnerTests/AvailableCamerasTest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

@import camera;
@import camera.Test;
@import XCTest;
@import AVFoundation;
#import <OCMock/OCMock.h>
#import "MockFLTThreadSafeFlutterResult.h"

@interface AvailableCamerasTest : XCTestCase
@end

@implementation AvailableCamerasTest

- (void)testAvailableCamerasShouldReturnAllCamerasOnMultiCameraIPhone {
CameraPlugin *camera = [[CameraPlugin alloc] initWithRegistry:nil messenger:nil];
XCTestExpectation *expectation =
[[XCTestExpectation alloc] initWithDescription:@"Result finished"];

// iPhone 13 Cameras:
AVCaptureDevice *wideAngleCamera = OCMClassMock([AVCaptureDevice class]);
OCMStub([wideAngleCamera uniqueID]).andReturn(@"0");
OCMStub([wideAngleCamera position]).andReturn(AVCaptureDevicePositionBack);

AVCaptureDevice *frontFacingCamera = OCMClassMock([AVCaptureDevice class]);
OCMStub([frontFacingCamera uniqueID]).andReturn(@"1");
OCMStub([frontFacingCamera position]).andReturn(AVCaptureDevicePositionFront);

AVCaptureDevice *ultraWideCamera = OCMClassMock([AVCaptureDevice class]);
OCMStub([ultraWideCamera uniqueID]).andReturn(@"2");
OCMStub([ultraWideCamera position]).andReturn(AVCaptureDevicePositionBack);

AVCaptureDevice *telephotoCamera = OCMClassMock([AVCaptureDevice class]);
OCMStub([telephotoCamera uniqueID]).andReturn(@"3");
OCMStub([telephotoCamera position]).andReturn(AVCaptureDevicePositionBack);

NSMutableArray *requiredTypes =
[@[ AVCaptureDeviceTypeBuiltInWideAngleCamera, AVCaptureDeviceTypeBuiltInTelephotoCamera ]
mutableCopy];
if (@available(iOS 13.0, *)) {
[requiredTypes addObject:AVCaptureDeviceTypeBuiltInUltraWideCamera];
}

id discoverySessionMock = OCMClassMock([AVCaptureDeviceDiscoverySession class]);
OCMStub([discoverySessionMock discoverySessionWithDeviceTypes:requiredTypes
mediaType:AVMediaTypeVideo
position:AVCaptureDevicePositionUnspecified])
.andReturn(discoverySessionMock);

NSMutableArray *cameras = [NSMutableArray array];
[cameras addObjectsFromArray:@[ wideAngleCamera, frontFacingCamera, telephotoCamera ]];
if (@available(iOS 13.0, *)) {
[cameras addObject:ultraWideCamera];
}
OCMStub([discoverySessionMock devices]).andReturn([NSArray arrayWithArray:cameras]);

MockFLTThreadSafeFlutterResult *resultObject =
[[MockFLTThreadSafeFlutterResult alloc] initWithExpectation:expectation];

// Set up method call
FlutterMethodCall *call = [FlutterMethodCall methodCallWithMethodName:@"availableCameras"
arguments:nil];

[camera handleMethodCallAsync:call result:resultObject];

// Verify the result
NSDictionary *dictionaryResult = (NSDictionary *)resultObject.receivedResult;
if (@available(iOS 13.0, *)) {
XCTAssertTrue([dictionaryResult count] == 4);
} else {
XCTAssertTrue([dictionaryResult count] == 3);
}
}
- (void)testAvailableCamerasShouldReturnOneCameraOnSingleCameraIPhone {
CameraPlugin *camera = [[CameraPlugin alloc] initWithRegistry:nil messenger:nil];
XCTestExpectation *expectation =
[[XCTestExpectation alloc] initWithDescription:@"Result finished"];

// iPhone 8 Cameras:
AVCaptureDevice *wideAngleCamera = OCMClassMock([AVCaptureDevice class]);
OCMStub([wideAngleCamera uniqueID]).andReturn(@"0");
OCMStub([wideAngleCamera position]).andReturn(AVCaptureDevicePositionBack);

AVCaptureDevice *frontFacingCamera = OCMClassMock([AVCaptureDevice class]);
OCMStub([frontFacingCamera uniqueID]).andReturn(@"1");
OCMStub([frontFacingCamera position]).andReturn(AVCaptureDevicePositionFront);

NSMutableArray *requiredTypes =
[@[ AVCaptureDeviceTypeBuiltInWideAngleCamera, AVCaptureDeviceTypeBuiltInTelephotoCamera ]
mutableCopy];
if (@available(iOS 13.0, *)) {
[requiredTypes addObject:AVCaptureDeviceTypeBuiltInUltraWideCamera];
}

id discoverySessionMock = OCMClassMock([AVCaptureDeviceDiscoverySession class]);
OCMStub([discoverySessionMock discoverySessionWithDeviceTypes:requiredTypes
mediaType:AVMediaTypeVideo
position:AVCaptureDevicePositionUnspecified])
.andReturn(discoverySessionMock);

NSMutableArray *cameras = [NSMutableArray array];
[cameras addObjectsFromArray:@[ wideAngleCamera, frontFacingCamera ]];
OCMStub([discoverySessionMock devices]).andReturn([NSArray arrayWithArray:cameras]);

MockFLTThreadSafeFlutterResult *resultObject =
[[MockFLTThreadSafeFlutterResult alloc] initWithExpectation:expectation];

// Set up method call
FlutterMethodCall *call = [FlutterMethodCall methodCallWithMethodName:@"availableCameras"
arguments:nil];

[camera handleMethodCallAsync:call result:resultObject];

// Verify the result
NSDictionary *dictionaryResult = (NSDictionary *)resultObject.receivedResult;
XCTAssertTrue([dictionaryResult count] == 2);
}

@end
8 changes: 7 additions & 1 deletion packages/camera/camera/ios/Classes/CameraPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,14 @@ - (void)handleMethodCallAsync:(FlutterMethodCall *)call
result:(FLTThreadSafeFlutterResult *)result {
if ([@"availableCameras" isEqualToString:call.method]) {
if (@available(iOS 10.0, *)) {
NSMutableArray *discoveryDevices =
[@[ AVCaptureDeviceTypeBuiltInWideAngleCamera, AVCaptureDeviceTypeBuiltInTelephotoCamera ]
mutableCopy];
if (@available(iOS 13.0, *)) {
[discoveryDevices addObject:AVCaptureDeviceTypeBuiltInUltraWideCamera];
}
AVCaptureDeviceDiscoverySession *discoverySession = [AVCaptureDeviceDiscoverySession
discoverySessionWithDeviceTypes:@[ AVCaptureDeviceTypeBuiltInWideAngleCamera ]
discoverySessionWithDeviceTypes:discoveryDevices
mediaType:AVMediaTypeVideo
position:AVCaptureDevicePositionUnspecified];
NSArray<AVCaptureDevice *> *devices = discoverySession.devices;
Expand Down
2 changes: 1 addition & 1 deletion packages/camera/camera/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: A Flutter plugin for controlling the camera. Supports previewing
Dart.
repository: https://github.com/flutter/plugins/tree/main/packages/camera/camera
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
version: 0.9.6
version: 0.9.7

environment:
sdk: ">=2.14.0 <3.0.0"
Expand Down

0 comments on commit 88b276e

Please sign in to comment.