Skip to content

Commit

Permalink
[camera] Add API support query for image streaming (app-facing) (#8422)
Browse files Browse the repository at this point in the history
Final step for introducing the `supportsImageStreaming` query method. Expose it through `CameraController`.

Previous steps:
 - #8250
 - #8307
  • Loading branch information
liff authored Jan 23, 2025
1 parent fba4a18 commit 8ec48ab
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
5 changes: 3 additions & 2 deletions packages/camera/camera/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## NEXT
## 0.11.1

* Updates minimum supported SDK version to Flutter 3.22/Dart 3.4.
* Adds API support query for image streaming.
* Updates minimum supported SDK version to Flutter 3.27/Dart 3.6.
* Updates example to dispose animation controllers and curved animations.

## 0.11.0+2
Expand Down
18 changes: 10 additions & 8 deletions packages/camera/camera/lib/src/camera_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -480,13 +480,12 @@ class CameraController extends ValueNotifier<CameraValue> {
/// Throws a [CameraException] if image streaming or video recording has
/// already started.
///
/// The `startImageStream` method is only available on Android and iOS (other
/// platforms won't be supported in current setup).
/// The `startImageStream` method is only available on platforms that
/// report support for image streaming via [supportsImageStreaming].
///
// TODO(bmparr): Add settings for resolution and fps.
Future<void> startImageStream(onLatestImageAvailable onAvailable) async {
assert(defaultTargetPlatform == TargetPlatform.android ||
defaultTargetPlatform == TargetPlatform.iOS);
assert(supportsImageStreaming());
_throwIfNotInitialized('startImageStream');
if (value.isRecordingVideo) {
throw CameraException(
Expand Down Expand Up @@ -518,11 +517,10 @@ class CameraController extends ValueNotifier<CameraValue> {
/// Throws a [CameraException] if image streaming was not started or video
/// recording was started.
///
/// The `stopImageStream` method is only available on Android and iOS (other
/// platforms won't be supported in current setup).
/// The `stopImageStream` method is only available on platforms that
/// report support for image streaming via [supportsImageStreaming].
Future<void> stopImageStream() async {
assert(defaultTargetPlatform == TargetPlatform.android ||
defaultTargetPlatform == TargetPlatform.iOS);
assert(supportsImageStreaming());
_throwIfNotInitialized('stopImageStream');
if (!value.isStreamingImages) {
throw CameraException(
Expand Down Expand Up @@ -871,6 +869,10 @@ class CameraController extends ValueNotifier<CameraValue> {
}
}

/// Check whether the camera platform supports image streaming.
bool supportsImageStreaming() =>
CameraPlatform.instance.supportsImageStreaming();

/// Releases the resources of this camera.
@override
Future<void> dispose() async {
Expand Down
12 changes: 6 additions & 6 deletions packages/camera/camera/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ description: A Flutter plugin for controlling the camera. Supports previewing
Dart.
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
version: 0.11.0+2
version: 0.11.1

environment:
sdk: ^3.4.0
flutter: ">=3.22.0"
sdk: ^3.6.0
flutter: ">=3.27.0"

flutter:
plugin:
Expand All @@ -21,9 +21,9 @@ flutter:
default_package: camera_web

dependencies:
camera_android_camerax: ^0.6.5
camera_avfoundation: ^0.9.15
camera_platform_interface: ^2.6.0
camera_android_camerax: ^0.6.13
camera_avfoundation: ^0.9.18
camera_platform_interface: ^2.9.0
camera_web: ^0.3.3
flutter:
sdk: flutter
Expand Down
22 changes: 18 additions & 4 deletions packages/camera/camera/test/camera_image_stream_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ void main() {

await cameraController.startImageStream((CameraImage image) {});

expect(mockPlatform.streamCallLog,
<String>['onStreamedFrameAvailable', 'listen']);
expect(mockPlatform.streamCallLog, <String>[
'supportsImageStreaming',
'onStreamedFrameAvailable',
'listen'
]);
});

test('stopImageStream() throws $CameraException when uninitialized', () {
Expand Down Expand Up @@ -160,8 +163,13 @@ void main() {
await cameraController.startImageStream((CameraImage image) {});
await cameraController.stopImageStream();

expect(mockPlatform.streamCallLog,
<String>['onStreamedFrameAvailable', 'listen', 'cancel']);
expect(mockPlatform.streamCallLog, <String>[
'supportsImageStreaming',
'onStreamedFrameAvailable',
'listen',
'supportsImageStreaming',
'cancel'
]);
});

test('startVideoRecording() can stream images', () async {
Expand Down Expand Up @@ -235,6 +243,12 @@ class MockStreamingCameraPlatform extends MockCameraPlatform {
streamCallLog.add('listen');
}

@override
bool supportsImageStreaming() {
streamCallLog.add('supportsImageStreaming');
return true;
}

FutureOr<void> _onFrameStreamCancel() async {
streamCallLog.add('cancel');
_streamController = null;
Expand Down
3 changes: 3 additions & 0 deletions packages/camera/camera/test/camera_preview_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ class FakeController extends ValueNotifier<CameraValue>

@override
CameraDescription get description => value.description;

@override
bool supportsImageStreaming() => true;
}

void main() {
Expand Down

0 comments on commit 8ec48ab

Please sign in to comment.