a2dp_encoding.cc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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. #include "a2dp_encoding.h"
  17. #include "client_interface.h"
  18. #include "a2dp_aac_constants.h"
  19. #include "a2dp_sbc_constants.h"
  20. #include "a2dp_vendor_ldac_constants.h"
  21. #include "bta/av/bta_av_int.h"
  22. #include "btif_a2dp_source.h"
  23. #include "btif_av.h"
  24. #include "btif_av_co.h"
  25. #include "btif_hf.h"
  26. #include "osi/include/properties.h"
  27. namespace {
  28. using ::android::hardware::bluetooth::audio::V2_0::AacObjectType;
  29. using ::android::hardware::bluetooth::audio::V2_0::AacVariableBitRate;
  30. using ::android::hardware::bluetooth::audio::V2_0::CodecType;
  31. using ::android::hardware::bluetooth::audio::V2_0::LdacChannelMode;
  32. using ::android::hardware::bluetooth::audio::V2_0::LdacQualityIndex;
  33. using ::android::hardware::bluetooth::audio::V2_0::SbcAllocMethod;
  34. using ::android::hardware::bluetooth::audio::V2_0::SbcBlockLength;
  35. using ::android::hardware::bluetooth::audio::V2_0::SbcChannelMode;
  36. using ::android::hardware::bluetooth::audio::V2_0::SbcNumSubbands;
  37. using ::bluetooth::audio::AudioConfiguration;
  38. using ::bluetooth::audio::BitsPerSample;
  39. using ::bluetooth::audio::BluetoothAudioCtrlAck;
  40. using ::bluetooth::audio::ChannelMode;
  41. using ::bluetooth::audio::CodecConfiguration;
  42. using ::bluetooth::audio::PcmParameters;
  43. using ::bluetooth::audio::SampleRate;
  44. using ::bluetooth::audio::SessionType;
  45. const CodecConfiguration kInvalidCodecConfiguration = {
  46. .codecType = CodecType::UNKNOWN,
  47. .encodedAudioBitrate = 0x00000000,
  48. .peerMtu = 0xffff,
  49. .isScmstEnabled = false,
  50. .config = {}};
  51. BluetoothAudioCtrlAck a2dp_ack_to_bt_audio_ctrl_ack(tA2DP_CTRL_ACK ack);
  52. // Provide call-in APIs for the Bluetooth Audio HAL
  53. class A2dpTransport : public ::bluetooth::audio::IBluetoothTransportInstance {
  54. public:
  55. A2dpTransport(SessionType sessionType)
  56. : IBluetoothTransportInstance(sessionType, {}),
  57. a2dp_pending_cmd_(A2DP_CTRL_CMD_NONE),
  58. remote_delay_report_(0),
  59. total_bytes_read_(0),
  60. data_position_({}){};
  61. BluetoothAudioCtrlAck StartRequest() override {
  62. // Check if a previous request is not finished
  63. if (a2dp_pending_cmd_ == A2DP_CTRL_CMD_START) {
  64. LOG(INFO) << __func__ << ": A2DP_CTRL_CMD_START in progress";
  65. return a2dp_ack_to_bt_audio_ctrl_ack(A2DP_CTRL_ACK_PENDING);
  66. } else if (a2dp_pending_cmd_ != A2DP_CTRL_CMD_NONE) {
  67. LOG(WARNING) << __func__ << ": busy in pending_cmd=" << a2dp_pending_cmd_;
  68. return a2dp_ack_to_bt_audio_ctrl_ack(A2DP_CTRL_ACK_FAILURE);
  69. }
  70. // Don't send START request to stack while we are in a call
  71. if (!bluetooth::headset::IsCallIdle()) {
  72. LOG(ERROR) << __func__ << ": call state is busy";
  73. return a2dp_ack_to_bt_audio_ctrl_ack(A2DP_CTRL_ACK_INCALL_FAILURE);
  74. }
  75. if (btif_a2dp_source_is_streaming()) {
  76. LOG(ERROR) << __func__ << ": source is busy streaming";
  77. return a2dp_ack_to_bt_audio_ctrl_ack(A2DP_CTRL_ACK_FAILURE);
  78. }
  79. if (btif_av_stream_ready()) {
  80. /*
  81. * Post start event and wait for audio path to open.
  82. * If we are the source, the ACK will be sent after the start
  83. * procedure is completed, othewise send it now.
  84. */
  85. a2dp_pending_cmd_ = A2DP_CTRL_CMD_START;
  86. btif_av_stream_start();
  87. if (btif_av_get_peer_sep() != AVDT_TSEP_SRC) {
  88. LOG(INFO) << __func__ << ": accepted";
  89. return a2dp_ack_to_bt_audio_ctrl_ack(A2DP_CTRL_ACK_PENDING);
  90. }
  91. a2dp_pending_cmd_ = A2DP_CTRL_CMD_NONE;
  92. return a2dp_ack_to_bt_audio_ctrl_ack(A2DP_CTRL_ACK_SUCCESS);
  93. }
  94. if (btif_av_stream_started_ready()) {
  95. // Already started, ACK back immediately.
  96. return a2dp_ack_to_bt_audio_ctrl_ack(A2DP_CTRL_ACK_SUCCESS);
  97. }
  98. LOG(ERROR) << __func__ << ": AV stream is not ready to start";
  99. return a2dp_ack_to_bt_audio_ctrl_ack(A2DP_CTRL_ACK_FAILURE);
  100. }
  101. BluetoothAudioCtrlAck SuspendRequest() override {
  102. // Previous request is not finished
  103. if (a2dp_pending_cmd_ == A2DP_CTRL_CMD_SUSPEND) {
  104. LOG(INFO) << __func__ << ": A2DP_CTRL_CMD_SUSPEND in progress";
  105. return a2dp_ack_to_bt_audio_ctrl_ack(A2DP_CTRL_ACK_PENDING);
  106. } else if (a2dp_pending_cmd_ != A2DP_CTRL_CMD_NONE) {
  107. LOG(WARNING) << __func__ << ": busy in pending_cmd=" << a2dp_pending_cmd_;
  108. return a2dp_ack_to_bt_audio_ctrl_ack(A2DP_CTRL_ACK_FAILURE);
  109. }
  110. // Local suspend
  111. if (btif_av_stream_started_ready()) {
  112. LOG(INFO) << __func__ << ": accepted";
  113. a2dp_pending_cmd_ = A2DP_CTRL_CMD_SUSPEND;
  114. btif_av_stream_suspend();
  115. return BluetoothAudioCtrlAck::PENDING;
  116. }
  117. /* If we are not in started state, just ack back ok and let
  118. * audioflinger close the channel. This can happen if we are
  119. * remotely suspended, clear REMOTE SUSPEND flag.
  120. */
  121. btif_av_clear_remote_suspend_flag();
  122. return a2dp_ack_to_bt_audio_ctrl_ack(A2DP_CTRL_ACK_SUCCESS);
  123. }
  124. void StopRequest() override {
  125. if (btif_av_get_peer_sep() == AVDT_TSEP_SNK &&
  126. !btif_a2dp_source_is_streaming()) {
  127. return;
  128. }
  129. LOG(INFO) << __func__ << ": handling";
  130. a2dp_pending_cmd_ = A2DP_CTRL_CMD_STOP;
  131. btif_av_stream_stop(RawAddress::kEmpty);
  132. }
  133. bool GetPresentationPosition(uint64_t* remote_delay_report_ns,
  134. uint64_t* total_bytes_read,
  135. timespec* data_position) override {
  136. *remote_delay_report_ns = remote_delay_report_ * 100000u;
  137. *total_bytes_read = total_bytes_read_;
  138. *data_position = data_position_;
  139. VLOG(2) << __func__ << ": delay=" << remote_delay_report_
  140. << "/10ms, data=" << total_bytes_read_
  141. << " byte(s), timestamp=" << data_position_.tv_sec << "."
  142. << data_position_.tv_nsec << "s";
  143. return true;
  144. }
  145. void MetadataChanged(const source_metadata_t& source_metadata) override {
  146. auto track_count = source_metadata.track_count;
  147. auto tracks = source_metadata.tracks;
  148. VLOG(1) << __func__ << ": " << track_count << " track(s) received";
  149. while (track_count) {
  150. VLOG(2) << __func__ << ": usage=" << tracks->usage
  151. << ", content_type=" << tracks->content_type
  152. << ", gain=" << tracks->gain;
  153. --track_count;
  154. ++tracks;
  155. }
  156. }
  157. tA2DP_CTRL_CMD GetPendingCmd() const { return a2dp_pending_cmd_; }
  158. void ResetPendingCmd() { a2dp_pending_cmd_ = A2DP_CTRL_CMD_NONE; }
  159. void ResetPresentationPosition() override {
  160. remote_delay_report_ = 0;
  161. total_bytes_read_ = 0;
  162. data_position_ = {};
  163. }
  164. void LogBytesRead(size_t bytes_read) override {
  165. if (bytes_read != 0) {
  166. total_bytes_read_ += bytes_read;
  167. clock_gettime(CLOCK_MONOTONIC, &data_position_);
  168. }
  169. }
  170. // delay reports from AVDTP is based on 1/10 ms (100us)
  171. void SetRemoteDelay(uint16_t delay_report) {
  172. remote_delay_report_ = delay_report;
  173. }
  174. private:
  175. tA2DP_CTRL_CMD a2dp_pending_cmd_;
  176. uint16_t remote_delay_report_;
  177. uint64_t total_bytes_read_;
  178. timespec data_position_;
  179. };
  180. A2dpTransport* a2dp_sink = nullptr;
  181. // Common interface to call-out into Bluetooth Audio HAL
  182. bluetooth::audio::BluetoothAudioClientInterface* a2dp_hal_clientif = nullptr;
  183. auto session_type = SessionType::UNKNOWN;
  184. // Save the value if the remote reports its delay before a2dp_sink is
  185. // initialized
  186. uint16_t remote_delay = 0;
  187. bool btaudio_a2dp_disabled = false;
  188. bool is_configured = false;
  189. BluetoothAudioCtrlAck a2dp_ack_to_bt_audio_ctrl_ack(tA2DP_CTRL_ACK ack) {
  190. switch (ack) {
  191. case A2DP_CTRL_ACK_SUCCESS:
  192. return BluetoothAudioCtrlAck::SUCCESS_FINISHED;
  193. case A2DP_CTRL_ACK_PENDING:
  194. return BluetoothAudioCtrlAck::PENDING;
  195. case A2DP_CTRL_ACK_INCALL_FAILURE:
  196. return BluetoothAudioCtrlAck::FAILURE_BUSY;
  197. case A2DP_CTRL_ACK_DISCONNECT_IN_PROGRESS:
  198. return BluetoothAudioCtrlAck::FAILURE_DISCONNECTING;
  199. case A2DP_CTRL_ACK_UNSUPPORTED: /* Offloading but resource failure */
  200. return BluetoothAudioCtrlAck::FAILURE_UNSUPPORTED;
  201. case A2DP_CTRL_ACK_FAILURE:
  202. return BluetoothAudioCtrlAck::FAILURE;
  203. default:
  204. return BluetoothAudioCtrlAck::FAILURE;
  205. }
  206. }
  207. SampleRate a2dp_codec_to_hal_sample_rate(
  208. const btav_a2dp_codec_config_t& a2dp_codec_config) {
  209. switch (a2dp_codec_config.sample_rate) {
  210. case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
  211. return SampleRate::RATE_44100;
  212. case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
  213. return SampleRate::RATE_48000;
  214. case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
  215. return SampleRate::RATE_88200;
  216. case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
  217. return SampleRate::RATE_96000;
  218. case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
  219. return SampleRate::RATE_176400;
  220. case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
  221. return SampleRate::RATE_192000;
  222. case BTAV_A2DP_CODEC_SAMPLE_RATE_16000:
  223. return SampleRate::RATE_16000;
  224. case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
  225. return SampleRate::RATE_24000;
  226. default:
  227. return SampleRate::RATE_UNKNOWN;
  228. }
  229. }
  230. BitsPerSample a2dp_codec_to_hal_bits_per_sample(
  231. const btav_a2dp_codec_config_t& a2dp_codec_config) {
  232. switch (a2dp_codec_config.bits_per_sample) {
  233. case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
  234. return BitsPerSample::BITS_16;
  235. case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
  236. return BitsPerSample::BITS_24;
  237. case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
  238. return BitsPerSample::BITS_32;
  239. default:
  240. return BitsPerSample::BITS_UNKNOWN;
  241. }
  242. }
  243. ChannelMode a2dp_codec_to_hal_channel_mode(
  244. const btav_a2dp_codec_config_t& a2dp_codec_config) {
  245. switch (a2dp_codec_config.channel_mode) {
  246. case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
  247. return ChannelMode::MONO;
  248. case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
  249. return ChannelMode::STEREO;
  250. default:
  251. return ChannelMode::UNKNOWN;
  252. }
  253. }
  254. bool a2dp_get_selected_hal_codec_config(CodecConfiguration* codec_config) {
  255. A2dpCodecConfig* a2dp_codec_configs = bta_av_get_a2dp_current_codec();
  256. if (a2dp_codec_configs == nullptr) {
  257. LOG(WARNING) << __func__ << ": failure to get A2DP codec config";
  258. *codec_config = kInvalidCodecConfiguration;
  259. return false;
  260. }
  261. btav_a2dp_codec_config_t current_codec = a2dp_codec_configs->getCodecConfig();
  262. tBT_A2DP_OFFLOAD a2dp_offload;
  263. a2dp_codec_configs->getCodecSpecificConfig(&a2dp_offload);
  264. switch (current_codec.codec_type) {
  265. case BTAV_A2DP_CODEC_INDEX_SOURCE_SBC:
  266. [[fallthrough]];
  267. case BTAV_A2DP_CODEC_INDEX_SINK_SBC: {
  268. codec_config->codecType = CodecType::SBC;
  269. codec_config->config.sbcConfig({});
  270. auto sbc_config = codec_config->config.sbcConfig();
  271. sbc_config.sampleRate = a2dp_codec_to_hal_sample_rate(current_codec);
  272. if (sbc_config.sampleRate == SampleRate::RATE_UNKNOWN) {
  273. LOG(ERROR) << __func__
  274. << ": Unknown SBC sample_rate=" << current_codec.sample_rate;
  275. return false;
  276. }
  277. uint8_t channel_mode = a2dp_offload.codec_info[3] & A2DP_SBC_IE_CH_MD_MSK;
  278. switch (channel_mode) {
  279. case A2DP_SBC_IE_CH_MD_JOINT:
  280. sbc_config.channelMode = SbcChannelMode::JOINT_STEREO;
  281. break;
  282. case A2DP_SBC_IE_CH_MD_STEREO:
  283. sbc_config.channelMode = SbcChannelMode::STEREO;
  284. break;
  285. case A2DP_SBC_IE_CH_MD_DUAL:
  286. sbc_config.channelMode = SbcChannelMode::DUAL;
  287. break;
  288. case A2DP_SBC_IE_CH_MD_MONO:
  289. sbc_config.channelMode = SbcChannelMode::MONO;
  290. break;
  291. default:
  292. LOG(ERROR) << __func__
  293. << ": Unknown SBC channel_mode=" << channel_mode;
  294. sbc_config.channelMode = SbcChannelMode::UNKNOWN;
  295. return false;
  296. }
  297. uint8_t block_length =
  298. a2dp_offload.codec_info[0] & A2DP_SBC_IE_BLOCKS_MSK;
  299. switch (block_length) {
  300. case A2DP_SBC_IE_BLOCKS_4:
  301. sbc_config.blockLength = SbcBlockLength::BLOCKS_4;
  302. break;
  303. case A2DP_SBC_IE_BLOCKS_8:
  304. sbc_config.blockLength = SbcBlockLength::BLOCKS_8;
  305. break;
  306. case A2DP_SBC_IE_BLOCKS_12:
  307. sbc_config.blockLength = SbcBlockLength::BLOCKS_12;
  308. break;
  309. case A2DP_SBC_IE_BLOCKS_16:
  310. sbc_config.blockLength = SbcBlockLength::BLOCKS_16;
  311. break;
  312. default:
  313. LOG(ERROR) << __func__
  314. << ": Unknown SBC block_length=" << block_length;
  315. return false;
  316. }
  317. uint8_t sub_bands = a2dp_offload.codec_info[0] & A2DP_SBC_IE_SUBBAND_MSK;
  318. switch (sub_bands) {
  319. case A2DP_SBC_IE_SUBBAND_4:
  320. sbc_config.numSubbands = SbcNumSubbands::SUBBAND_4;
  321. break;
  322. case A2DP_SBC_IE_SUBBAND_8:
  323. sbc_config.numSubbands = SbcNumSubbands::SUBBAND_8;
  324. break;
  325. default:
  326. LOG(ERROR) << __func__ << ": Unknown SBC Subbands=" << sub_bands;
  327. return false;
  328. }
  329. uint8_t alloc_method =
  330. a2dp_offload.codec_info[0] & A2DP_SBC_IE_ALLOC_MD_MSK;
  331. switch (alloc_method) {
  332. case A2DP_SBC_IE_ALLOC_MD_S:
  333. sbc_config.allocMethod = SbcAllocMethod::ALLOC_MD_S;
  334. break;
  335. case A2DP_SBC_IE_ALLOC_MD_L:
  336. sbc_config.allocMethod = SbcAllocMethod::ALLOC_MD_L;
  337. break;
  338. default:
  339. LOG(ERROR) << __func__
  340. << ": Unknown SBC alloc_method=" << alloc_method;
  341. return false;
  342. }
  343. sbc_config.minBitpool = a2dp_offload.codec_info[1];
  344. sbc_config.maxBitpool = a2dp_offload.codec_info[2];
  345. sbc_config.bitsPerSample =
  346. a2dp_codec_to_hal_bits_per_sample(current_codec);
  347. if (sbc_config.bitsPerSample == BitsPerSample::BITS_UNKNOWN) {
  348. LOG(ERROR) << __func__ << ": Unknown SBC bits_per_sample="
  349. << current_codec.bits_per_sample;
  350. return false;
  351. }
  352. codec_config->config.sbcConfig(sbc_config);
  353. break;
  354. }
  355. case BTAV_A2DP_CODEC_INDEX_SOURCE_AAC:
  356. [[fallthrough]];
  357. case BTAV_A2DP_CODEC_INDEX_SINK_AAC: {
  358. codec_config->codecType = CodecType::AAC;
  359. codec_config->config.aacConfig({});
  360. auto aac_config = codec_config->config.aacConfig();
  361. uint8_t object_type = a2dp_offload.codec_info[0];
  362. switch (object_type) {
  363. case A2DP_AAC_OBJECT_TYPE_MPEG2_LC:
  364. aac_config.objectType = AacObjectType::MPEG2_LC;
  365. break;
  366. case A2DP_AAC_OBJECT_TYPE_MPEG4_LC:
  367. aac_config.objectType = AacObjectType::MPEG4_LC;
  368. break;
  369. case A2DP_AAC_OBJECT_TYPE_MPEG4_LTP:
  370. aac_config.objectType = AacObjectType::MPEG4_LTP;
  371. break;
  372. case A2DP_AAC_OBJECT_TYPE_MPEG4_SCALABLE:
  373. aac_config.objectType = AacObjectType::MPEG4_SCALABLE;
  374. break;
  375. default:
  376. LOG(ERROR) << __func__
  377. << ": Unknown AAC object_type=" << +object_type;
  378. return false;
  379. }
  380. aac_config.sampleRate = a2dp_codec_to_hal_sample_rate(current_codec);
  381. if (aac_config.sampleRate == SampleRate::RATE_UNKNOWN) {
  382. LOG(ERROR) << __func__
  383. << ": Unknown AAC sample_rate=" << current_codec.sample_rate;
  384. return false;
  385. }
  386. aac_config.channelMode = a2dp_codec_to_hal_channel_mode(current_codec);
  387. if (aac_config.channelMode == ChannelMode::UNKNOWN) {
  388. LOG(ERROR) << __func__ << ": Unknown AAC channel_mode="
  389. << current_codec.channel_mode;
  390. return false;
  391. }
  392. uint8_t vbr_enabled =
  393. a2dp_offload.codec_info[1] & A2DP_AAC_VARIABLE_BIT_RATE_MASK;
  394. switch (vbr_enabled) {
  395. case A2DP_AAC_VARIABLE_BIT_RATE_ENABLED:
  396. aac_config.variableBitRateEnabled = AacVariableBitRate::ENABLED;
  397. break;
  398. case A2DP_AAC_VARIABLE_BIT_RATE_DISABLED:
  399. aac_config.variableBitRateEnabled = AacVariableBitRate::DISABLED;
  400. break;
  401. default:
  402. LOG(ERROR) << __func__ << ": Unknown AAC VBR=" << +vbr_enabled;
  403. return false;
  404. }
  405. aac_config.bitsPerSample =
  406. a2dp_codec_to_hal_bits_per_sample(current_codec);
  407. if (aac_config.bitsPerSample == BitsPerSample::BITS_UNKNOWN) {
  408. LOG(ERROR) << __func__ << ": Unknown AAC bits_per_sample="
  409. << current_codec.bits_per_sample;
  410. return false;
  411. }
  412. codec_config->config.aacConfig(aac_config);
  413. break;
  414. }
  415. case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX:
  416. [[fallthrough]];
  417. case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD: {
  418. if (current_codec.codec_type == BTAV_A2DP_CODEC_INDEX_SOURCE_APTX) {
  419. codec_config->codecType = CodecType::APTX;
  420. } else {
  421. codec_config->codecType = CodecType::APTX_HD;
  422. }
  423. codec_config->config.aptxConfig({});
  424. auto aptx_config = codec_config->config.aptxConfig();
  425. aptx_config.sampleRate = a2dp_codec_to_hal_sample_rate(current_codec);
  426. if (aptx_config.sampleRate == SampleRate::RATE_UNKNOWN) {
  427. LOG(ERROR) << __func__ << ": Unknown aptX sample_rate="
  428. << current_codec.sample_rate;
  429. return false;
  430. }
  431. aptx_config.channelMode = a2dp_codec_to_hal_channel_mode(current_codec);
  432. if (aptx_config.channelMode == ChannelMode::UNKNOWN) {
  433. LOG(ERROR) << __func__ << ": Unknown aptX channel_mode="
  434. << current_codec.channel_mode;
  435. return false;
  436. }
  437. aptx_config.bitsPerSample =
  438. a2dp_codec_to_hal_bits_per_sample(current_codec);
  439. if (aptx_config.bitsPerSample == BitsPerSample::BITS_UNKNOWN) {
  440. LOG(ERROR) << __func__ << ": Unknown aptX bits_per_sample="
  441. << current_codec.bits_per_sample;
  442. return false;
  443. }
  444. codec_config->config.aptxConfig(aptx_config);
  445. break;
  446. }
  447. case BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC: {
  448. codec_config->codecType = CodecType::LDAC;
  449. codec_config->config.ldacConfig({});
  450. auto ldac_config = codec_config->config.ldacConfig();
  451. ldac_config.sampleRate = a2dp_codec_to_hal_sample_rate(current_codec);
  452. if (ldac_config.sampleRate == SampleRate::RATE_UNKNOWN) {
  453. LOG(ERROR) << __func__ << ": Unknown LDAC sample_rate="
  454. << current_codec.sample_rate;
  455. return false;
  456. }
  457. switch (a2dp_offload.codec_info[7]) {
  458. case A2DP_LDAC_CHANNEL_MODE_STEREO:
  459. ldac_config.channelMode = LdacChannelMode::STEREO;
  460. break;
  461. case A2DP_LDAC_CHANNEL_MODE_DUAL:
  462. ldac_config.channelMode = LdacChannelMode::DUAL;
  463. break;
  464. case A2DP_LDAC_CHANNEL_MODE_MONO:
  465. ldac_config.channelMode = LdacChannelMode::MONO;
  466. break;
  467. default:
  468. LOG(ERROR) << __func__ << ": Unknown LDAC channel_mode="
  469. << a2dp_offload.codec_info[7];
  470. ldac_config.channelMode = LdacChannelMode::UNKNOWN;
  471. return false;
  472. }
  473. switch (a2dp_offload.codec_info[6]) {
  474. case A2DP_LDAC_QUALITY_HIGH:
  475. ldac_config.qualityIndex = LdacQualityIndex::QUALITY_HIGH;
  476. break;
  477. case A2DP_LDAC_QUALITY_MID:
  478. ldac_config.qualityIndex = LdacQualityIndex::QUALITY_MID;
  479. break;
  480. case A2DP_LDAC_QUALITY_LOW:
  481. ldac_config.qualityIndex = LdacQualityIndex::QUALITY_LOW;
  482. break;
  483. case A2DP_LDAC_QUALITY_ABR_OFFLOAD:
  484. ldac_config.qualityIndex = LdacQualityIndex::QUALITY_ABR;
  485. break;
  486. default:
  487. LOG(ERROR) << __func__ << ": Unknown LDAC QualityIndex="
  488. << a2dp_offload.codec_info[6];
  489. return false;
  490. }
  491. ldac_config.bitsPerSample =
  492. a2dp_codec_to_hal_bits_per_sample(current_codec);
  493. if (ldac_config.bitsPerSample == BitsPerSample::BITS_UNKNOWN) {
  494. LOG(ERROR) << __func__ << ": Unknown LDAC bits_per_sample="
  495. << current_codec.bits_per_sample;
  496. return false;
  497. }
  498. codec_config->config.ldacConfig(ldac_config);
  499. break;
  500. }
  501. case BTAV_A2DP_CODEC_INDEX_MAX:
  502. [[fallthrough]];
  503. default:
  504. LOG(ERROR) << __func__
  505. << ": Unknown codec_type=" << current_codec.codec_type;
  506. codec_config->codecType = CodecType::UNKNOWN;
  507. codec_config->config = {};
  508. return false;
  509. }
  510. codec_config->encodedAudioBitrate = a2dp_codec_configs->getTrackBitRate();
  511. // Obtain the MTU
  512. RawAddress peer_addr = btif_av_source_active_peer();
  513. tA2DP_ENCODER_INIT_PEER_PARAMS peer_param;
  514. bta_av_co_get_peer_params(peer_addr, &peer_param);
  515. int effectiveMtu = a2dp_codec_configs->getEffectiveMtu();
  516. if (effectiveMtu > 0 && effectiveMtu < peer_param.peer_mtu) {
  517. codec_config->peerMtu = effectiveMtu;
  518. } else {
  519. codec_config->peerMtu = peer_param.peer_mtu;
  520. }
  521. LOG(INFO) << __func__ << ": CodecConfiguration=" << toString(*codec_config);
  522. return true;
  523. }
  524. bool a2dp_get_selected_hal_pcm_config(PcmParameters* pcm_config) {
  525. if (pcm_config == nullptr) return false;
  526. A2dpCodecConfig* a2dp_codec_configs = bta_av_get_a2dp_current_codec();
  527. if (a2dp_codec_configs == nullptr) {
  528. LOG(WARNING) << __func__ << ": failure to get A2DP codec config";
  529. *pcm_config = ::bluetooth::audio::BluetoothAudioClientInterface::
  530. kInvalidPcmConfiguration;
  531. return false;
  532. }
  533. btav_a2dp_codec_config_t current_codec = a2dp_codec_configs->getCodecConfig();
  534. pcm_config->sampleRate = a2dp_codec_to_hal_sample_rate(current_codec);
  535. pcm_config->bitsPerSample = a2dp_codec_to_hal_bits_per_sample(current_codec);
  536. pcm_config->channelMode = a2dp_codec_to_hal_channel_mode(current_codec);
  537. return (pcm_config->sampleRate != SampleRate::RATE_UNKNOWN &&
  538. pcm_config->bitsPerSample != BitsPerSample::BITS_UNKNOWN &&
  539. pcm_config->channelMode != ChannelMode::UNKNOWN);
  540. }
  541. // Checking if new bluetooth_audio is supported
  542. bool is_hal_2_0_force_disabled() {
  543. if (!is_configured) {
  544. btaudio_a2dp_disabled = osi_property_get_bool(BLUETOOTH_AUDIO_HAL_PROP_DISABLED, false);
  545. is_configured = true;
  546. }
  547. return btaudio_a2dp_disabled;
  548. }
  549. } // namespace
  550. namespace bluetooth {
  551. namespace audio {
  552. namespace a2dp {
  553. // Checking if new bluetooth_audio is enabled
  554. bool is_hal_2_0_enabled() { return a2dp_hal_clientif != nullptr; }
  555. // Initialize BluetoothAudio HAL: openProvider
  556. bool init(bluetooth::common::MessageLoopThread* message_loop) {
  557. LOG(INFO) << __func__;
  558. if (is_hal_2_0_force_disabled()) {
  559. LOG(ERROR) << __func__ << ": BluetoothAudio HAL is disabled";
  560. return false;
  561. }
  562. if (btif_av_is_a2dp_offload_enabled()) {
  563. session_type = SessionType::A2DP_HARDWARE_OFFLOAD_DATAPATH;
  564. } else {
  565. session_type = SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH;
  566. }
  567. a2dp_sink = new A2dpTransport(session_type);
  568. a2dp_hal_clientif = new bluetooth::audio::BluetoothAudioClientInterface(
  569. a2dp_sink, message_loop);
  570. if (!a2dp_hal_clientif->IsValid()) {
  571. LOG(WARNING) << __func__ << ": BluetoothAudio HAL for A2DP session=" << toString(session_type) << " is invalid?!";
  572. delete a2dp_hal_clientif;
  573. a2dp_hal_clientif = nullptr;
  574. delete a2dp_sink;
  575. a2dp_sink = nullptr;
  576. return false;
  577. }
  578. if (remote_delay != 0) {
  579. LOG(INFO) << __func__ << ": restore DELAY "
  580. << static_cast<float>(remote_delay / 10.0) << " ms";
  581. a2dp_sink->SetRemoteDelay(remote_delay);
  582. remote_delay = 0;
  583. }
  584. return true;
  585. }
  586. // Clean up BluetoothAudio HAL
  587. void cleanup() {
  588. if (!is_hal_2_0_enabled()) return;
  589. end_session();
  590. delete a2dp_hal_clientif;
  591. a2dp_hal_clientif = nullptr;
  592. delete a2dp_sink;
  593. a2dp_sink = nullptr;
  594. session_type = SessionType::UNKNOWN;
  595. remote_delay = 0;
  596. }
  597. // Set up the codec into BluetoothAudio HAL
  598. bool setup_codec() {
  599. if (!is_hal_2_0_enabled()) {
  600. LOG(ERROR) << __func__ << ": BluetoothAudio HAL is not enabled";
  601. return false;
  602. }
  603. AudioConfiguration audio_config{};
  604. if (session_type == SessionType::A2DP_HARDWARE_OFFLOAD_DATAPATH) {
  605. CodecConfiguration codec_config{};
  606. if (!a2dp_get_selected_hal_codec_config(&codec_config)) {
  607. LOG(ERROR) << __func__ << ": Failed to get CodecConfiguration";
  608. return false;
  609. }
  610. audio_config.codecConfig(codec_config);
  611. } else if (session_type == SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH) {
  612. PcmParameters pcm_config{};
  613. if (!a2dp_get_selected_hal_pcm_config(&pcm_config)) {
  614. LOG(ERROR) << __func__ << ": Failed to get PcmConfiguration";
  615. return false;
  616. }
  617. audio_config.pcmConfig(pcm_config);
  618. }
  619. return a2dp_hal_clientif->UpdateAudioConfig(audio_config);
  620. }
  621. void start_session() {
  622. if (!is_hal_2_0_enabled()) {
  623. LOG(ERROR) << __func__ << ": BluetoothAudio HAL is not enabled";
  624. return;
  625. }
  626. a2dp_hal_clientif->StartSession();
  627. }
  628. void end_session() {
  629. if (!is_hal_2_0_enabled()) {
  630. LOG(ERROR) << __func__ << ": BluetoothAudio HAL is not enabled";
  631. return;
  632. }
  633. a2dp_hal_clientif->EndSession();
  634. }
  635. void ack_stream_started(const tA2DP_CTRL_ACK& ack) {
  636. auto ctrl_ack = a2dp_ack_to_bt_audio_ctrl_ack(ack);
  637. LOG(INFO) << __func__ << ": result=" << ctrl_ack;
  638. auto pending_cmd = a2dp_sink->GetPendingCmd();
  639. if (pending_cmd == A2DP_CTRL_CMD_START) {
  640. a2dp_hal_clientif->StreamStarted(ctrl_ack);
  641. } else {
  642. LOG(WARNING) << __func__ << ": pending=" << pending_cmd
  643. << " ignore result=" << ctrl_ack;
  644. return;
  645. }
  646. if (ctrl_ack != bluetooth::audio::BluetoothAudioCtrlAck::PENDING) {
  647. a2dp_sink->ResetPendingCmd();
  648. }
  649. }
  650. void ack_stream_suspended(const tA2DP_CTRL_ACK& ack) {
  651. auto ctrl_ack = a2dp_ack_to_bt_audio_ctrl_ack(ack);
  652. LOG(INFO) << __func__ << ": result=" << ctrl_ack;
  653. auto pending_cmd = a2dp_sink->GetPendingCmd();
  654. if (pending_cmd == A2DP_CTRL_CMD_SUSPEND) {
  655. a2dp_hal_clientif->StreamSuspended(ctrl_ack);
  656. } else if (pending_cmd == A2DP_CTRL_CMD_STOP) {
  657. LOG(INFO) << __func__ << ": A2DP_CTRL_CMD_STOP result=" << ctrl_ack;
  658. } else {
  659. LOG(WARNING) << __func__ << ": pending=" << pending_cmd
  660. << " ignore result=" << ctrl_ack;
  661. return;
  662. }
  663. if (ctrl_ack != bluetooth::audio::BluetoothAudioCtrlAck::PENDING) {
  664. a2dp_sink->ResetPendingCmd();
  665. }
  666. }
  667. // Read from the FMQ of BluetoothAudio HAL
  668. size_t read(uint8_t* p_buf, uint32_t len) {
  669. if (!is_hal_2_0_enabled()) {
  670. LOG(ERROR) << __func__ << ": BluetoothAudio HAL is not enabled";
  671. return 0;
  672. } else if (session_type != SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH) {
  673. LOG(ERROR) << __func__ << ": session_type=" << toString(session_type)
  674. << " is not A2DP_SOFTWARE_ENCODING_DATAPATH";
  675. return 0;
  676. }
  677. return a2dp_hal_clientif->ReadAudioData(p_buf, len);
  678. }
  679. // Update A2DP delay report to BluetoothAudio HAL
  680. void set_remote_delay(uint16_t delay_report) {
  681. if (!is_hal_2_0_enabled()) {
  682. LOG(INFO) << __func__ << ": not ready for DelayReport "
  683. << static_cast<float>(delay_report / 10.0) << " ms";
  684. remote_delay = delay_report;
  685. return;
  686. }
  687. LOG(INFO) << __func__ << ": DELAY " << static_cast<float>(delay_report / 10.0)
  688. << " ms";
  689. a2dp_sink->SetRemoteDelay(delay_report);
  690. }
  691. } // namespace a2dp
  692. } // namespace audio
  693. } // namespace bluetooth