4 <title>Waiting for a key.
</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 // For debugging timeouts, keep track of the number of the
14 // various events received.
15 var debugEncryptedEventCount
= 0;
16 var debugWaitingForKeyEventCount
= 0;
17 var debugTimeUpdateEventCount
= 0;
18 var debugMessage
= '';
20 promise_test(function(test
)
22 var video
= document
.getElementById('testVideo');
27 test
.timeout = function()
29 var message
= 'timeout. message = ' + debugMessage
30 + ', encrypted: ' + debugEncryptedEventCount
31 + ', waitingforkey: ' + debugWaitingForKeyEventCount
32 + ', timeupdate: ' + debugTimeUpdateEventCount
;
34 test
.timeout_id
= null;
35 test
.set_status(2, message
);
39 // As this code doesn't wait for the 'message' event to avoid
40 // race conditions with 'waitingforkey', specify the key ID and
41 // key used by the encrypted content.
42 var keyId
= stringToUint8Array('0123456789012345');
43 var rawKey
= new Uint8Array([0xeb, 0xdd, 0x62, 0xf1, 0x68, 0x14, 0xd2, 0x7b,
44 0x68, 0xef, 0x12, 0x2a, 0xfc, 0xe4, 0xae, 0x3c]);
46 return navigator
.requestMediaKeySystemAccess('org.w3.clearkey', [{}]).then(function(access
) {
47 debugMessage
= 'createMediaKeys()';
48 return access
.createMediaKeys();
49 }).then(function(mediaKeys
) {
50 debugMessage
= 'setMediaKeys()';
51 return video
.setMediaKeys(mediaKeys
);
53 video
.src
= '../content/test-encrypted.webm';
55 debugMessage
= 'wait_for_encrypted_event()';
56 return wait_for_encrypted_event(video
);
58 // Received the 'encrypted' event(s), so keep a copy of
59 // the initdata for use when creating the session later.
60 initData
= e
.initData
;
61 initDataType
= e
.initDataType
;
63 // Wait until the video indicates that it needs a key to
65 debugMessage
= 'wait_for_waitingforkey_event()';
66 return wait_for_waitingforkey_event(video
);
68 // Make sure the video is NOT paused and not progressing
69 // before a key is provided. This requires the video
70 // to NOT have a clear lead.
71 assert_false(video
.paused
);
72 assert_equals(video
.currentTime
, 0);
75 mediaKeySession
= video
.mediaKeys
.createSession();
76 debugMessage
= 'generateRequest()';
77 return mediaKeySession
.generateRequest(initDataType
, initData
);
79 // generateRequest() will cause a 'message' event to
80 // occur specifying the keyId that is needed, but we
81 // ignore it since we already know what keyId is needed.
82 // Add the key needed to decrypt.
83 var jwkSet
= stringToUint8Array(createJWKSet(createJWK(keyId
, rawKey
)));
84 debugMessage
= 'update()';
85 return mediaKeySession
.update(jwkSet
);
87 // Video should start playing now that it can decrypt the
88 // streams, so wait until a little bit of the video has
90 debugMessage
= 'wait_for_timeupdate_event()';
91 return wait_for_timeupdate_event(video
);
94 // Typical test duration is 6 seconds on release builds
95 // (12 seconds on debug). Since the test is timing out anyway,
96 // make the duration 5 seconds so that the timeout function
97 // is actually called (instead of simply aborting the test).
98 }, 'Waiting for a key.', { timeout
: 5000 });
100 // Wait for a pair of 'encrypted' events. Promise resolved on
102 function wait_for_encrypted_event(video
)
104 var encryptedEventCount
= 0;
105 return new Promise(function(resolve
) {
106 video
.addEventListener('encrypted', function listener(e
) {
107 assert_equals(e
.target
, video
);
108 assert_true(e
instanceof window
.MediaEncryptedEvent
);
109 assert_equals(e
.type
, 'encrypted');
111 // The same decryption key is used by both the audio
112 // and the video streams so wait for the second event
113 // to ensure we see both events.
114 ++debugEncryptedEventCount
;
115 if (++encryptedEventCount
!= 2)
118 video
.removeEventListener(listener
);
124 // Wait for a 'waitingforkey' event. Promise resolved when the
125 // event is received.
126 function wait_for_waitingforkey_event(video
)
128 var waitingForKeyEventCount
= 0;
129 return new Promise(function(resolve
) {
130 video
.addEventListener('waitingforkey', function listener(e
) {
131 assert_equals(e
.target
, video
);
132 assert_equals(e
.type
, 'waitingforkey');
134 ++debugWaitingForKeyEventCount
;
135 ++waitingForKeyEventCount
;
136 // TODO(jrummell): waitingforkey event should only
137 // occur once. http://crbug.com/461903
138 // assert_equals(waitingForKeyEventCount, 1, 'Multiple waitingforkey events');
140 video
.removeEventListener(listener
);
146 // Wait for a 'timeupdate' event. Promise resolved if |video| has
147 // played for more than 0.2 seconds.
148 function wait_for_timeupdate_event(video
)
150 return new Promise(function(resolve
) {
151 video
.addEventListener('timeupdate', function listener(e
) {
152 assert_equals(e
.target
, video
);
153 ++debugTimeUpdateEventCount
;
154 if (video
.currentTime
< 0.2)
156 video
.removeEventListener(listener
);