1 /*
2  * Copyright 2019 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 #define LOG_TAG "BTAudioA2dpHIDL"
17 
18 #include "a2dp_encoding_hidl.h"
19 
20 #include <bluetooth/log.h>
21 
22 #include <vector>
23 
24 #include "a2dp_sbc_constants.h"
25 #include "btif/include/btif_a2dp_source.h"
26 #include "btif/include/btif_av.h"
27 #include "btif/include/btif_av_co.h"
28 #include "btif/include/btif_hf.h"
29 #include "client_interface_hidl.h"
30 #include "codec_status_hidl.h"
31 #include "osi/include/properties.h"
32 #include "types/raw_address.h"
33 
34 namespace fmt {
35 template <>
36 struct formatter<tA2DP_CTRL_CMD> : enum_formatter<tA2DP_CTRL_CMD> {};
37 template <>
38 struct formatter<audio_usage_t> : enum_formatter<audio_usage_t> {};
39 template <>
40 struct formatter<audio_content_type_t> : enum_formatter<audio_content_type_t> {
41 };
42 }  // namespace fmt
43 
44 namespace {
45 
46 using ::bluetooth::audio::hidl::AudioCapabilities;
47 using ::bluetooth::audio::hidl::AudioConfiguration;
48 using ::bluetooth::audio::hidl::BitsPerSample;
49 using ::bluetooth::audio::hidl::BluetoothAudioCtrlAck;
50 using ::bluetooth::audio::hidl::ChannelMode;
51 using ::bluetooth::audio::hidl::PcmParameters;
52 using ::bluetooth::audio::hidl::SampleRate;
53 using ::bluetooth::audio::hidl::SessionType;
54 
55 using ::bluetooth::audio::hidl::BluetoothAudioSinkClientInterface;
56 using ::bluetooth::audio::hidl::codec::A2dpAacToHalConfig;
57 using ::bluetooth::audio::hidl::codec::A2dpAptxToHalConfig;
58 using ::bluetooth::audio::hidl::codec::A2dpCodecToHalBitsPerSample;
59 using ::bluetooth::audio::hidl::codec::A2dpCodecToHalChannelMode;
60 using ::bluetooth::audio::hidl::codec::A2dpCodecToHalSampleRate;
61 using ::bluetooth::audio::hidl::codec::A2dpLdacToHalConfig;
62 using ::bluetooth::audio::hidl::codec::A2dpSbcToHalConfig;
63 using ::bluetooth::audio::hidl::codec::CodecConfiguration;
64 
65 using namespace bluetooth;
66 
67 BluetoothAudioCtrlAck a2dp_ack_to_bt_audio_ctrl_ack(tA2DP_CTRL_ACK ack);
68 
69 // Provide call-in APIs for the Bluetooth Audio HAL
70 class A2dpTransport
71     : public ::bluetooth::audio::hidl::IBluetoothSinkTransportInstance {
72  public:
A2dpTransport(SessionType sessionType)73   A2dpTransport(SessionType sessionType)
74       : IBluetoothSinkTransportInstance(sessionType, (AudioConfiguration){}),
75         total_bytes_read_(0),
76         data_position_({}) {
77     a2dp_pending_cmd_ = A2DP_CTRL_CMD_NONE;
78     remote_delay_report_ = 0;
79   }
80 
StartRequest()81   BluetoothAudioCtrlAck StartRequest() override {
82     // Check if a previous request is not finished
83     if (a2dp_pending_cmd_ == A2DP_CTRL_CMD_START) {
84       log::info("A2DP_CTRL_CMD_START in progress");
85       return a2dp_ack_to_bt_audio_ctrl_ack(A2DP_CTRL_ACK_PENDING);
86     } else if (a2dp_pending_cmd_ != A2DP_CTRL_CMD_NONE) {
87       log::warn("busy in pending_cmd={}", a2dp_pending_cmd_);
88       return a2dp_ack_to_bt_audio_ctrl_ack(A2DP_CTRL_ACK_FAILURE);
89     }
90 
91     // Don't send START request to stack while we are in a call
92     if (!bluetooth::headset::IsCallIdle()) {
93       log::error("call state is busy");
94       return a2dp_ack_to_bt_audio_ctrl_ack(A2DP_CTRL_ACK_INCALL_FAILURE);
95     }
96 
97     if (btif_av_stream_started_ready(A2dpType::kSource)) {
98       // Already started, ACK back immediately.
99       return a2dp_ack_to_bt_audio_ctrl_ack(A2DP_CTRL_ACK_SUCCESS);
100     }
101     if (btif_av_stream_ready(A2dpType::kSource)) {
102       /*
103        * Post start event and wait for audio path to open.
104        * If we are the source, the ACK will be sent after the start
105        * procedure is completed, othewise send it now.
106        */
107       a2dp_pending_cmd_ = A2DP_CTRL_CMD_START;
108       btif_av_stream_start(A2dpType::kSource);
109       if (btif_av_get_peer_sep(A2dpType::kSource) != AVDT_TSEP_SRC) {
110         log::info("accepted");
111         return a2dp_ack_to_bt_audio_ctrl_ack(A2DP_CTRL_ACK_PENDING);
112       }
113       a2dp_pending_cmd_ = A2DP_CTRL_CMD_NONE;
114       return a2dp_ack_to_bt_audio_ctrl_ack(A2DP_CTRL_ACK_SUCCESS);
115     }
116     log::error("AV stream is not ready to start");
117     return a2dp_ack_to_bt_audio_ctrl_ack(A2DP_CTRL_ACK_FAILURE);
118   }
119 
SuspendRequest()120   BluetoothAudioCtrlAck SuspendRequest() override {
121     // Previous request is not finished
122     if (a2dp_pending_cmd_ == A2DP_CTRL_CMD_SUSPEND) {
123       log::info("A2DP_CTRL_CMD_SUSPEND in progress");
124       return a2dp_ack_to_bt_audio_ctrl_ack(A2DP_CTRL_ACK_PENDING);
125     } else if (a2dp_pending_cmd_ != A2DP_CTRL_CMD_NONE) {
126       log::warn("busy in pending_cmd={}", a2dp_pending_cmd_);
127       return a2dp_ack_to_bt_audio_ctrl_ack(A2DP_CTRL_ACK_FAILURE);
128     }
129     // Local suspend
130     if (btif_av_stream_started_ready(A2dpType::kSource)) {
131       log::info("accepted");
132       a2dp_pending_cmd_ = A2DP_CTRL_CMD_SUSPEND;
133       btif_av_stream_suspend();
134       return BluetoothAudioCtrlAck::PENDING;
135     }
136     /* If we are not in started state, just ack back ok and let
137      * audioflinger close the channel. This can happen if we are
138      * remotely suspended, clear REMOTE SUSPEND flag.
139      */
140     btif_av_clear_remote_suspend_flag(A2dpType::kSource);
141     return a2dp_ack_to_bt_audio_ctrl_ack(A2DP_CTRL_ACK_SUCCESS);
142   }
143 
StopRequest()144   void StopRequest() override {
145     if (btif_av_get_peer_sep(A2dpType::kSource) == AVDT_TSEP_SNK &&
146         !btif_av_stream_started_ready(A2dpType::kSource)) {
147       btif_av_clear_remote_suspend_flag(A2dpType::kSource);
148       return;
149     }
150     log::info("handling");
151     a2dp_pending_cmd_ = A2DP_CTRL_CMD_STOP;
152     btif_av_stream_stop(RawAddress::kEmpty);
153   }
154 
GetPresentationPosition(uint64_t * remote_delay_report_ns,uint64_t * total_bytes_read,timespec * data_position)155   bool GetPresentationPosition(uint64_t* remote_delay_report_ns,
156                                uint64_t* total_bytes_read,
157                                timespec* data_position) override {
158     *remote_delay_report_ns = remote_delay_report_ * 100000u;
159     *total_bytes_read = total_bytes_read_;
160     *data_position = data_position_;
161     log::verbose("delay={}/10ms, data={} byte(s), timestamp={}.{}s",
162                  remote_delay_report_, total_bytes_read_, data_position_.tv_sec,
163                  data_position_.tv_nsec);
164     return true;
165   }
166 
MetadataChanged(const source_metadata_t & source_metadata)167   void MetadataChanged(const source_metadata_t& source_metadata) override {
168     auto track_count = source_metadata.track_count;
169     auto tracks = source_metadata.tracks;
170     log::verbose("{} track(s) received", track_count);
171     while (track_count) {
172       log::verbose("usage={}, content_type={}, gain={}", tracks->usage,
173                    tracks->content_type, tracks->gain);
174       --track_count;
175       ++tracks;
176     }
177   }
178 
GetPendingCmd() const179   tA2DP_CTRL_CMD GetPendingCmd() const { return a2dp_pending_cmd_; }
180 
ResetPendingCmd()181   void ResetPendingCmd() { a2dp_pending_cmd_ = A2DP_CTRL_CMD_NONE; }
182 
ResetPresentationPosition()183   void ResetPresentationPosition() override {
184     remote_delay_report_ = 0;
185     total_bytes_read_ = 0;
186     data_position_ = {};
187   }
188 
LogBytesRead(size_t bytes_read)189   void LogBytesRead(size_t bytes_read) override {
190     if (bytes_read != 0) {
191       total_bytes_read_ += bytes_read;
192       clock_gettime(CLOCK_MONOTONIC, &data_position_);
193     }
194   }
195 
196   // delay reports from AVDTP is based on 1/10 ms (100us)
SetRemoteDelay(uint16_t delay_report)197   void SetRemoteDelay(uint16_t delay_report) {
198     remote_delay_report_ = delay_report;
199   }
200 
201  private:
202   static tA2DP_CTRL_CMD a2dp_pending_cmd_;
203   static uint16_t remote_delay_report_;
204   uint64_t total_bytes_read_;
205   timespec data_position_;
206 };
207 
208 tA2DP_CTRL_CMD A2dpTransport::a2dp_pending_cmd_ = A2DP_CTRL_CMD_NONE;
209 uint16_t A2dpTransport::remote_delay_report_ = 0;
210 
211 // Common interface to call-out into Bluetooth Audio HAL
212 BluetoothAudioSinkClientInterface* software_hal_interface = nullptr;
213 BluetoothAudioSinkClientInterface* offloading_hal_interface = nullptr;
214 BluetoothAudioSinkClientInterface* active_hal_interface = nullptr;
215 
216 // Save the value if the remote reports its delay before this interface is
217 // initialized
218 uint16_t remote_delay = 0;
219 
220 bool btaudio_a2dp_disabled = false;
221 bool is_configured = false;
222 
a2dp_ack_to_bt_audio_ctrl_ack(tA2DP_CTRL_ACK ack)223 BluetoothAudioCtrlAck a2dp_ack_to_bt_audio_ctrl_ack(tA2DP_CTRL_ACK ack) {
224   switch (ack) {
225     case A2DP_CTRL_ACK_SUCCESS:
226       return BluetoothAudioCtrlAck::SUCCESS_FINISHED;
227     case A2DP_CTRL_ACK_PENDING:
228       return BluetoothAudioCtrlAck::PENDING;
229     case A2DP_CTRL_ACK_INCALL_FAILURE:
230       return BluetoothAudioCtrlAck::FAILURE_BUSY;
231     case A2DP_CTRL_ACK_DISCONNECT_IN_PROGRESS:
232       return BluetoothAudioCtrlAck::FAILURE_DISCONNECTING;
233     case A2DP_CTRL_ACK_UNSUPPORTED: /* Offloading but resource failure */
234       return BluetoothAudioCtrlAck::FAILURE_UNSUPPORTED;
235     case A2DP_CTRL_ACK_FAILURE:
236       return BluetoothAudioCtrlAck::FAILURE;
237     default:
238       return BluetoothAudioCtrlAck::FAILURE;
239   }
240 }
241 
a2dp_get_selected_hal_codec_config(CodecConfiguration * codec_config)242 bool a2dp_get_selected_hal_codec_config(CodecConfiguration* codec_config) {
243   A2dpCodecConfig* a2dp_config = bta_av_get_a2dp_current_codec();
244   if (a2dp_config == nullptr) {
245     log::warn("failure to get A2DP codec config");
246     *codec_config = ::bluetooth::audio::hidl::codec::kInvalidCodecConfiguration;
247     return false;
248   }
249   btav_a2dp_codec_config_t current_codec = a2dp_config->getCodecConfig();
250   switch (current_codec.codec_type) {
251     case BTAV_A2DP_CODEC_INDEX_SOURCE_SBC:
252       [[fallthrough]];
253     case BTAV_A2DP_CODEC_INDEX_SINK_SBC: {
254       if (!A2dpSbcToHalConfig(codec_config, a2dp_config)) {
255         return false;
256       }
257       break;
258     }
259     case BTAV_A2DP_CODEC_INDEX_SOURCE_AAC:
260       [[fallthrough]];
261     case BTAV_A2DP_CODEC_INDEX_SINK_AAC: {
262       if (!A2dpAacToHalConfig(codec_config, a2dp_config)) {
263         return false;
264       }
265       break;
266     }
267     case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX:
268       [[fallthrough]];
269     case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD: {
270       if (!A2dpAptxToHalConfig(codec_config, a2dp_config)) {
271         return false;
272       }
273       break;
274     }
275     case BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC: {
276       if (!A2dpLdacToHalConfig(codec_config, a2dp_config)) {
277         return false;
278       }
279       break;
280     }
281     case BTAV_A2DP_CODEC_INDEX_MAX:
282       [[fallthrough]];
283     default:
284       log::error("Unknown codec_type={}", current_codec.codec_type);
285       *codec_config =
286           ::bluetooth::audio::hidl::codec::kInvalidCodecConfiguration;
287       return false;
288   }
289   codec_config->encodedAudioBitrate = a2dp_config->getTrackBitRate();
290   // Obtain the MTU
291   RawAddress peer_addr = btif_av_source_active_peer();
292   tA2DP_ENCODER_INIT_PEER_PARAMS peer_param;
293   bta_av_co_get_peer_params(peer_addr, &peer_param);
294   int effectiveMtu = bta_av_co_get_encoder_effective_frame_size();
295   if (effectiveMtu > 0 && effectiveMtu < peer_param.peer_mtu) {
296     codec_config->peerMtu = effectiveMtu;
297   } else {
298     codec_config->peerMtu = peer_param.peer_mtu;
299   }
300   if (current_codec.codec_type == BTAV_A2DP_CODEC_INDEX_SOURCE_SBC &&
301       codec_config->config.sbcConfig().maxBitpool <=
302           A2DP_SBC_BITPOOL_MIDDLE_QUALITY) {
303     codec_config->peerMtu = MAX_2MBPS_AVDTP_MTU;
304   } else if (codec_config->peerMtu > MAX_3MBPS_AVDTP_MTU) {
305     codec_config->peerMtu = MAX_3MBPS_AVDTP_MTU;
306   }
307   log::info("CodecConfiguration={}", toString(*codec_config));
308   return true;
309 }
310 
a2dp_get_selected_hal_pcm_config(PcmParameters * pcm_config)311 bool a2dp_get_selected_hal_pcm_config(PcmParameters* pcm_config) {
312   if (pcm_config == nullptr) return false;
313   A2dpCodecConfig* a2dp_codec_configs = bta_av_get_a2dp_current_codec();
314   if (a2dp_codec_configs == nullptr) {
315     log::warn("failure to get A2DP codec config");
316     *pcm_config = BluetoothAudioSinkClientInterface::kInvalidPcmConfiguration;
317     return false;
318   }
319 
320   btav_a2dp_codec_config_t current_codec = a2dp_codec_configs->getCodecConfig();
321   pcm_config->sampleRate = A2dpCodecToHalSampleRate(current_codec);
322   pcm_config->bitsPerSample = A2dpCodecToHalBitsPerSample(current_codec);
323   pcm_config->channelMode = A2dpCodecToHalChannelMode(current_codec);
324   return (pcm_config->sampleRate != SampleRate::RATE_UNKNOWN &&
325           pcm_config->bitsPerSample != BitsPerSample::BITS_UNKNOWN &&
326           pcm_config->channelMode != ChannelMode::UNKNOWN);
327 }
328 
329 // Checking if new bluetooth_audio is supported
is_hal_2_0_force_disabled()330 bool is_hal_2_0_force_disabled() {
331   if (!is_configured) {
332     btaudio_a2dp_disabled =
333         osi_property_get_bool(BLUETOOTH_AUDIO_HAL_PROP_DISABLED, false);
334     is_configured = true;
335   }
336   return btaudio_a2dp_disabled;
337 }
338 }  // namespace
339 
340 namespace bluetooth {
341 namespace audio {
342 namespace hidl {
343 namespace a2dp {
344 
update_codec_offloading_capabilities(const std::vector<btav_a2dp_codec_config_t> & framework_preference)345 bool update_codec_offloading_capabilities(
346     const std::vector<btav_a2dp_codec_config_t>& framework_preference) {
347   return ::bluetooth::audio::hidl::codec::UpdateOffloadingCapabilities(
348       framework_preference);
349 }
350 
351 // Checking if new bluetooth_audio is enabled
is_hal_2_0_enabled()352 bool is_hal_2_0_enabled() { return active_hal_interface != nullptr; }
353 
354 // Check if new bluetooth_audio is running with offloading encoders
is_hal_2_0_offloading()355 bool is_hal_2_0_offloading() {
356   if (!is_hal_2_0_enabled()) {
357     return false;
358   }
359   return active_hal_interface->GetTransportInstance()->GetSessionType() ==
360          SessionType::A2DP_HARDWARE_OFFLOAD_DATAPATH;
361 }
362 
363 // Initialize BluetoothAudio HAL: openProvider
init(bluetooth::common::MessageLoopThread * message_loop)364 bool init(bluetooth::common::MessageLoopThread* message_loop) {
365   log::info("");
366 
367   if (is_hal_2_0_force_disabled()) {
368     log::error("BluetoothAudio HAL is disabled");
369     return false;
370   }
371 
372   auto a2dp_sink =
373       new A2dpTransport(SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH);
374   software_hal_interface =
375       new BluetoothAudioSinkClientInterface(a2dp_sink, message_loop);
376   if (!software_hal_interface->IsValid()) {
377     log::warn("BluetoothAudio HAL for A2DP is invalid?!");
378     delete software_hal_interface;
379     software_hal_interface = nullptr;
380     delete a2dp_sink;
381     return false;
382   }
383 
384   if (btif_av_is_a2dp_offload_enabled()) {
385     a2dp_sink = new A2dpTransport(SessionType::A2DP_HARDWARE_OFFLOAD_DATAPATH);
386     offloading_hal_interface =
387         new BluetoothAudioSinkClientInterface(a2dp_sink, message_loop);
388     if (!offloading_hal_interface->IsValid()) {
389       log::fatal("BluetoothAudio HAL for A2DP offloading is invalid?!");
390       delete offloading_hal_interface;
391       offloading_hal_interface = nullptr;
392       delete a2dp_sink;
393       a2dp_sink = static_cast<A2dpTransport*>(
394           software_hal_interface->GetTransportInstance());
395       delete software_hal_interface;
396       software_hal_interface = nullptr;
397       delete a2dp_sink;
398       return false;
399     }
400   }
401 
402   active_hal_interface =
403       (offloading_hal_interface != nullptr ? offloading_hal_interface
404                                            : software_hal_interface);
405 
406   if (remote_delay != 0) {
407     log::info("restore DELAY {} ms", static_cast<float>(remote_delay / 10.0));
408     static_cast<A2dpTransport*>(active_hal_interface->GetTransportInstance())
409         ->SetRemoteDelay(remote_delay);
410     remote_delay = 0;
411   }
412   return true;
413 }
414 
415 // Clean up BluetoothAudio HAL
cleanup()416 void cleanup() {
417   if (!is_hal_2_0_enabled()) return;
418   end_session();
419 
420   auto a2dp_sink = active_hal_interface->GetTransportInstance();
421   static_cast<A2dpTransport*>(a2dp_sink)->ResetPendingCmd();
422   static_cast<A2dpTransport*>(a2dp_sink)->ResetPresentationPosition();
423   active_hal_interface = nullptr;
424 
425   a2dp_sink = software_hal_interface->GetTransportInstance();
426   delete software_hal_interface;
427   software_hal_interface = nullptr;
428   delete a2dp_sink;
429   if (offloading_hal_interface != nullptr) {
430     a2dp_sink = offloading_hal_interface->GetTransportInstance();
431     delete offloading_hal_interface;
432     offloading_hal_interface = nullptr;
433     delete a2dp_sink;
434   }
435 
436   remote_delay = 0;
437 }
438 
439 // Set up the codec into BluetoothAudio HAL
setup_codec()440 bool setup_codec() {
441   if (!is_hal_2_0_enabled()) {
442     log::error("BluetoothAudio HAL is not enabled");
443     return false;
444   }
445   CodecConfiguration codec_config{};
446   if (!a2dp_get_selected_hal_codec_config(&codec_config)) {
447     log::error("Failed to get CodecConfiguration");
448     return false;
449   }
450   bool should_codec_offloading =
451       bluetooth::audio::hidl::codec::IsCodecOffloadingEnabled(codec_config);
452   if (should_codec_offloading && !is_hal_2_0_offloading()) {
453     log::warn("Switching BluetoothAudio HAL to Hardware");
454     end_session();
455     active_hal_interface = offloading_hal_interface;
456   } else if (!should_codec_offloading && is_hal_2_0_offloading()) {
457     log::warn("Switching BluetoothAudio HAL to Software");
458     end_session();
459     active_hal_interface = software_hal_interface;
460   }
461 
462   AudioConfiguration audio_config{};
463   if (active_hal_interface->GetTransportInstance()->GetSessionType() ==
464       SessionType::A2DP_HARDWARE_OFFLOAD_DATAPATH) {
465     audio_config.codecConfig(codec_config);
466   } else {
467     PcmParameters pcm_config{};
468     if (!a2dp_get_selected_hal_pcm_config(&pcm_config)) {
469       log::error("Failed to get PcmConfiguration");
470       return false;
471     }
472     audio_config.pcmConfig(pcm_config);
473   }
474   return active_hal_interface->UpdateAudioConfig(audio_config);
475 }
476 
start_session()477 void start_session() {
478   if (!is_hal_2_0_enabled()) {
479     log::error("BluetoothAudio HAL is not enabled");
480     return;
481   }
482   active_hal_interface->StartSession();
483 }
484 
end_session()485 void end_session() {
486   if (!is_hal_2_0_enabled()) {
487     log::error("BluetoothAudio HAL is not enabled");
488     return;
489   }
490   active_hal_interface->EndSession();
491   static_cast<A2dpTransport*>(active_hal_interface->GetTransportInstance())
492       ->ResetPendingCmd();
493   static_cast<A2dpTransport*>(active_hal_interface->GetTransportInstance())
494       ->ResetPresentationPosition();
495 }
496 
ack_stream_started(const tA2DP_CTRL_ACK & ack)497 void ack_stream_started(const tA2DP_CTRL_ACK& ack) {
498   auto ctrl_ack = a2dp_ack_to_bt_audio_ctrl_ack(ack);
499   log::info("result={}", ctrl_ack);
500   auto a2dp_sink =
501       static_cast<A2dpTransport*>(active_hal_interface->GetTransportInstance());
502   auto pending_cmd = a2dp_sink->GetPendingCmd();
503   if (pending_cmd == A2DP_CTRL_CMD_START) {
504     active_hal_interface->StreamStarted(ctrl_ack);
505   } else {
506     log::warn("pending={} ignore result={}", pending_cmd, ctrl_ack);
507     return;
508   }
509   if (ctrl_ack != bluetooth::audio::hidl::BluetoothAudioCtrlAck::PENDING) {
510     a2dp_sink->ResetPendingCmd();
511   }
512 }
513 
ack_stream_suspended(const tA2DP_CTRL_ACK & ack)514 void ack_stream_suspended(const tA2DP_CTRL_ACK& ack) {
515   auto ctrl_ack = a2dp_ack_to_bt_audio_ctrl_ack(ack);
516   log::info("result={}", ctrl_ack);
517   auto a2dp_sink =
518       static_cast<A2dpTransport*>(active_hal_interface->GetTransportInstance());
519   auto pending_cmd = a2dp_sink->GetPendingCmd();
520   if (pending_cmd == A2DP_CTRL_CMD_SUSPEND) {
521     active_hal_interface->StreamSuspended(ctrl_ack);
522   } else if (pending_cmd == A2DP_CTRL_CMD_STOP) {
523     log::info("A2DP_CTRL_CMD_STOP result={}", ctrl_ack);
524   } else {
525     log::warn("pending={} ignore result={}", pending_cmd, ctrl_ack);
526     return;
527   }
528   if (ctrl_ack != bluetooth::audio::hidl::BluetoothAudioCtrlAck::PENDING) {
529     a2dp_sink->ResetPendingCmd();
530   }
531 }
532 
533 // Read from the FMQ of BluetoothAudio HAL
read(uint8_t * p_buf,uint32_t len)534 size_t read(uint8_t* p_buf, uint32_t len) {
535   if (!is_hal_2_0_enabled()) {
536     log::error("BluetoothAudio HAL is not enabled");
537     return 0;
538   } else if (is_hal_2_0_offloading()) {
539     log::error(
540         "session_type={} is not A2DP_SOFTWARE_ENCODING_DATAPATH",
541         toString(
542             active_hal_interface->GetTransportInstance()->GetSessionType()));
543     return 0;
544   }
545   return active_hal_interface->ReadAudioData(p_buf, len);
546 }
547 
548 // Update A2DP delay report to BluetoothAudio HAL
set_remote_delay(uint16_t delay_report)549 void set_remote_delay(uint16_t delay_report) {
550   if (!is_hal_2_0_enabled()) {
551     log::info("not ready for DelayReport {} ms",
552               static_cast<float>(delay_report / 10.0));
553     remote_delay = delay_report;
554     return;
555   }
556   log::verbose("DELAY {} ms", static_cast<float>(delay_report / 10.0));
557   static_cast<A2dpTransport*>(active_hal_interface->GetTransportInstance())
558       ->SetRemoteDelay(delay_report);
559 }
560 
561 }  // namespace a2dp
562 }  // namespace hidl
563 }  // namespace audio
564 }  // namespace bluetooth
565