initRenderer method

Future<void> initRenderer()

Initializes a RtcVideoRenderer for the current camera.

Implementation

Future<void> initRenderer() async {
  if (_initRendererGuard.isLocked) {
    return;
  }

  renderer.value?.dispose();
  renderer.value = null;
  _localTrack?.free();
  _localTrack = null;

  String? camera = this.camera.value;

  await _initRendererGuard.protect(() async {
    final List<LocalMediaTrack> tracks = await MediaUtils.getTracks(
      video: VideoPreferences(device: camera),
    );

    if (isClosed) {
      tracks.firstOrNull?.free();
      _localTrack = null;
    } else {
      _localTrack = tracks.firstOrNull;
    }

    if (_localTrack != null) {
      if (camera == null) {
        camera = _localTrack?.getTrack().deviceId();
        this.camera.value = camera;
      }

      final RtcVideoRenderer renderer = RtcVideoRenderer(_localTrack!);
      await renderer.initialize();

      renderer.srcObject = tracks.first.getTrack();

      if (isClosed) {
        renderer.dispose();
        this.renderer.value = null;
      } else {
        this.renderer.value = renderer;
      }
    } else {
      renderer.value = null;
    }
  });

  if (camera != this.camera.value && !isClosed) {
    initRenderer();
  }
}