• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "a2dp_encoding_host.h"
18 
19 #include <errno.h>
20 #include <grp.h>
21 #include <sys/stat.h>
22 
23 #include <memory>
24 
25 #include "a2dp_encoding.h"
26 #include "a2dp_sbc_constants.h"
27 #include "btif_a2dp_source.h"
28 #include "btif_av.h"
29 #include "btif_av_co.h"
30 #include "btif_hf.h"
31 #include "osi/include/log.h"
32 #include "osi/include/properties.h"
33 #include "types/raw_address.h"
34 #include "udrv/include/uipc.h"
35 
36 #define A2DP_DATA_READ_POLL_MS 10
37 #define A2DP_HOST_DATA_PATH "/var/run/bluetooth/audio/.a2dp_data"
38 // TODO(b/198260375): Make A2DP data owner group configurable.
39 #define A2DP_HOST_DATA_GROUP "bluetooth-audio"
40 
41 namespace {
42 
43 std::unique_ptr<tUIPC_STATE> a2dp_uipc = nullptr;
44 
btif_a2dp_data_cb(tUIPC_CH_ID ch_id,tUIPC_EVENT event)45 static void btif_a2dp_data_cb([[maybe_unused]] tUIPC_CH_ID ch_id,
46                               tUIPC_EVENT event) {
47   APPL_TRACE_WARNING("%s: BTIF MEDIA (A2DP-DATA) EVENT %s", __func__,
48                      dump_uipc_event(event));
49 
50   switch (event) {
51     case UIPC_OPEN_EVT:
52       /*
53        * Read directly from media task from here on (keep callback for
54        * connection events.
55        */
56       UIPC_Ioctl(*a2dp_uipc, UIPC_CH_ID_AV_AUDIO,
57                  UIPC_REG_REMOVE_ACTIVE_READSET, NULL);
58       UIPC_Ioctl(*a2dp_uipc, UIPC_CH_ID_AV_AUDIO, UIPC_SET_READ_POLL_TMO,
59                  reinterpret_cast<void*>(A2DP_DATA_READ_POLL_MS));
60 
61       // Will start audio on btif_a2dp_on_started
62 
63       /* ACK back when media task is fully started */
64       break;
65 
66     case UIPC_CLOSE_EVT:
67       /* Post stop event and wait for audio path to stop */
68       btif_av_stream_stop(RawAddress::kEmpty);
69       break;
70 
71     default:
72       APPL_TRACE_ERROR("%s: ### A2DP-DATA EVENT %d NOT HANDLED ###", __func__,
73                        event);
74       break;
75   }
76 }
77 
78 // If A2DP_HOST_DATA_GROUP exists we expect audio server and BT both are
79 // in this group therefore have access to A2DP socket. Otherwise audio
80 // server should be in the same group that BT stack runs with to access
81 // A2DP socket.
a2dp_data_path_open()82 static void a2dp_data_path_open() {
83   UIPC_Open(*a2dp_uipc, UIPC_CH_ID_AV_AUDIO, btif_a2dp_data_cb,
84             A2DP_HOST_DATA_PATH);
85   struct group* grp = getgrnam(A2DP_HOST_DATA_GROUP);
86   chmod(A2DP_HOST_DATA_PATH, 0770);
87   if (grp) {
88     int res = chown(A2DP_HOST_DATA_PATH, -1, grp->gr_gid);
89     if (res == -1) {
90       LOG_ERROR("%s failed: %s", __func__, strerror(errno));
91     }
92   }
93 }
94 
95 tA2DP_CTRL_CMD a2dp_pending_cmd_ = A2DP_CTRL_CMD_NONE;
96 uint64_t total_bytes_read_;
97 timespec data_position_;
98 uint16_t remote_delay_report_;
99 
100 }  // namespace
101 
102 namespace bluetooth {
103 namespace audio {
104 namespace a2dp {
105 
106 // Invoked by audio server to set audio config (PCM for now)
SetAudioConfig(AudioConfig config)107 bool SetAudioConfig(AudioConfig config) {
108   btav_a2dp_codec_config_t codec_config;
109   codec_config.sample_rate = config.sample_rate;
110   codec_config.bits_per_sample = config.bits_per_sample;
111   codec_config.channel_mode = config.channel_mode;
112   btif_a2dp_source_feeding_update_req(codec_config);
113   return true;
114 }
115 
116 // Invoked by audio server when it has audio data to stream.
StartRequest()117 bool StartRequest() {
118   // Reset total read bytes and timestamp to avoid confusing audio
119   // server at delay calculation.
120   total_bytes_read_ = 0;
121   data_position_ = {0, 0};
122 
123   // Check if a previous request is not finished
124   if (a2dp_pending_cmd_ == A2DP_CTRL_CMD_START) {
125     LOG_INFO("%s: A2DP_CTRL_CMD_START in progress", __func__);
126     return false;
127   } else if (a2dp_pending_cmd_ != A2DP_CTRL_CMD_NONE) {
128     LOG_WARN("%s: busy in pending_cmd=%u", __func__, a2dp_pending_cmd_);
129     return false;
130   }
131 
132   // Don't send START request to stack while we are in a call
133   if (!bluetooth::headset::IsCallIdle()) {
134     LOG_ERROR("%s: call state is busy", __func__);
135     return false;
136   }
137 
138   if (btif_av_stream_started_ready()) {
139     // Already started, ACK back immediately.
140     a2dp_data_path_open();
141     return true;
142   }
143   if (btif_av_stream_ready()) {
144     a2dp_data_path_open();
145     /*
146      * Post start event and wait for audio path to open.
147      * If we are the source, the ACK will be sent after the start
148      * procedure is completed.
149      */
150     a2dp_pending_cmd_ = A2DP_CTRL_CMD_START;
151     btif_av_stream_start();
152     if (btif_av_get_peer_sep() != AVDT_TSEP_SRC) {
153       LOG_INFO("%s: accepted", __func__);
154       return true;  // NOTE: The request is placed, but could still fail.
155     }
156     a2dp_pending_cmd_ = A2DP_CTRL_CMD_NONE;
157     return true;
158   }
159   LOG_ERROR("%s: AV stream is not ready to start", __func__);
160   return false;
161 }
162 
163 // Invoked by audio server when audio streaming is done.
StopRequest()164 bool StopRequest() {
165   if (btif_av_get_peer_sep() == AVDT_TSEP_SNK &&
166       !btif_av_stream_started_ready()) {
167     btif_av_clear_remote_suspend_flag();
168     return true;
169   }
170   LOG_INFO("%s: handling", __func__);
171   a2dp_pending_cmd_ = A2DP_CTRL_CMD_STOP;
172   btif_av_stream_stop(RawAddress::kEmpty);
173   return true;
174 }
175 
SuspendRequest()176 bool SuspendRequest() {
177   if (a2dp_pending_cmd_ != A2DP_CTRL_CMD_NONE) {
178     LOG_WARN("%s: busy in pending_cmd=%u", __func__, a2dp_pending_cmd_);
179     return false;
180   }
181   if (!btif_av_stream_started_ready()) {
182     LOG_WARN("%s: AV stream is not started", __func__);
183     return false;
184   }
185   LOG_INFO("%s: handling", __func__);
186   a2dp_pending_cmd_ = A2DP_CTRL_CMD_SUSPEND;
187   btif_av_stream_suspend();
188   return true;
189 }
190 
191 // Invoked by audio server to check audio presentation position periodically.
GetPresentationPosition()192 PresentationPosition GetPresentationPosition() {
193   PresentationPosition presentation_position{
194       .remote_delay_report_ns = remote_delay_report_ * 100000u,
195       .total_bytes_read = total_bytes_read_,
196       .data_position = data_position_,
197   };
198   return presentation_position;
199 }
200 
201 // delay reports from AVDTP is based on 1/10 ms (100us)
set_remote_delay(uint16_t delay_report)202 void set_remote_delay(uint16_t delay_report) {
203   remote_delay_report_ = delay_report;
204 }
205 
206 // Inform audio server about offloading codec; not used for now
update_codec_offloading_capabilities(const std::vector<btav_a2dp_codec_config_t> & framework_preference)207 bool update_codec_offloading_capabilities(
208     const std::vector<btav_a2dp_codec_config_t>& framework_preference) {
209   return false;
210 }
211 
212 // Checking if new bluetooth_audio is enabled
is_hal_enabled()213 bool is_hal_enabled() { return true; }
214 
215 // Check if new bluetooth_audio is running with offloading encoders
is_hal_offloading()216 bool is_hal_offloading() { return false; }
217 
218 // Initialize BluetoothAudio HAL: openProvider
init(bluetooth::common::MessageLoopThread * message_loop)219 bool init(bluetooth::common::MessageLoopThread* message_loop) {
220   a2dp_uipc = UIPC_Init();
221   total_bytes_read_ = 0;
222   data_position_ = {};
223   remote_delay_report_ = 0;
224 
225   return true;
226 }
227 
228 // Clean up BluetoothAudio HAL
cleanup()229 void cleanup() {
230   end_session();
231 
232   if (a2dp_uipc != nullptr) {
233     UIPC_Close(*a2dp_uipc, UIPC_CH_ID_ALL);
234   }
235 }
236 
237 // Set up the codec into BluetoothAudio HAL
setup_codec()238 bool setup_codec() {
239   // TODO: setup codec
240   return true;
241 }
242 
start_session()243 void start_session() {
244   // TODO: Notify server; or do we handle it during connected?
245 }
246 
end_session()247 void end_session() {
248   // TODO: Notify server; or do we handle it during disconnected?
249 
250   // Reset remote delay. New value will be set when new session starts.
251   remote_delay_report_ = 0;
252 
253   a2dp_pending_cmd_ = A2DP_CTRL_CMD_NONE;
254 }
255 
set_audio_low_latency_mode_allowed(bool allowed)256 void set_audio_low_latency_mode_allowed(bool allowed){
257 }
258 
259 
ack_stream_started(const tA2DP_CTRL_ACK & ack)260 void ack_stream_started(const tA2DP_CTRL_ACK& ack) {
261   a2dp_pending_cmd_ = A2DP_CTRL_CMD_NONE;
262   // TODO: Notify server
263 }
264 
ack_stream_suspended(const tA2DP_CTRL_ACK & ack)265 void ack_stream_suspended(const tA2DP_CTRL_ACK& ack) {
266   a2dp_pending_cmd_ = A2DP_CTRL_CMD_NONE;
267   // TODO: Notify server
268 }
269 
270 // Read from the FMQ of BluetoothAudio HAL
read(uint8_t * p_buf,uint32_t len)271 size_t read(uint8_t* p_buf, uint32_t len) {
272   uint32_t bytes_read = 0;
273   if (a2dp_uipc == nullptr) {
274     return 0;
275   }
276   bytes_read = UIPC_Read(*a2dp_uipc, UIPC_CH_ID_AV_AUDIO, p_buf, len);
277   total_bytes_read_ += bytes_read;
278   // MONOTONIC_RAW isn't affected by NTP, audio stack rely on this
279   // to get precise delay calculation.
280   clock_gettime(CLOCK_MONOTONIC_RAW, &data_position_);
281   return bytes_read;
282 }
283 
284 // Check if OPUS codec is supported
is_opus_supported()285 bool is_opus_supported() { return true; }
286 
287 }  // namespace a2dp
288 }  // namespace audio
289 }  // namespace bluetooth
290