Skip to content

Commit 04f80fc

Browse files
epilysstsquad
authored andcommitted
sound: add latency_bytes field to IOMessage
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 <[email protected]>
1 parent 2ba56be commit 04f80fc

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

staging/vhost-device-sound/src/device.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ impl VhostUserSoundThread {
483483
let message = Arc::new(IOMessage {
484484
vring: vring.clone(),
485485
status: VIRTIO_SND_S_OK.into(),
486+
latency_bytes: 0.into(),
486487
desc_chain: desc_chain.clone(),
487488
descriptor: descriptors.last().cloned().unwrap(),
488489
});

staging/vhost-device-sound/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ impl SoundConfig {
248248

249249
pub struct IOMessage {
250250
status: std::sync::atomic::AtomicU32,
251+
pub latency_bytes: std::sync::atomic::AtomicU32,
251252
desc_chain: SoundDescriptorChain,
252253
descriptor: virtio_queue::Descriptor,
253254
vring: VringRwLock,
@@ -258,7 +259,10 @@ impl Drop for IOMessage {
258259
log::trace!("dropping IOMessage");
259260
let resp = VirtioSoundPcmStatus {
260261
status: self.status.load(std::sync::atomic::Ordering::SeqCst).into(),
261-
latency_bytes: 0.into(),
262+
latency_bytes: self
263+
.latency_bytes
264+
.load(std::sync::atomic::Ordering::SeqCst)
265+
.into(),
262266
};
263267

264268
if let Err(err) = self

staging/vhost-device-sound/src/stream.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ impl Buffer {
279279

280280
impl Drop for Buffer {
281281
fn drop(&mut self) {
282+
self.message.latency_bytes.fetch_add(
283+
self.data_descriptor.len(),
284+
std::sync::atomic::Ordering::SeqCst,
285+
);
282286
log::trace!("dropping buffer {:?}", self);
283287
}
284288
}

0 commit comments

Comments
 (0)