tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

testmediasource.js (2613B)


      1 function testmediasource(config) {
      2 
      3    return new Promise(function(resolve, reject) {
      4        // Fetch the media resources
      5        var fetches = [config.audioPath, config.videoPath].map(function(path) {
      6            return fetch(path).then(function(response) {
      7                if (!response.ok) throw new Error('Resource fetch failed');
      8                return response.arrayBuffer();
      9            });
     10        });
     11 
     12        Promise.all(fetches).then(function(resources) {
     13            config.audioMedia = resources[0];
     14            config.videoMedia = resources[1];
     15            if (Object.hasOwn(config, 'audioBlankBytes')) {
     16                audioMedia = new Uint8Array(config.audioMedia);
     17                for (const pos of config.audioBlankBytes) {
     18                    audioMedia[pos] = ' '.charCodeAt();
     19                }
     20            }
     21            if (Object.hasOwn(config, 'videoBlankBytes')) {
     22                videoMedia = new Uint8Array(config.videoMedia);
     23                for (const pos of config.videoBlankBytes) {
     24                    videoMedia[pos] = ' '.charCodeAt();
     25                }
     26            }
     27 
     28            // Create media source
     29            var source = new MediaSource();
     30            source.done = new Promise(function(resolvesource,rejectsource){
     31 
     32                // Create and fill source buffers when the media source is opened
     33                source.addEventListener('sourceopen', onSourceOpen);
     34                resolve(source);
     35 
     36                function onSourceOpen(event) {
     37                    var audioSourceBuffer = source.addSourceBuffer(config.audioType),
     38                        videoSourceBuffer = source.addSourceBuffer(config.videoType);
     39 
     40                    audioSourceBuffer.addEventListener('updateend',onUpdateEnd);
     41                    videoSourceBuffer.addEventListener('updateend',onUpdateEnd);
     42 
     43                    audioSourceBuffer.appendBuffer(config.audioMedia);
     44                    videoSourceBuffer.appendBuffer(config.videoMedia);
     45 
     46                    function onUpdateEnd(event){
     47                        event.target.removeEventListener('updateend', onUpdateEnd);
     48                        if (!audioSourceBuffer.updating && !videoSourceBuffer.updating) {
     49                            if (source.readyState !== 'open') {
     50                                rejectsource(new Error("Media source error"));
     51                            } else {
     52                                source.endOfStream();
     53                                resolvesource();
     54                            }
     55                        }
     56                    }
     57                }
     58            });
     59        });
     60    });
     61 }