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
|
<!DOCTYPE html>
<!-- Copyright © 2016 Chromium authors and World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang). -->
<html>
<head>
<title>Clear Key Playback with Multiple Sessions</title>
<script src="encrypted-media-utils.js"></script>
<!--
Test has been migrated to the root directory and is being disabled here.
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
-->
</head>
<body>
<video id="testVideo"></video>
<div id="log"></div>
<script>
async_test(function(test)
{
var video = document.getElementById('testVideo');
var content = 'webm/test-encrypted-different-av-keys.webm';
var audioMediaKeySession = null;
var videoMediaKeySession = null;
var audioInitDataType = null;
var videoInitDataType = null;
var audioInitData = null;
var videoInitData = null;
var audioKeyProvided = false;
var videoKeyProvided = false;
// The 2 streams use different key ids and different keys.
var audioKeyId = '1234567890123456';
var audioKey = new Uint8Array([0x30, 0x30, 0x62, 0xF1, 0x68, 0x14, 0xD2, 0x7B,
0x68, 0xEF, 0x12, 0x2A, 0xFC, 0xE4, 0xAE, 0x0A]);
var videoKeyId = '0123456789012345';
var videoKey = new Uint8Array([0x7A, 0x7A, 0x62, 0xF1, 0x68, 0x14, 0xD2, 0x7B,
0x68, 0xEF, 0x12, 0x2A, 0xFC, 0xE4, 0xAE, 0x0A]);
function onEncrypted(event)
{
var keyId = String.fromCharCode.apply(null, new Uint8Array(event.initData));
// To avoid issues when comparing the expected.txt file
// (which logs the events in the order they occur), save
// the initData and make the calls to generateRequest()
// only after both "onencrypted" events are received.
// This prevents a "message" event from occurring before
// both "onencrypted" events are received.
var mediaKeySession = video.mediaKeys.createSession();
if (keyId == videoKeyId) {
assert_equals(videoMediaKeySession, null);
videoMediaKeySession = mediaKeySession;
videoInitDataType = event.initDataType;
videoInitData = event.initData;
// Return if audio "onencrypted" event not yet received.
if (audioMediaKeySession == null)
return;
} else {
assert_equals(keyId, audioKeyId);
assert_equals(audioMediaKeySession, null);
audioMediaKeySession = mediaKeySession;
audioInitDataType = event.initDataType;
audioInitData = event.initData;
// Return if video "onencrypted" event not yet received.
if (videoMediaKeySession == null)
return;
}
// Both sessions have been created.
assert_not_equals(videoMediaKeySession, null);
assert_not_equals(audioMediaKeySession, null);
var promises = [];
waitForEventAndRunStep('message', videoMediaKeySession, onMessage, test);
promises.push(videoMediaKeySession.generateRequest(videoInitDataType, videoInitData));
waitForEventAndRunStep('message', audioMediaKeySession, onMessage, test);
promises.push(audioMediaKeySession.generateRequest(audioInitDataType, audioInitData));
Promise.all(promises).catch(function(error) {
forceTestFailureFromPromise(test, error);
});
}
function onMessage(event)
{
assert_equals(event.messageType, 'license-request');
var keyId = extractSingleKeyIdFromMessage(event.message);
if (event.target == videoMediaKeySession) {
assert_equals(String.fromCharCode.apply(null, keyId), videoKeyId);
var jwkSet = stringToUint8Array(createJWKSet(createJWK(keyId, videoKey)));
videoMediaKeySession.update(jwkSet).then(function(result) {
videoKeyProvided = true;
}).catch(function(error) {
forceTestFailureFromPromise(test, error);
});
} else {
assert_equals(event.target, audioMediaKeySession);
assert_equals(String.fromCharCode.apply(null, keyId), audioKeyId);
var jwkSet = stringToUint8Array(createJWKSet(createJWK(keyId, audioKey)));
audioMediaKeySession.update(jwkSet).then(function(result) {
audioKeyProvided = true;
}).catch(function(error) {
forceTestFailureFromPromise(test, error);
});
}
}
function onPlaying(event)
{
// Video should not start playing until both keys have been
// provided.
assert_true(videoKeyProvided);
assert_true(audioKeyProvided);
// Not using waitForEventAndRunStep() to avoid too many
// EVENT(onTimeUpdate) logs.
video.addEventListener('timeupdate', onTimeUpdate, true);
}
function onTimeUpdate(event)
{
if (event.target.currentTime < 0.2)
return;
test.done();
}
navigator.requestMediaKeySystemAccess('org.w3.clearkey', getConfigurationForFile(content)).then(function(access) {
return access.createMediaKeys();
}).then(function(mediaKeys) {
waitForEventAndRunStep('encrypted', video, onEncrypted, test);
waitForEventAndRunStep('playing', video, onPlaying, test);
video.src = content;
return video.setMediaKeys(mediaKeys);
}).then(function(result) {
video.play();
}).catch(function(error) {
forceTestFailureFromPromise(test, error);
});
}, 'Playback using Clear Key key system with multiple sessions.');
</script>
</body>
</html>
|