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

Is it possible to configure the loader to intercept the remote request of the manifest and return the locally cached m3u8 content?

Open Xikee2023 opened this issue 11 months ago • 1 comments

What do you want to do with Hls.js?

Because I have special needs, my use case needs to load the same m3u8 manifest multiple times, I want to reduce the same request by configuring the loader options so that the same m3u8 request is obtained from the cache, my code is as follows:

const localM3u8 = {};
const hls = new Hls({
            loader: class CustomLoader extends Hls.DefaultConfig.loader {
                constructor(config) {
                    super(config);
                    const load = this.load.bind(this);
                    this.load = (context, config, callbacks) => {
                        const { url, type } = context;
                        const textTypes = ['manifest', 'level', 'audioTrack', 'subtitleTrack']
                        if (textTypes.includes(type)) {
                            const localData = localM3u8[url];
                            if (localData) {
                                //Hit the cache, intercept the request, and return data from the cache
                                const response = {
                                    code: 200,
                                    data: localData,
                                    url: context.url
                                };
                                const stats = {};
                                callbacks.onSuccess(
                                    response,
                                    stats,
                                    context
                                );
                                return;
                            }

                           //If the cache is not hit, the request is made normally and the result is stored in the cache for later use.
                            const onSuccess = callbacks.onSuccess;
                            callbacks.onSuccess = (response, stats, context) => {
                                if (typeof response.data === 'string') {
                                    localM3u8[url] = response.data;
                                }
                                onSuccess(response, stats, context);
                            };
                        }

                        load(context, config, callbacks);
                    }
                }
            }
        });

But unfortunately, when the cache triggers a hit, an error occurs, and the error code should be here:

const response = {
       code: 200,
       data: localData,
       url: context.url
 };
const stats = {};
callbacks.onSuccess(
       response,
       stats,
       context
);

Error details:

Image

Hls.js version is v1.5.20. Can any bro give me some advice? Thanks a lot!!!

What have you tried so far?

No response

Xikee2023 avatar Feb 10 '25 05:02 Xikee2023

Based on the error message and the code example, you haven't provided a compatible LoaderStats object.

robwalch avatar Feb 10 '25 18:02 robwalch