Skip to content

Commit

Permalink
sound: add latency_bytes field to IOMessage
Browse files Browse the repository at this point in the history
The latency field specifies how long it will take until the device
finishes playing the I/O message's buffers.

Since the I/O message is dropped as soon as the last bytes are copied
over to the host's internal buffers, assume the message is dropped
almost after the host has started playing this buffer.

This solves the issue of a guest app exiting before the host has done
playing audio because it kept sending new buffers as fast as possible
without waiting (latency).

Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
  • Loading branch information
epilys authored and stsquad committed Oct 24, 2023
1 parent 2ba56be commit 04f80fc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions staging/vhost-device-sound/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ impl VhostUserSoundThread {
let message = Arc::new(IOMessage {
vring: vring.clone(),
status: VIRTIO_SND_S_OK.into(),
latency_bytes: 0.into(),
desc_chain: desc_chain.clone(),
descriptor: descriptors.last().cloned().unwrap(),
});
Expand Down
6 changes: 5 additions & 1 deletion staging/vhost-device-sound/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ impl SoundConfig {

pub struct IOMessage {
status: std::sync::atomic::AtomicU32,
pub latency_bytes: std::sync::atomic::AtomicU32,
desc_chain: SoundDescriptorChain,
descriptor: virtio_queue::Descriptor,
vring: VringRwLock,
Expand All @@ -258,7 +259,10 @@ impl Drop for IOMessage {
log::trace!("dropping IOMessage");
let resp = VirtioSoundPcmStatus {
status: self.status.load(std::sync::atomic::Ordering::SeqCst).into(),
latency_bytes: 0.into(),
latency_bytes: self
.latency_bytes
.load(std::sync::atomic::Ordering::SeqCst)
.into(),
};

if let Err(err) = self
Expand Down
4 changes: 4 additions & 0 deletions staging/vhost-device-sound/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ impl Buffer {

impl Drop for Buffer {
fn drop(&mut self) {
self.message.latency_bytes.fetch_add(
self.data_descriptor.len(),
std::sync::atomic::Ordering::SeqCst,
);
log::trace!("dropping buffer {:?}", self);
}
}
Expand Down

0 comments on commit 04f80fc

Please sign in to comment.