tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_eme_sample_groups_playback.html (4485B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 
      4 <head>
      5  <title>Test Encrypted Media Extensions</title>
      6  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      7  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      8  <script type="text/javascript" src="manifest.js"></script>
      9  <script type="text/javascript" src="eme.js"></script>
     10 </head>
     11 
     12 <body>
     13  <video controls id="video"></video>
     14  <pre id="test">
     15    <script class="testbody" type="text/javascript">
     16 
     17      // Tests that files with a default key and a seperate sample keyids in the
     18      // sgpd box play correctly (if the keyid from the sgpd box is not parsed
     19      // or assigned to the sample we will wait indefinitely for the default
     20      // key).
     21 
     22      SimpleTest.waitForExplicitFinish();
     23 
     24      // Test files for samples encrypted with different media keys.
     25      var gEMESampleGoupTests = [
     26        {
     27          name:"video with 4 keys in sgpd (sbgp in traf sgpd in stbl)",
     28          track: {
     29            name:"video",
     30            type:"video/mp4; codecs=\"avc1.64000d\"",
     31            fragments:[ "sample-encrypted-sgpdstbl-sbgptraf.mp4"
     32            ]
     33          },
     34          keys: {
     35            // "keyid" : "key"
     36            "279926496a7f5d25da69f2b3b2799a7f": "5544694d47473326622665665a396b36",
     37            "597669572e55547e656b56586e2f6f68": "7959493a764556786527517849756635",
     38            "205b2b293a342f3d3268293e6f6f4e29": "3a4f3674376d6c48675a273464447b40",
     39            "32783e367c2e4d4d6b46467b3e6b5478": "3e213f6d45584f51713d534f4b417855",
     40          },
     41          sessionType:"temporary",
     42          sessionCount:1,
     43          duration:2,
     44        },
     45      ],
     46      test = gEMESampleGoupTests[0];
     47 
     48      var video = document.getElementById("video");
     49      video.addEventListener("encrypted", () => {
     50        Log(test.name, "Recieved encrypted event");
     51      });
     52 
     53      video.addEventListener("waitingforkey", () => {
     54        Log(test.name, "waitingforkey");
     55        ok(false, test.name + " Video is waitingforkey, indicating that the samples are not being assigned the correct id from the sgpd box!");
     56        SimpleTest.finish();
     57      });
     58 
     59      function LoadEME() {
     60        var options = [{
     61          initDataType: "cenc",
     62          videoType: test.track.type,
     63        }];
     64 
     65        return navigator.requestMediaKeySystemAccess("org.w3.clearkey", options)
     66          .then((keySystemAccess) => {
     67            return keySystemAccess.createMediaKeys();
     68          }, bail("Failed to request key system access."))
     69 
     70          .then((mediaKeys) => {
     71            video.setMediaKeys(mediaKeys);
     72 
     73            var session = mediaKeys.createSession();
     74            once(session, "message", (ev) => {
     75              is(ev.messageType, "license-request", "Expected a license-request");
     76              session.update(GenerateClearKeyLicense(ev.message, test.keys));
     77            });
     78 
     79            var json = JSON.stringify({
     80              "kids":Object.keys(test.keys).map(HexToBase64)
     81            });
     82            var request = new TextEncoder().encode(json);
     83            session.generateRequest("keyids", request)
     84              .then(() => {
     85                Log(test.name, "Request license success");
     86              }, reason => {
     87                Log("Request license failed! " + reason);
     88              });
     89          });
     90      }
     91 
     92      function DownloadMedia(url, type, mediaSource) {
     93        return new Promise((resolve) => {
     94          var sourceBuffer = mediaSource.addSourceBuffer(type);
     95          fetchWithXHR(url, (response) => {
     96            once(sourceBuffer, "updateend", resolve);
     97            sourceBuffer.appendBuffer(new Uint8Array(response));
     98          });
     99        });
    100      }
    101 
    102      function LoadMSE() {
    103        // Only set the source of the video and download the tracks after we
    104        // have set the license keys, so we don't hit the waitingforkey event
    105        // unless samples are being incorrectly assigned the default key
    106        // (and we can safely fail).
    107        LoadEME()
    108          .then(() => {
    109            var ms = new MediaSource();
    110            video.src = URL.createObjectURL(ms);
    111 
    112            once(ms, "sourceopen", () => {
    113              Promise.all(test.track.fragments.map(fragment => DownloadMedia(fragment, test.track.type, ms)))
    114                .then(() => {
    115                  ms.endOfStream();
    116                  video.play();
    117                });
    118            });
    119 
    120            once(video, "ended", SimpleTest.finish);
    121          });
    122      }
    123 
    124      LoadMSE();
    125 
    126  </script>
    127  </pre>
    128 </body>
    129 
    130 </html>