summaryrefslogtreecommitdiffstats
path: root/media/test/data/test_key_system_instantiation.html
blob: 93e48694ebad89c69823b2aca38777e51daba239 (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
<html>
  <body>
    <script type="text/javascript">
      // Since promises run asynchronously, use the pages title to keep track
      // of the result.
      function setResultInTitle(title) {
        document.title = title;
      };

      // Compares 2 arrays of MediaKeySystemMediaCapability, comparing only
      // |contentType|. This assumes the order is the same. Returns "success"
      // if they match, an error message if they don't.
      function verifyCapabilitiesAreEqual(actual, expected) {
        if (actual.length != expected.length)
          return "mismatched lengths";
        for (var i = 0; i < actual.length; i++) {
          // Only compare |contentType|. Other properties are ignored.
          if (actual[i].contentType !== expected[i].contentType)
            return actual[i].contentType + " does not match " +
                   expected[i].contentType + " at index " + i;
        }
        return "success";
      }

      // Calls navigator.requestMediaKeySystemAccess() using the supplied codec
      // lists, and then verifies the result. Sets page title when done.
      function requestMediaKeySystemAccessAndVerifyConfiguration(
          keySystem, initDataType, audioCodecList, videoCodecList) {
        var configuration = {initDataTypes: [initDataType]};
        if (audioCodecList !== null) {
          configuration.audioCapabilities = [];
          for (entry of audioCodecList) {
            configuration.audioCapabilities.push({contentType: entry});
          };
        }
        if (videoCodecList !== null) {
          configuration.videoCapabilities = [];
          for (entry of videoCodecList) {
            configuration.videoCapabilities.push({contentType: entry});
          };
        }
        // This is using promises which will run asynchronously.
        navigator.requestMediaKeySystemAccess(keySystem, [configuration])
            .then(function(response) {
              var allowedConfig = response.getConfiguration();
              if (allowedConfig.initDataTypes.length !== 1) {
                setResultInTitle("initDataType length mismatch");
                return;
              }
              if (allowedConfig.initDataTypes[0] !== initDataType) {
                setResultInTitle("initDataType returned " +
                                 allowedConfig.initDataTypes[0] +
                                 ", expected " + initDataType);
                return;
              }
              if (audioCodecList !== null) {
                var result =
                    verifyCapabilitiesAreEqual(allowedConfig.audioCapabilities,
                                            configuration.audioCapabilities);
                if (result !== "success") {
                  setResultInTitle(result);
                  return;
                }
              } else if (allowedConfig.audioCapabilities) {
                setResultInTitle("audioCapabilities set when none expected");
                return;
              }
              if (videoCodecList !== null) {
                setResultInTitle(
                    verifyCapabilitiesAreEqual(allowedConfig.videoCapabilities,
                                            configuration.videoCapabilities));
                return;
              } else if (allowedConfig.videoCapabilities) {
                setResultInTitle("videoCapabilities set when none expected");
                return;
              }
              setResultInTitle("success");
            })
            .catch(function(err) { setResultInTitle(err.message); });
      };

      function checkKeySystemWithMediaMimeType(keySystem, initDataType,
                                               audioCodecList, videoCodecList) {
        setResultInTitle("");
        requestMediaKeySystemAccessAndVerifyConfiguration(
            keySystem, initDataType, audioCodecList, videoCodecList);
      };
    </script>
  </body>
</html>