Skip to content

Commit 1a1d0d5

Browse files
committed
Merge branch 'bugfix/ai_audio_deinit' into 'main'
bugfix(gmf_ai_audio): Fixed a crash issue that occurred when the AFE manager was destroyed. See merge request adf/multimedia/esp-gmf!245
2 parents 6eeade3 + e1735dc commit 1a1d0d5

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

elements/gmf_ai_audio/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v0.7.4
4+
5+
### Bug Fixes
6+
7+
- Fixed a crash issue that occurred when the AFE manager was destroyed.
8+
39
## v0.7.3
410

511
### Features

elements/gmf_ai_audio/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "0.7.3"
1+
version: "0.7.4"
22
description: Espressif GMF AI Audio Elements Module
33
url: https://github.com/espressif/esp-gmf/tree/main/elements/gmf_ai_audio
44
documentation: "https://github.com/espressif/esp-gmf/blob/main/elements/gmf_ai_audio/README.md"

elements/gmf_ai_audio/src/esp_gmf_afe_manager.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ typedef struct __afe {
4848
esp_gmf_afe_manager_features_t feat; /*!< AFE feature state */
4949
EventGroupHandle_t ctrl_events; /*!< Control events */
5050
SemaphoreHandle_t read_cb_lock; /*!< Mutex for read callback */
51+
SemaphoreHandle_t result_cb_lock;/*!< Mutex for result callback */
5152
esp_afe_sr_iface_t *esp_afe; /*!< AFE interface */
5253
} esp_gmf_afe_manager_t;
5354

@@ -72,8 +73,11 @@ static void feed_task(void *arg)
7273
if (bits & AFE_DESTROYING_EVENT) {
7374
break;
7475
}
76+
int rlen = 0;
7577
xSemaphoreTake(afe_manager->read_cb_lock, portMAX_DELAY);
76-
int rlen = afe_manager->read_cb(buf, buf_size, afe_manager->read_ctx, pdMS_TO_TICKS(500));
78+
if (afe_manager->read_cb) {
79+
rlen = afe_manager->read_cb(buf, buf_size, afe_manager->read_ctx, pdMS_TO_TICKS(500));
80+
}
7781
xSemaphoreGive(afe_manager->read_cb_lock);
7882
if (rlen == buf_size) {
7983
afe_manager->esp_afe->feed(afe_manager->afe_data, buf);
@@ -101,9 +105,11 @@ static void fetch_task(void *arg)
101105
break;
102106
}
103107
afe_fetch_result_t *result = afe_manager->esp_afe->fetch(afe_manager->afe_data);
108+
xSemaphoreTake(afe_manager->result_cb_lock, portMAX_DELAY);
104109
if (afe_manager->result_proc != NULL) {
105110
afe_manager->result_proc(result, afe_manager->result_ctx);
106111
}
112+
xSemaphoreGive(afe_manager->result_cb_lock);
107113
}
108114
xEventGroupSetBits(afe_manager->ctrl_events, FETCH_TASK_DESTROYED);
109115
vTaskDelete(NULL);
@@ -113,8 +119,10 @@ esp_gmf_err_t esp_gmf_afe_manager_set_result_cb(esp_gmf_afe_manager_handle_t han
113119
{
114120
esp_gmf_afe_manager_t *afe_manager = (esp_gmf_afe_manager_t *)handle;
115121
ESP_RETURN_ON_FALSE(afe_manager, ESP_GMF_ERR_INVALID_ARG, TAG, "AFE set result cb invalid handle");
122+
xSemaphoreTake(afe_manager->result_cb_lock, portMAX_DELAY);
116123
afe_manager->result_proc = proc;
117124
afe_manager->result_ctx = ctx;
125+
xSemaphoreGive(afe_manager->result_cb_lock);
118126
return ESP_GMF_ERR_OK;
119127
}
120128

@@ -133,14 +141,20 @@ esp_gmf_err_t esp_gmf_afe_manager_destroy(esp_gmf_afe_manager_handle_t handle)
133141
afe_manager->fetch.running = false;
134142
wait_bits |= FETCH_TASK_DESTROYED;
135143
}
136-
EventBits_t bits = xEventGroupWaitBits(afe_manager->ctrl_events, wait_bits, true, true, pdMS_TO_TICKS(1000));
137-
ESP_LOGD(TAG, "AFE destroy wait bits %" PRIu32, bits);
144+
EventBits_t bits = xEventGroupWaitBits(afe_manager->ctrl_events, wait_bits, true, true, portMAX_DELAY);
145+
if ((bits & wait_bits) != wait_bits) {
146+
ESP_LOGE(TAG, "AFE destroy wait bits timeout %" PRIu32, bits);
147+
return ESP_GMF_ERR_TIMEOUT;
148+
}
138149
if (afe_manager->ctrl_events) {
139150
vEventGroupDelete(afe_manager->ctrl_events);
140151
}
141152
if (afe_manager->read_cb_lock) {
142153
vSemaphoreDelete(afe_manager->read_cb_lock);
143154
}
155+
if (afe_manager->result_cb_lock) {
156+
vSemaphoreDelete(afe_manager->result_cb_lock);
157+
}
144158
if (afe_manager->afe_data) {
145159
afe_manager->esp_afe->destroy(afe_manager->afe_data);
146160
afe_manager->afe_data = NULL;
@@ -173,6 +187,8 @@ esp_gmf_err_t esp_gmf_afe_manager_create(esp_gmf_afe_manager_cfg_t *cfg, esp_gmf
173187

174188
afe_manager->read_cb_lock = xSemaphoreCreateMutex();
175189
ESP_GOTO_ON_FALSE(afe_manager->read_cb_lock, ESP_GMF_ERR_MEMORY_LACK, __err, TAG, "AFE manager create mutex failed");
190+
afe_manager->result_cb_lock = xSemaphoreCreateMutex();
191+
ESP_GOTO_ON_FALSE(afe_manager->result_cb_lock, ESP_GMF_ERR_MEMORY_LACK, __err, TAG, "AFE manager create mutex failed");
176192

177193
if (afe_manager->read_cb) {
178194
xEventGroupSetBits(afe_manager->ctrl_events, AFE_RUN_EVENT);
@@ -330,10 +346,10 @@ esp_gmf_err_t esp_gmf_afe_manager_set_read_cb(esp_gmf_afe_manager_handle_t handl
330346
xSemaphoreTake(afe_manager->read_cb_lock, portMAX_DELAY);
331347
afe_manager->read_cb = read_cb;
332348
afe_manager->read_ctx = read_ctx;
333-
xSemaphoreGive(afe_manager->read_cb_lock);
334349
if (afe_manager->read_cb) {
335350
esp_gmf_afe_manager_suspend(afe_manager, false);
336351
}
352+
xSemaphoreGive(afe_manager->read_cb_lock);
337353
return ESP_GMF_ERR_OK;
338354
}
339355

0 commit comments

Comments
 (0)