Skip to content

Commit

Permalink
Merge branch 'bt/opt_audio_datapath_v4.3' into 'release/v4.3'
Browse files Browse the repository at this point in the history
bt/optimized a2dp_sink audio datapath (v4.3)

See merge request espressif/esp-idf!21224
  • Loading branch information
jack0c committed Nov 25, 2022
2 parents 81d598b + bda3683 commit a0967db
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 12 deletions.
Expand Up @@ -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)

Expand Down Expand Up @@ -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 - ");
Expand Down
Expand Up @@ -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
******************************/
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down Expand Up @@ -157,10 +184,17 @@ 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;
}
xTaskCreate(bt_i2s_task_handler, "BtI2STask", 1024, NULL, configMAX_PRIORITIES - 3, &s_bt_i2s_task_handle);
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", 2048, NULL, configMAX_PRIORITIES - 3, &s_bt_i2s_task_handle);
}

void bt_i2s_task_shut_down(void)
Expand All @@ -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;
}

0 comments on commit a0967db

Please sign in to comment.