Skip to content

webrtc: Add Sound volume and mute controls support#2213

Open
thjnk wants to merge 1 commit intogoogle:mainfrom
thjnk:main
Open

webrtc: Add Sound volume and mute controls support#2213
thjnk wants to merge 1 commit intogoogle:mainfrom
thjnk:main

Conversation

@thjnk
Copy link
Contributor

@thjnk thjnk commented Mar 3, 2026

This change implements volume and mute controls for the WebRTC audio backend in Cuttlefish. These controls allow the guest to dynamically adjust playback and capture audio levels via Virtio Sound control messages.

  • AudioMixer: Apply playback volume scaling during audio stream resampling by modifying the channel mix map.
  • Implement read/write logic for Virtio audio volume/mute control commands.
  • Guest Config: Allow enabling of Virtio Sound controls per stream.

@jemoreira jemoreira self-requested a review March 3, 2026 19:02
@thjnk thjnk force-pushed the main branch 3 times, most recently from 3400ea9 to 74ccc5d Compare March 4, 2026 14:11
@jemoreira
Copy link
Member

The three bullet points in the PR description sound like they should be different commits. That would make the PR much easier to review.

{SampleRate::Audio_SampleRate_RATE_64000, 64000},
};

static const auto parse_stream_settings =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this could be a regular function in an anonymous namespace instead of a lambda. Any reason to make it a lambda?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I use static variables (kChannelLayoutMap) inside the lambda which is defined in the SetupAudio function.
  • It is only invoked from within SetupAudio. I prefer to have definition near by usage in such cases.
    However I am not strongly against moving it (and static variables) to a anonymous namespace. what do you think ?

if (is_muted_by_control) {
memset(rx_buffer, 0, bytes_read);
} else if (volume < 1.){
static const auto apply_volume = [](auto* data, size_t size, float volume) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a template function instead of a lambda, and you could avoid some repetition by accepting the size in bytes as parameter and dividing by the size of the template parameter. Something like this:

template<typename T>
void apply_volume(T* data, size_bytes, float volume) {
  for (T& val: std::span(data, size_bytes / sizeof(T))) {
    val *= volume;
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I'd prefer to have the function be right next to where it is invoked, esp. since it is only few lines of code. And it acts exectly as a template function.

@thjnk thjnk marked this pull request as ready for review March 5, 2026 13:18
This change implements volume and mute controls for the WebRTC audio
backend in Cuttlefish. These controls allow the guest to dynamically
adjust playback and capture audio levels via Virtio Sound control messages.

- AudioMixer: Apply playback volume scaling during audio stream
  resampling by modifying the channel mix map.
- Implement read/write logic for Virtio audio volume/mute control commands.
- Guest Config: Allow enabling of Virtio Sound controls per stream.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants