Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
split audio and video request functions
  • Loading branch information
hellohuanlin committed May 17, 2022
commit 67f94355edcf71c1893aede61d314330970d4603
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ - (void)testRequestCameraPermission_completeWithoutErrorIfPrevoiuslyAuthorized {
OCMStub([mockDevice authorizationStatusForMediaType:AVMediaTypeVideo])
.andReturn(AVAuthorizationStatusAuthorized);

FLTRequestCameraPermission(/*forAudio*/ false, ^(FlutterError *error) {
FLTRequestCameraPermission(^(FlutterError *error) {
if (error == nil) {
[expectation fulfill];
}
Expand All @@ -46,7 +46,7 @@ - (void)testRequestCameraPermission_completeWithErrorIfPreviouslyDenied {
id mockDevice = OCMClassMock([AVCaptureDevice class]);
OCMStub([mockDevice authorizationStatusForMediaType:AVMediaTypeVideo])
.andReturn(AVAuthorizationStatusDenied);
FLTRequestCameraPermission(/*forAudio*/ false, ^(FlutterError *error) {
FLTRequestCameraPermission(^(FlutterError *error) {
if ([error isEqual:expectedError]) {
[expectation fulfill];
}
Expand All @@ -65,7 +65,7 @@ - (void)testRequestCameraPermission_completeWithErrorIfRestricted {
OCMStub([mockDevice authorizationStatusForMediaType:AVMediaTypeVideo])
.andReturn(AVAuthorizationStatusRestricted);

FLTRequestCameraPermission(/*forAudio*/ false, ^(FlutterError *error) {
FLTRequestCameraPermission(^(FlutterError *error) {
if ([error isEqual:expectedError]) {
[expectation fulfill];
}
Expand All @@ -87,7 +87,7 @@ - (void)testRequestCameraPermission_completeWithoutErrorIfUserGrantAccess {
return YES;
}]]);

FLTRequestCameraPermission(/*forAudio*/ false, ^(FlutterError *error) {
FLTRequestCameraPermission(^(FlutterError *error) {
if (error == nil) {
[grantedExpectation fulfill];
}
Expand All @@ -113,7 +113,7 @@ - (void)testRequestCameraPermission_completeWithErrorIfUserDenyAccess {
block(NO);
return YES;
}]]);
FLTRequestCameraPermission(/*forAudio*/ false, ^(FlutterError *error) {
FLTRequestCameraPermission(^(FlutterError *error) {
if ([error isEqual:expectedError]) {
[expectation fulfill];
}
Expand All @@ -133,7 +133,7 @@ - (void)testRequestAudioPermission_completeWithoutErrorIfPrevoiuslyAuthorized {
OCMStub([mockDevice authorizationStatusForMediaType:AVMediaTypeAudio])
.andReturn(AVAuthorizationStatusAuthorized);

FLTRequestCameraPermission(/*forAudio*/ true, ^(FlutterError *error) {
FLTRequestAudioPermission(^(FlutterError *error) {
if (error == nil) {
[expectation fulfill];
}
Expand All @@ -153,7 +153,7 @@ - (void)testRequestAudioPermission_completeWithErrorIfPreviouslyDenied {
id mockDevice = OCMClassMock([AVCaptureDevice class]);
OCMStub([mockDevice authorizationStatusForMediaType:AVMediaTypeAudio])
.andReturn(AVAuthorizationStatusDenied);
FLTRequestCameraPermission(/*forAudio*/ true, ^(FlutterError *error) {
FLTRequestAudioPermission(^(FlutterError *error) {
if ([error isEqual:expectedError]) {
[expectation fulfill];
}
Expand All @@ -172,7 +172,7 @@ - (void)testRequestAudioPermission_completeWithErrorIfRestricted {
OCMStub([mockDevice authorizationStatusForMediaType:AVMediaTypeAudio])
.andReturn(AVAuthorizationStatusRestricted);

FLTRequestCameraPermission(/*forAudio*/ true, ^(FlutterError *error) {
FLTRequestAudioPermission(^(FlutterError *error) {
if ([error isEqual:expectedError]) {
[expectation fulfill];
}
Expand All @@ -194,7 +194,7 @@ - (void)testRequestAudioPermission_completeWithoutErrorIfUserGrantAccess {
return YES;
}]]);

FLTRequestCameraPermission(/*forAudio*/ true, ^(FlutterError *error) {
FLTRequestAudioPermission(^(FlutterError *error) {
if (error == nil) {
[grantedExpectation fulfill];
}
Expand All @@ -219,7 +219,7 @@ - (void)testRequestAudioPermission_completeWithErrorIfUserDenyAccess {
block(NO);
return YES;
}]]);
FLTRequestCameraPermission(/*forAudio*/ true, ^(FlutterError *error) {
FLTRequestAudioPermission(^(FlutterError *error) {
if ([error isEqual:expectedError]) {
[expectation fulfill];
}
Expand Down
16 changes: 12 additions & 4 deletions packages/camera/camera/ios/Classes/CameraPermissionUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@ typedef void (^FLTCameraPermissionRequestCompletionHandler)(FlutterError *);
/// screen. Otherwise AVFoundation simply returns the user's previous choice, and in this case the
/// user will have to update the choice in Settings app.
///
/// @param forAudio Requests for `AVMediaTypeAudio` permission if `forAudio` is true, and
/// `AVMediaTypeVideo` permission otherwise.
/// @param handler if access permission is (or was previously) granted, completion handler will be
/// called without error; Otherwise completion handler will be called with error. Handler can be
/// called on an arbitrary dispatch queue.
extern void FLTRequestCameraPermission(BOOL forAudio,
FLTCameraPermissionRequestCompletionHandler handler);
extern void FLTRequestCameraPermission(FLTCameraPermissionRequestCompletionHandler handler);

/// Requests audio access permission.
///
/// If it is the first time requesting audio access, a permission dialog will show up on the
/// screen. Otherwise AVFoundation simply returns the user's previous choice, and in this case the
/// user will have to update the choice in Settings app.
///
/// @param handler if access permission is (or was previously) granted, completion handler will be
/// called without error; Otherwise completion handler will be called with error. Handler can be
/// called on an arbitrary dispatch queue.
extern void FLTRequestAudioPermission(FLTCameraPermissionRequestCompletionHandler handler);
11 changes: 9 additions & 2 deletions packages/camera/camera/ios/Classes/CameraPermissionUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
@import AVFoundation;
#import "CameraPermissionUtils.h"

void FLTRequestCameraPermission(BOOL forAudio,
FLTCameraPermissionRequestCompletionHandler handler) {
void RequestPermission(BOOL forAudio, FLTCameraPermissionRequestCompletionHandler handler) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't exposed on the header, but best not to collide with some other system function with the same name.

Suggested change
void RequestPermission(BOOL forAudio, FLTCameraPermissionRequestCompletionHandler handler) {
void FLTRequestPermission(BOOL forAudio, FLTCameraPermissionRequestCompletionHandler handler) {

AVMediaType mediaType;
if (forAudio) {
mediaType = AVMediaTypeAudio;
Expand Down Expand Up @@ -76,3 +75,11 @@ void FLTRequestCameraPermission(BOOL forAudio,
}
}
}

void FLTRequestCameraPermission(FLTCameraPermissionRequestCompletionHandler handler) {
RequestPermission(/*forAudio*/ NO, handler);
}

void FLTRequestAudioPermission(FLTCameraPermissionRequestCompletionHandler handler) {
RequestPermission(/*forAudio*/ YES, handler);
}
4 changes: 2 additions & 2 deletions packages/camera/camera/ios/Classes/CameraPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ - (void)handleMethodCallAsync:(FlutterMethodCall *)call
[result sendNotImplemented];
}
} else if ([@"create" isEqualToString:call.method]) {
FLTRequestCameraPermission(/*forAudio*/ false, ^(FlutterError *error) {
FLTRequestCameraPermission(^(FlutterError *error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the old name WithCompletionHandler was good.

// Create FLTCam only if granted camera access.
if (error) {
[result sendFlutterError:error];
Expand Down Expand Up @@ -195,7 +195,7 @@ - (void)handleMethodCallAsync:(FlutterMethodCall *)call
[result sendSuccess];
} else if ([@"prepareForVideoRecording" isEqualToString:call.method]) {
// Setup audio capture session only if granted audio access.
FLTRequestCameraPermission(/*forAudio*/ true, ^(FlutterError *error) {
FLTRequestAudioPermission(^(FlutterError *error) {
if (error) {
[result sendFlutterError:error];
} else {
Expand Down