Fix crash when adjusting volume via button while Es8311 audio device is disabled (#1917)

* Initial plan

* Fix crash when adjusting volume via button while audio device is disabled

When both input and output are disabled, UpdateDeviceState() sets dev_ to
nullptr. Pressing a volume button calls SetOutputVolume() which previously
called esp_codec_dev_set_out_vol(dev_, volume) without null-checking dev_,
causing a crash via ESP_ERROR_CHECK.

Fix: Add null guard for dev_ and mutex lock for thread safety. The volume
is still saved via AudioCodec::SetOutputVolume() and will be applied when
the device is reopened by UpdateDeviceState().

Agent-Logs-Url: https://github.com/78/xiaozhi-esp32/sessions/945c653a-ed16-49af-aefe-5cfb473402c6

Co-authored-by: 78 <4488133+78@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: 78 <4488133+78@users.noreply.github.com>
This commit is contained in:
Copilot
2026-04-07 18:35:06 +08:00
committed by GitHub
parent 7a7c74a747
commit ab2cae5746

View File

@ -156,7 +156,10 @@ void Es8311AudioCodec::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, gp
}
void Es8311AudioCodec::SetOutputVolume(int volume) {
ESP_ERROR_CHECK(esp_codec_dev_set_out_vol(dev_, volume));
std::lock_guard<std::mutex> lock(data_if_mutex_);
if (dev_ != nullptr) {
ESP_ERROR_CHECK(esp_codec_dev_set_out_vol(dev_, volume));
}
AudioCodec::SetOutputVolume(volume);
}