Search code examples
javascriptvideohttp-live-streaming

Hls fast recconect


In the hls documentation, it is recommended to use this function for error cases


case Hls.ErrorTypes.MEDIA_ERROR:
 console.log("fatal media error encountered, try to recover");
 hls.recoverMediaError();
        break;

https://github.com/video-dev/hls.js/blob/master/docs/API.md#hlsrecovermediaerror

However, in the case of live, it takes a long time to recover the video in case of error or transmission failure. Is there a way to recover the transmission more quickly?


Solution

  • If you follow the documentation or demo source code you'll notice that both are suggesting to call recover media error if any media related error occurred.

    • Now what this recoverMediaError will do, is it will remove the video element and append it back.
    • This method has few backdrops
      • If a subtitle is selected then all its cue will be lost
      • since the element is removed there will be a blank screen without a video thumbnail or loader
      • this makes the process longer

    After a few experiment, this is what I Prefer to resolve the errors

    • instead of calling recoverMediaError we can call the startLoad() function which will start the loading of the current segment
    • this function is suitable as it will keep your video element at its place and also will show a loader

    Besides that, you can make a function which will first call the startLoad still if the error raised then your final option should be calling recoverMediaError()