summaryrefslogtreecommitdiffstats
path: root/dom/media/test/test_eme_unsetMediaKeys_then_capture.html
blob: 3ecdc79dbfe4710f5b24c47c001406a8c3c1f73f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<!DOCTYPE HTML>
<html>
<head>
  <title>Test Encrypted Media Extensions</title>
  <script 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="https://example.com:443/tests/dom/media/test/eme.js"></script>
</head>
<body>
<pre id="test">
<script class="testbody" type="text/javascript">
/* import-globals-from eme.js */
var manager = new MediaTestManager;

// Test that if we can capture a video frame while playing clear content after
// removing the MediaKeys object which was used for a previous encrypted content
// playback on the same video element
function startTest(test, token)
{
  manager.started(token);
  var sessions = [];
  function onSessionCreated(session) {
    sessions.push(session);
  }

  function closeSessions() {
    let p = new EMEPromise;
    Promise.all(sessions.map(s => s.close()))
    .then(p.resolve, p.reject);
    return p.promise;
  }

  let v = document.createElement("video");
  document.body.appendChild(v);

  let finish = new EMEPromise;

  function onVideoEnded(ev) {
    ok(true, TimeStamp(token) + " (ENCRYPTED) content playback ended.");

    function playClearVideo() {
      var p1 = once(v, 'loadeddata', (e) => {
        ok(true, TimeStamp(token) + " Receiving event 'loadeddata' for (CLEAR) content.");
        let canvasElem = document.createElement('canvas');
        document.body.appendChild(canvasElem);
        let ctx2d = canvasElem.getContext('2d');

        var gotTypeError = false;
        try {
          ctx2d.drawImage(v, 0, 0);
        } catch (ex) {
          if (ex instanceof TypeError) {
            gotTypeError = true;
          }
        }
        ok(!gotTypeError, TimeStamp(token) + " Canvas2D context drawImage succeed.")
      });
      v.src = 'bipbop_225w_175kbps.mp4';
      v.play();
      Promise.all([p1]).then(() => {
        manager.finished(token);
      }, () => {
        ok(false, TimeStamp(token) + " Something wrong.");
        manager.finished(token);
      });
    }

    closeSessions()
    .then(() => {
      v.setMediaKeys(null)
      .then(() => {
        ok(true, TimeStamp(token) + " Setting MediaKeys to null.");
        playClearVideo();
      }, () => {
        ok(false, TimeStamp(token) + " Setting MediaKeys to null.");
      });;
    });
  }

  once(v, "ended", onVideoEnded);

  // Create a MediaKeys object and set to HTMLMediaElement then start the playback.
  Promise.all([
    LoadInitData(v, test, token),
    CreateAndSetMediaKeys(v, test, token),
    LoadTest(test, v, token)])
  .then(values => {
    let initData = values[0];
    v.play();
    initData.map(ev => {
      let session = v.mediaKeys.createSession();
      onSessionCreated(session);
      MakeRequest(test, token, ev, session);
    });
  })
  .then(() => {
    return finish.promise;
  })
  .catch(reason => ok(false, reason))
}

SimpleTest.waitForExplicitFinish();
manager.runTests(gEMETests, startTest);
</script>
</pre>
</body>
</html>