-
Notifications
You must be signed in to change notification settings - Fork 21
/
eme.js
168 lines (145 loc) · 5 KB
/
eme.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright 2014, Mozilla Foundation and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function e(id) {
return document.getElementById(id);
}
function log(msg) {
var log_pane = e('log');
log_pane.appendChild(document.createTextNode(msg));
log_pane.appendChild(document.createElement("br"));
}
function bail(message)
{
return function(err) {
log(message + (err ? " " + err : ""));
}
}
function ArrayBufferToString(arr)
{
var str = '';
var view = new Uint8Array(arr);
for (var i = 0; i < view.length; i++) {
str += String.fromCharCode(view[i]);
}
return str;
}
function StringToArrayBuffer(str)
{
var arr = new ArrayBuffer(str.length);
var view = new Uint8Array(arr);
for (var i = 0; i < str.length; i++) {
view[i] = str.charCodeAt(i);
}
return arr;
}
function Base64ToHex(str)
{
var bin = window.atob(str.replace(/-/g, "+").replace(/_/g, "/"));
var res = "";
for (var i = 0; i < bin.length; i++) {
res += ("0" + bin.charCodeAt(i).toString(16)).substr(-2);
}
return res;
}
function HexToBase64(hex)
{
var bin = "";
for (var i = 0; i < hex.length; i += 2) {
bin += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
}
return window.btoa(bin).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
}
function UpdateSessionFunc(name, keys) {
return function(ev) {
var msgStr = ArrayBufferToString(ev.message);
log(name + " got message from CDM: " + msgStr);
var msg = JSON.parse(msgStr);
var outKeys = [];
for (var i = 0; i < msg.kids.length; i++) {
var id64 = msg.kids[i];
var idHex = Base64ToHex(msg.kids[i]).toLowerCase();
var key = keys[idHex];
if (key) {
log(name + " found key " + key + " for key id " + idHex);
outKeys.push({
"kty":"oct",
"alg":"A128KW",
"kid":id64,
"k":HexToBase64(key)
});
} else {
bail(name + " couldn't find key for key id " + idHex);
}
}
var update = JSON.stringify({
"keys" : outKeys,
"type" : msg.type
});
log(name + " sending update message to CDM: " + update);
ev.target.update(StringToArrayBuffer(update)).then(function() {
log(name + " MediaKeySession update ok!");
}, bail(name + " MediaKeySession update failed"));
}
}
function KeysChange(event) {
var session = event.target;
log("keystatuseschange event on session" + session.sessionId);
var map = session.keyStatuses;
for (var entry of map.entries()) {
var keyId = entry[0];
var status = entry[1];
var base64KeyId = Base64ToHex(window.btoa(ArrayBufferToString(keyId)));
log("SessionId=" + session.sessionId + " keyId=" + base64KeyId + " status=" + status);
}
}
var ensurePromise;
function EnsureMediaKeysCreated(video, keySystem, options, encryptedEvent) {
// We may already have a MediaKeys object if we initialized EME for a
// different MSE SourceBuffer's "encrypted" event, or the initialization
// may still be in progress.
if (ensurePromise) {
return ensurePromise;
}
log("navigator.requestMediaKeySystemAccess("+ JSON.stringify(options) + ")");
ensurePromise = navigator.requestMediaKeySystemAccess(keySystem, options)
.then(function(keySystemAccess) {
return keySystemAccess.createMediaKeys();
}, bail(name + " Failed to request key system access."))
.then(function(mediaKeys) {
log(name + " created MediaKeys object ok");
return video.setMediaKeys(mediaKeys);
}, bail(name + " failed to create MediaKeys object"))
return ensurePromise;
}
function SetupEME(video, keySystem, name, keys, options)
{
video.sessions = [];
video.addEventListener("encrypted", function(ev) {
log(name + " got encrypted event");
EnsureMediaKeysCreated(video, keySystem, options, ev)
.then(function() {
log(name + " ensured MediaKeys available on HTMLMediaElement");
var session = video.mediaKeys.createSession();
video.sessions.push(session);
session.addEventListener("message", UpdateSessionFunc(name, keys));
session.addEventListener("keystatuseschange", KeysChange);
return session.generateRequest(ev.initDataType, ev.initData);
}, bail(name + " failed to ensure MediaKeys on HTMLMediaElement"))
.then(function() {
log(name + " generated request");
}, bail(name + " Failed to generate request."));
});
}