Page 1 of 1

VLC for Android reconnect the current stream

Posted: 23 Mar 2023 09:02
by peterpet
Hi everyone,

I have a little strange problem. I have developed a player which is based on VLC, and if the player is on and stream is opened, after that if I need to switch to standby/switchoff the android device and after some time need to switch on them, the player is resuming faster than network connections getting up and of course the stream cannot be played with following errors.

09:53:38.297 VLC E [8f9fa530/6d19] libvlc stream: connection failed: Network is unreachable
09:53:38.297 VLC E [8f9fa530/6d19] libvlc stream: cannot connect to http://XXXXXX
09:53:38.297 VLC D [a1d9a3d0/6d19] libvlc keystore: removing module "file"
09:53:38.297 VLC D [8f9fa530/6d19] libvlc stream: no access modules matched
09:53:38.297 VLC E [8ca83030/6d19] libvlc input: Your input can't be opened
09:53:38.297 VLC E [8ca83030/6d19] libvlc input: VLC is unable to open the MRL 'http://XXXXXX'. Check the log for details.

I have trying to put args like --loop --repeat --http-reconnect. But there is no effect to retry to play the stream.

Is there some tweaky to resolve this problem? Or I need to make a loop in Android activity to wait the network is on or not?
Thank you

Re: VLC for Android reconnect the current stream

Posted: 11 Apr 2023 12:40
by ladipro
Hi, in my project I fixed this by subscribing to the TimeChanged event and restarting playback when the event stops firing. If there's a better solution I'd like to know about it too :)

Re: VLC for Android reconnect the current stream

Posted: 11 Apr 2023 17:38
by peterpet
Thats a good/interesting way how you fixed that problem. I will try to implement .... but how to catch TimeChanged event if the player was released? With some Hanlder? Can you show me an example how did you do that? Thanks in advanced

Re: VLC for Android reconnect the current stream

Posted: 12 Apr 2023 22:26
by ladipro
My code is not public and I use the library in a Xamarin app so I can't guarantee the snippet is useful or you, but here it goes:

Code: Select all

/// <summary> /// Restarts stalled stream playback. /// </summary> private class PlaybackWatchdog { private static readonly TimeSpan _checkInterval = TimeSpan.FromMilliseconds(500); private static readonly TimeSpan _maxHeartbeatInterval = TimeSpan.FromSeconds(2); private static readonly TimeSpan _maxInitialHeartbeatInterval = TimeSpan.FromSeconds(10) - _maxHeartbeatInterval; private readonly CameraFragment _fragment; private readonly DateTime[] _lastHeartbeats = new DateTime[NumStreams]; private bool _disposed = false; public PlaybackWatchdog(CameraFragment fragment) { _fragment = fragment; _ = LoopAsync(); } public void MarkAlive(int index) { lock (this) { _lastHeartbeats[index] = DateTime.Now; } } public void Dispose() { _disposed = true; } private async Task LoopAsync() { while (!_disposed) { await Task.Delay(_checkInterval); if (_disposed) { break; } DateTime now = DateTime.Now; for (int i = 0; i < NumStreams; i++) { TimeSpan interval; lock (this) { interval = now - _lastHeartbeats[i]; } if (interval > _maxHeartbeatInterval) { LogToFile($"Restarting stalled playback for stream {i}"); _fragment.KillOneStream(i); _fragment.InitOneStream(i); _fragment.PlayOneStream(i); lock (this) { _lastHeartbeats[i] = now + _maxInitialHeartbeatInterval; } } } } } }
The class above keeps track of the time the TimeChanged event was last fired for each of NumStreams players. The events are trivially routed to the MarkAlive method, which is not captured in the snippet. Note that the Time in the TimeChanged event is not changing and is ignored. What we keep here is the timestamp of when the event was delivered.

LoopAsync simply loops and checks if too much time has elapsed since the event was last seen. If it has, it reinitialized the relevant player.

Re: VLC for Android reconnect the current stream

Posted: 12 Apr 2023 22:30
by ladipro
Thats a good/interesting way how you fixed that problem. I will try to implement .... but how to catch TimeChanged event if the player was released? With some Hanlder? Can you show me an example how did you do that? Thanks in advanced
Actually, I'm not sure I'm following the concern with a released player. I may be ignorant to Java specific issues since I use the library in C#.