-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdash-eme.js
229 lines (202 loc) · 7.55 KB
/
dash-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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
function prefixedAttributeName(obj, suffix, opt_preprefix) {
suffix = suffix.toLowerCase()
if (opt_preprefix === undefined) {
opt_preprefix = '';
}
for (var attr in obj) {
lattr = attr.toLowerCase();
if (lattr.indexOf(opt_preprefix) == 0 &&
lattr.indexOf(suffix, lattr.length - suffix.length) != -1) {
return attr;
}
}
return null;
}
function normalizeAttribute(obj, suffix, opt_preprefix) {
if (opt_preprefix === undefined) {
opt_preprefix = '';
}
attr = prefixedAttributeName(obj, suffix, opt_preprefix);
if (attr) {
obj[opt_preprefix + suffix] = obj[attr];
}
}
var EMEHandler = function(video) {
this.video = video;
normalizeAttribute(video, 'generateKeyRequest');
normalizeAttribute(video, 'addKey');
this.initDataQueue = [];
this.flavor = null;
this.keySystem = null;
this.licenseServerURL = null;
var attr = prefixedAttributeName(video, 'needkey', 'on');
if (!attr) {
// just a shot in the dark here
video.addEventListener('needkey', this.onNeedKey.bind(this));
video.addEventListener('keymessage', this.onKeyMessage.bind(this));
video.addEventListener('keyerror', this.onKeyError.bind(this));
} else {
video.addEventListener(attr.substring(2), this.onNeedKey.bind(this));
attr = prefixedAttributeName(video, 'keymessage', 'on');
if (attr)
video.addEventListener(attr.substring(2), this.onKeyMessage.bind(this));
attr = prefixedAttributeName(video, 'keyerror', 'on');
if (attr)
video.addEventListener(attr.substring(2), this.onKeyError.bind(this));
}
normalizeAttribute(window, 'MediaKeys');
attr = prefixedAttributeName(video, 'setMediaKeys');
if (attr && !window.MediaKeys) {
// try extra hard to scrounge up a MediaKeys
var index = attr.indexOf('etMediaKeys');
if (index != -1) {
this.mediaKeysPrefix = attr.substring(0, index - 1).toLowerCase();
window.MediaKeys = window[this.mediaKeysPrefix.toUpperCase() + 'MediaKeys'];
}
}
normalizeAttribute(video, 'setMediaKeys');
};
window.EMEHandler = EMEHandler;
// We only use BMFF for this demo player (soon to change tho)
EMEHandler.kMime = 'video/mp4; codecs="avc1.640028"';
EMEHandler.kFlavorToSystem = {
'clearkey': ['webkit-org.w3.clearkey', 'org.w3.clearkey'],
'widevine': ['com.widevine.alpha'],
'playready': ['com.youtube.playready', 'com.microsoft.playready']
};
EMEHandler.prototype.init = function(flavorMap, opt_flavor) {
this.chooseFlavor(EMEHandler.kMime, flavorMap, opt_flavor);
this.isClearKey = this.flavor == 'clearkey';
}
EMEHandler.prototype.chooseFlavor = function(mime, flavorMap, opt_flavor) {
for (var flavor in flavorMap) {
if (opt_flavor && flavor != opt_flavor) continue;
var systems = EMEHandler.kFlavorToSystem[flavor];
if (!systems) continue;
for (var i in systems) {
if (window.MediaKeys && MediaKeys.isTypeSupported) {
if(!MediaKeys.isTypeSupported(systems[i], mime) &&
!MediaKeys.isTypeSupported(mime, systems[i]))
continue;
} else if (!this.video.canPlayType(mime, systems[i])) {
continue;
}
this.flavor = flavor;
this.keySystem = systems[i];
this.licenseServerURL = flavorMap[flavor];
return;
}
}
throw 'Could not find a compatible key system';
}
EMEHandler.prototype.extractBMFFClearKeyID = function(initData) {
// Accessing the Uint8Array's underlying ArrayBuffer is impossible, so we
// copy it to a new one for parsing.
var abuf = new ArrayBuffer(initData.length);
var view = new Uint8Array(abuf);
view.set(initData);
var dv = new DataView(abuf);
var pos = 0;
while (pos < abuf.byteLength) {
var box_size = dv.getUint32(pos, false);
var type = dv.getUint32(pos + 4, false);
if (type != 0x70737368)
throw 'Box type ' + type.toString(16) + ' not equal to "pssh"';
// Scan for Clear Key header
if ((dv.getUint32(pos + 12, false) == 0x58147ec8) &&
(dv.getUint32(pos + 16, false) == 0x04234659) &&
(dv.getUint32(pos + 20, false) == 0x92e6f52c) &&
(dv.getUint32(pos + 24, false) == 0x5ce8c3cc)) {
var size = dv.getUint32(pos + 28, false);
if (size != 16) throw 'Unexpected KID size ' + size;
return new Uint8Array(abuf.slice(pos + 32, pos + 32 + size));
}
// Failing that, scan for Widevine protobuf header
if ((dv.getUint32(pos + 12, false) == 0xedef8ba9) &&
(dv.getUint32(pos + 16, false) == 0x79d64ace) &&
(dv.getUint32(pos + 20, false) == 0xa3c827dc) &&
(dv.getUint32(pos + 24, false) == 0xd51d21ed)) {
return new Uint8Array(abuf.slice(pos + 36, pos + 52));
}
pos += box_size;
}
// Couldn't find it, give up hope.
return initData;
}
EMEHandler.prototype.onNeedKey = function(e) {
dlog(2, 'onNeedKey()');
if (!this.keySystem)
throw 'Not initialized! Bad manifest parse?';
if (e.initData.length == 16) {
dlog(2, 'Dropping non-BMFF needKey event');
return;
}
var initData = e.initData;
if (this.isClearKey) {
initData = this.extractBMFFClearKeyID(e.initData);
}
if (window.MediaKeys) {
if (!this.mediaKeys) {
this.mediaKeys = new MediaKeys(this.keySystem);
this.video.setMediaKeys(this.mediaKeys);
}
var session = this.mediaKeys.createSession(EMEHandler.kMime, initData);
session.addEventListener('keymessage', this.onKeyMessage.bind(this));
session.addEventListener('keyerror', this.onKeyError.bind(this));
session.addEventListener(this.mediaKeysPrefix + 'keymessage', this.onKeyMessage.bind(this));
session.addEventListener(this.mediaKeysPrefix + 'keyerror', this.onKeyError.bind(this));
} else {
this.video.generateKeyRequest(this.keySystem, initData);
}
this.initDataQueue.push(initData);
}
function parsePlayReadyKeyMessage(message) {
// message is UTF-16LE, let's throw out every second element.
var message_ascii = new Array();
for (var i = 0; i < message.length; i += 2) {
message_ascii.push(message[i]);
}
var str = String.fromCharCode.apply(String, message_ascii);
var doc = (new DOMParser()).parseFromString(str, 'text/xml');
return stringToArray(atob(doc.childNodes[0].childNodes[0].childNodes[0].childNodes[0].data));
}
EMEHandler.prototype.onKeyMessage = function(e) {
dlog(2, 'onKeyMessage()');
var message = e.message;
if (this.keySystem == 'com.microsoft.playready') {
message = parsePlayReadyKeyMessage(message);
if (!message) throw 1;
}
var initData = this.initDataQueue.shift();
var xhr = new XMLHttpRequest();
xhr.open("POST", this.licenseServerURL);
xhr.addEventListener('load', this.onLoad.bind(this, initData, e.sessionId || e.target));
xhr.responseType = 'arraybuffer';
xhr.send(message);
}
EMEHandler.prototype.onKeyError = function(e) {
dlog(2, 'onKeyError(' + e.keySystem + ', ' + e.errorCode.code + ', ' + e.systemCode + ')');
}
function stringToArray(s) {
var array = new Uint8Array(s.length);
for (var i = 0; i < s.length; i++) {
array[i] = s.charCodeAt(i);
}
return array;
}
function arrayToString(a) {
return String.fromCharCode.apply(String, a);
}
EMEHandler.prototype.onLoad = function(initData, session, e) {
dlog(2, 'onLoad(' + this.licenseServerURL + ')');
if (e.target.status < 200 || e.target.status > 299)
throw 'Bad XHR status: ' + e.target.statusText;
// Parse "GLS/1.0 0 OK\r\nHeader: Value\r\n\r\n<xml>HERE BE SOAP</xml>
var responseString = arrayToString(new Uint8Array(e.target.response)).split('\r\n').pop();
var license = stringToArray(responseString);
if (window.MediaKeys) {
session.update(license);
} else {
this.video.addKey(this.keySystem, license, initData, session);
}
}