Skip to content

Commit 65b6fab

Browse files
authored
[camera_web] Skip video input devices that cannot be started in availableCameras (flutter#13196)
`availableCameras` opens every video input device to read its facing mode. If one device cannot be started (for example the OBS Virtual Camera on Windows while OBS is not running), `getUserMedia` rejects with `NotReadableError` and the whole call throws `cameraNotReadable`, so the cameras that work are not returned either. This PR skips a device whose stream cannot be started and continues with the remaining devices. If no camera is available and a device failed, the first device error is still thrown, so a single broken camera keeps reporting its error instead of returning an empty list. The platform interface documents that `availableCameras` returns an empty list when no cameras are available. `availableCameras` already throws today when a device cannot be started, and this PR keeps that behavior when no device can be started at all, so a single busy or broken camera still reports why it failed. I'm happy to return an empty list instead if that is preferred. Fixes flutter/flutter#194184 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^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 4a1f3c5 commit 65b6fab

4 files changed

Lines changed: 111 additions & 5 deletions

File tree

‎packages/camera/camera_web/CHANGELOG.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
## NEXT
1+
## 0.3.5+7
22

3+
* Fixes `availableCameras` failing when a video input device cannot be started, for example
4+
a virtual camera without a video source. Such devices are now skipped.
35
* Updates minimum supported SDK version to Flutter 3.41/Dart 3.11.
46

57
## 0.3.5+6

‎packages/camera/camera_web/example/integration_test/camera_web_devices_test.dart‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,63 @@ void main() {
380380
expect(stops.every((bool e) => e), isTrue);
381381
});
382382

383+
testWidgets('skips a video input device '
384+
'that cannot be started', (WidgetTester tester) async {
385+
final unreadableVideoDevice =
386+
createJSInteropWrapper(FakeMediaDeviceInfo('1', 'Camera 1', MediaDeviceKind.videoInput))
387+
as MediaDeviceInfo;
388+
389+
final videoDevice =
390+
createJSInteropWrapper(FakeMediaDeviceInfo('2', 'Camera 2', MediaDeviceKind.videoInput))
391+
as MediaDeviceInfo;
392+
393+
final videoStream =
394+
createJSInteropWrapper(
395+
FakeMediaStream(<MediaStreamTrack>[
396+
createJSInteropWrapper(MockMediaStreamTrack()) as MediaStreamTrack,
397+
]),
398+
)
399+
as MediaStream;
400+
401+
mockMediaDevices.enumerateDevices = () {
402+
return Future<JSArray<MediaDeviceInfo>>.value(
403+
<MediaDeviceInfo>[unreadableVideoDevice, videoDevice].toJS,
404+
).toJS;
405+
}.toJS;
406+
407+
when(
408+
cameraService.getMediaStreamForOptions(
409+
CameraOptions(video: VideoConstraints(deviceId: unreadableVideoDevice.deviceId)),
410+
),
411+
).thenThrow(CameraWebException(0, CameraErrorCode.notReadable, 'description'));
412+
413+
when(
414+
cameraService.getMediaStreamForOptions(
415+
CameraOptions(video: VideoConstraints(deviceId: videoDevice.deviceId)),
416+
),
417+
).thenAnswer((_) => Future<MediaStream>.value(videoStream));
418+
419+
final List<CameraDescription> cameras = await CameraPlatform.instance.availableCameras();
420+
421+
expect(
422+
cameras,
423+
equals(<CameraDescription>[
424+
CameraDescription(
425+
name: videoDevice.label,
426+
lensDirection: CameraLensDirection.external,
427+
sensorOrientation: 0,
428+
),
429+
]),
430+
);
431+
432+
expect(
433+
(CameraPlatform.instance as CameraPlugin).camerasMetadata,
434+
equals(<CameraDescription, CameraMetadata>{
435+
cameras.first: CameraMetadata(deviceId: videoDevice.deviceId, facingMode: null),
436+
}),
437+
);
438+
});
439+
383440
group('throws CameraException', () {
384441
testWidgets('when MediaDevices.enumerateDevices throws DomException', (
385442
WidgetTester tester,
@@ -418,6 +475,37 @@ void main() {
418475
);
419476
});
420477

478+
testWidgets('when no video input device can be started', (WidgetTester tester) async {
479+
final videoDevice =
480+
createJSInteropWrapper(
481+
FakeMediaDeviceInfo('1', 'Camera 1', MediaDeviceKind.videoInput),
482+
)
483+
as MediaDeviceInfo;
484+
485+
mockMediaDevices.enumerateDevices = () {
486+
return Future<JSArray<MediaDeviceInfo>>.value(<MediaDeviceInfo>[videoDevice].toJS).toJS;
487+
}.toJS;
488+
489+
final exception = CameraWebException(0, CameraErrorCode.notReadable, 'description');
490+
491+
when(
492+
cameraService.getMediaStreamForOptions(
493+
CameraOptions(video: VideoConstraints(deviceId: videoDevice.deviceId)),
494+
),
495+
).thenThrow(exception);
496+
497+
expect(
498+
CameraPlatform.instance.availableCameras(),
499+
throwsA(
500+
isA<CameraException>().having(
501+
(CameraException e) => e.code,
502+
'code',
503+
exception.code.toString(),
504+
),
505+
),
506+
);
507+
});
508+
421509
testWidgets('when CameraService.getMediaStreamForOptions '
422510
'throws PlatformException', (WidgetTester tester) async {
423511
final exception = PlatformException(

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,23 @@ class CameraPlugin extends CameraPlatform {
119119
/// https://developer.mozilla.org/en-US/docs/Web/API/MediaDeviceInfo/deviceId#browser_compatibility
120120
.where((web.MediaDeviceInfo device) => device.deviceId.isNotEmpty);
121121

122+
// The first error of a video input device that could not be started.
123+
CameraWebException? videoInputDeviceException;
124+
122125
// Map video input devices to camera descriptions.
123126
for (final videoInputDevice in videoInputDevices) {
124127
// Get the video stream for the current video input device
125128
// to later use for the available video tracks.
126-
final web.MediaStream videoStream = await _getVideoStreamForDevice(
127-
videoInputDevice.deviceId,
128-
);
129+
final web.MediaStream videoStream;
130+
try {
131+
videoStream = await _getVideoStreamForDevice(videoInputDevice.deviceId);
132+
} on CameraWebException catch (e) {
133+
// Skip a video input device that cannot be started,
134+
// e.g. a virtual camera without a video source,
135+
// so that the remaining cameras stay available.
136+
videoInputDeviceException ??= e;
137+
continue;
138+
}
129139

130140
// Get all video tracks in the video stream
131141
// to later extract the lens direction from the first track.
@@ -176,6 +186,12 @@ class CameraPlugin extends CameraPlatform {
176186
}
177187
}
178188

189+
// Report the error of a video input device that could not be started
190+
// if no camera is available.
191+
if (cameras.isEmpty && videoInputDeviceException != null) {
192+
throw videoInputDeviceException;
193+
}
194+
179195
return cameras;
180196
} on web.DOMException catch (e) {
181197
throw CameraException(e.name, e.message);

‎packages/camera/camera_web/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: camera_web
22
description: A Flutter plugin for getting information about and controlling the camera on Web.
33
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_web
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
5-
version: 0.3.5+6
5+
version: 0.3.5+7
66

77
environment:
88
sdk: ^3.11.0

0 commit comments

Comments
 (0)