4 <title>Clear Key Playback with Multiple Sessions
</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 audioMediaKeySession
= null;
17 var videoMediaKeySession
= null;
18 var audioInitDataType
= null;
19 var videoInitDataType
= null;
20 var audioInitData
= null;
21 var videoInitData
= null;
22 var audioKeyProvided
= false;
23 var videoKeyProvided
= false;
25 // The 2 streams use different key ids and different keys.
26 var audioKeyId
= '1234567890123456';
27 var audioKey
= new Uint8Array([0x30, 0x30, 0x62, 0xF1, 0x68, 0x14, 0xD2, 0x7B,
28 0x68, 0xEF, 0x12, 0x2A, 0xFC, 0xE4, 0xAE, 0x0A]);
29 var videoKeyId
= '0123456789012345';
30 var videoKey
= new Uint8Array([0x7A, 0x7A, 0x62, 0xF1, 0x68, 0x14, 0xD2, 0x7B,
31 0x68, 0xEF, 0x12, 0x2A, 0xFC, 0xE4, 0xAE, 0x0A]);
33 function onEncrypted(event
)
35 var keyId
= String
.fromCharCode
.apply(null, new Uint8Array(event
.initData
));
37 // To avoid issues when comparing the expected.txt file
38 // (which logs the events in the order they occur), save
39 // the initData and make the calls to generateRequest()
40 // only after both "onencrypted" events are received.
41 // This prevents a "message" event from occurring before
42 // both "onencrypted" events are received.
43 var mediaKeySession
= video
.mediaKeys
.createSession();
44 if (keyId
== videoKeyId
) {
45 assert_equals(videoMediaKeySession
, null);
46 videoMediaKeySession
= mediaKeySession
;
47 videoInitDataType
= event
.initDataType
;
48 videoInitData
= event
.initData
;
49 // Return if audio "onencrypted" event not yet received.
50 if (audioMediaKeySession
== null)
53 assert_equals(keyId
, audioKeyId
);
54 assert_equals(audioMediaKeySession
, null);
55 audioMediaKeySession
= mediaKeySession
;
56 audioInitDataType
= event
.initDataType
;
57 audioInitData
= event
.initData
;
58 // Return if video "onencrypted" event not yet received.
59 if (videoMediaKeySession
== null)
63 // Both sessions have been created.
64 assert_not_equals(videoMediaKeySession
, null);
65 assert_not_equals(audioMediaKeySession
, null);
68 waitForEventAndRunStep('message', videoMediaKeySession
, onMessage
, test
);
69 promises
.push(videoMediaKeySession
.generateRequest(videoInitDataType
, videoInitData
));
71 waitForEventAndRunStep('message', audioMediaKeySession
, onMessage
, test
);
72 promises
.push(audioMediaKeySession
.generateRequest(audioInitDataType
, audioInitData
));
74 Promise
.all(promises
).catch(function(error
) {
75 forceTestFailureFromPromise(test
, error
);
79 function onMessage(event
)
81 assert_equals(event
.messageType
, 'license-request');
82 var keyId
= extractSingleKeyIdFromMessage(event
.message
);
83 if (event
.target
== videoMediaKeySession
) {
84 assert_equals(String
.fromCharCode
.apply(null, keyId
), videoKeyId
);
85 var jwkSet
= stringToUint8Array(createJWKSet(createJWK(keyId
, videoKey
)));
86 videoMediaKeySession
.update(jwkSet
).then(function(result
) {
87 videoKeyProvided
= true;
88 }).catch(function(error
) {
89 forceTestFailureFromPromise(test
, error
);
92 assert_equals(event
.target
, audioMediaKeySession
);
93 assert_equals(String
.fromCharCode
.apply(null, keyId
), audioKeyId
);
94 var jwkSet
= stringToUint8Array(createJWKSet(createJWK(keyId
, audioKey
)));
95 audioMediaKeySession
.update(jwkSet
).then(function(result
) {
96 audioKeyProvided
= true;
97 }).catch(function(error
) {
98 forceTestFailureFromPromise(test
, error
);
103 function onPlaying(event
)
105 // Video should not start playing until both keys have been
107 assert_true(videoKeyProvided
);
108 assert_true(audioKeyProvided
);
110 // Not using waitForEventAndRunStep() to avoid too many
111 // EVENT(onTimeUpdate) logs.
112 video
.addEventListener('timeupdate', onTimeUpdate
, true);
115 function onTimeUpdate(event
)
117 if (event
.target
.currentTime
< 0.2)
123 navigator
.requestMediaKeySystemAccess('org.w3.clearkey', [{}]).then(function(access
) {
124 return access
.createMediaKeys();
125 }).then(function(mediaKeys
) {
126 waitForEventAndRunStep('encrypted', video
, onEncrypted
, test
);
127 waitForEventAndRunStep('playing', video
, onPlaying
, test
);
128 video
.src
= '../content/test-encrypted-different-av-keys.webm';
129 return video
.setMediaKeys(mediaKeys
);
130 }).then(function(result
) {
132 }).catch(function(error
) {
133 forceTestFailureFromPromise(test
, error
);
135 }, 'Playback using Clear Key key system with multiple sessions.');