From f08556e330e1070fb993da15f7c0e70d3e2b9684 Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Thu, 16 Nov 2023 18:07:37 +0800 Subject: [PATCH] fix: video renderer dispose issue and correctly handle metadataMuted for TrackPublication. (#401) * fix: video renderer dispose issue. * remove strong-mode in analysis_options.yaml to compatible flutter 3.16.0. * Correctly handle metadataMuted for TrackPublication. --- analysis_options.yaml | 1 - example/analysis_options.yaml | 1 - lib/src/publication/track_publication.dart | 5 ++++- lib/src/widgets/video_track_renderer.dart | 18 +++++++++--------- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/analysis_options.yaml b/analysis_options.yaml index e4bff4920..871aaf20e 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -32,7 +32,6 @@ analyzer: # https://dart.dev/guides/language/analysis-options#enabling-additional-type-checks # https://dash-overflow.net/articles/getting_started/#step-3-disabling-_implicit-dynamic_--_implicit-cast_ # - strong-mode: # # exclude protobuf files diff --git a/example/analysis_options.yaml b/example/analysis_options.yaml index b6b201dd1..c743242fa 100644 --- a/example/analysis_options.yaml +++ b/example/analysis_options.yaml @@ -32,7 +32,6 @@ analyzer: # https://dart.dev/guides/language/analysis-options#enabling-additional-type-checks # https://dash-overflow.net/articles/getting_started/#step-3-disabling-_implicit-dynamic_--_implicit-cast_ # - strong-mode: # # exclude protobuf files diff --git a/lib/src/publication/track_publication.dart b/lib/src/publication/track_publication.dart index 3e4407076..f87222bf6 100644 --- a/lib/src/publication/track_publication.dart +++ b/lib/src/publication/track_publication.dart @@ -32,6 +32,7 @@ abstract class TrackPublication extends Disposable { final String name; final lk_models.TrackType kind; final TrackSource source; + bool _metadataMuted = false; /// The current [Track] for this publication (readonly). T? get track => _track; @@ -40,7 +41,7 @@ abstract class TrackPublication extends Disposable { /// The [Participant] this publication belongs to. abstract final Participant participant; - bool get muted => track?.muted ?? false; + bool get muted => _metadataMuted; /// If the [Track] is published with simulcast, only for video. (readonly) bool get simulcasted => _simulcasted; @@ -72,6 +73,7 @@ abstract class TrackPublication extends Disposable { kind = info.type, source = info.source.toLKType(), _simulcasted = info.simulcast, + _metadataMuted = info.muted, _mimeType = info.mimeType { updateFromInfo(info); } @@ -84,6 +86,7 @@ abstract class TrackPublication extends Disposable { void updateFromInfo(lk_models.TrackInfo info) { _simulcasted = info.simulcast; _mimeType = info.mimeType; + _metadataMuted = info.muted; if (info.type == lk_models.TrackType.VIDEO) { _dimensions = VideoDimensions(info.width, info.height); } diff --git a/lib/src/widgets/video_track_renderer.dart b/lib/src/widgets/video_track_renderer.dart index 301521667..22615867f 100644 --- a/lib/src/widgets/video_track_renderer.dart +++ b/lib/src/widgets/video_track_renderer.dart @@ -50,7 +50,7 @@ class VideoTrackRenderer extends StatefulWidget { } class _VideoTrackRendererState extends State { - final _renderer = rtc.RTCVideoRenderer(); + rtc.RTCVideoRenderer? _renderer; bool _rendererReady = false; EventsListener? _listener; // Used to compute visibility information @@ -60,9 +60,9 @@ class _VideoTrackRendererState extends State { void initState() { super.initState(); _internalKey = widget.track.addViewKey(); - (() async { - await _renderer.initialize(); + _renderer ??= rtc.RTCVideoRenderer(); + await _renderer?.initialize(); await _attach(); setState(() => _rendererReady = true); })(); @@ -72,18 +72,18 @@ class _VideoTrackRendererState extends State { void dispose() { widget.track.removeViewKey(_internalKey); _listener?.dispose(); - _renderer.srcObject = null; - _renderer.dispose(); + _renderer?.srcObject = null; + _renderer?.dispose(); super.dispose(); } Future _attach() async { - _renderer.srcObject = widget.track.mediaStream; + _renderer?.srcObject = widget.track.mediaStream; await _listener?.dispose(); _listener = widget.track.createListener() ..on((event) { if (!mounted) return; - _renderer.srcObject = event.stream; + _renderer?.srcObject = event.stream; }) ..on((event) { if (!mounted) return; @@ -105,7 +105,7 @@ class _VideoTrackRendererState extends State { if ([BrowserType.safari, BrowserType.firefox].contains(lkBrowser()) && oldWidget.key != widget.key) { - _renderer.srcObject = widget.track.mediaStream; + _renderer?.srcObject = widget.track.mediaStream; } } @@ -121,7 +121,7 @@ class _VideoTrackRendererState extends State { widget.track.onVideoViewBuild?.call(_internalKey); }); return rtc.RTCVideoView( - _renderer, + _renderer!, mirror: _shouldMirror(), filterQuality: FilterQuality.medium, objectFit: widget.fit,