tor-browser

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

test_eme_mfcdm_generate_request_from_new_session.html (4085B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <title>Media Engine only test : test generateRequest on a new session created by same media keys</title>
      5  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      7  <script type="text/javascript" src="manifest.js"></script>
      8 </head>
      9 <body>
     10 <video id="video" controls width="640" height="360"></video>
     11 <script class="testbody" type="text/javascript">
     12 
     13 /**
     14 * When using the MFCDM, the CDM should not be closed when the media engine
     15 * using it is shut down. Since the CDM is independent and reusable, this test
     16 * verifies that a MediaKeys object can be reused to create a new session and
     17 * generate a request after the initial media engine is shut down.
     18 */
     19 add_task(async function setupTestingPrefs() {
     20  await SpecialPowers.pushPrefEnv({
     21    set: [
     22      ["media.wmf.media-engine.enabled", 2],
     23      ["media.eme.wmf.use-mock-cdm-for-external-cdms", true],
     24    ],
     25  });
     26 });
     27 
     28 let gMediaKeys;
     29 // Extract only one video frame from https://test.playready.microsoft.com/media/profficialsite/tearsofsteel_1080p_60s_24fps.6000kbps.1920x1080.h264-8b.2ch.128kbps.aac.avsep.cenc.mp4
     30 const gVideoUrl = 'tearsofsteel_1frame_encrypted.mp4';
     31 const gMimeType = 'video/mp4; codecs="avc1.640028"';
     32 const gKeyId = 'a2c786d0-f9ef-4cb3-b333-cd323a4284a5';
     33 
     34 add_task(async function testGenerateRequestOnNewSessionCreatedBySameMediaKeys() {
     35  // This test is about the CDM implementation; the key system in use doesn't matter.
     36  const keySystem = "com.microsoft.playready.recommendation";
     37  const video = document.getElementById('video');
     38  await Promise.all([
     39    once(video, "loadedmetadata"),
     40    setupMSE(video),
     41  ]);
     42  await setupEME(video, keySystem);
     43  await resetVideoElement(video);
     44  await generateRequestOnNewSessionCreatedBySameMediaKeys();
     45 });
     46 
     47 // Helper functions below
     48 function base64UrlEncode(str) {
     49  return btoa(str)
     50    .replace(/\+/g, '-')
     51    .replace(/\//g, '_')
     52    .replace(/=+$/, '');
     53 }
     54 
     55 async function setupEME(video, keySystem) {
     56  info(`Setting up EME`);
     57  const sessionType = 'temporary';
     58  const keyIdBytes = gKeyId.replace(/-/g, '').match(/.{1,2}/g).map(b => parseInt(b, 16));
     59  const keyIdStr = String.fromCharCode.apply(null, keyIdBytes);
     60  const keyIdJson = JSON.stringify({ kids: [base64UrlEncode(keyIdStr)] });
     61 
     62  const configs = [{
     63    initDataTypes: ['keyids'],
     64    videoCapabilities: [{ contentType: gMimeType }],
     65    sessionTypes: [sessionType]
     66  }];
     67 
     68  const access = await navigator.requestMediaKeySystemAccess(keySystem, configs);
     69  gMediaKeys = await access.createMediaKeys();
     70  await video.setMediaKeys(gMediaKeys);
     71 
     72  const session = gMediaKeys.createSession(sessionType);
     73  await session.generateRequest('keyids', new TextEncoder().encode(keyIdJson));
     74 }
     75 
     76 async function setupMSE(video) {
     77  info(`Setting up MSE`);
     78  const mediaSource = new MediaSource();
     79  video.src = URL.createObjectURL(mediaSource);
     80  await once(mediaSource, "sourceopen");
     81 
     82  info(`Fetching data`);
     83  const sourceBuffer = mediaSource.addSourceBuffer(gMimeType);
     84  const res = await fetch(gVideoUrl);
     85  const data = await res.arrayBuffer();
     86 
     87  info(`Appending data`);
     88  sourceBuffer.appendBuffer(data);
     89  await once(sourceBuffer, "updateend");
     90  mediaSource.endOfStream();
     91 }
     92 
     93 async function resetVideoElement(video) {
     94  info(`Resetting video`);
     95  video.removeAttribute('src');
     96  video.load();
     97  await once(video, "emptied");
     98 }
     99 
    100 async function generateRequestOnNewSessionCreatedBySameMediaKeys() {
    101  const sessionType = 'temporary';
    102  const keyIdBytes = gKeyId.replace(/-/g, '').match(/.{1,2}/g).map(b => parseInt(b, 16));
    103  const keyIdStr = String.fromCharCode.apply(null, keyIdBytes);
    104  const keyIdJson = JSON.stringify({ kids: [base64UrlEncode(keyIdStr)] });
    105  const session = gMediaKeys.createSession(sessionType);
    106  try {
    107    await session.generateRequest('keyids', new TextEncoder().encode(keyIdJson));
    108    ok(true, "Generated request succesfully on a new session!");
    109  } catch (error) {
    110    ok(false, "Failed to generate request again!");
    111  }
    112 }
    113 
    114 </script>
    115 </body>
    116 </html>