Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load manifest via local function bypass XMLHttpRequest #6863

Open
nathanh2011 opened this issue Nov 24, 2024 · 3 comments
Open

Load manifest via local function bypass XMLHttpRequest #6863

nathanh2011 opened this issue Nov 24, 2024 · 3 comments

Comments

@nathanh2011
Copy link

nathanh2011 commented Nov 24, 2024

What do you want to do with Hls.js?

Is it possible to set the calls to load the manifest to go via a local function rather than having to use a url in the load method? I need to do some other logic to generate the manifest so wanted to use a local function to generate it.

I tried creating my own custom loader and it calls the function but Hls is still calling an XMLHttpRequest for the entire page and parsing that response, and error'ing out on it, rather than using the supplied manifest. I tried copying the example code from the docs but am at a loss. Any help would be greatly appreciated.

What have you tried so far?

const hls = new Hls({
                        liveSyncDurationCount: 3,
                        debug: true,
                        loader: class CustomLoader extends Hls.DefaultConfig.loader {
                            constructor(config) {
                                super(config); 
                                var load = this.load.bind(this);
                                this.load = function(context, config, callbacks) {
                                    if (context.type === 'manifest') {
                                        console.log('Custom loader invoked for manifest');
                                        
                                        let manifestData = getCustomManifest();
                                        var onSuccess = callbacks.onSuccess;
                                        callbacks.onSuccess = function(response, stats, context) {
                                            context.responseType = 'text';
                                            response.data = manifestData;
                                            response.url = 'http://localhost/dummy.m3u8';
                                            onSuccess(response, stats, context);
                                        }                                        
                                    
                                        load(context, config, callbacks);
                                    } else {                                    
                                        // Fallback for other resource types
                                        load(context, config, callbacks);
                                    }
                                }
                            }                            
                        }
                    });
@nathanh2011 nathanh2011 added Needs Triage If there is a suspected stream issue, apply this label to triage if it is something we should fix. Question labels Nov 24, 2024
@robwalch
Copy link
Collaborator

I tried creating my own custom loader and it calls the function but Hls is still calling an XMLHttpRequest for the entire page and parsing that response, and error'ing out on it, rather than using the supplied manifest.

That would be because the custom loader calls the default load function. All it needs to do is call callbacks.onSuccess directly.

@robwalch robwalch added answered and removed Needs Triage If there is a suspected stream issue, apply this label to triage if it is something we should fix. labels Nov 27, 2024
@nathanh2011
Copy link
Author

That would be because the custom loader calls the default load function. All it needs to do is call callbacks.onSuccess directly.

Sorry I thought I actually replied back to this, after many hours, the second I posted this I saw that I had the extra load() there. But when I take it out, it does stop calling the XMLHttpRequest for the page, but it doesn't actually do anything with the manifest I give it and nothing happens. No files are loaded and nothing is played.

I put event listeners on for errors and nothing comes out and the debug shows no extra information. Manifest load/parsed event listeners also don't fire.

@robwalch
Copy link
Collaborator

Sorry I thought I actually replied back to this, after many hours, the second I posted this I saw that I had the extra load() there. But when I take it out, it does stop calling the XMLHttpRequest for the page, but it doesn't actually do anything with the manifest I give it and nothing happens. No files are loaded and nothing is played.

Can you provide an example?

The custom loader should look like this (you are responsible for any async logic and updating of loader stats):

loader: class CustomLoader extends Hls.DefaultConfig.loader {
  constructor(config) {
    super(config); 
  }
  load(context, config, callbacks) {
    if (context.type === 'manifest') {
      console.log('Custom loader invoked for manifest');
      const manifestData = getCustomManifest();
      const response = {
        url: context.url,
        data: manifestData,
        code: 200
      };
      const stats = this.stats;
      callbacks.onSuccess(response, stats, context, {});
    } else {                                    
      // Fallback for other resource types
      super.load(context, config, callbacks);
    }
  }                            
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants