summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webcodecs/audio-encoder-codec-specific.https.any.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
commit6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch)
treea68f146d7fa01f0134297619fbe7e33db084e0aa /testing/web-platform/tests/webcodecs/audio-encoder-codec-specific.https.any.js
parentInitial commit. (diff)
downloadthunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz
thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/webcodecs/audio-encoder-codec-specific.https.any.js')
-rw-r--r--testing/web-platform/tests/webcodecs/audio-encoder-codec-specific.https.any.js98
1 files changed, 98 insertions, 0 deletions
diff --git a/testing/web-platform/tests/webcodecs/audio-encoder-codec-specific.https.any.js b/testing/web-platform/tests/webcodecs/audio-encoder-codec-specific.https.any.js
new file mode 100644
index 0000000000..28c75d2a60
--- /dev/null
+++ b/testing/web-platform/tests/webcodecs/audio-encoder-codec-specific.https.any.js
@@ -0,0 +1,98 @@
+// META: global=window
+// META: script=/webcodecs/utils.js
+
+function make_silent_audio_data(timestamp, channels, sampleRate, frames) {
+ let data = new Float32Array(frames*channels);
+
+ return new AudioData({
+ timestamp: timestamp,
+ data: data,
+ numberOfChannels: channels,
+ numberOfFrames: frames,
+ sampleRate: sampleRate,
+ format: "f32-planar",
+ });
+}
+
+// The Opus DTX flag (discontinuous transmission) reduces the encoding bitrate
+// for silence. This test ensures the DTX flag is working properly by encoding
+// almost 10s of silence and comparing the bitrate with and without the flag.
+promise_test(async t => {
+ let sample_rate = 48000;
+ let total_duration_s = 10;
+ let data_count = 100;
+ let normal_outputs = [];
+ let dtx_outputs = [];
+
+ let normal_encoder = new AudioEncoder({
+ error: e => {
+ assert_unreached('error: ' + e);
+ },
+ output: chunk => {
+ normal_outputs.push(chunk);
+ }
+ });
+
+ let dtx_encoder = new AudioEncoder({
+ error: e => {
+ assert_unreached('error: ' + e);
+ },
+ output: chunk => {
+ dtx_outputs.push(chunk);
+ }
+ });
+
+ let config = {
+ codec: 'opus',
+ sampleRate: sample_rate,
+ numberOfChannels: 2,
+ bitrate: 256000, // 256kbit
+ };
+
+ let normal_config = {...config, opus: {usedtx: false}};
+ let dtx_config = {...config, opus: {usedtx: true}};
+
+ let normal_config_support = await AudioEncoder.isConfigSupported(normal_config);
+ assert_implements_optional(normal_config_support.supported, "Opus not supported");
+
+ let dtx_config_support = await AudioEncoder.isConfigSupported(dtx_config);
+ assert_implements_optional(dtx_config_support.supported, "Opus DTX not supported");
+
+ // Configure one encoder with and one without the DTX flag
+ normal_encoder.configure(normal_config);
+ dtx_encoder.configure(dtx_config);
+
+ let timestamp_us = 0;
+ let data_duration_s = total_duration_s / data_count;
+ let data_length = data_duration_s * config.sampleRate;
+ for (let i = 0; i < data_count; i++) {
+ let data;
+
+ if (i == 0 || i == (data_count - 1)) {
+ // Send real data for the first and last 100ms.
+ data = make_audio_data(
+ timestamp_us, config.numberOfChannels, config.sampleRate,
+ data_length);
+
+ } else {
+ // Send silence for the rest of the 10s.
+ data = make_silent_audio_data(
+ timestamp_us, config.numberOfChannels, config.sampleRate,
+ data_length);
+ }
+
+ normal_encoder.encode(data);
+ dtx_encoder.encode(data);
+ data.close();
+
+ timestamp_us += data_duration_s * 1_000_000;
+ }
+
+ await Promise.all([normal_encoder.flush(), dtx_encoder.flush()])
+
+ normal_encoder.close();
+ dtx_encoder.close();
+
+ // We expect a significant reduction in the number of packets, over ~10s of silence.
+ assert_less_than(dtx_outputs.length, (normal_outputs.length / 2));
+}, 'Test the Opus DTX flag works.');