fix(audio): ES8311 BOTH complementary open + audio-stream close ref-count + mic unmute

- es8311 driver open() now allows OUTPUT->INPUT complementary (promotes to BOTH) when same native rate 44100
- audio-stream close_stream() ref-counts shared BOTH codec (don't close if other direction still open)
- MCP recordVoice + VoiceRecorder app explicitly unmute and set 100% gain

Fixes mic input not working - was returning ERROR_RESOURCE when output already open
This commit is contained in:
Adolfo Reyna
2026-07-18 19:55:47 -04:00
parent 15aad3f585
commit c7dc66e04b
3 changed files with 38 additions and 8 deletions
@@ -553,9 +553,14 @@ error_t close_stream(AudioStreamHandle handle_base) {
Device* codec = is_input ? data->input_codec : data->output_codec;
AudioStreamHandleImpl** slot = is_input ? &data->open_input : &data->open_output;
// Determine if underlying codec is shared (BOTH codec used for both directions)
// In that case we must NOT close the codec if the other direction is still active.
Device* other_codec = is_input ? data->output_codec : data->input_codec;
AudioStreamHandleImpl** other_slot = is_input ? &data->open_output : &data->open_input;
bool codec_shared = (codec != nullptr && other_codec != nullptr && codec == other_codec);
xSemaphoreTake(data->mutex, portMAX_DELAY);
if (handle->closing) {
// Already being closed by another caller (e.g. concurrent set_enabled + app close).
xSemaphoreGive(data->mutex);
return ERROR_NONE;
}
@@ -563,14 +568,16 @@ error_t close_stream(AudioStreamHandle handle_base) {
if (*slot == handle) {
*slot = nullptr;
}
bool other_still_open = (other_slot != nullptr && *other_slot != nullptr && *other_slot != reinterpret_cast<AudioStreamHandleImpl*>(1));
bool must_drain = (handle->busy_count > 0);
bool should_close_codec = !codec_shared || !other_still_open;
xSemaphoreGive(data->mutex);
if (must_drain && handle->drain_semaphore != nullptr) {
xSemaphoreTake(handle->drain_semaphore, portMAX_DELAY);
}
if (codec != nullptr) {
if (should_close_codec && codec != nullptr) {
audio_codec_close(codec);
}