gecko-dev/dom/media/test/test_eme_pssh_in_moof.html
Chris Pearce 965450cd7c Bug 1351124 - Test for PSSH boxes in MOOF boxes. r=jya
Includes "bear" samples from the Chromium project which include several
runs of PSSH boxes in their MOOF boxes.


MozReview-Commit-ID: 4KIJ2FCOyWT

--HG--
rename : dom/media/test/short-cenc.mp4 => dom/media/test/short-cenc-pssh-in-moof.mp4
extra : rebase_source : 29cd7ea0e3b76935d76e376ee0f88f4c93aca85d
2017-07-13 20:04:13 +12:00

142 lines
4.6 KiB
HTML

<!DOCTYPE HTML>
<html>
<head>
<title>Test Encrypted Media Extensions</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<script type="text/javascript" src="manifest.js"></script>
<script type="text/javascript" src="eme.js"></script>
</head>
<body>
<video controls id="video"></video>
<div id="log"></div>
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
function e(id) {
return document.getElementById(id);
}
function log(msg) {
var log_pane = e('log');
log_pane.appendChild(document.createTextNode(msg));
log_pane.appendChild(document.createElement("br"));
}
function DownloadMedia(url, type, mediaSource) {
return new Promise((resolve, reject) => {
var sourceBuffer = mediaSource.addSourceBuffer(type);
fetch(url)
.then((response) => { return response.arrayBuffer(); })
.then((arrayBuffer) => {
once(sourceBuffer, "updateend", resolve);
sourceBuffer.appendBuffer(arrayBuffer);
});
});
}
function LoadMSE(video, tracks) {
var ms = new MediaSource();
video.src = URL.createObjectURL(ms);
once(ms, "sourceopen", () => {
Promise.all(tracks.map(track => DownloadMedia(track.url, track.type, ms)))
.then(() => {
ms.endOfStream();
})
});
}
function CreateMediaKeys(video, configurations) {
return navigator.requestMediaKeySystemAccess("org.w3.clearkey", configurations)
.then((keySystemAccess) => {
return keySystemAccess.createMediaKeys();
}, bail("Failed to request key system access."))
.then((mediaKeys) => {
video.mediaKeys = mediaKeys;
return video.setMediaKeys(mediaKeys);
}, bail("Failed to set media keys."));
}
var encryptedEventCount = 0;
var initDatas = new Map();
var keys = {
"30313233343536373839303132333435" : "ebdd62f16814d27b68ef122afce4ae3c"
};
function handleEncrypted(event) {
// We get one 'encrypted' event for every run of contiguous PSSH boxes
// in each stream. Note that some of the PSSH boxes contained in the
// "bear" streams aren't in the Common Open PSSH box format, so our
// ClearKey CDM will reject those license requests. Some of the init
// data is also repeated.
encryptedEventCount++;
var hexStr = StringToHex(new TextDecoder().decode(event.initData));
if (initDatas.has(hexStr)) {
// Already have a session for this.
return;
}
initDatas.set(hexStr, session);
var initData = new Uint8Array(event.initData);
log("encrypted event => len=" + initData.length + " " + hexStr);
var session = event.target.mediaKeys.createSession();
session.addEventListener("message", function(event) {
event.target.update(GenerateClearKeyLicense(event.message, keys));
});
session.generateRequest(event.initDataType, event.initData);
}
const videoContentType = "video/mp4; codecs=\"avc1.64000d\"";
const audioContentType = "audio/mp4; codecs=\"mp4a.40.2\"";
var tracks = [
{
type: videoContentType,
url: "bear-640x360-v_frag-cenc-key_rotation.mp4",
initDatas: 6,
},
{
type: audioContentType,
url: "bear-640x360-a_frag-cenc-key_rotation.mp4",
initDatas: 7,
}
];
var configurations = [{
initDataTypes: ["cenc"],
audioCapabilities: [{
contentType: audioContentType
}],
videoCapabilities: [{
contentType: videoContentType
}]
}];
var video = document.getElementById("video");
video.addEventListener("encrypted", handleEncrypted, false);
video.addEventListener("ended", ()=>{
var expectedEncryptedEvents = tracks.reduce((sum, track) => sum += track.initDatas, 0);
is(encryptedEventCount, expectedEncryptedEvents, "Should get one 'encrypted' event per run of contiguous PSSH boxes in media.");
SimpleTest.finish();
});
video.autoplay = true;
CreateMediaKeys(video, configurations)
.then(()=>{
LoadMSE(video, tracks);
});
</script>
</body>
</html>