Skip to content

Commit

Permalink
Adding error handling if video hasn't loaded in 10 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
clone1018 committed Mar 22, 2022
1 parent 9edf165 commit 7529046
Showing 1 changed file with 67 additions and 24 deletions.
91 changes: 67 additions & 24 deletions lib/components/FTLPlayer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class _FTLPlayerState extends State<FTLPlayer> {

bool _loading = true;
bool _errored = false;
bool _fatalErrored = false;
int _seconds = 0;

watchChannel(int channelId) {
plugin!.send(data: {"request": "watch", "channelId": channelId});
Expand Down Expand Up @@ -105,6 +107,7 @@ class _FTLPlayerState extends State<FTLPlayer> {
setState(() {
_errored = false;
_loading = false;
_seconds = 0;
});
} catch (error) {
// We can likely retry loading this stream with initJanusClient();
Expand All @@ -115,20 +118,43 @@ class _FTLPlayerState extends State<FTLPlayer> {
setState(() {
_errored = true;
_loading = false;
_seconds = 0;
});
}
return;
}
}
});

// Pathetic excuse for error handling...
Timer.periodic(Duration(seconds: 1), (timer) async {
Timer.periodic(Duration(seconds: 1), (innerTimer) async {
if (mounted == false) {
return;
}

// if we don't have a videoHeight after 10 seconds, assume we errored due to VPN or similar.
setState(() {
_seconds += 1;
});
if (_seconds > 10 && _remoteRenderer.videoHeight == 0) {
print("Video loading for greater than 10 seconds, aborting");
await Sentry.captureMessage(
"Video loading for greater than 10 seconds, aborting");

innerTimer.cancel();
stopVideo();

setState(() {
_fatalErrored = true;
_loading = false;
});
}

// Check to see if Janus lost connection without telling us
if (plugin != null && plugin!.pollingActive == false) {
print("Polling has failed without telling us, resetting the widget");
await Sentry.captureMessage(
"Polling has failed without telling us, resetting the widget");
timer.cancel();
innerTimer.cancel();

if (mounted) {
// If we're not mounted, we can just forget about it
Expand All @@ -153,9 +179,7 @@ class _FTLPlayerState extends State<FTLPlayer> {
}

if (plugin != null) {
plugin!.send(data: {"request": "stop"});
plugin!.remoteStream = null;
plugin!.dispose();
stopVideo();
}

if (session != null) {
Expand All @@ -168,26 +192,45 @@ class _FTLPlayerState extends State<FTLPlayer> {
super.dispose();
}

void stopVideo() {
plugin!.send(data: {"request": "stop"});
plugin!.remoteStream = null;
plugin!.dispose();
}

@override
Widget build(BuildContext context) {
return Stack(
children: [
// Background layers that the video will take over when properly loaded
Image.network(widget.channel.thumbnail),
Container(
decoration: BoxDecoration(color: Colors.black45),
child: Loading("Loading Video"),
),
_errored
? Center(
child: Text("Error loading video, please try again."),
)
: RTCVideoView(
_remoteRenderer,
objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitContain,
return _fatalErrored
? Center(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 5),
child: Text(
"Error loading video, please disable any VPNs or other software that could negatively impact WebRTC video and then try again.",
textAlign: TextAlign.center,
),
),
)
: Stack(
children: [
// Background layers that the video will take over when properly loaded
Image.network(widget.channel.thumbnail),
Container(
decoration: BoxDecoration(color: Colors.black45),
child: Loading("Loading Video"),
),
_loading ? Loading("Loading Video") : Padding(padding: EdgeInsets.zero),
],
);
_errored
? Center(
child: Text("Error loading video, please try again."),
)
: RTCVideoView(
_remoteRenderer,
objectFit:
RTCVideoViewObjectFit.RTCVideoViewObjectFitContain,
),
_loading
? Loading("Loading Video")
: Padding(padding: EdgeInsets.zero),
],
);
}
}

0 comments on commit 7529046

Please sign in to comment.