1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /*****************************************************************************
8  *
9  *  Filename:      btc_a2dp.c
10  *
11  *****************************************************************************/
12 #include "common/bt_target.h"
13 #include "common/bt_trace.h"
14 #include "bta/bta_api.h"
15 #include "bta/bta_av_api.h"
16 #include "btc_av.h"
17 #include "btc_av_co.h"
18 #include "btc_a2dp.h"
19 #include "btc_a2dp_control.h"
20 #include "btc_a2dp_sink.h"
21 #include "btc_a2dp_source.h"
22 
23 
24 #if BTC_AV_INCLUDED
25 
26 /*****************************************************************************
27 **
28 ** Function        btc_a2dp_on_init
29 **
30 *******************************************************************************/
btc_a2dp_on_init(void)31 void btc_a2dp_on_init(void)
32 {
33     BTC_TRACE_EVENT("A2DP Initialized.");
34 }
35 
36 /*****************************************************************************
37 **
38 ** Function        btc_a2dp_on_idle
39 **
40 *******************************************************************************/
41 
btc_a2dp_on_idle(void)42 void btc_a2dp_on_idle(void)
43 {
44     APPL_TRACE_EVENT("## ON A2DP IDLE ## peer_sep = %d, service id = %d", btc_av_get_peer_sep(),
45             btc_av_get_service_id());
46 #if BTC_AV_SRC_INCLUDED
47     if (btc_av_get_peer_sep() == AVDT_TSEP_SNK && btc_av_get_service_id() == BTA_A2DP_SOURCE_SERVICE_ID) {
48         btc_a2dp_source_on_idle();
49     }
50 #endif // BTC_AV_SRC_INCLUDED
51 
52     bta_av_co_init();
53 
54 #if BTC_AV_SINK_INCLUDED
55     if (btc_av_get_peer_sep() == AVDT_TSEP_SRC && btc_av_get_service_id() == BTA_A2DP_SINK_SERVICE_ID) {
56         btc_a2dp_sink_on_idle();
57     }
58 #endif // BTC_AV_SINK_INCLUDED
59 }
60 
61 /*****************************************************************************
62 **
63 ** Function        btc_a2dp_on_started
64 **
65 ** Description
66 **
67 ** Returns
68 **
69 *******************************************************************************/
70 
btc_a2dp_on_started(tBTA_AV_START * p_av,BOOLEAN pending_start)71 BOOLEAN btc_a2dp_on_started(tBTA_AV_START *p_av, BOOLEAN pending_start)
72 {
73     BOOLEAN ack = FALSE;
74 
75     APPL_TRACE_EVENT("## ON A2DP STARTED ##");
76 #if BTC_AV_SRC_INCLUDED
77     if (p_av == NULL) {
78         /* ack back a local start request */
79         btc_a2dp_control_command_ack(ESP_A2D_MEDIA_CTRL_ACK_SUCCESS);
80         return TRUE;
81     }
82 
83     if (p_av->status == BTA_AV_SUCCESS) {
84         if (p_av->suspending == FALSE) {
85             if (p_av->initiator) {
86                 if (pending_start) {
87                     btc_a2dp_control_command_ack(ESP_A2D_MEDIA_CTRL_ACK_SUCCESS);
88                     ack = TRUE;
89                 }
90             } else {
91                 /* we were remotely started,  make sure codec
92                    is setup before datapath is started */
93                 btc_a2dp_source_setup_codec();
94             }
95 
96             /* media task is autostarted upon a2dp audiopath connection */
97         }
98     } else if (pending_start) {
99         btc_a2dp_control_command_ack(ESP_A2D_MEDIA_CTRL_ACK_FAILURE);
100         ack = TRUE;
101     }
102 #endif /* BTC_AV_SRC_INCLUDED */
103     return ack;
104 }
105 
106 /*****************************************************************************
107 **
108 ** Function        btc_a2dp_on_stopped
109 **
110 *******************************************************************************/
111 
btc_a2dp_on_stopped(tBTA_AV_SUSPEND * p_av)112 void btc_a2dp_on_stopped(tBTA_AV_SUSPEND *p_av)
113 {
114     APPL_TRACE_EVENT("## ON A2DP STOPPED ##");
115 #if BTC_AV_SINK_INCLUDED
116     if (btc_av_get_peer_sep() == AVDT_TSEP_SRC && btc_av_get_service_id() == BTA_A2DP_SINK_SERVICE_ID) {
117         btc_a2dp_sink_on_stopped(p_av);
118         return;
119     }
120 #endif // BTC_AV_SINK_INCLUDED
121 
122 #if BTC_AV_SRC_INCLUDED
123     btc_a2dp_source_on_stopped(p_av);
124 #endif // BTC_AV_SRC_INCLUDED
125 }
126 
127 /*****************************************************************************
128 **
129 ** Function        btc_a2dp_on_suspended
130 **
131 *******************************************************************************/
btc_a2dp_on_suspended(tBTA_AV_SUSPEND * p_av)132 void btc_a2dp_on_suspended(tBTA_AV_SUSPEND *p_av)
133 {
134     APPL_TRACE_EVENT("## ON A2DP SUSPENDED ##");
135 #if BTC_AV_SINK_INCLUDED
136     if (btc_av_get_peer_sep() == AVDT_TSEP_SRC && btc_av_get_service_id() == BTA_A2DP_SINK_SERVICE_ID) {
137         btc_a2dp_sink_on_suspended(p_av);
138         return;
139     }
140 #endif // BTC_AV_SINK_INCLUDED
141 #if BTC_AV_SRC_INCLUDED
142     btc_a2dp_source_on_suspended(p_av);
143 #endif // BTC_AV_SRC_INCLUDED
144 }
145 
146 #endif /* #if BTC_AV_INCLUDED */
147