Skip to content

Commit 9588cce

Browse files
authored
[camera] Change default Android implementation from camera_android to camera_android_camerax (#6629)
Breaking change: Changes default Android implementation from `camera_android` to `camera_android_camerax` via a breaking change and updates relevant documentation. ~<ins>**Note:**</ins> Planning to land this after #6608, as it is a semi-crucial fix.~ Done :)
1 parent ee8ee2e commit 9588cce

File tree

10 files changed

+101
-51
lines changed

10 files changed

+101
-51
lines changed

packages/camera/camera/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 0.11.0
2+
3+
* **Breaking Change** Changes the Android implementation of the camera plugin from `camera_android`
4+
to `camera_android_camerax`, which has better support for a wider range of devices. The CameraX
5+
implementation full feature parity with `camera_android` except for the limitations listed in
6+
`README.md`. To continue using `camera_android`, follow [these instructions](https://pub.dev/packages/camera_android#usage).
7+
18
## 0.10.6
29

310
* Adds support to control video fps and bitrate. See `CameraController` constructor.

packages/camera/camera/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ Change the minimum Android sdk version to 21 (or higher) in your `android/app/bu
4545
minSdkVersion 21
4646
```
4747

48-
It's important to note that the `MediaRecorder` class is not working properly on emulators, as stated in the documentation: https://developer.android.com/reference/android/media/MediaRecorder. Specifically, when recording a video with sound enabled and trying to play it back, the duration won't be correct and you will only see the first frame.
48+
The endorsed [`camera_android_camerax`][2] implementation of the camera plugin built with CameraX has
49+
better support for more devices than `camera_android`, but has some limitations; please see [this list][3]
50+
for more details. If you wish to use the [`camera_android`][4] implementation of the camera plugin
51+
built with Camera2 that lacks these limitations, please follow [these instructions][5].
4952

5053
### Web integration
5154

@@ -167,3 +170,7 @@ class _CameraAppState extends State<CameraApp> {
167170
For a more elaborate usage example see [here](https://github.com/flutter/packages/tree/main/packages/camera/camera/example).
168171

169172
[1]: https://pub.dev/packages/camera_web#limitations-on-the-web-platform
173+
[2]: https://pub.dev/packages/camera_android_camerax
174+
[3]: https://pub.dev/packages/camera_android_camerax#limitations
175+
[4]: https://pub.dev/packages/camera_android
176+
[5]: https://pub.dev/packages/camera_android#usage

packages/camera/camera/example/integration_test/camera_test.dart

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,40 @@ void main() {
145145
skip: true,
146146
);
147147

148+
testWidgets('Video capture records valid video', (WidgetTester tester) async {
149+
final List<CameraDescription> cameras = await availableCameras();
150+
if (cameras.isEmpty) {
151+
return;
152+
}
153+
154+
final CameraController controller = CameraController(
155+
cameras[0],
156+
ResolutionPreset.low,
157+
enableAudio: false,
158+
);
159+
await controller.initialize();
160+
await controller.prepareForVideoRecording();
161+
162+
await controller.startVideoRecording();
163+
final int recordingStart = DateTime.now().millisecondsSinceEpoch;
164+
165+
sleep(const Duration(seconds: 2));
166+
167+
final XFile file = await controller.stopVideoRecording();
168+
final int recordingTime =
169+
DateTime.now().millisecondsSinceEpoch - recordingStart;
170+
171+
final File videoFile = File(file.path);
172+
final VideoPlayerController videoController = VideoPlayerController.file(
173+
videoFile,
174+
);
175+
await videoController.initialize();
176+
final int duration = videoController.value.duration.inMilliseconds;
177+
await videoController.dispose();
178+
179+
expect(duration, lessThan(recordingTime));
180+
});
181+
148182
testWidgets('Pause and resume video recording', (WidgetTester tester) async {
149183
final List<CameraDescription> cameras = await availableCameras();
150184
if (cameras.isEmpty) {
@@ -162,26 +196,21 @@ void main() {
162196

163197
int startPause;
164198
int timePaused = 0;
199+
const int pauseIterations = 2;
165200

166201
await controller.startVideoRecording();
167202
final int recordingStart = DateTime.now().millisecondsSinceEpoch;
168203
sleep(const Duration(milliseconds: 500));
169204

170-
await controller.pauseVideoRecording();
171-
startPause = DateTime.now().millisecondsSinceEpoch;
172-
sleep(const Duration(milliseconds: 500));
173-
await controller.resumeVideoRecording();
174-
timePaused += DateTime.now().millisecondsSinceEpoch - startPause;
175-
176-
sleep(const Duration(milliseconds: 500));
177-
178-
await controller.pauseVideoRecording();
179-
startPause = DateTime.now().millisecondsSinceEpoch;
180-
sleep(const Duration(milliseconds: 500));
181-
await controller.resumeVideoRecording();
182-
timePaused += DateTime.now().millisecondsSinceEpoch - startPause;
205+
for (int i = 0; i < pauseIterations; i++) {
206+
await controller.pauseVideoRecording();
207+
startPause = DateTime.now().millisecondsSinceEpoch;
208+
sleep(const Duration(milliseconds: 500));
209+
await controller.resumeVideoRecording();
210+
timePaused += DateTime.now().millisecondsSinceEpoch - startPause;
183211

184-
sleep(const Duration(milliseconds: 500));
212+
sleep(const Duration(milliseconds: 500));
213+
}
185214

186215
final XFile file = await controller.stopVideoRecording();
187216
final int recordingTime =

packages/camera/camera/pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: A Flutter plugin for controlling the camera. Supports previewing
44
Dart.
55
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
7-
version: 0.10.6
7+
version: 0.11.0
88

99
environment:
1010
sdk: ^3.2.3
@@ -14,14 +14,14 @@ flutter:
1414
plugin:
1515
platforms:
1616
android:
17-
default_package: camera_android
17+
default_package: camera_android_camerax
1818
ios:
1919
default_package: camera_avfoundation
2020
web:
2121
default_package: camera_web
2222

2323
dependencies:
24-
camera_android: ^0.10.9
24+
camera_android_camerax: ^0.6.5
2525
camera_avfoundation: ^0.9.15
2626
camera_platform_interface: ^2.6.0
2727
camera_web: ^0.3.3

packages/camera/camera_android/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.10.9+2
2+
3+
* Updates `README.md` to reflect the fact that the `camera_android_camerax` camera plugin implementation
4+
is the endorsed Android implementation for `camera: ^0.11.0`.
5+
16
## 0.10.9+1
27

38
* Changes the visibility of a number of fields to `@VisibleForTesting` in order simplify testing.
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
# camera\_android
22

3-
The Android implementation of [`camera`][1].
4-
5-
*Note*: [`camera_android_camerax`][3] will become the default implementation of
6-
`camera` on Android by May 2024, so **we strongly encourage you to opt into it**
7-
by using [these instructions][4]. If any [limitations][5] of `camera_android_camerax`
8-
prevent you from using it or if you run into any problems, please report these
9-
issues under [`flutter/flutter`][5] with `[camerax]` in the title.
3+
An Android implementation of [`camera`][1] built with the [Camera2 library][4].
104

115
## Usage
126

13-
This package is [endorsed][2], which means you can simply use `camera`
14-
normally. This package will be automatically included in your app when you do,
15-
so you do not need to add it to your `pubspec.yaml`.
7+
As of `camera: ^0.11.0`, to use this plugin instead of [`camera_android_camerax`][3],
8+
run
9+
10+
```sh
11+
$ flutter pub add camera_android
12+
```
1613

17-
However, if you `import` this package to use any of its APIs directly, you
18-
should add it to your `pubspec.yaml` as usual.
14+
## Limitation of testing video recording on emulators
15+
`MediaRecorder` does not work properly on emulators, as stated in [the documentation][5]. Specifically,
16+
when recording a video with sound enabled and trying to play it back, the duration won't be correct and
17+
you will only see the first frame.
1918

2019
[1]: https://pub.dev/packages/camera
2120
[2]: https://flutter.dev/docs/development/packages-and-plugins/developing-packages#endorsed-federated-plugin
2221
[3]: https://pub.dev/packages/camera_android_camerax
23-
[4]: https://pub.dev/packages/camera_android_camerax#usage
24-
[5]: https://pub.dev/packages/camera_android_camerax#limitations
22+
[4]: https://developer.android.com/media/camera/camera2
23+
[5]: https://developer.android.com/reference/android/media/MediaRecorder

packages/camera/camera_android/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Android implementation of the camera plugin.
33
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_android
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
55

6-
version: 0.10.9+1
6+
version: 0.10.9+2
77

88
environment:
99
sdk: ^3.1.0

packages/camera/camera_android_camerax/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.6.5+1
2+
3+
* Updates `README.md` to reflect the fact that the `camera_android_camerax` camera plugin implementation
4+
is the endorsed Android implementation for `camera: ^0.11.0`.
5+
16
## 0.6.5
27

38
* Modifies `stopVideoRecording` to ensure that the method only returns when CameraX reports that the

packages/camera/camera_android_camerax/README.md

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
# camera\_android\_camerax
22

3-
An Android implementation of [`camera`][1] that uses the [CameraX library][2].
3+
The Android implementation of [`camera`][1] built with the [CameraX library][2].
44

5-
*Note*: This implementation will become the default implementation of `camera`
6-
on Android by May 2024, so **we strongly encourage you to opt into it**
7-
by using [the instructions](#usage) below. If any of [the limitations](#limitations)
8-
prevent you from using `camera_android_camerax` or if you run into any problems,
9-
please report these issues under [`flutter/flutter`][5] with `[camerax]` in
10-
the title.
5+
*Note*: If any of [the limitations](#limitations) prevent you from using
6+
using `camera_android_camerax` or if you run into any problems, please report
7+
report these issues under [`flutter/flutter`][5] with `[camerax]` in the title.
8+
You may also opt back into the [`camera_android`][9] implementation if you need.
119

1210
## Usage
1311

14-
To use this plugin instead of [`camera_android`][4], run
12+
As of `camera: ^0.11.0`, this package is [endorsed][3], which means you can
13+
simply use `camera` normally. This package will be automatically be included
14+
in your app when you do, so you do not need to add it to your `pubspec.yaml`.
1515

16-
```sh
17-
$ flutter pub add camera_android_camerax
18-
```
19-
20-
from your project's root directory.
16+
However, if you `import` this package to use any of its APIs directly, you
17+
should add it to your `pubspec.yaml` as usual.
2118

2219
## Limitations
2320

@@ -43,9 +40,9 @@ due to this not currently being supported by CameraX.
4340

4441
### 240p resolution configuration for video recording
4542

46-
240p resolution configuration for video recording is unsupported by CameraX,
47-
and thus, the plugin will fall back to 480p if configured with a
48-
`ResolutionPreset`.
43+
240p resolution configuration for video recording is unsupported by CameraX, and thus,
44+
the plugin will fall back to target 480p (`ResolutionPreset.medium`) if configured with
45+
`ResolutionPreset.low`.
4946

5047
### Setting maximum duration and stream options for video capture
5148

@@ -62,10 +59,11 @@ For more information on contributing to this plugin, see [`CONTRIBUTING.md`](CON
6259

6360
[1]: https://pub.dev/packages/camera
6461
[2]: https://developer.android.com/training/camerax
65-
[3]: https://docs.flutter.dev/packages-and-plugins/developing-packages#non-endorsed-federated-plugin
62+
[3]: https://flutter.dev/docs/development/packages-and-plugins/developing-packages#endorsed-federated-plugin
6663
[4]: https://pub.dev/packages/camera_android
6764
[5]: https://github.com/flutter/flutter/issues/new/choose
6865
[6]: https://developer.android.com/media/camera/camerax/architecture#combine-use-cases
6966
[7]: https://developer.android.com/reference/android/hardware/camera2/CameraMetadata#INFO_SUPPORTED_HARDWARE_LEVEL_3
7067
[8]: https://developer.android.com/reference/android/hardware/camera2/CameraMetadata#INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED
68+
[9]: https://pub.dev/packages/camera_android#usage
7169
[148013]: https://github.com/flutter/flutter/issues/148013

packages/camera/camera_android_camerax/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: camera_android_camerax
22
description: Android implementation of the camera plugin using the CameraX library.
33
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_android_camerax
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
5-
version: 0.6.5
5+
version: 0.6.5+1
66

77
environment:
88
sdk: ^3.1.0

0 commit comments

Comments
 (0)