Skip to content

Commit bbfd87e

Browse files
[camera] Dart Interface for camera refactor (flutter-team-archive#1832)
1 parent 4f339fb commit bbfd87e

10 files changed

Lines changed: 360 additions & 0 deletions

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
A Flutter plugin for iOS and Android allowing access to the device cameras.
66

7+
*Note*: This plugin is still under development, and some APIs might not be available yet. We are working on a refactor which can be followed here: [issue](https://github.com/flutter/flutter/issues/31225)
8+
79
## Features:
810

911
* Display live camera preview in a widget.

lib/new/camera.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
export 'src/camera_controller.dart';
6+
export 'src/camera_testing.dart';
7+
export 'src/common/camera_interface.dart';
8+
export 'src/common/native_texture.dart';

lib/new/src/camera_controller.dart

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:async';
6+
7+
import 'package:flutter/foundation.dart';
8+
9+
import 'common/camera_interface.dart';
10+
11+
/// Controls a device camera.
12+
///
13+
/// Use [CameraController.availableCameras] to get a list of available cameras.
14+
///
15+
/// This class is used as a simple interface that works for Android and iOS.
16+
///
17+
/// When using iOS, simultaneously calling [start] on two [CameraController]s
18+
/// will throw a [PlatformException].
19+
///
20+
/// When using Android, simultaneously calling [start] on two
21+
/// [CameraController]s may throw a [PlatformException] depending on the
22+
/// hardware resources of the device.
23+
class CameraController {
24+
/// Default constructor.
25+
///
26+
/// Use [CameraController.availableCameras] to get a list of available
27+
/// cameras.
28+
///
29+
/// This will choose the best [CameraConfigurator] for the current device.
30+
factory CameraController({@required CameraDescription description}) {
31+
assert(description != null);
32+
return CameraController._(
33+
description: description,
34+
configurator: _createDefaultConfigurator(description),
35+
api: _getCameraApi(description),
36+
);
37+
}
38+
39+
CameraController._({
40+
@required this.description,
41+
@required this.configurator,
42+
@required this.api,
43+
}) : assert(description != null),
44+
assert(configurator != null),
45+
assert(api != null);
46+
47+
/// Constructor for defining your own [CameraConfigurator].
48+
///
49+
/// Use [CameraController.availableCameras] to get a list of available
50+
/// cameras.
51+
factory CameraController.customConfigurator({
52+
@required CameraDescription description,
53+
@required CameraConfigurator configurator,
54+
}) {
55+
return CameraController._(
56+
description: description,
57+
configurator: configurator,
58+
api: _getCameraApi(description),
59+
);
60+
}
61+
62+
/// Details for the camera this controller accesses.
63+
final CameraDescription description;
64+
65+
/// Configurator used to control the camera.
66+
final CameraConfigurator configurator;
67+
68+
/// Api used by the [configurator].
69+
final CameraApi api;
70+
71+
/// Retrieves a list of available cameras for the current device.
72+
///
73+
/// This will choose the best [CameraAPI] for the current device.
74+
static Future<List<CameraDescription>> availableCameras() async {
75+
throw UnimplementedError('$defaultTargetPlatform not supported');
76+
}
77+
78+
/// Begins the flow of data between the inputs and outputs connected the camera instance.
79+
Future<void> start() => configurator.start();
80+
81+
/// Stops the flow of data between the inputs and outputs connected the camera instance.
82+
Future<void> stop() => configurator.stop();
83+
84+
/// Deallocate all resources and disables further use of the controller.
85+
Future<void> dispose() => configurator.dispose();
86+
87+
static CameraConfigurator _createDefaultConfigurator(
88+
CameraDescription description,
89+
) {
90+
final CameraApi api = _getCameraApi(description);
91+
switch (api) {
92+
case CameraApi.android:
93+
throw UnimplementedError();
94+
case CameraApi.iOS:
95+
throw UnimplementedError();
96+
case CameraApi.supportAndroid:
97+
throw UnimplementedError();
98+
}
99+
100+
return null; // Unreachable code
101+
}
102+
103+
static CameraApi _getCameraApi(CameraDescription description) {
104+
throw ArgumentError.value(
105+
description.runtimeType,
106+
'description.runtimeType',
107+
'Failed to get $CameraApi from',
108+
);
109+
}
110+
}

lib/new/src/camera_testing.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/foundation.dart';
6+
import 'package:flutter/services.dart';
7+
8+
import 'common/camera_channel.dart';
9+
10+
@visibleForTesting
11+
class CameraTesting {
12+
CameraTesting._();
13+
14+
static final MethodChannel channel = CameraChannel.channel;
15+
static int get nextHandle => CameraChannel.nextHandle;
16+
static set nextHandle(int handle) => CameraChannel.nextHandle = handle;
17+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/services.dart';
6+
7+
typedef CameraCallback = void Function(dynamic result);
8+
9+
// Non exported class
10+
class CameraChannel {
11+
static final Map<int, dynamic> callbacks = <int, CameraCallback>{};
12+
13+
static final MethodChannel channel = const MethodChannel(
14+
'flutter.plugins.io/camera',
15+
)..setMethodCallHandler(
16+
(MethodCall call) async {
17+
assert(call.method == 'handleCallback');
18+
19+
final int handle = call.arguments['handle'];
20+
if (callbacks[handle] != null) callbacks[handle](call.arguments);
21+
},
22+
);
23+
24+
static int nextHandle = 0;
25+
26+
static void registerCallback(int handle, CameraCallback callback) {
27+
assert(handle != null);
28+
assert(CameraCallback != null);
29+
30+
assert(!callbacks.containsKey(handle));
31+
callbacks[handle] = callback;
32+
}
33+
34+
static void unregisterCallback(int handle) {
35+
assert(handle != null);
36+
callbacks.remove(handle);
37+
}
38+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:async';
6+
7+
/// Available APIs compatible with [CameraController].
8+
enum CameraApi {
9+
/// [Camera2](https://developer.android.com/reference/android/hardware/camera2/package-summary)
10+
android,
11+
12+
/// [AVFoundation](https://developer.apple.com/av-foundation/)
13+
iOS,
14+
15+
/// [Camera](https://developer.android.com/reference/android/hardware/Camera)
16+
supportAndroid,
17+
}
18+
19+
/// Location of the camera on the device.
20+
enum LensDirection { front, back, unknown }
21+
22+
/// Abstract class used to create a common interface to describe a camera from different platform APIs.
23+
///
24+
/// This provides information such as the [name] of the camera and [direction]
25+
/// the lens face.
26+
abstract class CameraDescription {
27+
/// Location of the camera on the device.
28+
LensDirection get direction;
29+
30+
/// Identifier for this camera.
31+
String get name;
32+
}
33+
34+
/// Abstract class used to create a common interface across platform APIs.
35+
abstract class CameraConfigurator {
36+
/// Texture id that can be used to send camera frames to a [Texture] widget.
37+
///
38+
/// You must call [addPreviewTexture] first or this will only return null.
39+
int get previewTextureId;
40+
41+
/// Begins the flow of data between the inputs and outputs connected the camera instance.
42+
///
43+
/// This will start updating the texture with id: [previewTextureId].
44+
Future<void> start();
45+
46+
/// Stops the flow of data between the inputs and outputs connected the camera instance.
47+
Future<void> stop();
48+
49+
/// Dispose all resources and disables further use of this configurator.
50+
Future<void> dispose();
51+
52+
/// Retrieves a valid texture Id to be used with a [Texture] widget.
53+
Future<int> addPreviewTexture();
54+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'camera_channel.dart';
6+
7+
mixin NativeMethodCallHandler {
8+
/// Identifier for an object on the native side of the plugin.
9+
///
10+
/// Only used internally and for debugging.
11+
final int handle = CameraChannel.nextHandle++;
12+
}
13+
14+
mixin CameraMappable {
15+
/// Creates a description of the object compatible with [PlatformChannel]s.
16+
///
17+
/// Only used as an internal method and for debugging.
18+
Map<String, dynamic> asMap();
19+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:async';
6+
7+
import 'package:flutter/foundation.dart';
8+
9+
import 'camera_channel.dart';
10+
import 'camera_mixins.dart';
11+
12+
/// Used to allocate a buffer for displaying a preview camera texture.
13+
///
14+
/// This is used to for a developer to have a control over the
15+
/// `TextureRegistry.SurfaceTextureEntry` (Android) and FlutterTexture (iOS).
16+
/// This gives direct access to the textureId and can be reused with separate
17+
/// camera instances.
18+
///
19+
/// The [textureId] can be passed to a [Texture] widget.
20+
class NativeTexture with CameraMappable {
21+
NativeTexture._({@required int handle, @required this.textureId})
22+
: _handle = handle,
23+
assert(handle != null),
24+
assert(textureId != null);
25+
26+
final int _handle;
27+
28+
bool _isClosed = false;
29+
30+
/// Id that can be passed to a [Texture] widget.
31+
final int textureId;
32+
33+
static Future<NativeTexture> allocate() async {
34+
final int handle = CameraChannel.nextHandle++;
35+
36+
final int textureId = await CameraChannel.channel.invokeMethod<int>(
37+
'$NativeTexture#allocate',
38+
<String, dynamic>{'textureHandle': handle},
39+
);
40+
41+
return NativeTexture._(handle: handle, textureId: textureId);
42+
}
43+
44+
/// Deallocate this texture.
45+
Future<void> release() {
46+
if (_isClosed) return Future<void>.value();
47+
48+
_isClosed = true;
49+
return CameraChannel.channel.invokeMethod<void>(
50+
'$NativeTexture#release',
51+
<String, dynamic>{'handle': _handle},
52+
);
53+
}
54+
55+
@override
56+
Map<String, dynamic> asMap() {
57+
return <String, dynamic>{'handle': _handle};
58+
}
59+
}

pubspec.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ description: A Flutter plugin for getting information about and controlling the
33
camera on Android and iOS. Supports previewing the camera feed, capturing images, capturing video,
44
and streaming image buffers to dart.
55
version: 0.5.2+1
6+
publish_to: none
67
authors:
78
- Flutter Team <flutter-dev@googlegroups.com>
89
- Luigi Agosti <luigi@tengio.com>
@@ -17,6 +18,8 @@ dependencies:
1718
sdk: flutter
1819

1920
dev_dependencies:
21+
flutter_test:
22+
sdk: flutter
2023
path_provider: ^0.5.0
2124
video_player: ^0.10.0
2225

test/camera_test.dart

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/services.dart';
6+
import 'package:flutter_test/flutter_test.dart';
7+
import 'package:camera/new/src/camera_testing.dart';
8+
import 'package:camera/new/src/common/native_texture.dart';
9+
10+
void main() {
11+
group('Camera', () {
12+
final List<MethodCall> log = <MethodCall>[];
13+
14+
setUpAll(() {
15+
CameraTesting.channel
16+
.setMockMethodCallHandler((MethodCall methodCall) async {
17+
log.add(methodCall);
18+
switch (methodCall.method) {
19+
case 'NativeTexture#allocate':
20+
return 15;
21+
}
22+
23+
throw ArgumentError.value(
24+
methodCall.method,
25+
'methodCall.method',
26+
'No method found for',
27+
);
28+
});
29+
});
30+
31+
setUp(() {
32+
log.clear();
33+
CameraTesting.nextHandle = 0;
34+
});
35+
36+
group('$NativeTexture', () {
37+
test('allocate', () async {
38+
final NativeTexture texture = await NativeTexture.allocate();
39+
40+
expect(texture.textureId, 15);
41+
expect(log, <Matcher>[
42+
isMethodCall(
43+
'$NativeTexture#allocate',
44+
arguments: <String, dynamic>{'textureHandle': 0},
45+
)
46+
]);
47+
});
48+
});
49+
});
50+
}

0 commit comments

Comments
 (0)