UNPKG

@flowplayer/player

Version:
2 lines (1 loc) 6.4 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):((e="undefined"!=typeof globalThis?globalThis:e||self).flowplayer=e.flowplayer||{},e.flowplayer.drm=t())}(this,(function(){"use strict";const e="com.widevine.alpha",t="com.microsoft.playready",r="com.apple.fps.1_0";var n=Object.freeze({__proto__:null,WIDEVINE:e,PLAYREADY:t,CLEARKEY:"org.w3.clearkey",FAIRPLAY:r,LICENSE_SERVER:"license_server",HTTP_HEADERS:"http_headers",CERTIFICATE:"certificate",VENDOR:"vendor",REQUEST_MEDIA_KEY_SYSTEM_ACCESS_FUNCTION:"request_media_key_system_access_function",QUERY_PARAMS:"query_params",KEY:"key",KEY_ID:"kid"});function s(){const e="undefined"!=typeof document&&"undefined"!=typeof window,t=e?navigator.userAgent:"",r=/iP(hone|od)/i.test(t)&&!/iPad/.test(t)&&!/IEMobile/i.test(t),n=/Android/.test(t)&&!/Firefox/.test(t),s=/^((?!chrome|android).)*safari/i.test(t),o=/chrome|crios/i.test(t)&&!/opr|opera|chromium|edg|ucbrowser|googlebot/i.test(t),i=/firefox|fxios/i.test(t)&&!/seamonkey/i.test(t),a=/edg/i.test(t),c=/opr|opera/i.test(t),d=/SamsungBrowser/.test(t),u=d&&/SMART-TV/.test(t);return{controls:!r,video:function(t){return e&&document.createElement("video").canPlayType(t)},lang:e&&window.navigator.language,android:n,iphone:r,safari:s,edge:a,opera:c,chrome:o,firefox:i,ios:e&&/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream,samsung:d,samsung_tv:d&&u,touch:"ontouchstart"in window,tizen:"tizen"in window,webOS:"webos"in window}}function o(n){const o={};return Object.keys(n).forEach((function(i){(function(n){const o=s();switch(n){case e:return o.chrome||o.firefox||o.edge||o.opera;case"org.w3.clearkey":return o.chrome||o.firefox;case r:return o.safari;case t:return o.edge}})(i)&&function(e,t,n){Object.assign(n,t),n.emeEnabled=!0,t.request_media_key_system_access_function&&(n.requestMediaKeySystemAccessFunc=t.request_media_key_system_access_function);const s=t.http_headers;s&&(n.licenseXhrSetup=function(e){Object.keys(s).forEach((function(t){e.setRequestHeader(t,s[t])}))}),n.drmSystems=n.drmSystems||{},Object.assign(n.drmSystems,{[e===r?"com.apple.fps":e]:{licenseUrl:t.license_server,serverCertificateUrl:t.certificate}})}(i,n[i],o)})),o}function i(r){const n={};return Object.keys(r).forEach((function(o){(function(r){const n=s();switch(r){case e:return n.chrome||n.firefox||n.edge||n.opera;case"org.w3.clearkey":return n.chrome||n.firefox;case t:return n.edge}})(o)&&(n[o]=function(e){return Object.assign({serverURL:e.license_server,httpRequestHeaders:e.http_headers},e)}(r[o]))})),n}const a=(e,t)=>fetch(e,t).then(c).then(e=>new Uint8Array(e)),c=e=>{if(e.ok)return e.arrayBuffer();throw d(e)},d=e=>new Error(`http:error status=${e.status} during key retrieval`);var u=Object.freeze({__proto__:null,fairplay_fetch_certificate:(e,t)=>{a(e).then(t)},fairplay_request_license:function(e,t,r){const n=((e,t)=>e.includes(t.assetId)&&Object.keys(t.queryParams||{}).find(t=>e.includes(t))?e:e.includes(t.assetId)?`${e}?${new URLSearchParams(t.queryParams).toString()}`:`${e}/${t.assetId}?${new URLSearchParams(t.queryParams).toString()}`)(e,t);a(n,{method:"POST",headers:{"Content-Type":"application/octet-stream"},body:t.message}).then(r)}});var f,l=Object.freeze({__proto__:null,fairplay_fetch_certificate:(e,t)=>{a(e).then(t)},fairplay_request_license:function(e,t,r){const n=new FormData;var s;n.set("assetid",t.assetId),n.set("spc",(s=t.message,btoa(Array.prototype.map.call(s,(function(e){return String.fromCharCode(e)})).join("")))),fetch(e,{method:"POST",body:n,headers:t.headers}).then(e=>{if(e.ok)return e.text();throw d(e)}).then(e=>function(e){const t=atob(e),r=new Uint8Array(t.length);return Array.prototype.forEach.call(t,(function(e,t){r[t]=e.charCodeAt(0)})),r}(e)).then(r)}}),y=Object.freeze({__proto__:null,ezdrm:u,buydrm:l});function h(e,t,n){const s=t[r],o=null==s?void 0:s.certificate;if(!s||!o)return;if(e.src&&0===e.src.indexOf("blob:"))return;const i=s.vendor||"ezdrm",a="string"==typeof i?y[i]:i;a.fairplay_fetch_certificate(o,(function(o){e.webkitSetMediaKeys(new window.WebKitMediaKeys(r));const i=n.initData,c=function(e){const t=function(e){const t=new Uint16Array(e);return String.fromCharCode(...t)}(e.buffer);return t.substring(t.indexOf("skd:")+6).split(";").pop()}(i);if(!c)return console.debug("flowplayer::drm::fairplay - unable to extract content id from init data");const d=function(e,t,r){"string"==typeof t&&(t=function(e){const t=new ArrayBuffer(2*e.length),r=new Uint16Array(t);return Array(...Array(e.length)).map((e,t)=>t).forEach(t=>{r[t]=e.charCodeAt(t)}),r}(t));let n=0;const s=new ArrayBuffer(e.byteLength+4+t.byteLength+4+r.byteLength),o=new DataView(s);new Uint8Array(s,n,e.byteLength).set(e),n+=e.byteLength,o.setUint32(n,t.byteLength,!0),n+=4;const i=new Uint16Array(s,n,t.length);i.set(t),n+=i.byteLength,o.setUint32(n,r.byteLength,!0),n+=4;return new Uint8Array(s,n,r.byteLength).set(r),new Uint8Array(s,0,s.byteLength)}(i,c,o),u=e.webkitKeys.createSession("application/x-mpegurl",d);u.addEventListener("webkitkeyerror",console.debug.bind(console,"webkitkeyerror")),u.addEventListener("webkitkeymessage",(function(e){const n={message:e.message,assetId:c,headers:s.http_headers||{},queryParams:t[r].query_params||{}};a.fairplay_request_license(s.license_server,n,e=>u.update(e))}))}))}const m=((f=class DRM{init(e,t,r){let n;r.on("webkitneedkey",e=>h(r,n.drm||{},e)),r.on("src",t=>{n=t.detail,function(e){var t,r,n,s,o;null===(t=e.hls)||void 0===t||delete t.drmSystems,null===(r=e.hls)||void 0===r||delete r.licenseXhrSetup,null===(n=e.hls)||void 0===n||delete n.requestMediaKeySystemAccessFunc,null===(s=e.hls)||void 0===s||delete s.drmSystemOptions,null===(o=e.hls)||void 0===o||delete o.emeEnabled}(e),this.hls_drm(r,e,t.detail.drm),this.dash_drm(r,e,t.detail.drm)})}hls_drm(e,t,r){if(!r)return;const n=o(r);e.setOpts({hls:Object.assign(t.hls||{},n)})}dash_drm(e,t,r){if(!r)return;const n=t.dash||{},s=i(r);e.setOpts({dash:Object.assign(n,{drm:s})})}}).pluginName="drm",f);Object.assign(m,n);return function(e,t){if("object"==typeof exports&&"undefined"!=typeof module)return t;if(null===document.currentScript)return t;"flowplayer"in e||(e.flowplayer={extensions:[]});const r=e.flowplayer;return"function"==typeof r?(r(t),t):(Array.isArray(r.extensions)||(r.extensions=[]),~r.extensions.indexOf(t)||r.extensions.push(t),t)}(window,m)}));