Skip to content

Commit 5e67d84

Browse files
authored
[camera] Add zero-shutter-lag support to camera_android_camerax and camera_avfoundation (#13199)
Platform implementation portion of #12374, split out per the [federated plugin process](https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changing-federated-plugins) now that `camera_platform_interface` 2.15.0 is published (#12996). No changes from what was reviewed there, other than rebasing onto `main` and resolving version/CHANGELOG conflicts. Implements `isZeroShutterLagSupported` / `setZeroShutterLagEnabled` in: - `camera_android_camerax` (0.7.6) - `camera_avfoundation` (0.11.1, iOS 17+) Part of flutter/flutter#190598 ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 618afba commit 5e67d84

29 files changed

Lines changed: 825 additions & 15 deletions

‎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.7.6
2+
3+
* Adds support for zero-shutter-lag still image capture via
4+
`isZeroShutterLagSupported` and `setZeroShutterLagEnabled`.
5+
16
## 0.7.5+2
27

38
* Updates Robolectric to 4.17 and adds the JVM arguments it requires for unit tests.

‎packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/CameraInfoProxyApi.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import androidx.camera.core.CameraInfo;
1010
import androidx.camera.core.CameraSelector;
1111
import androidx.camera.core.ExperimentalLensFacing;
12+
import androidx.camera.core.ExperimentalZeroShutterLag;
1213
import androidx.camera.core.ExposureState;
1314

1415
/**
@@ -61,4 +62,10 @@ public LiveDataProxyApi.LiveDataWrapper getZoomState(CameraInfo pigeonInstance)
6162
return new LiveDataProxyApi.LiveDataWrapper(
6263
pigeonInstance.getZoomState(), LiveDataSupportedType.ZOOM_STATE);
6364
}
65+
66+
@Override
67+
@OptIn(markerClass = ExperimentalZeroShutterLag.class)
68+
public boolean isZslSupported(CameraInfo pigeonInstance) {
69+
return pigeonInstance.isZslSupported();
70+
}
6471
}

‎packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/CameraXLibrary.g.kt‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,6 +2199,13 @@ abstract class PigeonApiCameraInfo(
21992199
pigeon_instance: androidx.camera.core.CameraInfo
22002200
): io.flutter.plugins.camerax.LiveDataProxyApi.LiveDataWrapper
22012201

2202+
/**
2203+
* Returns whether the camera supports zero-shutter-lag capture.
2204+
*
2205+
* See https://developer.android.com/reference/androidx/camera/core/CameraInfo#isZslSupported().
2206+
*/
2207+
abstract fun isZslSupported(pigeon_instance: androidx.camera.core.CameraInfo): Boolean
2208+
22022209
companion object {
22032210
@Suppress("LocalVariableName")
22042211
fun setUpMessageHandlers(binaryMessenger: BinaryMessenger, api: PigeonApiCameraInfo?) {
@@ -2247,6 +2254,28 @@ abstract class PigeonApiCameraInfo(
22472254
channel.setMessageHandler(null)
22482255
}
22492256
}
2257+
run {
2258+
val channel =
2259+
BasicMessageChannel<Any?>(
2260+
binaryMessenger,
2261+
"dev.flutter.pigeon.camera_android_camerax.CameraInfo.isZslSupported",
2262+
codec)
2263+
if (api != null) {
2264+
channel.setMessageHandler { message, reply ->
2265+
val args = message as List<Any?>
2266+
val pigeon_instanceArg = args[0] as androidx.camera.core.CameraInfo
2267+
val wrapped: List<Any?> =
2268+
try {
2269+
listOf(api.isZslSupported(pigeon_instanceArg))
2270+
} catch (exception: Throwable) {
2271+
CameraXLibraryPigeonUtils.wrapError(exception)
2272+
}
2273+
reply.reply(wrapped)
2274+
}
2275+
} else {
2276+
channel.setMessageHandler(null)
2277+
}
2278+
}
22502279
}
22512280
}
22522281

@@ -4248,7 +4277,8 @@ abstract class PigeonApiImageCapture(
42484277
resolutionSelector: androidx.camera.core.resolutionselector.ResolutionSelector?,
42494278
targetRotation: Long?,
42504279
flashMode: CameraXFlashMode?,
4251-
jpegQuality: Long?
4280+
jpegQuality: Long?,
4281+
zeroShutterLagEnabled: Boolean?
42524282
): androidx.camera.core.ImageCapture
42534283

42544284
abstract fun resolutionSelector(
@@ -4290,11 +4320,16 @@ abstract class PigeonApiImageCapture(
42904320
val targetRotationArg = args[2] as Long?
42914321
val flashModeArg = args[3] as CameraXFlashMode?
42924322
val jpegQualityArg = args[4] as Long?
4323+
val zeroShutterLagEnabledArg = args[5] as Boolean?
42934324
val wrapped: List<Any?> =
42944325
try {
42954326
api.pigeonRegistrar.instanceManager.addDartCreatedInstance(
42964327
api.pigeon_defaultConstructor(
4297-
resolutionSelectorArg, targetRotationArg, flashModeArg, jpegQualityArg),
4328+
resolutionSelectorArg,
4329+
targetRotationArg,
4330+
flashModeArg,
4331+
jpegQualityArg,
4332+
zeroShutterLagEnabledArg),
42984333
pigeon_identifierArg)
42994334
listOf(null)
43004335
} catch (exception: Throwable) {

‎packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/ImageCaptureProxyApi.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import androidx.annotation.NonNull;
88
import androidx.annotation.Nullable;
9+
import androidx.annotation.OptIn;
10+
import androidx.camera.core.ExperimentalZeroShutterLag;
911
import androidx.camera.core.ImageCapture;
1012
import androidx.camera.core.ImageCaptureException;
1113
import androidx.camera.core.resolutionselector.ResolutionSelector;
@@ -37,15 +39,20 @@ public ProxyApiRegistrar getPigeonRegistrar() {
3739

3840
@NonNull
3941
@Override
42+
@OptIn(markerClass = ExperimentalZeroShutterLag.class)
4043
public ImageCapture pigeon_defaultConstructor(
4144
@Nullable ResolutionSelector resolutionSelector,
4245
@Nullable Long targetRotation,
4346
@Nullable CameraXFlashMode flashMode,
44-
@Nullable Long jpegQuality) {
47+
@Nullable Long jpegQuality,
48+
@Nullable Boolean zeroShutterLagEnabled) {
4549
final ImageCapture.Builder builder = new ImageCapture.Builder();
4650
if (targetRotation != null) {
4751
builder.setTargetRotation(targetRotation.intValue());
4852
}
53+
if (Boolean.TRUE.equals(zeroShutterLagEnabled)) {
54+
builder.setCaptureMode(ImageCapture.CAPTURE_MODE_ZERO_SHUTTER_LAG);
55+
}
4956
if (flashMode != null) {
5057
// This sets the requested flash mode, but may fail silently.
5158
switch (flashMode) {

‎packages/camera/camera_android_camerax/android/src/test/java/io/flutter/plugins/camerax/CameraInfoTest.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
package io.flutter.plugins.camerax;
66

77
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertTrue;
89
import static org.mockito.Mockito.mock;
910
import static org.mockito.Mockito.when;
1011

12+
import androidx.annotation.OptIn;
1113
import androidx.camera.core.CameraInfo;
1214
import androidx.camera.core.CameraSelector;
1315
import androidx.camera.core.CameraState;
16+
import androidx.camera.core.ExperimentalZeroShutterLag;
1417
import androidx.camera.core.ExposureState;
1518
import androidx.camera.core.ZoomState;
1619
import androidx.lifecycle.LiveData;
@@ -19,6 +22,17 @@
1922
import org.junit.Test;
2023

2124
public class CameraInfoTest {
25+
@Test
26+
@OptIn(markerClass = ExperimentalZeroShutterLag.class)
27+
public void isZslSupported_makesCallToRetrieveZslSupport() {
28+
final PigeonApiCameraInfo api = new TestProxyApiRegistrar().getPigeonApiCameraInfo();
29+
30+
final CameraInfo instance = mock(CameraInfo.class);
31+
when(instance.isZslSupported()).thenReturn(true);
32+
33+
assertTrue(api.isZslSupported(instance));
34+
}
35+
2236
@Test
2337
public void sensorRotationDegrees_makesCallToRetrieveSensorRotationDegrees() {
2438
final PigeonApiCameraInfo api = new TestProxyApiRegistrar().getPigeonApiCameraInfo();

‎packages/camera/camera_android_camerax/android/src/test/java/io/flutter/plugins/camerax/ImageCaptureTest.java‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import android.os.Looper;
1919
import android.view.Surface;
2020
import androidx.annotation.NonNull;
21+
import androidx.annotation.OptIn;
22+
import androidx.camera.core.ExperimentalZeroShutterLag;
2123
import androidx.camera.core.ImageCapture;
2224
import androidx.camera.core.ImageCaptureException;
2325
import androidx.camera.core.resolutionselector.ResolutionSelector;
@@ -44,7 +46,7 @@ public void pigeon_defaultConstructor_createsImageCaptureWithCorrectConfiguratio
4446
final long targetResolution = Surface.ROTATION_0;
4547
final ImageCapture imageCapture =
4648
api.pigeon_defaultConstructor(
47-
mockResolutionSelector, targetResolution, CameraXFlashMode.OFF, null);
49+
mockResolutionSelector, targetResolution, CameraXFlashMode.OFF, null, null);
4850

4951
assertEquals(imageCapture.getResolutionSelector(), mockResolutionSelector);
5052
assertEquals(imageCapture.getTargetRotation(), Surface.ROTATION_0);
@@ -60,14 +62,39 @@ public void pigeon_defaultConstructor_setsJpegQualityWhenProvided() {
6062
final long jpegQuality = 75;
6163
final ImageCapture imageCapture =
6264
api.pigeon_defaultConstructor(
63-
mockResolutionSelector, targetRotation, CameraXFlashMode.OFF, jpegQuality);
65+
mockResolutionSelector, targetRotation, CameraXFlashMode.OFF, jpegQuality, null);
6466

6567
assertEquals(imageCapture.getResolutionSelector(), mockResolutionSelector);
6668
assertEquals(imageCapture.getTargetRotation(), Surface.ROTATION_0);
6769
assertEquals(imageCapture.getFlashMode(), ImageCapture.FLASH_MODE_OFF);
6870
assertEquals(imageCapture.getJpegQuality(), 75);
6971
}
7072

73+
@Test
74+
@OptIn(markerClass = ExperimentalZeroShutterLag.class)
75+
public void pigeon_defaultConstructor_setsZeroShutterLagCaptureModeWhenEnabled() {
76+
final PigeonApiImageCapture api = new TestProxyApiRegistrar().getPigeonApiImageCapture();
77+
78+
final ImageCapture imageCapture =
79+
api.pigeon_defaultConstructor(null, null, CameraXFlashMode.OFF, null, true);
80+
81+
assertEquals(ImageCapture.CAPTURE_MODE_ZERO_SHUTTER_LAG, imageCapture.getCaptureMode());
82+
}
83+
84+
@Test
85+
public void pigeon_defaultConstructor_leavesDefaultCaptureModeWhenZeroShutterLagIsNotEnabled() {
86+
final PigeonApiImageCapture api = new TestProxyApiRegistrar().getPigeonApiImageCapture();
87+
88+
assertEquals(
89+
ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY,
90+
api.pigeon_defaultConstructor(null, null, CameraXFlashMode.OFF, null, false)
91+
.getCaptureMode());
92+
assertEquals(
93+
ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY,
94+
api.pigeon_defaultConstructor(null, null, CameraXFlashMode.OFF, null, null)
95+
.getCaptureMode());
96+
}
97+
7198
@Test
7299
public void resolutionSelector_returnsExpectedResolutionSelector() {
73100
final PigeonApiImageCapture api = new TestProxyApiRegistrar().getPigeonApiImageCapture();

‎packages/camera/camera_android_camerax/lib/src/android_camera_camerax.dart‎

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,14 @@ class AndroidCameraCameraX extends CameraPlatform {
203203
///
204204
int? _lockedCaptureOrientation;
205205

206+
/// Whether zero-shutter-lag capture was requested via
207+
/// [setZeroShutterLagEnabled].
208+
bool _zeroShutterLagEnabled = false;
209+
210+
/// The JPEG compression quality requested via [setJpegImageQuality], if
211+
/// any.
212+
int? _jpegQuality;
213+
206214
/// Whether or not the default rotation for [UseCase]s needs to be set
207215
/// manually because the capture orientation was previously locked.
208216
///
@@ -415,6 +423,8 @@ class AndroidCameraCameraX extends CameraPlatform {
415423
resolutionSelector: _presetResolutionSelector,
416424
/* use CameraX default target rotation */ targetRotation: await deviceOrientationManager
417425
.getDefaultDisplayRotation(),
426+
jpegQuality: _jpegQuality,
427+
zeroShutterLagEnabled: _zeroShutterLagEnabled,
418428
);
419429

420430
// Configure VideoCapture and Recorder instances.
@@ -1089,19 +1099,63 @@ class AndroidCameraCameraX extends CameraPlatform {
10891099
/// the new instance automatically.
10901100
@override
10911101
Future<void> setJpegImageQuality(int cameraId, int quality) async {
1092-
// Unbind the current ImageCapture if it exists and is bound.
1093-
if (imageCapture != null) {
1094-
await _unbindUseCaseFromLifecycle(imageCapture!);
1102+
_jpegQuality = quality;
1103+
await _recreateImageCapture();
1104+
}
1105+
1106+
/// Returns whether the selected camera supports zero-shutter-lag capture.
1107+
///
1108+
/// The default implementation returns `false`.
1109+
@override
1110+
Future<bool> isZeroShutterLagSupported(int cameraId) async {
1111+
return cameraInfo?.isZslSupported() ?? false;
1112+
}
1113+
1114+
/// Enables or disables zero-shutter-lag capture for still image capture.
1115+
///
1116+
/// CameraX only supports setting the capture mode via `ImageCapture.Builder`
1117+
/// at construction time, so this recreates the `ImageCapture` use case with
1118+
/// the requested mode. The next call to [takePicture] will bind the new
1119+
/// instance automatically.
1120+
///
1121+
/// If the selected camera does not support zero-shutter-lag, this only
1122+
/// records the requested mode and skips recreating the use case.
1123+
@override
1124+
Future<void> setZeroShutterLagEnabled(int cameraId, bool enabled) async {
1125+
_zeroShutterLagEnabled = enabled;
1126+
if (!await isZeroShutterLagSupported(cameraId)) {
1127+
return;
1128+
}
1129+
await _recreateImageCapture();
1130+
}
1131+
1132+
/// Recreates [imageCapture] from the currently requested settings
1133+
/// ([_jpegQuality], [_zeroShutterLagEnabled]).
1134+
///
1135+
/// CameraX only supports several `ImageCapture` settings via
1136+
/// `ImageCapture.Builder` at construction time, so any change to one of
1137+
/// them requires unbinding and recreating the whole use case. The next
1138+
/// call to [takePicture] will bind the new instance automatically.
1139+
///
1140+
/// If no camera has been created yet, this only updates the requested
1141+
/// settings; [createCamera] applies them when the camera is created.
1142+
Future<void> _recreateImageCapture() async {
1143+
if (imageCapture == null) {
1144+
return;
10951145
}
10961146

1097-
// Recreate ImageCapture with the requested JPEG quality.
1147+
// Unbind the current ImageCapture if it exists and is bound.
1148+
await _unbindUseCaseFromLifecycle(imageCapture!);
1149+
1150+
// Recreate ImageCapture with the requested settings.
10981151
// Preserve locked orientation if set, otherwise use default display rotation.
10991152
final int targetRotation =
11001153
_lockedCaptureOrientation ?? await deviceOrientationManager.getDefaultDisplayRotation();
11011154
imageCapture = ImageCapture(
11021155
resolutionSelector: _presetResolutionSelector,
11031156
targetRotation: targetRotation,
1104-
jpegQuality: quality,
1157+
jpegQuality: _jpegQuality,
1158+
zeroShutterLagEnabled: _zeroShutterLagEnabled,
11051159
);
11061160
}
11071161

‎packages/camera/camera_android_camerax/lib/src/camerax_library.g.dart‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class PigeonOverrides {
125125
int? targetRotation,
126126
CameraXFlashMode? flashMode,
127127
int? jpegQuality,
128+
bool? zeroShutterLagEnabled,
128129
})?
129130
imageCapture_new;
130131

@@ -1917,6 +1918,30 @@ class CameraInfo extends PigeonInternalProxyApiBaseClass {
19171918
return pigeonVar_replyValue! as LiveData;
19181919
}
19191920

1921+
/// Returns whether the camera supports zero-shutter-lag capture.
1922+
///
1923+
/// See https://developer.android.com/reference/androidx/camera/core/CameraInfo#isZslSupported().
1924+
Future<bool> isZslSupported() async {
1925+
final _PigeonInternalProxyApiBaseCodec pigeonChannelCodec = _pigeonVar_codecCameraInfo;
1926+
final BinaryMessenger? pigeonVar_binaryMessenger = pigeon_binaryMessenger;
1927+
const pigeonVar_channelName =
1928+
'dev.flutter.pigeon.camera_android_camerax.CameraInfo.isZslSupported';
1929+
final pigeonVar_channel = BasicMessageChannel<Object?>(
1930+
pigeonVar_channelName,
1931+
pigeonChannelCodec,
1932+
binaryMessenger: pigeonVar_binaryMessenger,
1933+
);
1934+
final Future<Object?> pigeonVar_sendFuture = pigeonVar_channel.send(<Object?>[this]);
1935+
final pigeonVar_replyList = await pigeonVar_sendFuture as List<Object?>?;
1936+
1937+
final Object? pigeonVar_replyValue = _extractReplyValueOrThrow(
1938+
pigeonVar_replyList,
1939+
pigeonVar_channelName,
1940+
isNullValid: false,
1941+
);
1942+
return pigeonVar_replyValue! as bool;
1943+
}
1944+
19201945
@override
19211946
CameraInfo pigeon_copy() {
19221947
return CameraInfo.pigeon_detached(
@@ -4081,13 +4106,15 @@ class ImageCapture extends UseCase {
40814106
int? targetRotation,
40824107
CameraXFlashMode? flashMode,
40834108
int? jpegQuality,
4109+
bool? zeroShutterLagEnabled,
40844110
}) {
40854111
if (PigeonOverrides.imageCapture_new != null) {
40864112
return PigeonOverrides.imageCapture_new!(
40874113
resolutionSelector: resolutionSelector,
40884114
targetRotation: targetRotation,
40894115
flashMode: flashMode,
40904116
jpegQuality: jpegQuality,
4117+
zeroShutterLagEnabled: zeroShutterLagEnabled,
40914118
);
40924119
}
40934120
return ImageCapture.pigeon_new(
@@ -4097,6 +4124,7 @@ class ImageCapture extends UseCase {
40974124
targetRotation: targetRotation,
40984125
flashMode: flashMode,
40994126
jpegQuality: jpegQuality,
4127+
zeroShutterLagEnabled: zeroShutterLagEnabled,
41004128
);
41014129
}
41024130

@@ -4108,6 +4136,7 @@ class ImageCapture extends UseCase {
41084136
int? targetRotation,
41094137
CameraXFlashMode? flashMode,
41104138
int? jpegQuality,
4139+
bool? zeroShutterLagEnabled,
41114140
}) : super.pigeon_detached() {
41124141
final int pigeonVar_instanceIdentifier = pigeon_instanceManager.addDartCreatedInstance(this);
41134142
final _PigeonInternalProxyApiBaseCodec pigeonChannelCodec = _pigeonVar_codecImageCapture;
@@ -4125,6 +4154,7 @@ class ImageCapture extends UseCase {
41254154
targetRotation,
41264155
flashMode,
41274156
jpegQuality,
4157+
zeroShutterLagEnabled,
41284158
]);
41294159
() async {
41304160
final pigeonVar_replyList = await pigeonVar_sendFuture as List<Object?>?;

0 commit comments

Comments
 (0)