hls.js
hls.js copied to clipboard
Extending HLS default loader.load() class with a promise
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);
});
};
}
}

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
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.
xhrSetup can now be asynch/return a Promise.