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
|
<!DOCTYPE html>
<!-- Copyright © 2016 Chromium authors and World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang). -->
<html>
<head>
<title>Multiple playbacks alternating between encrypted and clear sources.</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 isUpdatePromiseResolved = false;
var encryptedEventCount = 0;
var playbackCount = 0;
// Content to be played. These files must be the same format.
var encryptedContent = 'webm/test-encrypted.webm';
var unencryptedContent = 'webm/test.webm';
var rawKey = new Uint8Array([0xeb, 0xdd, 0x62, 0xf1, 0x68, 0x14, 0xd2, 0x7b,
0x68, 0xef, 0x12, 0x2a, 0xfc, 0xe4, 0xae, 0x3c]);
function onEncrypted(event)
{
assert_equals(event.target, video);
assert_true(event instanceof window.MediaEncryptedEvent);
assert_equals(event.type, 'encrypted');
// The same decryption key is used by both the audio and
// the video streams so only create a session once. To
// avoid issues when comparing the expected.txt file
// (which logs the events in the order they occur), create
// the session on the second event. This also ensures we
// see both events.
if (++encryptedEventCount != 2)
return;
assert_false(video.mediaKeys === null, "video.mediaKeys is null.");
var mediaKeySession = video.mediaKeys.createSession();
waitForEventAndRunStep('message', mediaKeySession, onMessage, test);
mediaKeySession.generateRequest(event.initDataType, event.initData).catch(function(error) {
forceTestFailureFromPromise(test, error);
});
}
function onMessage(event)
{
assert_true(event instanceof window.MediaKeyMessageEvent);
assert_equals(event.type, 'message');
assert_equals(event.messageType, 'license-request');
var keyId = extractSingleKeyIdFromMessage(event.message);
var jwkSet = stringToUint8Array(createJWKSet(createJWK(keyId, rawKey)));
event.target.update(jwkSet).then(function(result) {
isUpdatePromiseResolved = true;
}).catch(function(error) {
forceTestFailureFromPromise(test, error);
});
}
function onPlaying(event)
{
// Not using waitForEventAndRunStep() to avoid too many
// EVENT(onTimeUpdate) logs.
video.addEventListener('timeupdate', onTimeUpdate, true);
}
function onTimeUpdate(event)
{
if (event.target.currentTime < 0.2 || !isUpdatePromiseResolved)
return;
video.removeEventListener('timeupdate', onTimeUpdate, true);
if (playbackCount > 2) {
test.done();
return;
}
playbackCount++;
resetSrc().then(function(){
startPlayback();
});
}
function resetSrc() {
encryptedEventCount = 0;
video.removeAttribute('src');
video.load();
return video.setMediaKeys(null);
}
function startPlayback() {
// Alternate between encrypted and unencrypted files.
if (playbackCount % 2) {
// Unencrypted files don't require MediaKeys.
video.src = unencryptedContent;
video.play();
return;
}
navigator.requestMediaKeySystemAccess('org.w3.clearkey', getConfigurationForFile(encryptedContent)).then(function(access) {
return access.createMediaKeys();
}).then(function(mediaKeys) {
return video.setMediaKeys(mediaKeys);
}).then(function(result) {
video.src = encryptedContent;
assert_false(video.mediaKeys === null, "video.mediaKeys is null.");
video.play();
}).catch(function(error) {
forceTestFailureFromPromise(test, error);
});
}
waitForEventAndRunStep('playing', video, onPlaying, test);
waitForEventAndRunStep('encrypted', video, onEncrypted, test);
startPlayback();
}, 'Multiple playbacks alternating between encrypted and clear sources.');
</script>
</body>
</html>
|