forked from flutter/packages
/
CameraPlugin.m
331 lines (307 loc) · 14.6 KB
/
CameraPlugin.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// 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 "CameraPlugin.h"
#import "CameraPlugin_Test.h"
@import AVFoundation;
#import "CameraPermissionUtils.h"
#import "CameraProperties.h"
#import "FLTCam.h"
#import "FLTThreadSafeEventChannel.h"
#import "FLTThreadSafeFlutterResult.h"
#import "FLTThreadSafeMethodChannel.h"
#import "FLTThreadSafeTextureRegistry.h"
#import "QueueUtils.h"
@interface CameraPlugin ()
@property(readonly, nonatomic) FLTThreadSafeTextureRegistry *registry;
@property(readonly, nonatomic) NSObject<FlutterBinaryMessenger> *messenger;
@end
@implementation CameraPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
FlutterMethodChannel *channel =
[FlutterMethodChannel methodChannelWithName:@"plugins.flutter.io/camera_avfoundation"
binaryMessenger:[registrar messenger]];
CameraPlugin *instance = [[CameraPlugin alloc] initWithRegistry:[registrar textures]
messenger:[registrar messenger]];
[registrar addMethodCallDelegate:instance channel:channel];
}
- (instancetype)initWithRegistry:(NSObject<FlutterTextureRegistry> *)registry
messenger:(NSObject<FlutterBinaryMessenger> *)messenger {
self = [super init];
NSAssert(self, @"super init cannot be nil");
_registry = [[FLTThreadSafeTextureRegistry alloc] initWithTextureRegistry:registry];
_messenger = messenger;
_captureSessionQueue = dispatch_queue_create("io.flutter.camera.captureSessionQueue", NULL);
dispatch_queue_set_specific(_captureSessionQueue, FLTCaptureSessionQueueSpecific,
(void *)FLTCaptureSessionQueueSpecific, NULL);
[self initDeviceEventMethodChannel];
[self startOrientationListener];
return self;
}
- (void)initDeviceEventMethodChannel {
FlutterMethodChannel *methodChannel = [FlutterMethodChannel
methodChannelWithName:@"plugins.flutter.io/camera_avfoundation/fromPlatform"
binaryMessenger:_messenger];
_deviceEventMethodChannel =
[[FLTThreadSafeMethodChannel alloc] initWithMethodChannel:methodChannel];
}
- (void)detachFromEngineForRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
[UIDevice.currentDevice endGeneratingDeviceOrientationNotifications];
}
- (void)startOrientationListener {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
}
- (void)orientationChanged:(NSNotification *)note {
UIDevice *device = note.object;
UIDeviceOrientation orientation = device.orientation;
if (orientation == UIDeviceOrientationFaceUp || orientation == UIDeviceOrientationFaceDown) {
// Do not change when oriented flat.
return;
}
__weak typeof(self) weakSelf = self;
dispatch_async(self.captureSessionQueue, ^{
// `FLTCam::setDeviceOrientation` must be called on capture session queue.
[weakSelf.camera setDeviceOrientation:orientation];
// `CameraPlugin::sendDeviceOrientation` can be called on any queue.
[weakSelf sendDeviceOrientation:orientation];
});
}
- (void)sendDeviceOrientation:(UIDeviceOrientation)orientation {
[_deviceEventMethodChannel
invokeMethod:@"orientation_changed"
arguments:@{@"orientation" : FLTGetStringForUIDeviceOrientation(orientation)}];
}
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
// Invoke the plugin on another dispatch queue to avoid blocking the UI.
__weak typeof(self) weakSelf = self;
dispatch_async(self.captureSessionQueue, ^{
FLTThreadSafeFlutterResult *threadSafeResult =
[[FLTThreadSafeFlutterResult alloc] initWithResult:result];
[weakSelf handleMethodCallAsync:call result:threadSafeResult];
});
}
- (void)handleMethodCallAsync:(FlutterMethodCall *)call
result:(FLTThreadSafeFlutterResult *)result {
if ([@"availableCameras" isEqualToString:call.method]) {
NSMutableArray *discoveryDevices =
[@[ AVCaptureDeviceTypeBuiltInWideAngleCamera, AVCaptureDeviceTypeBuiltInTelephotoCamera ]
mutableCopy];
if (@available(iOS 13.0, *)) {
[discoveryDevices addObject:AVCaptureDeviceTypeBuiltInUltraWideCamera];
}
AVCaptureDeviceDiscoverySession *discoverySession = [AVCaptureDeviceDiscoverySession
discoverySessionWithDeviceTypes:discoveryDevices
mediaType:AVMediaTypeVideo
position:AVCaptureDevicePositionUnspecified];
NSArray<AVCaptureDevice *> *devices = discoverySession.devices;
NSMutableArray<NSDictionary<NSString *, NSObject *> *> *reply =
[[NSMutableArray alloc] initWithCapacity:devices.count];
for (AVCaptureDevice *device in devices) {
NSString *lensFacing;
switch ([device position]) {
case AVCaptureDevicePositionBack:
lensFacing = @"back";
break;
case AVCaptureDevicePositionFront:
lensFacing = @"front";
break;
case AVCaptureDevicePositionUnspecified:
lensFacing = @"external";
break;
}
[reply addObject:@{
@"name" : [device uniqueID],
@"lensFacing" : lensFacing,
@"sensorOrientation" : @90,
}];
}
[result sendSuccessWithData:reply];
} else if ([@"create" isEqualToString:call.method]) {
[self handleCreateMethodCall:call result:result];
} else if ([@"startImageStream" isEqualToString:call.method]) {
[_camera startImageStreamWithMessenger:_messenger];
[result sendSuccess];
} else if ([@"stopImageStream" isEqualToString:call.method]) {
[_camera stopImageStream];
[result sendSuccess];
} else if ([@"receivedImageStreamData" isEqualToString:call.method]) {
[_camera receivedImageStreamData];
[result sendSuccess];
} else {
NSDictionary *argsMap = call.arguments;
NSUInteger cameraId = ((NSNumber *)argsMap[@"cameraId"]).unsignedIntegerValue;
if ([@"initialize" isEqualToString:call.method]) {
NSString *videoFormatValue = ((NSString *)argsMap[@"imageFormatGroup"]);
[_camera setVideoFormat:FLTGetVideoFormatFromString(videoFormatValue)];
__weak CameraPlugin *weakSelf = self;
_camera.onFrameAvailable = ^{
if (![weakSelf.camera isPreviewPaused]) {
[weakSelf.registry textureFrameAvailable:cameraId];
}
};
FlutterMethodChannel *methodChannel = [FlutterMethodChannel
methodChannelWithName:
[NSString stringWithFormat:@"plugins.flutter.io/camera_avfoundation/camera%lu",
(unsigned long)cameraId]
binaryMessenger:_messenger];
FLTThreadSafeMethodChannel *threadSafeMethodChannel =
[[FLTThreadSafeMethodChannel alloc] initWithMethodChannel:methodChannel];
_camera.methodChannel = threadSafeMethodChannel;
[threadSafeMethodChannel
invokeMethod:@"initialized"
arguments:@{
@"previewWidth" : @(_camera.previewSize.width),
@"previewHeight" : @(_camera.previewSize.height),
@"exposureMode" : FLTGetStringForFLTExposureMode([_camera exposureMode]),
@"focusMode" : FLTGetStringForFLTFocusMode([_camera focusMode]),
@"exposurePointSupported" :
@([_camera.captureDevice isExposurePointOfInterestSupported]),
@"focusPointSupported" : @([_camera.captureDevice isFocusPointOfInterestSupported]),
}];
[self sendDeviceOrientation:[UIDevice currentDevice].orientation];
[_camera start];
[result sendSuccess];
} else if ([@"takePicture" isEqualToString:call.method]) {
[_camera captureToFile:result];
} else if ([@"dispose" isEqualToString:call.method]) {
[_registry unregisterTexture:cameraId];
[_camera close];
[result sendSuccess];
} else if ([@"prepareForVideoRecording" isEqualToString:call.method]) {
[self.camera setUpCaptureSessionForAudio];
[result sendSuccess];
} else if ([@"startVideoRecording" isEqualToString:call.method]) {
BOOL enableStream = [call.arguments[@"enableStream"] boolValue];
if (enableStream) {
[_camera startVideoRecordingWithResult:result messengerForStreaming:_messenger];
} else {
[_camera startVideoRecordingWithResult:result];
}
} else if ([@"stopVideoRecording" isEqualToString:call.method]) {
[_camera stopVideoRecordingWithResult:result];
} else if ([@"pauseVideoRecording" isEqualToString:call.method]) {
[_camera pauseVideoRecordingWithResult:result];
} else if ([@"resumeVideoRecording" isEqualToString:call.method]) {
[_camera resumeVideoRecordingWithResult:result];
} else if ([@"getMaxZoomLevel" isEqualToString:call.method]) {
[_camera getMaxZoomLevelWithResult:result];
} else if ([@"getMinZoomLevel" isEqualToString:call.method]) {
[_camera getMinZoomLevelWithResult:result];
} else if ([@"setZoomLevel" isEqualToString:call.method]) {
CGFloat zoom = ((NSNumber *)argsMap[@"zoom"]).floatValue;
[_camera setZoomLevel:zoom Result:result];
} else if ([@"setFlashMode" isEqualToString:call.method]) {
[_camera setFlashModeWithResult:result mode:call.arguments[@"mode"]];
} else if ([@"setExposureMode" isEqualToString:call.method]) {
[_camera setExposureModeWithResult:result mode:call.arguments[@"mode"]];
} else if ([@"setExposurePoint" isEqualToString:call.method]) {
BOOL reset = ((NSNumber *)call.arguments[@"reset"]).boolValue;
double x = 0.5;
double y = 0.5;
if (!reset) {
x = ((NSNumber *)call.arguments[@"x"]).doubleValue;
y = ((NSNumber *)call.arguments[@"y"]).doubleValue;
}
[_camera setExposurePointWithResult:result x:x y:y];
} else if ([@"getMinExposureOffset" isEqualToString:call.method]) {
[result sendSuccessWithData:@(_camera.captureDevice.minExposureTargetBias)];
} else if ([@"getMaxExposureOffset" isEqualToString:call.method]) {
[result sendSuccessWithData:@(_camera.captureDevice.maxExposureTargetBias)];
} else if ([@"getExposureOffsetStepSize" isEqualToString:call.method]) {
[result sendSuccessWithData:@(0.0)];
} else if ([@"setExposureOffset" isEqualToString:call.method]) {
[_camera setExposureOffsetWithResult:result
offset:((NSNumber *)call.arguments[@"offset"]).doubleValue];
} else if ([@"lockCaptureOrientation" isEqualToString:call.method]) {
[_camera lockCaptureOrientationWithResult:result orientation:call.arguments[@"orientation"]];
} else if ([@"unlockCaptureOrientation" isEqualToString:call.method]) {
[_camera unlockCaptureOrientationWithResult:result];
} else if ([@"setFocusMode" isEqualToString:call.method]) {
[_camera setFocusModeWithResult:result mode:call.arguments[@"mode"]];
} else if ([@"setFocusPoint" isEqualToString:call.method]) {
BOOL reset = ((NSNumber *)call.arguments[@"reset"]).boolValue;
double x = 0.5;
double y = 0.5;
if (!reset) {
x = ((NSNumber *)call.arguments[@"x"]).doubleValue;
y = ((NSNumber *)call.arguments[@"y"]).doubleValue;
}
[_camera setFocusPointWithResult:result x:x y:y];
} else if ([@"pausePreview" isEqualToString:call.method]) {
[_camera pausePreviewWithResult:result];
} else if ([@"resumePreview" isEqualToString:call.method]) {
[_camera resumePreviewWithResult:result];
} else {
[result sendNotImplemented];
}
}
}
- (void)handleCreateMethodCall:(FlutterMethodCall *)call
result:(FLTThreadSafeFlutterResult *)result {
// Create FLTCam only if granted camera access (and audio access if audio is enabled)
__weak typeof(self) weakSelf = self;
FLTRequestCameraPermissionWithCompletionHandler(^(FlutterError *error) {
typeof(self) strongSelf = weakSelf;
if (!strongSelf) return;
if (error) {
[result sendFlutterError:error];
} else {
// Request audio permission on `create` call with `enableAudio` argument instead of the
// `prepareForVideoRecording` call. This is because `prepareForVideoRecording` call is
// optional, and used as a workaround to fix a missing frame issue on iOS.
BOOL audioEnabled = [call.arguments[@"enableAudio"] boolValue];
if (audioEnabled) {
// Setup audio capture session only if granted audio access.
FLTRequestAudioPermissionWithCompletionHandler(^(FlutterError *error) {
// cannot use the outter `strongSelf`
typeof(self) strongSelf = weakSelf;
if (!strongSelf) return;
if (error) {
[result sendFlutterError:error];
} else {
[strongSelf createCameraOnSessionQueueWithCreateMethodCall:call result:result];
}
});
} else {
[strongSelf createCameraOnSessionQueueWithCreateMethodCall:call result:result];
}
}
});
}
- (void)createCameraOnSessionQueueWithCreateMethodCall:(FlutterMethodCall *)createMethodCall
result:(FLTThreadSafeFlutterResult *)result {
__weak typeof(self) weakSelf = self;
dispatch_async(self.captureSessionQueue, ^{
typeof(self) strongSelf = weakSelf;
if (!strongSelf) return;
NSString *cameraName = createMethodCall.arguments[@"cameraName"];
NSString *resolutionPreset = createMethodCall.arguments[@"resolutionPreset"];
NSNumber *enableAudio = createMethodCall.arguments[@"enableAudio"];
NSError *error;
FLTCam *cam = [[FLTCam alloc] initWithCameraName:cameraName
resolutionPreset:resolutionPreset
enableAudio:[enableAudio boolValue]
orientation:[[UIDevice currentDevice] orientation]
captureSessionQueue:strongSelf.captureSessionQueue
error:&error];
if (error) {
[result sendError:error];
} else {
if (strongSelf.camera) {
[strongSelf.camera close];
}
strongSelf.camera = cam;
[strongSelf.registry registerTexture:cam
completion:^(int64_t textureId) {
[result sendSuccessWithData:@{
@"cameraId" : @(textureId),
}];
}];
}
});
}
@end