From 79b4d8fe4fb9d4df97c28a9e69f07d0f6a8eacef Mon Sep 17 00:00:00 2001 From: Jin Cheng Date: Tue, 11 Oct 2022 15:06:12 +0800 Subject: [PATCH] optimized a2dp_sink audio datapath 1. removed audio cache in BTC layer of Bluedroid 2. added flow control for audio data in application layer Closes https://github.com/espressif/esp-idf/issues/9622 --- .../btc/profile/std/a2dp/btc_a2dp_sink.c | 5 +- .../classic_bt/a2dp_sink/main/bt_app_core.c | 81 +++++++++++++++++-- 2 files changed, 75 insertions(+), 11 deletions(-) diff --git a/components/bt/host/bluedroid/btc/profile/std/a2dp/btc_a2dp_sink.c b/components/bt/host/bluedroid/btc/profile/std/a2dp/btc_a2dp_sink.c index 0bb7e49e90..b6a91c5feb 100644 --- a/components/bt/host/bluedroid/btc/profile/std/a2dp/btc_a2dp_sink.c +++ b/components/bt/host/bluedroid/btc/profile/std/a2dp/btc_a2dp_sink.c @@ -85,7 +85,6 @@ enum { /* 18 frames is equivalent to 6.89*18*2.9 ~= 360 ms @ 44.1 khz, 20 ms mediatick */ #define MAX_OUTPUT_A2DP_SNK_FRAME_QUEUE_SZ (25) -#define JITTER_BUFFER_WATER_LEVEL (5) #define BTC_A2DP_SNK_DATA_QUEUE_IDX (1) @@ -696,9 +695,7 @@ UINT8 btc_a2dp_sink_enque_buf(BT_HDR *p_pkt) p_msg->num_frames_to_be_processed = (*((UINT8 *)(p_msg + 1) + p_msg->offset)) & 0x0f; APPL_TRACE_VERBOSE("btc_a2dp_sink_enque_buf %d + \n", p_msg->num_frames_to_be_processed); fixed_queue_enqueue(a2dp_sink_local_param.btc_aa_snk_cb.RxSbcQ, p_msg, FIXED_QUEUE_MAX_TIMEOUT); - if (fixed_queue_length(a2dp_sink_local_param.btc_aa_snk_cb.RxSbcQ) >= JITTER_BUFFER_WATER_LEVEL) { - osi_thread_post_event(a2dp_sink_local_param.btc_aa_snk_cb.data_ready_event, OSI_THREAD_MAX_TIMEOUT); - } + osi_thread_post_event(a2dp_sink_local_param.btc_aa_snk_cb.data_ready_event, OSI_THREAD_MAX_TIMEOUT); } else { /* let caller deal with a failed allocation */ APPL_TRACE_WARNING("btc_a2dp_sink_enque_buf No Buffer left - "); diff --git a/examples/bluetooth/bluedroid/classic_bt/a2dp_sink/main/bt_app_core.c b/examples/bluetooth/bluedroid/classic_bt/a2dp_sink/main/bt_app_core.c index ffee1f442b..cf5241b71d 100644 --- a/examples/bluetooth/bluedroid/classic_bt/a2dp_sink/main/bt_app_core.c +++ b/examples/bluetooth/bluedroid/classic_bt/a2dp_sink/main/bt_app_core.c @@ -13,12 +13,23 @@ #include "freertos/FreeRTOSConfig.h" #include "freertos/FreeRTOS.h" #include "freertos/queue.h" +#include "freertos/semphr.h" #include "freertos/task.h" #include "esp_log.h" #include "bt_app_core.h" #include "driver/i2s.h" #include "freertos/ringbuf.h" + +#define RINGBUF_HIGHEST_WATER_LEVEL (32 * 1024) +#define RINGBUF_PREFETCH_WATER_LEVEL (20 * 1024) + +enum { + RINGBUFFER_MODE_PROCESSING, /* ringbuffer is buffering incoming audio data, I2S is working */ + RINGBUFFER_MODE_PREFETCHING, /* ringbuffer is buffering incoming audio data, I2S is waiting */ + RINGBUFFER_MODE_DROPPING /* ringbuffer is not buffering (dropping) incoming audio data, I2S is working */ +}; + /******************************* * STATIC FUNCTION DECLARATIONS ******************************/ @@ -40,6 +51,8 @@ static xQueueHandle s_bt_app_task_queue = NULL; /* handle of work queue */ static xTaskHandle s_bt_app_task_handle = NULL; /* handle of application task */ static xTaskHandle s_bt_i2s_task_handle = NULL; /* handle of I2S task */ static RingbufHandle_t s_ringbuf_i2s = NULL; /* handle of ringbuffer for I2S */ +static SemaphoreHandle_t s_i2s_write_semaphore = NULL; +static uint16_t ringbuffer_mode = RINGBUFFER_MODE_PROCESSING; /******************************* * STATIC FUNCTION DEFINITIONS @@ -94,14 +107,28 @@ static void bt_i2s_task_handler(void *arg) { uint8_t *data = NULL; size_t item_size = 0; + /** + * The total length of DMA buffer of I2S is: + * `dma_frame_num * dma_desc_num * i2s_channel_num * i2s_data_bit_width / 8`. + * Transmit `dma_frame_num * dma_desc_num` bytes to DMA is trade-off. + */ + const size_t item_size_upto = 60 * 6; size_t bytes_written = 0; for (;;) { - /* receive data from ringbuffer and write it to I2S DMA transmit buffer */ - data = (uint8_t *)xRingbufferReceive(s_ringbuf_i2s, &item_size, (portTickType)portMAX_DELAY); - if (item_size != 0){ - i2s_write(0, data, item_size, &bytes_written, portMAX_DELAY); - vRingbufferReturnItem(s_ringbuf_i2s, (void *)data); + if (pdTRUE == xSemaphoreTake(s_i2s_write_semaphore, portMAX_DELAY)) { + for (;;) { + item_size = 0; + /* receive data from ringbuffer and write it to I2S DMA transmit buffer */ + data = (uint8_t *)xRingbufferReceiveUpTo(s_ringbuf_i2s, &item_size, (TickType_t)pdMS_TO_TICKS(5), item_size_upto); + if (item_size == 0) { + ESP_LOGI(BT_APP_CORE_TAG, "ringbuffer underflowed! mode changed: RINGBUFFER_MODE_PREFETCHING"); + ringbuffer_mode = RINGBUFFER_MODE_PREFETCHING; + break; + } + i2s_write(0, data, item_size, &bytes_written, portMAX_DELAY); + vRingbufferReturnItem(s_ringbuf_i2s, (void *)data); + } } } } @@ -157,7 +184,14 @@ void bt_app_task_shut_down(void) void bt_i2s_task_start_up(void) { - if ((s_ringbuf_i2s = xRingbufferCreate(8 * 1024, RINGBUF_TYPE_BYTEBUF)) == NULL) { + ESP_LOGI(BT_APP_CORE_TAG, "ringbuffer data empty! mode changed: RINGBUFFER_MODE_PREFETCHING"); + ringbuffer_mode = RINGBUFFER_MODE_PREFETCHING; + if ((s_i2s_write_semaphore = xSemaphoreCreateBinary()) == NULL) { + ESP_LOGE(BT_APP_CORE_TAG, "%s, Semaphore create failed", __func__); + return; + } + if ((s_ringbuf_i2s = xRingbufferCreate(RINGBUF_HIGHEST_WATER_LEVEL, RINGBUF_TYPE_BYTEBUF)) == NULL) { + ESP_LOGE(BT_APP_CORE_TAG, "%s, ringbuffer create failed", __func__); return; } xTaskCreate(bt_i2s_task_handler, "BtI2STask", 1024, NULL, configMAX_PRIORITIES - 3, &s_bt_i2s_task_handle); @@ -173,11 +207,44 @@ void bt_i2s_task_shut_down(void) vRingbufferDelete(s_ringbuf_i2s); s_ringbuf_i2s = NULL; } + if (s_i2s_write_semaphore) { + vSemaphoreDelete(s_i2s_write_semaphore); + s_i2s_write_semaphore = NULL; + } } size_t write_ringbuf(const uint8_t *data, size_t size) { - BaseType_t done = xRingbufferSend(s_ringbuf_i2s, (void *)data, size, (portTickType)portMAX_DELAY); + size_t item_size = 0; + BaseType_t done = pdFALSE; + + if (ringbuffer_mode == RINGBUFFER_MODE_DROPPING) { + ESP_LOGW(BT_APP_CORE_TAG, "ringbuffer is full, drop this packet!"); + vRingbufferGetInfo(s_ringbuf_i2s, NULL, NULL, NULL, NULL, &item_size); + if (item_size <= RINGBUF_PREFETCH_WATER_LEVEL) { + ESP_LOGI(BT_APP_CORE_TAG, "ringbuffer data decreased! mode changed: RINGBUFFER_MODE_PROCESSING"); + ringbuffer_mode = RINGBUFFER_MODE_PROCESSING; + } + return 0; + } + + done = xRingbufferSend(s_ringbuf_i2s, (void *)data, size, (TickType_t)0); + + if (!done) { + ESP_LOGW(BT_APP_CORE_TAG, "ringbuffer overflowed, ready to decrease data! mode changed: RINGBUFFER_MODE_DROPPING"); + ringbuffer_mode = RINGBUFFER_MODE_DROPPING; + } + + if (ringbuffer_mode == RINGBUFFER_MODE_PREFETCHING) { + vRingbufferGetInfo(s_ringbuf_i2s, NULL, NULL, NULL, NULL, &item_size); + if (item_size >= RINGBUF_PREFETCH_WATER_LEVEL) { + ESP_LOGI(BT_APP_CORE_TAG, "ringbuffer data increased! mode changed: RINGBUFFER_MODE_PROCESSING"); + ringbuffer_mode = RINGBUFFER_MODE_PROCESSING; + if (pdFALSE == xSemaphoreGive(s_i2s_write_semaphore)) { + ESP_LOGE(BT_APP_CORE_TAG, "semphore give failed"); + } + } + } return done ? size : 0; }