android_audio: Defer capture stream opening
Defer opening the AAudio capture stream from AudioStream::new(), which
runs at VIRTIO_SND_R_PCM_PREPARE, to AudioStream::start(), which runs
when the guest starts capturing. Guests prepare capture streams during
boot, so eagerly opening them turned on the host microphone (and its
privacy indicator) before any application was recording.
This is the first change that runs a synchronous AAudio open, in the
middle of streaming, on the executor thread that all streams of a card
share. AAudioStreamBuilder_openStream plus AAudioStream_requestStart
can take tens of milliseconds, which is longer than one period. When
that happens, the other streams' pacing deadline is already in the past
and the computed delay is zero. A zero-duration TimerAsync::sleep()
disarms the underlying timerfd and never wakes, so the worker would
stall forever. Factor the pacing code into pace_period(), which never
requests a zero-duration delay: when slightly behind it skips the
sleep, and when more than two periods behind it re-anchors the pacing
clock instead of bursting periods to catch up.
Also skip only the first capture period after start instead of
aaudio_buffer_size * 3 / 2 / period + 1 periods. The stream capacity
is two periods, so the longer skip overflowed AudioFlinger's
RecordThread and delayed the stream's STARTING -> STARTED transition.
This change:
1. Adds create_and_open_aaudio_stream() with leak-free error paths,
used eagerly for playback in new() and lazily for capture.
2. Leaves capture streams unopened (null stream_ptr) in new() and
opens them synchronously in start() via open_capture_stream().
3. Adds pace_period() and uses it for playback and capture.
4. Skips only the first capture period after start.
BUG=b:553670496
TEST=tools/dev_container cargo test -p android_audio \
--features libaaudio_stub
TEST=tools/dev_container cargo clippy -p android_audio \
--all-targets --features libaaudio_stub
TEST=tools/dev_container ./tools/presubmit all
Change-Id: I1e4481a87cb94d860193cf6f0d3e7dc23141e5ad
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/8401124
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
Commit-Queue: Cassie Wang <cassiewang@google.com>
The ChromeOS Virtual Machine Monitor
crosvm is a secure, lightweight, and performant Virtual Machine Monitor (VMM) written in Rust. Originally developed for ChromeOS to run Linux (Crostini) and Android guests (ARCVM). It is now used across multiple products and platforms such as TerminalApp on Android, Cuttlefish and Windows.
crosvm focuses on security through strong isolation and a modern, memory-safe implementation. It leverages hardware-assisted virtualization to provide a robust execution environment for untrusted code.
io_uring, vhost, and an internal async runtime (cros_async).crosvm implements a wide range of paravirtualized devices via the virtio standard:
virtio-net with optional vhost and slirp backends.virtio-block supporting raw, qcow2, zstd, and Android sparse formats.virtio-gpu with 2D and 3D acceleration (via virglrenderer, gfxstream, or vulkano).virtio-snd with backends for CRAS (ChromeOS), AAudio (Android), and more.virtio-fs and virtio-9p.crosvm is designed with a “process-per-device” model:
forked into its own process.The recommended way to build and develop crosvm is using the provided development container.
Use the dev container to build a release version of crosvm:
./tools/dev_container ./tools/build_release
Follow this example usage to run a simple Linux guest.
#crosvm channel on Matrix.crosvm is an open-source project licensed under the BSD-3-Clause License.