1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 /*****************************************************************************
16  *
17  *  Filename:      btc_a2dp.c
18  *
19  *****************************************************************************/
20 #include "common/bt_target.h"
21 #include "common/bt_trace.h"
22 #include "bta/bta_api.h"
23 #include "bta/bta_av_api.h"
24 #include "btc_av.h"
25 #include "btc_av_co.h"
26 #include "btc_a2dp.h"
27 #include "btc_a2dp_control.h"
28 #include "btc_a2dp_sink.h"
29 #include "btc_a2dp_source.h"
30 
31 
32 #if BTC_AV_INCLUDED
33 
34 /*****************************************************************************
35 **
36 ** Function        btc_a2dp_on_init
37 **
38 *******************************************************************************/
btc_a2dp_on_init(void)39 void btc_a2dp_on_init(void)
40 {
41     BTC_TRACE_EVENT("A2DP Initialized.");
42 }
43 
44 /*****************************************************************************
45 **
46 ** Function        btc_a2dp_on_idle
47 **
48 *******************************************************************************/
49 
btc_a2dp_on_idle(void)50 void btc_a2dp_on_idle(void)
51 {
52     APPL_TRACE_EVENT("## ON A2DP IDLE ## peer_sep = %d, service id = %d", btc_av_get_peer_sep(),
53             btc_av_get_service_id());
54 #if BTC_AV_SRC_INCLUDED
55     if (btc_av_get_peer_sep() == AVDT_TSEP_SNK && btc_av_get_service_id() == BTA_A2DP_SOURCE_SERVICE_ID) {
56         btc_a2dp_source_on_idle();
57     }
58 #endif // BTC_AV_SRC_INCLUDED
59 
60     bta_av_co_init();
61 
62 #if BTC_AV_SINK_INCLUDED
63     if (btc_av_get_peer_sep() == AVDT_TSEP_SRC && btc_av_get_service_id() == BTA_A2DP_SINK_SERVICE_ID) {
64         btc_a2dp_sink_on_idle();
65     }
66 #endif // BTC_AV_SINK_INCLUDED
67 }
68 
69 /*****************************************************************************
70 **
71 ** Function        btc_a2dp_on_started
72 **
73 ** Description
74 **
75 ** Returns
76 **
77 *******************************************************************************/
78 
btc_a2dp_on_started(tBTA_AV_START * p_av,BOOLEAN pending_start)79 BOOLEAN btc_a2dp_on_started(tBTA_AV_START *p_av, BOOLEAN pending_start)
80 {
81     BOOLEAN ack = FALSE;
82 
83     APPL_TRACE_EVENT("## ON A2DP STARTED ##");
84 #if BTC_AV_SRC_INCLUDED
85     if (p_av == NULL) {
86         /* ack back a local start request */
87         btc_a2dp_control_command_ack(ESP_A2D_MEDIA_CTRL_ACK_SUCCESS);
88         return TRUE;
89     }
90 
91     if (p_av->status == BTA_AV_SUCCESS) {
92         if (p_av->suspending == FALSE) {
93             if (p_av->initiator) {
94                 if (pending_start) {
95                     btc_a2dp_control_command_ack(ESP_A2D_MEDIA_CTRL_ACK_SUCCESS);
96                     ack = TRUE;
97                 }
98             } else {
99                 /* we were remotely started,  make sure codec
100                    is setup before datapath is started */
101                 btc_a2dp_source_setup_codec();
102             }
103 
104             /* media task is autostarted upon a2dp audiopath connection */
105         }
106     } else if (pending_start) {
107         btc_a2dp_control_command_ack(ESP_A2D_MEDIA_CTRL_ACK_FAILURE);
108         ack = TRUE;
109     }
110 #endif /* BTC_AV_SRC_INCLUDED */
111     return ack;
112 }
113 
114 /*****************************************************************************
115 **
116 ** Function        btc_a2dp_on_stopped
117 **
118 *******************************************************************************/
119 
btc_a2dp_on_stopped(tBTA_AV_SUSPEND * p_av)120 void btc_a2dp_on_stopped(tBTA_AV_SUSPEND *p_av)
121 {
122     APPL_TRACE_EVENT("## ON A2DP STOPPED ##");
123 #if BTC_AV_SINK_INCLUDED
124     if (btc_av_get_peer_sep() == AVDT_TSEP_SRC && btc_av_get_service_id() == BTA_A2DP_SINK_SERVICE_ID) {
125         btc_a2dp_sink_on_stopped(p_av);
126         return;
127     }
128 #endif // BTC_AV_SINK_INCLUDED
129 
130 #if BTC_AV_SRC_INCLUDED
131     btc_a2dp_source_on_stopped(p_av);
132 #endif // BTC_AV_SRC_INCLUDED
133 }
134 
135 /*****************************************************************************
136 **
137 ** Function        btc_a2dp_on_suspended
138 **
139 *******************************************************************************/
btc_a2dp_on_suspended(tBTA_AV_SUSPEND * p_av)140 void btc_a2dp_on_suspended(tBTA_AV_SUSPEND *p_av)
141 {
142     APPL_TRACE_EVENT("## ON A2DP SUSPENDED ##");
143 #if BTC_AV_SINK_INCLUDED
144     if (btc_av_get_peer_sep() == AVDT_TSEP_SRC && btc_av_get_service_id() == BTA_A2DP_SINK_SERVICE_ID) {
145         btc_a2dp_sink_on_suspended(p_av);
146         return;
147     }
148 #endif // BTC_AV_SINK_INCLUDED
149 #if BTC_AV_SRC_INCLUDED
150     btc_a2dp_source_on_suspended(p_av);
151 #endif // BTC_AV_SRC_INCLUDED
152 }
153 
154 #endif /* #if BTC_AV_INCLUDED */
155