Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use FutureBuilder for RTCVideoRenderer's initialization and dispose to avoid timing failures. #402

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 34 additions & 25 deletions lib/src/widgets/video_track_renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,24 @@ class VideoTrackRenderer extends StatefulWidget {

class _VideoTrackRendererState extends State<VideoTrackRenderer> {
rtc.RTCVideoRenderer? _renderer;
bool _rendererReady = false;
late Future<rtc.RTCVideoRenderer> _rendererFuture;

EventsListener<TrackEvent>? _listener;
// Used to compute visibility information
late GlobalKey _internalKey;

Future<rtc.RTCVideoRenderer> 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
Expand Down Expand Up @@ -110,25 +113,31 @@ class _VideoTrackRendererState extends State<VideoTrackRenderer> {
}

@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<rtc.RTCVideoRenderer>(
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;
Expand Down
Loading