From eaf2e7fce97c40c634cdf34016f231f65ef392d6 Mon Sep 17 00:00:00 2001 From: cloudwebrtc Date: Fri, 17 Nov 2023 08:32:21 +0800 Subject: [PATCH 1/2] fix: Use FutureBuilder for RTCVideoRenderer's initialization and dispose to avoid timing failures. --- lib/src/widgets/video_track_renderer.dart | 60 +++++++++++++---------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/lib/src/widgets/video_track_renderer.dart b/lib/src/widgets/video_track_renderer.dart index 22615867..ced86a3a 100644 --- a/lib/src/widgets/video_track_renderer.dart +++ b/lib/src/widgets/video_track_renderer.dart @@ -15,7 +15,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_webrtc/flutter_webrtc.dart' as rtc; - import '../events.dart'; import '../extensions.dart'; import '../internal/events.dart'; @@ -51,21 +50,24 @@ class VideoTrackRenderer extends StatefulWidget { class _VideoTrackRendererState extends State { rtc.RTCVideoRenderer? _renderer; - bool _rendererReady = false; + late Future _rendererFuture; + EventsListener? _listener; // Used to compute visibility information late GlobalKey _internalKey; + Future initRenderer() async { + _renderer ??= rtc.RTCVideoRenderer(); + await _renderer?.initialize(); + await _attach(); + return _renderer!; + } + @override void initState() { super.initState(); _internalKey = widget.track.addViewKey(); - (() async { - _renderer ??= rtc.RTCVideoRenderer(); - await _renderer?.initialize(); - await _attach(); - setState(() => _rendererReady = true); - })(); + _rendererFuture = initRenderer(); } @override @@ -110,25 +112,31 @@ class _VideoTrackRendererState extends State { } @override - Widget build(BuildContext context) => !_rendererReady - ? Container() - : Builder( - key: _internalKey, - builder: (ctx) { - // let it render before notifying build - WidgetsBindingCompatible.instance - ?.addPostFrameCallback((timeStamp) { - widget.track.onVideoViewBuild?.call(_internalKey); - }); - return rtc.RTCVideoView( - _renderer!, - mirror: _shouldMirror(), - filterQuality: FilterQuality.medium, - objectFit: widget.fit, + Widget build(BuildContext context) => FutureBuilder( + future: _rendererFuture, + builder: (context, snapshot) { + if (snapshot.hasData) { + return Builder( + key: _internalKey, + builder: (ctx) { + // let it render before notifying build + WidgetsBindingCompatible.instance + ?.addPostFrameCallback((timeStamp) { + widget.track.onVideoViewBuild?.call(_internalKey); + }); + return rtc.RTCVideoView( + snapshot.requireData, + mirror: _shouldMirror(), + filterQuality: FilterQuality.medium, + objectFit: widget.fit, + ); + }, ); - }, - ); - + } else { + return Container(); + } + }, + ); bool _shouldMirror() { // off for screen share if (widget.track.source == TrackSource.screenShareVideo) return false; From 5fd97371fff32a2a7a4c4d8165ab51ef14e250b9 Mon Sep 17 00:00:00 2001 From: cloudwebrtc Date: Fri, 17 Nov 2023 08:43:15 +0800 Subject: [PATCH 2/2] import_sorter. --- lib/src/widgets/video_track_renderer.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/src/widgets/video_track_renderer.dart b/lib/src/widgets/video_track_renderer.dart index ced86a3a..6012aa12 100644 --- a/lib/src/widgets/video_track_renderer.dart +++ b/lib/src/widgets/video_track_renderer.dart @@ -15,6 +15,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_webrtc/flutter_webrtc.dart' as rtc; + import '../events.dart'; import '../extensions.dart'; import '../internal/events.dart';