summaryrefslogtreecommitdiffstats
path: root/dom/media/test/test_mediarecorder_bitrate.html
blob: 21e6f5b90bba4a14e8dfdadec4505f25d172ed45 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<!DOCTYPE HTML>
<html>
<head>
  <title>Test MediaRecorder Bitrate</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>
</head>
<body>
<pre id="test">
<script class="testbody" type="text/javascript">
var manager = new MediaTestManager;
var results = [];

/**
 * Starts a test on every media recorder file included to check that
 * the bitrate control works
 */
function startTest(test, token) {
  manager.started(token);
  runTest(test, token, 1000000);
  runTest(test, token, 100000);
}

function runTest(test, token, bitrate) {
  var element = document.createElement('video');
  var expectedMimeType = test.type.substring(0, test.type.indexOf(';'));

  element.token = token;

  element.src = test.name;
  element.preload = "metadata";
  element.onloadedmetadata = function () {
    info("loadedmetadata");
    const stream = element.mozCaptureStreamUntilEnded();
    element.onloadedmetadata = null;
    element.play();

    const mediaRecorder = new MediaRecorder(stream, {videoBitsPerSecond: bitrate});
    mediaRecorder.start();
    is(mediaRecorder.state, 'recording', 'Media recorder should be recording');
    is(mediaRecorder.stream, stream,
     'Media recorder stream = element stream at the start of recording');

    var onStopFired = false;
    var onDataAvailableFired = false;
    var encoded_size = 0;

    mediaRecorder.onerror = function () {
      ok(false, 'Unexpected onerror callback fired');
    };

    mediaRecorder.onwarning = function () {
      ok(false, 'Unexpected onwarning callback fired');
    };

    // This handler verifies that only a single onstop event handler is fired.
    mediaRecorder.onstop = function () {
      if (onStopFired) {
        ok(false, 'onstop unexpectedly fired more than once');
      } else {
        onStopFired = true;

        // ondataavailable should always fire before onstop
        if (onDataAvailableFired) {
          ok(true, 'onstop fired after ondataavailable');
          info("test " + test.name + " encoded@" + bitrate + "=" + encoded_size);
          if (results[test.name]) {
            var big, small, temp;
            big = {};
            big.bitrate = bitrate;
            big.size = encoded_size;
            small = results[test.name];
            // Don't assume the order that these will finish in
            if (results[test.name].bitrate > bitrate) {
              temp = big;
              big = small;
              small = temp;
            }
            // Ensure there is a big enough difference in the encoded
            // sizes
            ok(small.size*1.25 < big.size,
               test.name + ' encoded@' + big.bitrate + '=' + big.size +
               ' > encoded@' + small.bitrate + '=' + small.size);
            manager.finished(token);
          } else {
            results[test.name] = {};
            results[test.name].bitrate = bitrate;
            results[test.name].size = encoded_size;
          }
        } else {
          ok(false, 'onstop fired without an ondataavailable event first');
        }
      }
    };

    // This handler verifies that only a single ondataavailable event handler
    // is fired with the blob generated having greater than zero size
    // and a correct mime type.
    mediaRecorder.ondataavailable = function (evt) {
      if (onDataAvailableFired) {
        ok(false, 'ondataavailable unexpectedly fired more than once');
      } else {
        onDataAvailableFired = true;

        ok(evt instanceof BlobEvent,
           'Events fired from ondataavailable should be BlobEvent');
        is(evt.type, 'dataavailable',
           'Event type should dataavailable');
        ok(evt.data.size > 0,
           'Blob data received should be greater than zero');
        encoded_size = evt.data.size;

        // onstop should not have fired before ondataavailable
        if (onStopFired) {
          ok(false, 'ondataavailable unexpectedly fired later than onstop');
          manager.finished(token);
        }
      }
    };
  };
}

manager.runTests(gMediaRecorderVideoTests, startTest);
</script>
</pre>
</body>
</html>