Merge branch 'bt/opt_audio_datapath' into 'master'

bt/optimized a2dp_sink audio datapath

Closes IDFGH-8124 and AUD-4106

See merge request espressif/esp-idf!20592
This commit is contained in:
Wang Meng Yang 2022-10-18 19:27:29 +08:00
commit 55f92ec268
2 changed files with 84 additions and 15 deletions

View File

@ -77,7 +77,6 @@ enum {
/* 18 frames is equivalent to 6.89*18*2.9 ~= 360 ms @ 44.1 khz, 20 ms mediatick */ /* 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 MAX_OUTPUT_A2DP_SNK_FRAME_QUEUE_SZ (25)
#define JITTER_BUFFER_WATER_LEVEL (5)
#define BTC_A2DP_SNK_DATA_QUEUE_IDX (1) #define BTC_A2DP_SNK_DATA_QUEUE_IDX (1)
@ -688,9 +687,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; 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); 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); 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 { } else {
/* let caller deal with a failed allocation */ /* let caller deal with a failed allocation */
APPL_TRACE_WARNING("btc_a2dp_sink_enque_buf No Buffer left - "); APPL_TRACE_WARNING("btc_a2dp_sink_enque_buf No Buffer left - ");

View File

@ -11,6 +11,7 @@
#include "freertos/FreeRTOSConfig.h" #include "freertos/FreeRTOSConfig.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/queue.h" #include "freertos/queue.h"
#include "freertos/semphr.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "esp_log.h" #include "esp_log.h"
#include "bt_app_core.h" #include "bt_app_core.h"
@ -22,6 +23,16 @@
#endif #endif
#include "freertos/ringbuf.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 * STATIC FUNCTION DECLARATIONS
******************************/ ******************************/
@ -43,6 +54,12 @@ static QueueHandle_t s_bt_app_task_queue = NULL; /* handle of work queue */
static TaskHandle_t s_bt_app_task_handle = NULL; /* handle of application task */ static TaskHandle_t s_bt_app_task_handle = NULL; /* handle of application task */
static TaskHandle_t s_bt_i2s_task_handle = NULL; /* handle of I2S task */ static TaskHandle_t s_bt_i2s_task_handle = NULL; /* handle of I2S task */
static RingbufHandle_t s_ringbuf_i2s = NULL; /* handle of ringbuffer for I2S */ 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;
/*********************************
* EXTERNAL FUNCTION DECLARATIONS
********************************/
#ifndef CONFIG_EXAMPLE_A2DP_SINK_OUTPUT_INTERNAL_DAC #ifndef CONFIG_EXAMPLE_A2DP_SINK_OUTPUT_INTERNAL_DAC
extern i2s_chan_handle_t tx_chan; extern i2s_chan_handle_t tx_chan;
#endif #endif
@ -101,18 +118,33 @@ static void bt_i2s_task_handler(void *arg)
{ {
uint8_t *data = NULL; uint8_t *data = NULL;
size_t item_size = 0; 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 = 240 * 6;
size_t bytes_written = 0; size_t bytes_written = 0;
for (;;) { for (;;) {
/* receive data from ringbuffer and write it to I2S DMA transmit buffer */ if (pdTRUE == xSemaphoreTake(s_i2s_write_semaphore, portMAX_DELAY)) {
data = (uint8_t *)xRingbufferReceive(s_ringbuf_i2s, &item_size, (TickType_t)portMAX_DELAY); for (;;) {
if (item_size != 0){ item_size = 0;
#ifdef CONFIG_EXAMPLE_A2DP_SINK_OUTPUT_INTERNAL_DAC /* receive data from ringbuffer and write it to I2S DMA transmit buffer */
i2s_write(0, data, item_size, &bytes_written, portMAX_DELAY); data = (uint8_t *)xRingbufferReceiveUpTo(s_ringbuf_i2s, &item_size, (TickType_t)pdMS_TO_TICKS(20), item_size_upto);
#else if (item_size == 0) {
i2s_channel_write(tx_chan, data, item_size, &bytes_written, portMAX_DELAY); ESP_LOGI(BT_APP_CORE_TAG, "ringbuffer underflowed! mode changed: RINGBUFFER_MODE_PREFETCHING");
#endif ringbuffer_mode = RINGBUFFER_MODE_PREFETCHING;
vRingbufferReturnItem(s_ringbuf_i2s, (void *)data); break;
}
#ifdef CONFIG_EXAMPLE_A2DP_SINK_OUTPUT_INTERNAL_DAC
i2s_write(0, data, item_size, &bytes_written, portMAX_DELAY);
#else
i2s_channel_write(tx_chan, data, item_size, &bytes_written, portMAX_DELAY);
#endif
vRingbufferReturnItem(s_ringbuf_i2s, (void *)data);
}
} }
} }
} }
@ -168,7 +200,14 @@ void bt_app_task_shut_down(void)
void bt_i2s_task_start_up(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; return;
} }
xTaskCreate(bt_i2s_task_handler, "BtI2STask", 1024, NULL, configMAX_PRIORITIES - 3, &s_bt_i2s_task_handle); xTaskCreate(bt_i2s_task_handler, "BtI2STask", 1024, NULL, configMAX_PRIORITIES - 3, &s_bt_i2s_task_handle);
@ -184,11 +223,44 @@ void bt_i2s_task_shut_down(void)
vRingbufferDelete(s_ringbuf_i2s); vRingbufferDelete(s_ringbuf_i2s);
s_ringbuf_i2s = NULL; 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) size_t write_ringbuf(const uint8_t *data, size_t size)
{ {
BaseType_t done = xRingbufferSend(s_ringbuf_i2s, (void *)data, size, (TickType_t)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; return done ? size : 0;
} }