gecko-dev/dom/media/test/test_eme_pssh_in_moof.html

142 строки
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>