hls.js icon indicating copy to clipboard operation
hls.js copied to clipboard

Extending HLS default loader.load() class with a promise

Open tronikelis opened this issue 3 years ago • 1 comments

What do you want to do with Hls.js?

Hello, I want to wait for credentials generated from the chunk's url by the server before loading that .ts chunk. This is sort of related to #1354

The xhr setup method is not async so it won't work, but I found that by extending the loader with a promise it works:

const wait = () =>
    new Promise((res) => {
        setTimeout(
            () => res("?credentials=key"),
            // simulate server response
            Math.round(Math.random() * 1000)
        );
    });


class loader extends Hls.DefaultConfig.loader {
    constructor(config: any) {
        super(config);
        const load = this.load.bind(this);

        this.load = function (context, ...rest) {
            wait().then((credentials) => {
                load({ ...context, url: context.url + credentials }, ...rest);
            });
        };
    }
}

firefox_oeTneUcpm7

Now my question: can I just wait for a promise to finish before firing the .load() method? Will there be any race conditions, weird behavior?

Or is there a better way to do this?

Thanks

What have you tried so far?

No response

tronikelis avatar Jul 21 '22 11:07 tronikelis

Hi @Tronikelis,

Have a look at load and loadInternal in utils/xhr-loader. This is where timing of the request begins. Your initial async method won't be accounted for with regards to bandwidth estimation and load timeout. As long as fetching credentials is not a significant portion of that process, then it should not have much impact.

robwalch avatar Jul 22 '22 15:07 robwalch

xhrSetup can now be asynch/return a Promise.

robwalch avatar Jun 19 '23 19:06 robwalch