changeCam method

Future<void> changeCam({
  1. String? deviceId,
  2. CustomStreamTrack? customCameraTrack,
})

Switches the camera to customCameraTrack (turns the camera on), to the camera deviceId, or to the next camera when both are null. A device switch keeps the camera on or off. An unknown deviceId throws 3051.

Implementation

Future<void> changeCam({
  String? deviceId,
  CustomStreamTrack? customCameraTrack,
}) async {
  try {
    final selectedId = await _backend.changeCam(
      customCameraTrack == null ? deviceId : null,
      customTrack: customCameraTrack,
    );
    if (customCameraTrack != null) _camEnabled = true;
    final id = selectedId ?? deviceId;
    if (id != null) {
      final cameras = await VideoSDK.getCameras() ?? const [];
      final match =
          cameras.where((c) => c.deviceId == id || c.label == id).firstOrNull;
      if (match != null) setSelectedCamera(match);
    }
  } catch (_) {
    await customCameraTrack?.stop();
    rethrow;
  }
}