mediasource-worker-util.js (1879B)
1 // This script is intended to be imported into a worker's script, and provides 2 // common preparation for multiple test cases. Errors encountered are either 3 // postMessaged with subject of messageSubject.ERROR, or in the case of failed 4 // mediaLoadPromise, result in promise rejection. 5 6 importScripts("mediasource-message-util.js"); 7 8 if (!this.MediaSource) 9 postMessage({ subject: messageSubject.ERROR, info: "MediaSource API missing from Worker" }); 10 11 let MEDIA_LIST = [ 12 { 13 url: '../mp4/test.mp4', 14 type: 'video/mp4; codecs="mp4a.40.2,avc1.4d400d"', 15 }, 16 { 17 url: '../webm/test.webm', 18 type: 'video/webm; codecs="vp8, vorbis"', 19 }, 20 ]; 21 22 class MediaSourceWorkerUtil { 23 constructor() { 24 this.mediaSource = new MediaSource(); 25 26 // Find supported test media, if any. 27 this.foundSupportedMedia = false; 28 for (let i = 0; i < MEDIA_LIST.length; ++i) { 29 this.mediaMetadata = MEDIA_LIST[i]; 30 if (MediaSource.isTypeSupported(this.mediaMetadata.type)) { 31 this.foundSupportedMedia = true; 32 break; 33 } 34 } 35 36 // Begin asynchronous fetch of the test media. 37 if (this.foundSupportedMedia) { 38 this.mediaLoadPromise = MediaSourceWorkerUtil.loadBinaryAsync(this.mediaMetadata.url); 39 } else { 40 postMessage({ subject: messageSubject.ERROR, info: "No supported test media" }); 41 } 42 } 43 44 static loadBinaryAsync(url) { 45 return new Promise((resolve, reject) => { 46 let request = new XMLHttpRequest(); 47 request.open("GET", url, true); 48 request.responseType = "arraybuffer"; 49 request.onerror = event => { reject(event); }; 50 request.onload = () => { 51 if (request.status != 200) { 52 reject("Unexpected loadData_ status code : " + request.status); 53 } 54 let response = new Uint8Array(request.response); 55 resolve(response); 56 }; 57 request.send(); 58 }); 59 } 60 }