|
| 1 | +<!doctype html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | +<title>MediaRecorder {audio|video}bitsPerSecond attributes</title> |
| 5 | +<link rel="help" href="https://w3c.github.io/mediacapture-record/MediaRecorder.html"> |
| 6 | +<script src="/resources/testharness.js"></script> |
| 7 | +<script src="/resources/testharnessreport.js"></script> |
| 8 | +</head> |
| 9 | +<script> |
| 10 | + |
| 11 | +/* |
| 12 | + * The bitrate handling is difficult to test, given that the spec uses text such |
| 13 | + * as: "values the User Agent deems reasonable" and "such that the sum of |
| 14 | + * videoBitsPerSecond and audioBitsPerSecond is close to the value of recorder’s |
| 15 | + * [[ConstrainedBitsPerSecond]] slot". For cases like that this test tries to |
| 16 | + * use values that are reasonable for the tested track types. Should a UA vendor |
| 17 | + * see a need to update this to fit their definition of reasonable, they should |
| 18 | + * feel free to do so, doing their best to avoid regressing existing compliant |
| 19 | + * implementations. |
| 20 | + */ |
| 21 | + |
| 22 | +async function getStream(t, constraints) { |
| 23 | + const stream = await navigator.mediaDevices.getUserMedia(constraints); |
| 24 | + const tracks = stream.getTracks(); |
| 25 | + t.add_cleanup(() => tracks.forEach(tr => tr.stop())); |
| 26 | + return stream; |
| 27 | +} |
| 28 | + |
| 29 | +function getAudioStream(t) { |
| 30 | + return getStream(t, {audio: true}); |
| 31 | +} |
| 32 | + |
| 33 | +function getVideoStream(t) { |
| 34 | + return getStream(t, {video: true}); |
| 35 | +} |
| 36 | + |
| 37 | +function getAudioVideoStream(t) { |
| 38 | + return getStream(t, {audio: true, video: true}); |
| 39 | +} |
| 40 | + |
| 41 | +const AUDIO_BITRATE = 1e5; // 100kbps |
| 42 | +const VIDEO_BITRATE = 1e6; // 1Mbps |
| 43 | +const LOW_TOTAL_BITRATE = 5e5; // 500kbps |
| 44 | +const HIGH_TOTAL_BITRATE = 2e6; // 2Mbps |
| 45 | +const BITRATE_EPSILON = 1e5; // 100kbps |
| 46 | + |
| 47 | +promise_test(async t => { |
| 48 | + const rec = new MediaRecorder(await getAudioVideoStream(t)); |
| 49 | + assert_not_equals(rec.audioBitsPerSecond, 0); |
| 50 | + assert_not_equals(rec.videoBitsPerSecond, 0); |
| 51 | +}, "Passing no bitrate config results in defaults"); |
| 52 | + |
| 53 | +promise_test(async t => { |
| 54 | + const rec = new MediaRecorder(await getAudioVideoStream(t), { |
| 55 | + bitsPerSecond: 0, |
| 56 | + }); |
| 57 | + assert_not_equals(rec.audioBitsPerSecond, 0); |
| 58 | + assert_not_equals(rec.videoBitsPerSecond, 0); |
| 59 | + assert_approx_equals(rec.audioBitsPerSecond + rec.videoBitsPerSecond, 0, |
| 60 | + BITRATE_EPSILON); |
| 61 | +}, "Passing bitsPerSecond:0 results in targets close to 0"); |
| 62 | + |
| 63 | +promise_test(async t => { |
| 64 | + const rec = new MediaRecorder(await getAudioVideoStream(t), { |
| 65 | + audioBitsPerSecond: 0, |
| 66 | + }); |
| 67 | + assert_equals(rec.audioBitsPerSecond, 0); |
| 68 | + assert_not_equals(rec.videoBitsPerSecond, 0); |
| 69 | +}, "Passing only audioBitsPerSecond:0 results in 0 for audio, default for video"); |
| 70 | + |
| 71 | +promise_test(async t => { |
| 72 | + const rec = new MediaRecorder(await getAudioVideoStream(t), { |
| 73 | + videoBitsPerSecond: 0, |
| 74 | + }); |
| 75 | + assert_not_equals(rec.audioBitsPerSecond, 0); |
| 76 | + assert_equals(rec.videoBitsPerSecond, 0); |
| 77 | +}, "Passing only videoBitsPerSecond:0 results in 0 for video, default for audio"); |
| 78 | + |
| 79 | +promise_test(async t => { |
| 80 | + const rec = new MediaRecorder(await getAudioVideoStream(t), { |
| 81 | + bitsPerSecond: 0, |
| 82 | + audioBitsPerSecond: AUDIO_BITRATE, |
| 83 | + videoBitsPerSecond: VIDEO_BITRATE, |
| 84 | + }); |
| 85 | + assert_not_equals(rec.audioBitsPerSecond, 0); |
| 86 | + assert_not_equals(rec.videoBitsPerSecond, 0); |
| 87 | + assert_approx_equals(rec.audioBitsPerSecond + rec.videoBitsPerSecond, 0, |
| 88 | + BITRATE_EPSILON); |
| 89 | +}, "Passing bitsPerSecond:0 overrides audio/video-specific values"); |
| 90 | + |
| 91 | +promise_test(async t => { |
| 92 | + const rec = new MediaRecorder(await getAudioVideoStream(t), { |
| 93 | + bitsPerSecond: HIGH_TOTAL_BITRATE, |
| 94 | + audioBitsPerSecond: 0, |
| 95 | + videoBitsPerSecond: 0, |
| 96 | + }); |
| 97 | + assert_not_equals(rec.audioBitsPerSecond, 0); |
| 98 | + assert_not_equals(rec.videoBitsPerSecond, 0); |
| 99 | + assert_approx_equals(rec.audioBitsPerSecond + rec.videoBitsPerSecond, |
| 100 | + HIGH_TOTAL_BITRATE, BITRATE_EPSILON); |
| 101 | +}, "Passing bitsPerSecond overrides audio/video zero values"); |
| 102 | + |
| 103 | +promise_test(async t => { |
| 104 | + const rec = new MediaRecorder(await getAudioVideoStream(t), { |
| 105 | + bitsPerSecond: HIGH_TOTAL_BITRATE, |
| 106 | + }); |
| 107 | + assert_not_equals(rec.audioBitsPerSecond, 0); |
| 108 | + assert_not_equals(rec.videoBitsPerSecond, 0); |
| 109 | + assert_approx_equals(rec.audioBitsPerSecond + rec.videoBitsPerSecond, |
| 110 | + HIGH_TOTAL_BITRATE, BITRATE_EPSILON); |
| 111 | +}, "Passing bitsPerSecond sets audio/video bitrate values"); |
| 112 | + |
| 113 | +promise_test(async t => { |
| 114 | + const rec = new MediaRecorder(await getAudioVideoStream(t), { |
| 115 | + audioBitsPerSecond: AUDIO_BITRATE, |
| 116 | + }); |
| 117 | + assert_equals(rec.audioBitsPerSecond, AUDIO_BITRATE); |
| 118 | + assert_not_equals(rec.videoBitsPerSecond, 0); |
| 119 | +}, "Passing only audioBitsPerSecond results in default for video"); |
| 120 | + |
| 121 | +promise_test(async t => { |
| 122 | + const rec = new MediaRecorder(await getAudioVideoStream(t), { |
| 123 | + videoBitsPerSecond: VIDEO_BITRATE, |
| 124 | + }); |
| 125 | + assert_not_equals(rec.audioBitsPerSecond, 0); |
| 126 | + assert_equals(rec.videoBitsPerSecond, VIDEO_BITRATE); |
| 127 | +}, "Passing only videoBitsPerSecond results in default for audio"); |
| 128 | + |
| 129 | +promise_test(async t => { |
| 130 | + const rec = new MediaRecorder(await getAudioStream(t), { |
| 131 | + videoBitsPerSecond: VIDEO_BITRATE, |
| 132 | + }); |
| 133 | + assert_not_equals(rec.audioBitsPerSecond, 0); |
| 134 | + assert_equals(rec.videoBitsPerSecond, VIDEO_BITRATE); |
| 135 | +}, "Passing videoBitsPerSecond for audio-only stream still results in something for video"); |
| 136 | + |
| 137 | +promise_test(async t => { |
| 138 | + const rec = new MediaRecorder(await getVideoStream(t), { |
| 139 | + audioBitsPerSecond: AUDIO_BITRATE, |
| 140 | + }); |
| 141 | + assert_equals(rec.audioBitsPerSecond, AUDIO_BITRATE); |
| 142 | + assert_not_equals(rec.videoBitsPerSecond, 0); |
| 143 | +}, "Passing audioBitsPerSecond for video-only stream still results in something for audio"); |
| 144 | + |
| 145 | +promise_test(async t => { |
| 146 | + const rec = new MediaRecorder(await getAudioStream(t), { |
| 147 | + bitsPerSecond: HIGH_TOTAL_BITRATE, |
| 148 | + }); |
| 149 | + assert_not_equals(rec.audioBitsPerSecond, 0); |
| 150 | + assert_not_equals(rec.videoBitsPerSecond, 0); |
| 151 | +}, "Passing bitsPerSecond for audio-only stream still results in something for video"); |
| 152 | + |
| 153 | +promise_test(async t => { |
| 154 | + const rec = new MediaRecorder(await getVideoStream(t), { |
| 155 | + bitsPerSecond: HIGH_TOTAL_BITRATE, |
| 156 | + }); |
| 157 | + assert_not_equals(rec.audioBitsPerSecond, 0); |
| 158 | + assert_not_equals(rec.videoBitsPerSecond, 0); |
| 159 | +}, "Passing bitsPerSecond for video-only stream still results in something for audio"); |
| 160 | + |
| 161 | +promise_test(async t => { |
| 162 | + const rec = new MediaRecorder(await getAudioVideoStream(t)); |
| 163 | + t.add_cleanup(() => rec.stop()); |
| 164 | + const abps = rec.audioBitsPerSecond; |
| 165 | + const vbps = rec.videoBitsPerSecond; |
| 166 | + rec.start(); |
| 167 | + assert_equals(rec.audioBitsPerSecond, abps); |
| 168 | + assert_equals(rec.videoBitsPerSecond, vbps); |
| 169 | +}, "Selected default track bitrates are not changed by start()"); |
| 170 | + |
| 171 | +promise_test(async t => { |
| 172 | + const rec = new MediaRecorder(await getAudioVideoStream(t), { |
| 173 | + audioBitsPerSecond: AUDIO_BITRATE, |
| 174 | + videoBitsPerSecond: VIDEO_BITRATE, |
| 175 | + }); |
| 176 | + t.add_cleanup(() => rec.stop()); |
| 177 | + const abps = rec.audioBitsPerSecond; |
| 178 | + const vbps = rec.videoBitsPerSecond; |
| 179 | + rec.start(); |
| 180 | + assert_equals(rec.audioBitsPerSecond, abps); |
| 181 | + assert_equals(rec.videoBitsPerSecond, vbps); |
| 182 | +}, "Passed-in track bitrates are not changed by start()"); |
| 183 | + |
| 184 | +promise_test(async t => { |
| 185 | + const rec = new MediaRecorder(await getAudioVideoStream(t), { |
| 186 | + bitsPerSecond: HIGH_TOTAL_BITRATE, |
| 187 | + }); |
| 188 | + t.add_cleanup(() => rec.stop()); |
| 189 | + const abps = rec.audioBitsPerSecond; |
| 190 | + const vbps = rec.videoBitsPerSecond; |
| 191 | + rec.start(); |
| 192 | + assert_equals(rec.audioBitsPerSecond, abps); |
| 193 | + assert_equals(rec.videoBitsPerSecond, vbps); |
| 194 | +}, "Passing bitsPerSecond for audio/video stream does not change track bitrates in start()"); |
| 195 | + |
| 196 | +promise_test(async t => { |
| 197 | + const rec = new MediaRecorder(await getAudioStream(t), { |
| 198 | + bitsPerSecond: LOW_TOTAL_BITRATE, |
| 199 | + }); |
| 200 | + t.add_cleanup(() => rec.stop()); |
| 201 | + const abps = rec.audioBitsPerSecond; |
| 202 | + const vbps = rec.videoBitsPerSecond; |
| 203 | + rec.start(); |
| 204 | + assert_approx_equals(rec.audioBitsPerSecond, LOW_TOTAL_BITRATE, |
| 205 | + BITRATE_EPSILON); |
| 206 | + assert_equals(rec.videoBitsPerSecond, 0); |
| 207 | + assert_not_equals(rec.audioBitsPerSecond, abps); |
| 208 | + assert_not_equals(rec.videoBitsPerSecond, vbps); |
| 209 | +}, "Passing bitsPerSecond for audio stream sets video track bitrate to 0 in start()"); |
| 210 | + |
| 211 | +promise_test(async t => { |
| 212 | + const rec = new MediaRecorder(await getVideoStream(t), { |
| 213 | + bitsPerSecond: HIGH_TOTAL_BITRATE, |
| 214 | + }); |
| 215 | + t.add_cleanup(() => rec.stop()); |
| 216 | + const abps = rec.audioBitsPerSecond; |
| 217 | + const vbps = rec.videoBitsPerSecond; |
| 218 | + rec.start(); |
| 219 | + assert_equals(rec.audioBitsPerSecond, 0); |
| 220 | + assert_approx_equals(rec.videoBitsPerSecond, HIGH_TOTAL_BITRATE, |
| 221 | + BITRATE_EPSILON); |
| 222 | + assert_not_equals(rec.audioBitsPerSecond, abps); |
| 223 | + assert_not_equals(rec.videoBitsPerSecond, vbps); |
| 224 | +}, "Passing bitsPerSecond for video stream sets audio track bitrate to 0 in start()"); |
| 225 | +</script> |
| 226 | +</html> |
0 commit comments