Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit c13218f

Browse files
authored
[camera_web] Release the camera stream used to request video and audio permissions (#4342)
1 parent 5d64092 commit c13218f

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed
+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## NEXT
2+
3+
* Fix cameraNotReadable error that prevented access to the camera on some Android devices.
4+
15
## 0.2.0
26

37
* Initial release, adapted from the Flutter [I/O Photobooth](https://photobooth.flutter.dev/) project.

packages/camera/camera_web/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The web implementation of [`camera`][camera].
99
### Depend on the package
1010

1111
This package is not an [endorsed implementation](https://flutter.dev/docs/development/packages-and-plugins/developing-packages#endorsed-federated-plugin)
12-
of the google_maps_flutter plugin yet, so you'll need to [add it explicitly](https://pub.dev/packages/camera_web/install).
12+
of the camera plugin yet, so you'll need to [add it explicitly](https://pub.dev/packages/camera_web/install).
1313

1414
## Example
1515

packages/camera/camera_web/example/integration_test/camera_web_test.dart

+27
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,33 @@ void main() {
110110
).called(1);
111111
});
112112

113+
testWidgets(
114+
'releases the camera stream '
115+
'used to request video and audio permissions', (tester) async {
116+
final videoTrack = MockMediaStreamTrack();
117+
118+
var videoTrackStopped = false;
119+
when(videoTrack.stop).thenAnswer((_) {
120+
videoTrackStopped = true;
121+
});
122+
123+
when(
124+
() => cameraService.getMediaStreamForOptions(
125+
CameraOptions(
126+
audio: AudioConstraints(enabled: true),
127+
),
128+
),
129+
).thenAnswer(
130+
(_) => Future.value(
131+
FakeMediaStream([videoTrack]),
132+
),
133+
);
134+
135+
final _ = await CameraPlatform.instance.availableCameras();
136+
137+
expect(videoTrackStopped, isTrue);
138+
});
139+
113140
testWidgets(
114141
'gets a video stream '
115142
'for a video input device', (tester) async {

packages/camera/camera_web/lib/src/camera_web.dart

+4-1
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,15 @@ class CameraPlugin extends CameraPlatform {
8989
}
9090

9191
// Request video and audio permissions.
92-
await _cameraService.getMediaStreamForOptions(
92+
final cameraStream = await _cameraService.getMediaStreamForOptions(
9393
CameraOptions(
9494
audio: AudioConstraints(enabled: true),
9595
),
9696
);
9797

98+
// Release the camera stream used to request video and audio permissions.
99+
cameraStream.getVideoTracks().forEach((videoTrack) => videoTrack.stop());
100+
98101
// Request available media devices.
99102
final devices = await mediaDevices.enumerateDevices();
100103

0 commit comments

Comments
 (0)