Fix: Implement Bidirectional Volume Sync and Prevent Feedback Loop#765
Merged
pschatzmann merged 1 commit intopschatzmann:mainfrom Jan 28, 2026
Merged
Fix: Implement Bidirectional Volume Sync and Prevent Feedback Loop#765pschatzmann merged 1 commit intopschatzmann:mainfrom
pschatzmann merged 1 commit intopschatzmann:mainfrom
Conversation
Owner
|
I assume that you have tested this solution |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request addresses a feature gap where
set_volume()in A2DP Source mode only updated the local volume state without synchronizing it to the connected sink device via AVRC. It also corrects a potential infinite feedback loop that could occur from the volume synchronization mechanism.Problem
When the library is used in A2DP Source mode, there were two primary issues with volume control:
One-Way Synchronization: Volume changes initiated on the ESP32 using the
set_volume()method were not transmitted back to the sink device. This resulted in a desynchronization where the ESP32's internal volume state updated, but the actual audio output level on the sink device remained at its previous setting.Infinite Feedback Loop: The mechanism for receiving volume updates from the sink involves an AVRC notification (
ESP_AVRC_RN_VOLUME_CHANGE) that, in turn, calls theset_volume()function. Ifset_volume()also sends an update to the sink, it creates a circular dependency:set_volume()-> sends command to sink.set_volume()again.This would result in an infinite loop of commands and notifications, wasting resources and potentially causing instability.
Solution
To resolve this and provide a truly bidirectional and stable synchronized volume control, this PR implements the following changes:
Feedback Loop Prevention: A guard condition has been added at the beginning of the
set_volumefunction:It breaks the infinite loop by immediately exiting the function if the requested volume is the same as the current volume. This ensures that when the ESP32 receives a volume-changed notification from the sink for a volume it just set, it doesn't trigger another redundant command.
Integration of AVRC Command: Inside the
set_volumemethod, a call to the ESP-IDF functionesp_avrc_ct_send_set_absolute_volume_cmd()has been added. This function is responsible for sending the new volume level to the connected sink device via AVRC.Conditional Logic for Safety: The AVRC command is wrapped in a conditional check:
This ensures that the
esp_avrc_ct_send_set_absolute_volume_cmd()is only executed when the instance is operating as an A2DP Source and is connected to a remote device