Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
split audio and video request functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hellohuanlin committed May 17, 2022
1 parent a4f07fc commit 8f703be
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
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) {
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) {
// 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

0 comments on commit 8f703be

Please sign in to comment.