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