diff --git a/getChallenge.js b/getChallenge.js index 7a97fd8..a2b7350 100644 --- a/getChallenge.js +++ b/getChallenge.js @@ -1,16 +1,17 @@ -export default function() { - navigator.requestMediaKeySystemAccess('com.widevine.alpha', config).then(keySystemAccess => { - createMediaKeys(keySystemAccess); - }) -}; - -const b64 = { - decode: s => Uint8Array.from(atob(s), c => c.charCodeAt(0)), - encode: b => btoa(String.fromCharCode(...new Uint8Array(b))) +export default async function() { + let keySystemAccess; + try{ + keySystemAccess = await navigator.requestMediaKeySystemAccess('com.widevine.alpha', config) + } catch { + config[0]['sessionTypes'] = ['temporary'] + keySystemAccess = await navigator.requestMediaKeySystemAccess('com.widevine.alpha', config) + } + let challenge = await getChallenge(keySystemAccess); + return challenge }; // Tears of Steel Init data Widevine -let initData = b64.decode('AAAARHBzc2gAAAAA7e+LqXnWSs6jyCfc1R0h7QAAACQIARIBMRoNd2lkZXZpbmVfdGVzdCIKMjAxNV90ZWFycyoCU0Q=') +let initData = Uint8Array.from(atob('AAAARHBzc2gAAAAA7e+LqXnWSs6jyCfc1R0h7QAAACQIARIBMRoNd2lkZXZpbmVfdGVzdCIKMjAxNV90ZWFycyoCU0Q='), c => c.charCodeAt(0)) // Widevine CDM config MediaKeyAccess default let config = [{ @@ -24,22 +25,16 @@ let config = [{ }] }]; -async function createMediaKeys(keySystemAccess) { - try{ - let mediaKeys = await keySystemAccess.createMediaKeys(); - let challenge = await onCreate(mediaKeys); - console.log(b64.encode(challenge)) - } catch(err) { - console.error("Unable to create MediaKeys : " + err) - } -} - -// Initialization of the CDM session and generation of the request -function onCreate(mediaKeys) { - return new Promise(resolve => { - let keySession = mediaKeys.createSession(config[0]['sessionTypes']) - - keySession.addEventListener("message", () => resolve(event.message)); - keySession.generateRequest("cenc", initData) +async function getChallenge(keySystemAccess) { + return new Promise((resolve, reject) => { + try{ + keySystemAccess.createMediaKeys().then(mediaKeys => { + let keySession = mediaKeys.createSession(config[0]['sessionTypes']) + keySession.addEventListener("message", () => resolve(event.message)); + keySession.generateRequest("cenc", initData) + }) + } catch(err) { + reject("Unable to create MediaKeys : " + err) + } }) } diff --git a/index.html b/index.html index 2e75e96..41ace1c 100644 --- a/index.html +++ b/index.html @@ -4,7 +4,9 @@