component/bt: use a callback function instead of UIPC function to write audio stream

This commit is contained in:
wangmengyang 2016-12-02 14:39:03 +08:00
parent 1d8355f54b
commit 6b4c2e8dc0
5 changed files with 76 additions and 79 deletions

View File

@ -6,59 +6,53 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "EspAudio.h"
#include "EspAudioCom.h"
#include "bt_app_common.h"
#include "btif_stack_manager.h"
#include "esp_gap_bt_api.h"
#include "bta_api.h"
// #include "bt_av.h"
#include "esp_a2dp_api.h"
/* utl_set_device_class() */
#include "utl.h"
#include "alarm.h"
typedef enum {
BT_APP_EVT_STACK_ON,
BT_APP_EVT_STACK_OFF,
BT_APP_EVT_AV_OPEN_TO,
BT_APP_EVT
BT_APP_EVT_STACK_ON = 0xa0,
BT_APP_EVT_MAX
} tBT_APP_EVT;
typedef union {
uint32_t dummy;
esp_a2d_cb_param_t a2d;
} tBT_APP_EVT_DATA;
// extern const btav_interface_t *btif_av_get_sink_interface(void);
static void bt_stack_evt(tBT_APP_EVT event, tBT_APP_EVT_DATA *p_data);
// static bt_bdaddr_t peer_bd_addr = {{0x00, 0x1b, 0xdc, 0x08, 0x0f, 0xe7}};
static void bt_app_evt(tBT_APP_EVT event, tBT_APP_EVT_DATA *p_data);
osi_alarm_t *app_alarm = NULL;
static void esp_a2d_cb(uint32_t event, void *param)
static void bt_app_a2d_cb(uint32_t event, void *param)
{
esp_a2d_cb_param_t *p = (esp_a2d_cb_param_t *)param;
switch (event) {
case ESP_A2D_CONNECTION_STATE_EVT:
LOG_ERROR("===a2dp conn_state_cb %d ===\n", p->conn_stat.state);
break;
case ESP_A2D_AUDIO_STATE_EVT:
LOG_ERROR("===a2dp audio_state_cb %d ===\n", p->audio_stat.state);
break;
case ESP_A2D_AUDIO_CFG_EVT:
LOG_ERROR("===a2dp audio_cfg_cb type %d ===\n", p->audio_cfg.mcc.type);
{
bt_app_evt(event, (tBT_APP_EVT_DATA *)param);
break;
}
default:
LOG_ERROR("===a2dp invalid cb event: %d\n", event);
break;
}
}
static void btav_open_to(void *context)
static void bt_app_a2d_data_cb(uint8_t *data, uint32_t len)
{
(void)(context);
bt_stack_evt(BT_APP_EVT_AV_OPEN_TO, NULL);
// uint32_t t_now = system_get_time();
// printf("t: %u, l %d\n", t_now, len);
EspAudioPlayerStreamWrite(data, len);
}
static void btav_set_device_class(void)
{
tBTA_UTL_COD cod;
@ -71,39 +65,54 @@ static void btav_set_device_class(void)
cod.major, cod.minor, cod.service);
}
static void bt_app_stack_evt(UINT16 event, char *p_param)
static void bt_app_handle_evt(UINT16 event, char *p_param)
{
esp_a2d_cb_param_t *a2d = NULL;
switch (event) {
case BT_APP_EVT_STACK_ON: {
char *dev_name = "SDP_SERVER_CLIENT";
BTM_SetTraceLevel(BT_TRACE_LEVEL_WARNING);
btav_set_device_class();
BTA_DmSetDeviceName(dev_name);
esp_bt_gap_set_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
esp_a2d_register_callback(esp_a2d_cb);
esp_a2d_sink_init();
// app_alarm = osi_alarm_new("app_alarm", bt_sdp_add_record_to, NULL, 1000, false);
app_alarm = osi_alarm_new("app_alarm", btav_open_to, NULL, 1000, false);
osi_alarm_set(app_alarm, 1000);
esp_a2d_register_callback(bt_app_a2d_cb);
esp_a2d_register_data_callback(bt_app_a2d_data_cb);
esp_a2d_sink_init();
esp_bt_gap_set_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
break;
}
break;
case BT_APP_EVT_AV_OPEN_TO: {
LOG_ERROR("**BT_APP_EVT_AV_OPEN_TO\n");
// btif_av_get_sink_interface()->connect(&peer_bd_addr);
osi_alarm_free(app_alarm);
app_alarm = NULL;
case ESP_A2D_CONNECTION_STATE_EVT: {
a2d = (esp_a2d_cb_param_t *)(p_param);
LOG_ERROR("===a2dp conn_state_cb %d ===\n", a2d->conn_stat.state);
break;
}
case ESP_A2D_AUDIO_STATE_EVT: {
a2d = (esp_a2d_cb_param_t *)(p_param);
LOG_ERROR("===a2dp audio_state_cb %d ===\n", a2d->audio_stat.state);
break;
}
case ESP_A2D_AUDIO_CFG_EVT: {
a2d = (esp_a2d_cb_param_t *)(p_param);
LOG_ERROR("===a2dp audio_cfg_cb type %d ===\n", a2d->audio_cfg.mcc.type);
if (a2d->audio_cfg.mcc.type == ESP_A2D_MCT_SBC) {
// temporarily hardcoded the PCM configuaration
EspAudioPlayerStreamCfg(StreamSampleRate_44k, 2, StreamBitLen_16BIT);
EspAudio_SetupStream("stream.pcm", InputSrcType_Stream);
EspAudio_SetVolume(99);
}
break;
}
break;
default:
LOG_ERROR("===application invalid event: %d\n", event);
break;
}
}
static void bt_stack_evt(tBT_APP_EVT event, tBT_APP_EVT_DATA *p_data)
static void bt_app_evt(tBT_APP_EVT event, tBT_APP_EVT_DATA *p_data)
{
LOG_ERROR("bt_stack_evt: %d\n", (uint16_t)event);
bt_app_transfer_context(bt_app_stack_evt, (uint16_t)event,
LOG_ERROR("bt_app_evt: %d\n", (uint16_t)event);
bt_app_transfer_context(bt_app_handle_evt, (uint16_t)event,
(void *)p_data, sizeof(tBT_APP_EVT_DATA), NULL);
}
@ -120,6 +129,6 @@ void app_main_entry(void)
return;
}
bt_stack_evt(BT_APP_EVT_STACK_ON, NULL);
bt_app_evt(BT_APP_EVT_STACK_ON, NULL);
}

View File

@ -101,6 +101,7 @@ static esp_profile_cb_t bt_av_sink_callback = NULL;
static btif_av_cb_t btif_av_cb = {0};
// static TIMER_LIST_ENT tle_av_open_on_rc;
// TODO: need protection against race
#define BTIF_A2D_CB_TO_APP(_event, _param) do { \
if (bt_av_sink_callback) { \
bt_av_sink_callback(_event, _param); \
@ -983,7 +984,7 @@ static bt_status_t init_src(btav_callbacks_t *callbacks)
*/
esp_err_t esp_a2d_register_callback(esp_profile_cb_t callback)
{
// TODO: add concurrency protection
// TODO: need protection against race
bt_av_sink_callback = callback;
}

View File

@ -84,6 +84,7 @@
#include "btif_media.h"
#include "allocator.h"
#include "bt_utils.h"
#include "esp_a2dp_api.h"
#if (BTA_AV_SINK_INCLUDED == TRUE)
OI_CODEC_SBC_DECODER_CONTEXT context;
@ -347,6 +348,21 @@ static fixed_queue_t *btif_media_cmd_msg_queue = NULL;
static xTaskHandle xBtifMediaTaskHandle = NULL;
static QueueHandle_t xBtifMediaQueue = NULL;
static esp_a2d_data_cb_t bt_av_sink_data_callback = NULL;
esp_err_t esp_a2d_register_data_callback(esp_a2d_data_cb_t cb)
{
// TODO: need protection against race
bt_av_sink_data_callback = cb;
}
// TODO: need protection against race
#define BTIF_A2D_DATA_CB_TO_APP(data, len) do { \
if (bt_av_sink_data_callback) { \
bt_av_sink_data_callback(data, len); \
} \
} while (0)
/*****************************************************************************
** temporary hacked functions. TODO: port these functions or remove them?
*****************************************************************************/
@ -1431,7 +1447,9 @@ static void btif_media_task_handle_inc_media(tBT_SBC_HDR *p_msg)
}
// LOG_ERROR("pre-send: %d\n", availPcmBytes);
UIPC_Send(UIPC_CH_ID_AV_AUDIO, 0, (UINT8 *)pcmData, (2 * sizeof(pcmData) - availPcmBytes));
// UIPC_Send(UIPC_CH_ID_AV_AUDIO, 0, (UINT8 *)pcmData, (2 * sizeof(pcmData) - availPcmBytes));
BTIF_A2D_DATA_CB_TO_APP((uint8_t *)pcmData, (2 * sizeof(pcmData) - availPcmBytes));
}
#endif

View File

@ -86,6 +86,8 @@ typedef union {
} audio_cfg;
} esp_a2d_cb_param_t;
typedef void (* esp_a2d_data_cb_t)(uint8_t *buf, uint32_t len);
/**
* NOTE:
* A2DP data path is handled via below function sets, between the Audio HAL
@ -97,6 +99,8 @@ typedef union {
*/
esp_err_t esp_a2d_register_callback(esp_profile_cb_t callback);
esp_err_t esp_a2d_register_data_callback(esp_a2d_data_cb_t cb);
esp_err_t esp_a2d_sink_init(void);
esp_err_t esp_a2d_sink_connect(esp_bd_addr_t *remote_bda);

View File

@ -26,16 +26,8 @@
#include <stddef.h>
#include <stdio.h>
#include "uipc.h"
#include "esp_system.h"
#include "EspAudio.h"
#include "EspAudioCom.h"
#include "bt_trace.h"
#include "freertos/xtensa_api.h"
#include "freertos/FreeRTOSConfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
/*****************************************************************************
** Constants & Macros
******************************************************************************/
@ -58,18 +50,6 @@ const char *dump_uipc_event(tUIPC_EVENT event)
*******************************************************************************/
void UIPC_Init(void *dummy)
{
LOG_ERROR("UIPC_Init: Free memory: %d bytes\n", system_get_free_heap_size());
// EspAudio_Init();
{
int volumn;
// TODO: review the stream param config parameter here
EspAudioPlayerStreamCfg(StreamSampleRate_44k, 2, StreamBitLen_16BIT);
EspAudio_SetupStream("stream.pcm", InputSrcType_Stream);
EspAudio_GetVolume(&volumn);
LOG_ERROR("UIPC: Vol: %d\n", volumn);
EspAudio_SetVolume(99);
}
return;
}
@ -84,16 +64,6 @@ void UIPC_Init(void *dummy)
*******************************************************************************/
BOOLEAN UIPC_Open(tUIPC_CH_ID ch_id, tUIPC_RCV_CBACK *p_cback)
{
LOG_ERROR("UIPC_Open\n");
//if (ch_id == UIPC_CH_ID_AV_AUDIO) {
// }
/*
if (p_cback) {
p_cback(ch_id, UIPC_OPEN_EVT);
}
*/
return TRUE;
}
@ -137,11 +107,6 @@ BOOLEAN UIPC_SendBuf(tUIPC_CH_ID ch_id, BT_HDR *p_msg)
*******************************************************************************/
BOOLEAN UIPC_Send(tUIPC_CH_ID ch_id, UINT16 msg_evt, UINT8 *p_buf, UINT16 msglen)
{
if (ch_id == UIPC_CH_ID_AV_AUDIO) {
uint32_t t_now = system_get_time();
printf("t: %u, l %d\n", t_now, msglen);
EspAudioPlayerStreamWrite(p_buf, msglen);
}
return TRUE;
}