Skip to content

Commit 84c5840

Browse files
epilysstsquad
authored andcommitted
sound/alsa: fix consume() use for playback
Use of consume() method was incorrect. This commit fixes the buffer bounds used with playback in ALSA. Signed-off-by: Manos Pitsidianakis <[email protected]>
1 parent c10b8e1 commit 84c5840

File tree

1 file changed

+10
-12
lines changed
  • staging/vhost-device-sound/src/audio_backends

1 file changed

+10
-12
lines changed

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ fn write_samples_direct(
148148
if !matches!(stream.state, PCMState::Start) {
149149
return Ok(false);
150150
}
151-
let mut buf = vec![0; buffer.data_descriptor.len() as usize];
151+
let n_bytes = buffer.data_descriptor.len() as usize - buffer.pos;
152+
let mut buf = vec![0; n_bytes];
152153
let read_bytes = match buffer.consume(&mut buf) {
153154
Err(err) => {
154155
log::error!(
@@ -209,10 +210,14 @@ fn write_samples_io(
209210
stream.buffers.pop_front();
210211
return 0;
211212
}
212-
let mut data = vec![0; buffer.data_descriptor.len() as usize];
213+
214+
let n_bytes = std::cmp::min(
215+
buf.len(),
216+
buffer.data_descriptor.len() as usize - buffer.pos,
217+
);
213218
// consume() always reads (buffer.data_descriptor.len() -
214219
// buffer.pos) bytes
215-
let read_bytes = match buffer.consume(&mut data) {
220+
let read_bytes = match buffer.consume(&mut buf[0..n_bytes]) {
216221
Ok(v) => v,
217222
Err(err) => {
218223
log::error!("Could not read TX buffer, dropping it immediately: {}", err);
@@ -221,18 +226,11 @@ fn write_samples_io(
221226
}
222227
};
223228

224-
let mut iter = data[0..read_bytes as usize].iter().cloned();
225-
226-
let mut written_bytes = 0;
227-
for (sample, byte) in buf.iter_mut().zip(&mut iter) {
228-
*sample = byte;
229-
written_bytes += 1;
230-
}
231-
buffer.pos += written_bytes as usize;
229+
buffer.pos += read_bytes as usize;
232230
if buffer.pos >= buffer.data_descriptor.len() as usize {
233231
stream.buffers.pop_front();
234232
}
235-
p.bytes_to_frames(written_bytes)
233+
p.bytes_to_frames(read_bytes as isize)
236234
.try_into()
237235
.unwrap_or_default()
238236
})?;

0 commit comments

Comments
 (0)