11diff --git a/components/bluetooth_service/a2dp_stream.c b/components/bluetooth_service/a2dp_stream.c
2- index 06dc1b4b..c1cacea7 100644
2+ index 1d5c1905..1ddc0346 100644
33--- a/components/bluetooth_service/a2dp_stream.c
44+++ b/components/bluetooth_service/a2dp_stream.c
5- @@ -32,9 +32,16 @@
5+ @@ -34,11 +34,20 @@
66 #include "sdkconfig.h"
77
88 #include "a2dp_stream.h"
99+ #include "esp_avrc_api.h"
1010
1111 #if CONFIG_BT_ENABLED
1212
13+ static const char *TAG = "A2DP_STREAM";
14+
1315+ #define APP_RC_CT_TL_GET_CAPS (0)
1416+ #define APP_RC_CT_TL_GET_META_DATA (1)
1517+ #define APP_RC_CT_TL_RN_TRACK_CHANGE (2)
1618+ #define APP_RC_CT_TL_RN_PLAYBACK_CHANGE (3)
1719+ #define APP_RC_CT_TL_RN_PLAY_POS_CHANGE (4)
1820+
19- static const char *TAG = "A2DP_STREAM";
20-
21- static audio_element_handle_t a2dp_sink_stream_handle = NULL;
22- @@ -44,6 +51,7 @@ static esp_periph_handle_t bt_avrc_periph = NULL;
23- static bool avrcp_conn_state = false;
24- static audio_stream_type_t a2d_stream_type = 0;
25- static uint8_t trans_label = 0;
2621+ static esp_avrc_rn_evt_cap_mask_t s_avrc_peer_rn_cap;
22+ +
23+ typedef struct {
24+ audio_element_handle_t sink_stream;
25+ audio_element_handle_t source_stream;
26+ @@ -84,19 +93,19 @@ static void audio_a2dp_stream_thread(void *pvParameters)
2727
28- #if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0))
29- static audio_hal_handle_t audio_hal = NULL;
30- @@ -219,7 +227,7 @@ audio_element_handle_t a2dp_stream_init(a2dp_stream_config_t *config)
28+ a2dp_data_t recv_msg;
29+ bool _aadp_task_run = true;
30+ - while(_aadp_task_run) {
31+ + while (_aadp_task_run) {
32+ if ( pdTRUE == xQueueReceive (s_aadp_handler.a2dp_queue, &recv_msg, portMAX_DELAY) ) {
33+ switch (recv_msg.type) {
34+ - case A2DP_TYPE_SINK:
35+ + case A2DP_TYPE_SINK:
36+ audio_element_output(s_aadp_handler.sink_stream, (char *)recv_msg.data, recv_msg.size);
37+ audio_free(recv_msg.data);
38+ recv_msg.data = NULL;
39+ - break;
40+ - case A2DP_TYPE_DESTORY:
41+ - _aadp_task_run = false;
42+ - break;
43+ - default:
44+ - break;
45+ + break;
46+ + case A2DP_TYPE_DESTORY:
47+ + _aadp_task_run = false;
48+ + break;
49+ + default:
50+ + break;
51+ }
52+ }
53+ }
54+ @@ -194,7 +203,7 @@ static void bt_a2d_sink_data_cb(const uint8_t *data, uint32_t len)
55+ ESP_LOGW(TAG, "discard a2dp(%p) sink pkt, A2DP_STREAM_QUEUE_SIZE value needs to be expanded", s_aadp_handler.a2dp_queue);
56+ audio_free(send_msg.data);
57+ send_msg.data = NULL;
58+ - }
59+ + }
60+ }
61+ }
62+ }
63+ @@ -288,7 +297,7 @@ audio_element_handle_t a2dp_stream_init(a2dp_stream_config_t *config)
64+ }
3165
3266 cfg.task_stack = -1; // No need task
33- cfg.tag = "aadp";
34- -
35- +
67+ - cfg.tag = "aadp";
68+ + cfg.tag = "aadp";
69+
3670 esp_avrc_ct_init();
3771 esp_avrc_ct_register_callback(bt_avrc_ct_cb);
72+ @@ -325,7 +334,7 @@ audio_element_handle_t a2dp_stream_init(a2dp_stream_config_t *config)
73+ }
3874
39- @@ -295,13 +303,63 @@ static void bt_avrc_volume_set_by_local(int16_t volume)
40- rn_param.volume = g_volume;
75+ AUDIO_MEM_CHECK(TAG, el, return NULL);
76+ -
77+ +
78+ memcpy(&s_aadp_handler.user_callback, &config->user_callback, sizeof(a2dp_stream_user_callback_t));
79+
80+ if ( config->type == AUDIO_STREAM_READER ) {
81+ @@ -335,7 +344,7 @@ audio_element_handle_t a2dp_stream_init(a2dp_stream_config_t *config)
82+ return NULL;
83+ }
84+ esp_err_t err = audio_thread_create(&s_aadp_handler.a2dp_thread, "audio_a2dp_stream_thread", audio_a2dp_stream_thread, NULL,
85+ - A2DP_STREAM_TASK_STACK, A2DP_STREAM_TASK_PRIO, A2DP_STREAM_TASK_IN_EXT, A2DP_STREAM_TASK_CORE);
86+ + A2DP_STREAM_TASK_STACK, A2DP_STREAM_TASK_PRIO, A2DP_STREAM_TASK_IN_EXT, A2DP_STREAM_TASK_CORE);
87+ if (err != ESP_OK) {
88+ ESP_LOGE(TAG, "Create audio_a2dp_stream_thread failed(%d)", __LINE__);
89+ return NULL;
90+ @@ -378,13 +387,64 @@ static void bt_avrc_volume_set_by_local(int16_t volume)
91+ rn_param.volume = s_aadp_handler.volume;
4192 esp_avrc_tg_send_rn_rsp(ESP_AVRC_RN_VOLUME_CHANGE, ESP_AVRC_RN_RSP_CHANGED, &rn_param);
42- if (audio_hal) {
43- - audio_hal_set_volume(audio_hal, g_volume );
93+ if (s_aadp_handler. audio_hal) {
94+ - audio_hal_set_volume(s_aadp_handler. audio_hal, s_aadp_handler.volume );
4495- }
45- + audio_hal_set_volume(audio_hal, g_volume );
96+ + audio_hal_set_volume(s_aadp_handler. audio_hal, s_aadp_handler.volume );
4697+ }
47- g_volume_notify = false;
98+ s_aadp_handler.volume_notify = false;
4899 }
49100 }
50101 #endif
@@ -98,19 +149,18 @@ index 06dc1b4b..c1cacea7 100644
98149+ break;
99150+ }
100151+ }
152+ +
101153+
102154 static void bt_avrc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *p_param)
103155 {
104156 esp_avrc_ct_cb_param_t *rc = p_param;
105- @@ -312 ,17 +370,21 @@ static void bt_avrc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *
157+ @@ -395 ,17 +455,19 @@ static void bt_avrc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *
106158 if (rc->conn_stat.connected) {
107159 ESP_LOGD(TAG, "ESP_AVRC_CT_CONNECTION_STATE_EVT");
108160 bt_key_act_sm_init();
109- + // get remote supported event_ids of peer AVRCP Target
110161+ esp_avrc_ct_send_get_rn_capabilities_cmd(APP_RC_CT_TL_GET_CAPS);
111162 } else if (0 == rc->conn_stat.connected) {
112163 bt_key_act_sm_deinit();
113- + // clear peer notification capability record
114164+ s_avrc_peer_rn_cap.bits = 0;
115165 }
116166
@@ -120,24 +170,16 @@ index 06dc1b4b..c1cacea7 100644
120170 break;
121171 }
122172 case ESP_AVRC_CT_PASSTHROUGH_RSP_EVT: {
123- if (avrcp_conn_state) {
173+ if (s_aadp_handler. avrcp_conn_state) {
124174- ESP_LOGD(TAG, "AVRC passthrough rsp: key_code 0x%x, key_state %d", rc->psth_rsp.key_code, rc->psth_rsp.key_state);
125175+ ESP_LOGI(TAG, "AVRC passthrough rsp: key_code 0x%x, key_state %d", rc->psth_rsp.key_code, rc->psth_rsp.key_state);
126176 bt_key_act_param_t param;
127177 memset(¶m, 0, sizeof(bt_key_act_param_t));
128178 param.evt = event;
129- @@ -334,16 +396,26 @@ static void bt_avrc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *
130- break;
131- }
132- case ESP_AVRC_CT_METADATA_RSP_EVT: {
133- - ESP_LOGD(TAG, "AVRC metadata rsp: attribute id 0x%x, %s", rc->meta_rsp.attr_id, rc->meta_rsp.attr_text);
134- + ESP_LOGI(TAG, "AVRC metadata rsp: attribute id 0x%x, %s", rc->meta_rsp.attr_id, rc->meta_rsp.attr_text);
135- // free(rc->meta_rsp.attr_text);
136- break;
179+ @@ -423,10 +485,20 @@ static void bt_avrc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *
137180 }
138181 case ESP_AVRC_CT_CHANGE_NOTIFY_EVT: {
139- - // ESP_LOGD(TAG, "AVRC event notification: %u, param: %u", rc->change_ntf.event_id, rc->change_ntf.event_parameter);
140- + // ESP_LOGI(TAG, "AVRC event notification: %u, param: %u", rc->change_ntf.event_id, rc->change_ntf.event_parameter);
182+ // ESP_LOGD(TAG, "AVRC event notification: %u, param: %u", rc->change_ntf.event_id, rc->change_ntf.event_parameter);
141183+ bt_av_notify_evt_handler(rc->change_ntf.event_id, &rc->change_ntf.event_parameter);
142184 break;
143185 }
@@ -156,15 +198,15 @@ index 06dc1b4b..c1cacea7 100644
156198 break;
157199 }
158200 default:
159- @@ -358 ,45 +430 ,45 @@ static void bt_avrc_tg_cb(esp_avrc_tg_cb_event_t event, esp_avrc_tg_cb_param_t *
201+ @@ -441 ,45 +513 ,45 @@ static void bt_avrc_tg_cb(esp_avrc_tg_cb_event_t event, esp_avrc_tg_cb_param_t *
160202 ESP_LOGD(TAG, "%s evt %d", __func__, event);
161203 esp_avrc_tg_cb_param_t *rc = (esp_avrc_tg_cb_param_t *)(param);
162204 switch (event) {
163205- case ESP_AVRC_TG_CONNECTION_STATE_EVT: {
164206- uint8_t *bda = rc->conn_stat.remote_bda;
165207- ESP_LOGI(TAG, "AVRC conn_state evt: state %d, [%02x:%02x:%02x:%02x:%02x:%02x]",
166208- rc->conn_stat.connected, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
167- - avrcp_conn_tg_state = rc->conn_stat.connected;
209+ - s_aadp_handler. avrcp_conn_tg_state = rc->conn_stat.connected;
168210- break;
169211- }
170212- case ESP_AVRC_TG_PASSTHROUGH_CMD_EVT: {
@@ -179,9 +221,9 @@ index 06dc1b4b..c1cacea7 100644
179221- case ESP_AVRC_TG_REGISTER_NOTIFICATION_EVT: {
180222- ESP_LOGI(TAG, "AVRC register event notification: %d, param: 0x%x", rc->reg_ntf.event_id, rc->reg_ntf.event_parameter);
181223- if (rc->reg_ntf.event_id == ESP_AVRC_RN_VOLUME_CHANGE) {
182- - g_volume_notify = true;
224+ - s_aadp_handler.volume_notify = true;
183225- esp_avrc_rn_param_t rn_param;
184- - rn_param.volume = g_volume ;
226+ - rn_param.volume = s_aadp_handler.volume ;
185227- esp_avrc_tg_send_rn_rsp(ESP_AVRC_RN_VOLUME_CHANGE, ESP_AVRC_RN_RSP_INTERIM, &rn_param);
186228- }
187229- break;
@@ -197,7 +239,7 @@ index 06dc1b4b..c1cacea7 100644
197239+ uint8_t *bda = rc->conn_stat.remote_bda;
198240+ ESP_LOGI(TAG, "AVRC conn_state evt: state %d, [%02x:%02x:%02x:%02x:%02x:%02x]",
199241+ rc->conn_stat.connected, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
200- + avrcp_conn_tg_state = rc->conn_stat.connected;
242+ + s_aadp_handler. avrcp_conn_tg_state = rc->conn_stat.connected;
201243+ break;
202244+ }
203245+ case ESP_AVRC_TG_PASSTHROUGH_CMD_EVT: {
@@ -212,9 +254,9 @@ index 06dc1b4b..c1cacea7 100644
212254+ case ESP_AVRC_TG_REGISTER_NOTIFICATION_EVT: {
213255+ ESP_LOGI(TAG, "AVRC register event notification: %d, param: 0x%x", rc->reg_ntf.event_id, rc->reg_ntf.event_parameter);
214256+ if (rc->reg_ntf.event_id == ESP_AVRC_RN_VOLUME_CHANGE) {
215- + g_volume_notify = true;
257+ + s_aadp_handler.volume_notify = true;
216258+ esp_avrc_rn_param_t rn_param;
217- + rn_param.volume = g_volume ;
259+ + rn_param.volume = s_aadp_handler.volume ;
218260+ esp_avrc_tg_send_rn_rsp(ESP_AVRC_RN_VOLUME_CHANGE, ESP_AVRC_RN_RSP_INTERIM, &rn_param);
219261+ }
220262+ break;
@@ -236,12 +278,12 @@ index 06dc1b4b..c1cacea7 100644
236278 return ESP_OK;
237279 }
238280
239- @@ -428 ,7 +500 ,7 @@ static esp_err_t periph_bt_avrc_passthrough_cmd(esp_periph_handle_t periph, uint
281+ @@ -511 ,7 +583 ,7 @@ static esp_err_t periph_bt_avrc_passthrough_cmd(esp_periph_handle_t periph, uint
240282 esp_err_t err = ESP_OK;
241283
242284 #if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0))
243- - if(avrcp_conn_tg_state) {
244- + if (avrcp_conn_tg_state) {
285+ - if(s_aadp_handler. avrcp_conn_tg_state) {
286+ + if (s_aadp_handler. avrcp_conn_tg_state) {
245287 if (cmd == ESP_AVRC_PT_CMD_VOL_DOWN) {
246- int16_t volume = (g_volume - 5) < 0 ? 0 : (g_volume - 5);
288+ int16_t volume = (s_aadp_handler.volume - 5) < 0 ? 0 : (s_aadp_handler.volume - 5);
247289 bt_avrc_volume_set_by_local(volume);
0 commit comments