Skip to content

Commit

Permalink
cras: Limit frames in dev_stream_mix with playable frames
Browse files Browse the repository at this point in the history
Limit the number of readable frames to the number of playable frames to
prevent the playable frame count from becoming negative.

It is possible that the combined number of readable frames from two
shmem buffers exceeds the number of playable frames. When this happens,
all readable frames will be consumed, causing the playable frame count
to be negative and the stream to be removed.

BUG=b:363857765
TEST=Switch between ARCVM and YouTube streams with BT headset and check
     that the music is playing as expected.

Change-Id: Ic0013b3b91b470c131525228c082775ff41f6136
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/adhd/+/5904749
Commit-Queue: Pattara Teerapong <pteerapong@chromium.org>
Tested-by: chromeos-cop-builder@chromeos-cop.iam.gserviceaccount.com <chromeos-cop-builder@chromeos-cop.iam.gserviceaccount.com>
Reviewed-by: Yu-Hsuan Hsu <yuhsuan@chromium.org>
  • Loading branch information
Pattara Teerapong authored and Chromeos LUCI committed Oct 4, 2024
1 parent 4c1f967 commit 1b84ec6
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cras/src/server/dev_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ int dev_stream_mix(struct dev_stream* dev_stream,
unsigned int num_samples;
size_t frames = 0;
unsigned int dev_frames;
unsigned int playable_frames;
float mix_vol;

fr_in_buf = dev_stream_playback_frames(dev_stream);
Expand All @@ -210,12 +211,19 @@ int dev_stream_mix(struct dev_stream* dev_stream,
// Stream volume scaler.
mix_vol = cras_rstream_get_volume_scaler(dev_stream->stream);

playable_frames = cras_rstream_playable_frames(rstream, dev_stream->dev_id);

fr_written = 0;
fr_read = 0;
while (fr_written < num_to_write) {
unsigned int read_frames;
src = cras_rstream_get_readable_frames(rstream, buffer_offset + fr_read,
&frames);

// b/363857765: the combined readable frames can exceed the playable frames.
// Limit the number of frames by playable frames here.
frames = MIN(frames, playable_frames);

if (frames == 0) {
break;
}
Expand All @@ -235,6 +243,7 @@ int dev_stream_mix(struct dev_stream* dev_stream,
target += dev_frames * cras_get_format_bytes(fmt);
fr_written += dev_frames;
fr_read += read_frames;
playable_frames -= read_frames;
}

cras_rstream_dev_offset_update(rstream, fr_read, dev_stream->dev_id);
Expand Down

0 comments on commit 1b84ec6

Please sign in to comment.