WebAudio: after a main-thread stall, playback runs at 2x speed until the time cursor catches up
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 72/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- rust, wasm
- Lĩnh vực
- audio-video-rtc
Hướng nghiên cứu
Start with the WebAudio scheduling logic linked in the issue, especially the time cursor updates in src/host/webaudio.rs and both worker closures. Reproduce the main-thread stall in Chromium with the provided wasm-bindgen example, then check that playback returns to real-time speed after resynchronization and the cursor no longer produces negative playback offsets. The expected result is a brief silence during the underrun instead of sped-up playback.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Summary
On the WebAudio host, if the main thread is blocked for longer than the scheduling lookahead (a long synchronous task, a GC pause, a tab being throttled…), playback afterwards runs at 2× speed until it has "caught up" for as long as the stall lasted. Instead of a gap, the user hears sped-up audio.
Reproduced with cpal 0.18.2 (latest release) in Chromium, and the same logic is present in 0.15.3, 0.16, 0.17 and on master (99ec27f). It also reaches downstream users, e.g. kira (which uses cpal on wasm); we first noticed it in a game engine compiled to wasm32-unknown-unknown, in both Chromium and Safari.
Cause
The time cursor is only initialised when it is 0.0; afterwards each callback schedules its buffer at the cursor and advances it by one buffer, without ever comparing it to the context clock:
https://github.com/RustAudio/cpal/blob/e1612d5d98152f8dc2a62e1b51ef7cbf4f7f26b7/src/host/webaudio/mod.rs#L443-L450
https://github.com/RustAudio/cpal/blob/e1612d5d98152f8dc2a62e1b51ef7cbf4f7f26b7/src/host/webaudio/mod.rs#L622
After a stall the cursor is in the past, so start_with_when(time_at_start_of_buffer) starts each buffer immediately. Both worker closures do this, so two consecutive buffers play at the same time (the offsets below arrive in identical pairs); when they end, the next two are again scheduled in the past, and so on. The stream is consumed at twice the real-time rate until the cursor overtakes currentTime.
Reproduction
Minimal cdylib (built with wasm-bindgen --target web), cpal =0.18.2 with the wasm-bindgen feature. It plays a quiet sine wave and records the signed offset playback - callback from OutputCallbackInfo::timestamp() for every callback:
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
fn record(offset_secs: f64, frames: u32); // defined by the page
}
#[wasm_bindgen]
pub struct Player(#[allow(dead_code)] cpal::Stream);
#[wasm_bindgen]
pub fn start() -> Player {
let device = cpal::default_host().default_output_device().unwrap();
let config: cpal::StreamConfig = device.default_output_config().unwrap().into();
let (channels, rate) = (config.channels as usize, config.sample_rate as f32);
let mut phase = 0f32;
let stream = device
.build_output_stream(
config,
move |data: &mut [f32], info: &cpal::OutputCallbackInfo| {
let ts = info.timestamp();
let offset = match ts.playback.checked_duration_since(ts.callback) {
Some(d) => d.as_secs_f64(),
None => -ts.callback.duration_since(ts.playback).as_secs_f64(),
};
record(offset, (data.len() / channels) as u32);
for frame in data.chunks_mut(channels) {
frame.fill((phase * std::f32::consts::TAU).sin() * 0.1);
phase = (phase + 440.0 / rate) % 1.0;
}
},
|e| panic!("{e}"),
None,
)
.unwrap();
stream.play().unwrap();
Player(stream)
}
<button id="go">Start</button>
<script type="module">
import init, { start } from "./pkg/repro.js";
const log = [];
globalThis.record = (offset, frames) => log.push({ t: performance.now(), offset, frames });
document.getElementById("go").onclick = async () => {
await init();
globalThis.player = start();
setTimeout(() => { // after 2 s, block the main thread for 2 s
const end = performance.now() + 2000;
while (performance.now() < end) {}
}, 2000);
};
</script>
Click Start and listen: after the stall, the stream runs at double speed for about 2 s (consecutive buffers overlapping) before returning to normal.
Measurements (Chromium, 48 kHz, 2048-frame buffers)
| before the stall | after a 2 s stall | |
|---|---|---|
playback - callback |
+64 ms | −1925 ms, rising in pairs: −1883, −1883, −1840, −1840, … |
| frames consumed per second | 48 002 | 96 115 (2.00×) for 1.92 s (91 callbacks) |
Possible fix
Resynchronise whenever the cursor is not ahead of the context clock, not only on the first buffer:
- if *time_at_start_of_buffer > 0.0 {
+ if *time_at_start_of_buffer > now {
*time_at_start_of_buffer
} else {
+ // First buffer, or the cursor fell behind the context clock
+ // (e.g. the main thread was blocked): resynchronise instead of
+ // scheduling in the past.
now + base_latency_secs + buffer_time_step_secs
}
With this change applied to 0.18.2, the same repro gives 1.00× after the stall (47 892 vs 47 969 frames/s), no negative offsets, and the stall becomes a short gap of silence, which seems like the expected behaviour for an underrun. Happy to open a PR if this approach looks right.
- Ngôn ngữ chính
- Rust
- Star
- 4k
- Fork
- 554
- Merge trung bình
- 1 ngày 3 giờ
- Pull request đã merge (30 ngày)
- 1
Chuẩn bị môi trường
- Có Dockerfile hoặc tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của RustAudio/cpal
-
API design
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
-
correctness
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
-
aaudio: dynamic buffer tuning never grows the buffer (set_buffer_size_in_frames reports Err on success)Có thể đã có người làm @dannyward630 đã nhận 39 ngày trước. Đang mởhost - aaudio platform - android
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 79/100
-
WebAudio wasm build fails with wasm-bindgen panic=unwind closuresCó thể đã có người làm @bogusyogi đã nhận 48 ngày trước. Đang mởhost - webaudio platform - web
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
PipeWire: dropping callback captures on the realtime worker can terminate the process with SIGXCPUĐang mởhost - pipewire platform - linux
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 45/100
Tất cả issue của RustAudio/cpal
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
ci: migrate pinned macos-15 runners to macos-26 and macos-26-intelCó thể đã có người làm @hawkingrei đã nhận hôm nay. Đang mởenhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
nowledge-co/hawdb#2224 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Effective coverage reads function totals as line totals when the LLVM report has no branchesĐang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
eclipse-score/coverage_tool#95 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
bug nvim
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100