videojs-contrib-eme
Version:
Supports Encrypted Media Extensions for playback of encrypted content in Video.js
3 lines (2 loc) • 10.3 kB
JavaScript
/*! @name videojs-contrib-eme @version 3.4.0 @license Apache-2.0 */
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("video.js"),require("global/window"),require("global/document")):"function"==typeof define&&define.amd?define(["exports","video.js","global/window","global/document"],t):t(e.videojsContribEme={},e.videojs,e.window,e.document)}(this,function(e,t,n,i){"use strict";t=t&&t.hasOwnProperty("default")?t.default:t,n=n&&n.hasOwnProperty("default")?n.default:n,i=i&&i.hasOwnProperty("default")?i.default:i;var r=function(e,i,r){var s=function(e){var t=(new n.DOMParser).parseFromString(String.fromCharCode.apply(null,new Uint16Array(e)),"application/xml"),i=t.getElementsByTagName("HttpHeaders")[0],r={};if(i)for(var s=i.getElementsByTagName("name"),o=i.getElementsByTagName("value"),a=0;a<s.length;a++)r[s[a].childNodes[0].nodeValue]=o[a].childNodes[0].nodeValue;var c,u=t.getElementsByTagName("Challenge")[0];return u&&(c=n.atob(u.childNodes[0].nodeValue)),{headers:r,message:c}}(i),o=s.headers,a=s.message;t.xhr({uri:e,method:"post",headers:o,body:a,responseType:"arraybuffer"},r)},s=function(e){var t;return Object.keys(e).forEach(function(i){var r={},s=e[i].audioContentType,o=e[i].videoContentType;s&&(r.audioCapabilities=[{contentType:s}]),o&&(r.videoCapabilities=[{contentType:o}]),t=t?t.catch(function(e){return n.navigator.requestMediaKeySystemAccess(i,[r])}):n.navigator.requestMediaKeySystemAccess(i,[r])}),t},o=function(e){var n=e.mediaKeys,i=e.initDataType,r=e.initData,s=e.options,o=e.getLicense,a=e.removeSession,c=e.eventBus,u=n.createSession();u.addEventListener("message",function(e){o(s,e.message).then(function(e){return u.update(e)}).catch(t.log.error.bind(t.log.error,"failed to get and set license"))},!1),u.addEventListener("keystatuseschange",function(e){var n=!1;u.keyStatuses.forEach(function(i,r){switch(c.trigger({keyId:r,status:i,target:u,type:"keystatuschange"}),i){case"expired":n=!0;break;case"internal-error":t.log.warn('Key status reported as "internal-error." Leaving the session open since we don\'t have enough details to know if this error is fatal.',e)}}),n&&u.close().then(function(){a(r)})},!1),u.generateRequest(i,r).catch(t.log.error.bind(t.log.error,"Unable to create or initialize key session"))},a=function(e,t){return function(n,i){return new Promise(function(r,s){e(n,i,function(e,n){t&&t.trigger("licenserequestattempted"),e&&s(e),r(n)})})}},c=function(e,n){if("string"==typeof n&&(n={url:n}),!n.url&&!n.getLicense)throw new Error("Neither URL nor getLicense function provided to get license");var i;return n.url&&!n.getLicense&&(n.getLicense="com.microsoft.playready"===e?(i=n.url,function(e,t,n){r(i,t,function(e,t,i){e?n(e):n(null,i)})}):function(e){return function(n,i,r){t.xhr({uri:e,method:"POST",responseType:"arraybuffer",body:i,headers:{"Content-type":"application/octet-stream"}},function(e,t,n){e?r(e):r(null,n)})}}(n.url)),n},u=function(e){var n=e.video,i=e.initDataType,r=e.initData,u=e.options,y=e.removeSession,d=e.eventBus,f=Promise.resolve();if(void 0===n.mediaKeysObject){var l,m;if(n.mediaKeysObject=null,n.pendingSessionData=[],!(f=s(u.keySystems)))return t.log.error("No supported key system found"),Promise.resolve();f=f.then(function(e){return new Promise(function(t,i){n.keySystem=e.keySystem,(m=c(e.keySystem,u.keySystems[e.keySystem])).getCertificate?m.getCertificate(u,function(n,r){n?i(n):(l=r,t(e))}):t(e)})}).then(function(e){return e.createMediaKeys()}).then(function(e){return function(e){var t=e.video,n=e.certificate,i=e.createdMediaKeys,r=e.options,s=e.getLicense,a=e.removeSession,c=e.eventBus;t.mediaKeysObject=i,n&&i.setServerCertificate(n);for(var u=0;u<t.pendingSessionData.length;u++){var y=t.pendingSessionData[u];o({mediaKeys:t.mediaKeysObject,initDataType:y.initDataType,initData:y.initData,options:r,getLicense:s,removeSession:a,eventBus:c})}return t.pendingSessionData=[],t.setMediaKeys(i)}({video:n,certificate:l,createdMediaKeys:e,options:u,getLicense:a(m.getLicense,d),removeSession:y,eventBus:d})}).catch(t.log.error.bind(t.log.error,"Failed to create and initialize a MediaKeys object"))}return f.then(function(){!function(e){var t=e.video,n=e.initDataType,i=e.initData,r=e.options,s=e.getLicense,a=e.removeSession,c=e.eventBus;t.mediaKeysObject?o({mediaKeys:t.mediaKeysObject,initDataType:n,initData:i,options:r,getLicense:s,removeSession:a,eventBus:c}):t.pendingSessionData.push({initDataType:n,initData:i})}({video:n,initDataType:i,initData:r,options:u,getLicense:n.keySystem?a(c(n.keySystem,u.keySystems[n.keySystem]).getLicense,d):null,removeSession:y,eventBus:d})})},y=function(e,t){if(e===t)return!0;if(e.byteLength!==t.byteLength)return!1;for(var n=new DataView(e),i=new DataView(t),r=0;r<n.byteLength;r++)if(n.getUint8(r)!==i.getUint8(r))return!1;return!0},d=function(e){return e instanceof Uint8Array||e instanceof Uint16Array?e.buffer:e},f=function(e){var t=e.initData,n=e.id,i=e.cert;"string"==typeof n&&(n=function(e){for(var t=new ArrayBuffer(2*e.length),n=new Uint16Array(t),i=0;i<e.length;i++)n[i]=e.charCodeAt(i);return n}(n));var r=0,s=new ArrayBuffer(t.byteLength+4+n.byteLength+4+i.byteLength),o=new DataView(s);new Uint8Array(s,r,t.byteLength).set(t),r+=t.byteLength,o.setUint32(r,n.byteLength,!0),r+=4;var a=new Uint16Array(s,r,n.length);return a.set(n),r+=a.byteLength,o.setUint32(r,i.byteLength,!0),r+=4,new Uint8Array(s,r,i.byteLength).set(i),new Uint8Array(s,0,s.byteLength)},l=function(e,t){return s=t,n=String.fromCharCode.apply(null,new Uint16Array(s.buffer)),(r=i.createElement("a")).href=n,r.hostname;var n,r,s},m=function(e){var i,r,s=e.video,o=e.initData,a=e.options,c=e.eventBus,u=a.keySystems["com.apple.fps.1_0"],y=u.getCertificate||(i=u.certificateUri,function(e,n){t.xhr({uri:i,responseType:"arraybuffer"},function(e,t,i){e?n(e):n(null,new Uint8Array(i))})}),d=u.getContentId||l,m=u.getLicense||(r=u.licenseUri,function(e,n,i,s){t.xhr({uri:r,method:"POST",responseType:"arraybuffer",body:i,headers:{"Content-type":"application/octet-stream"}},function(e,t,n){e?s(e):s(null,n)})});return new Promise(function(e,t){y(a,function(n,i){n?t(n):e(i)})}).then(function(e){return function(e){var t=e.video,i=e.contentId,r=e.initData,s=e.cert,o=e.options,a=e.getLicense,c=e.eventBus;return new Promise(function(e,u){if(t.webkitKeys||t.webkitSetMediaKeys(new n.WebKitMediaKeys("com.apple.fps.1_0")),t.webkitKeys){var y=t.webkitKeys.createSession("video/mp4",f({id:i,initData:r,cert:s}));y?(y.contentId=i,y.addEventListener("webkitkeymessage",function(e){a(o,i,e.message,function(e,t){c&&c.trigger("licenserequestattempted"),e?u(e):y.update(new Uint8Array(t))})}),y.addEventListener("webkitkeyadded",function(t){e(t)}),y.addEventListener("webkitkeyerror",function(e){u(e)})):u("Could not create key session")}else u("Could not create MediaKeys")})}({video:s,cert:e,initData:o,getLicense:m,options:a,contentId:d(a,o),eventBus:c})}).catch(t.log.error.bind(t.log.error))},p=function(e,n,i,s){var o=e.msKeys.createSession("video/mp4",n);o?(o.addEventListener("mskeymessage",function(e){!function(e,n,i,s){var o=e.keySystems["com.microsoft.playready"];if("function"!=typeof o.getKey){"string"==typeof o&&(o={url:o});var a=o.url||i.destinationURL;r(a,i.message.buffer,function(e,i){s&&s.trigger("licenserequestattempted"),e?t.log.error("Unable to request key from url: "+a):n.update(new Uint8Array(i.body))})}else o.getKey(e,i.destinationURL,i.message.buffer,function(e,i){e?t.log.error("Unable to get key: "+e):n.update(i)})}(i,o,e,s)}),o.addEventListener("mskeyerror",function(e){t.log.error("Unexpected key error from key session with code: "+o.error.code+" and systemCode: "+o.error.systemCode)})):t.log.error("Could not create key session.")},g=function(e,t){for(var n=0;n<e.length;n++)if(e[n].initData){var i=d(e[n].initData),r=d(t);if(y(i,r))return!0}return!1},v=function(e,t){for(var n=0;n<e.length;n++)if(e[n].initData===t)return void e.splice(n,1)},h=function(e,t,n,i){if(!t||!t.keySystems)return Promise.resolve();var r=e.initData;return s(t.keySystems).then(function(s){var o=s.keySystem;if(t.keySystems[o]&&t.keySystems[o].pssh&&(r=t.keySystems[o].pssh),!g(n,r))return n.push({initData:r}),u({video:e.target,initDataType:e.initDataType,initData:r,options:t,removeSession:v.bind(null,n),eventBus:i})})},b=function(e,t,n){if(t.keySystems&&t.keySystems["com.apple.fps.1_0"])return m({video:e.target,initData:e.initData,options:t,eventBus:n})},S=function(e,i,r,s){if(i.keySystems&&i.keySystems["com.microsoft.playready"]&&!r.reduce(function(e,t){return e||t.playready},!1)){var o=e.initData;i.keySystems["com.microsoft.playready"]&&i.keySystems["com.microsoft.playready"].pssh&&(o=i.keySystems["com.microsoft.playready"].pssh),r.push({playready:!0,initData:o}),function(e){var i=e.video,r=e.initData,s=e.options,o=e.eventBus;i.msKeys&&delete i.msKeys;try{i.msSetMediaKeys(new n.MSMediaKeys("com.microsoft.playready"))}catch(e){return void t.log.error("Unable to create media keys for PlayReady key system. Error: "+e.message)}p(i,r,s,o)}({video:e.target,initData:o,options:i,eventBus:s})}},k=function(e){return t.mergeOptions(e.currentSource(),e.eme.options)},w=function(e){var t=e.src();t!==e.eme.activeSrc&&(e.eme.activeSrc=t,e.eme.sessions=[])},D=function(e){void 0===e&&(e={});var n=this;n.ready(function(){return function(e){"video"===e.$(".vjs-tech").tagName.toLowerCase()&&(w(e),e.tech_.el_.addEventListener("encrypted",function(t){w(e),h(t,k(e),e.eme.sessions,e.tech_)}),e.tech_.el_.addEventListener("webkitneedkey",function(t){w(e),b(t,k(e),e.tech_)}),t.browser.IS_EDGE||e.tech_.el_.addEventListener("msneedkey",function(t){w(e),S(t,k(e),e.eme.sessions,e.tech_)}))}(n)}),n.eme={initializeMediaKeys:function(i,r){void 0===i&&(i={}),void 0===r&&(r=function(){});var s=t.mergeOptions(n.currentSource(),e,i),o={initDataType:"cenc",initData:null,target:n.tech_.el_};w(n),n.tech_.el_.setMediaKeys?h(o,s,n.eme.sessions,n.tech_).then(function(){return r()}).catch(function(e){return r(e)}):n.tech_.el_.msSetMediaKeys&&(S(o,s,n.eme.sessions,n.tech_),r())},options:e}};(t.registerPlugin||t.plugin)("eme",D),e.hasSession=g,e.removeSession=v,e.handleEncryptedEvent=h,e.handleWebKitNeedKeyEvent=b,e.handleMsNeedKeyEvent=S,e.getOptions=k,e.setupSessions=w,e.default=D,Object.defineProperty(e,"__esModule",{value:!0})});