4 <title>Multiple playbacks alternating between encrypted and clear sources.
</title>
5 <script src=
"encrypted-media-utils.js"></script>
6 <script src=
"../../resources/testharness.js"></script>
7 <script src=
"../../resources/testharnessreport.js"></script>
10 <video id=
"testVideo"></video>
13 async_test(function(test
)
15 var video
= document
.getElementById('testVideo');
16 var isUpdatePromiseResolved
= false;
17 var encryptedEventCount
= 0;
18 var playbackCount
= 0;
20 var rawKey
= new Uint8Array([0xeb, 0xdd, 0x62, 0xf1, 0x68, 0x14, 0xd2, 0x7b,
21 0x68, 0xef, 0x12, 0x2a, 0xfc, 0xe4, 0xae, 0x3c]);
23 function onEncrypted(event
)
25 assert_equals(event
.target
, video
);
26 assert_true(event
instanceof window
.MediaEncryptedEvent
);
27 assert_equals(event
.type
, 'encrypted');
29 // The same decryption key is used by both the audio and
30 // the video streams so only create a session once. To
31 // avoid issues when comparing the expected.txt file
32 // (which logs the events in the order they occur), create
33 // the session on the second event. This also ensures we
35 if (++encryptedEventCount
!= 2)
38 assert_false(video
.mediaKeys
=== null, "video.mediaKeys is null.");
39 var mediaKeySession
= video
.mediaKeys
.createSession();
40 waitForEventAndRunStep('message', mediaKeySession
, onMessage
, test
);
41 mediaKeySession
.generateRequest(event
.initDataType
, event
.initData
).catch(function(error
) {
42 forceTestFailureFromPromise(test
, error
);
46 function onMessage(event
)
48 assert_true(event
instanceof window
.MediaKeyMessageEvent
);
49 assert_equals(event
.type
, 'message');
50 assert_equals(event
.messageType
, 'license-request');
52 var keyId
= extractSingleKeyIdFromMessage(event
.message
);
53 var jwkSet
= stringToUint8Array(createJWKSet(createJWK(keyId
, rawKey
)));
54 event
.target
.update(jwkSet
).then(function(result
) {
55 isUpdatePromiseResolved
= true;
56 }).catch(function(error
) {
57 forceTestFailureFromPromise(test
, error
);
61 function onPlaying(event
)
63 // Not using waitForEventAndRunStep() to avoid too many
64 // EVENT(onTimeUpdate) logs.
65 video
.addEventListener('timeupdate', onTimeUpdate
, true);
68 function onTimeUpdate(event
)
70 if (event
.target
.currentTime
< 0.2 || !isUpdatePromiseResolved
)
73 video
.removeEventListener('timeupdate', onTimeUpdate
, true);
75 if (playbackCount
> 2) {
82 resetSrc().then(function(){
88 encryptedEventCount
= 0;
89 video
.removeAttribute('src');
91 return video
.setMediaKeys(null);
94 function startPlayback() {
95 if (playbackCount
% 2) {
96 video
.src
= '../content/test-vp8-vorbis-webvtt.webm';
101 navigator
.requestMediaKeySystemAccess('org.w3.clearkey', [{}]).then(function(access
) {
102 return access
.createMediaKeys();
103 }).then(function(mediaKeys
) {
104 return video
.setMediaKeys(mediaKeys
);
105 }).then(function(result
) {
106 video
.src
= '../content/test-encrypted.webm';
107 assert_false(video
.mediaKeys
=== null, "video.mediaKeys is null.");
109 }).catch(function(error
) {
110 forceTestFailureFromPromise(test
, error
);
114 waitForEventAndRunStep('playing', video
, onPlaying
, test
);
115 waitForEventAndRunStep('encrypted', video
, onEncrypted
, test
);
117 }, 'Multiple playbacks alternating between encrypted and clear sources.');