1 /*
2 * Copyright 2020 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 #define LOG_TAG "BTAudioProviderA2dpSoftware"
18
19 #include "A2dpSoftwareAudioProvider.h"
20
21 #include <android-base/logging.h>
22
23 #include "BluetoothAudioSessionReport_2_1.h"
24 #include "BluetoothAudioSupportedCodecsDB_2_1.h"
25
26 namespace android {
27 namespace hardware {
28 namespace bluetooth {
29 namespace audio {
30 namespace V2_1 {
31 namespace implementation {
32
33 using ::android::bluetooth::audio::BluetoothAudioSessionReport_2_1;
34 using ::android::hardware::Void;
35 using ::android::hardware::bluetooth::audio::V2_0::AudioConfiguration;
36
37 // Here the buffer size is based on SBC
38 static constexpr uint32_t kPcmFrameSize = 4; // 16 bits per sample / stereo
39 // SBC is 128, and here we choose the LCM of 16, 24, and 32
40 static constexpr uint32_t kPcmFrameCount = 96;
41 static constexpr uint32_t kRtpFrameSize = kPcmFrameSize * kPcmFrameCount;
42 // The max counts by 1 tick (20ms) for SBC is about 7. Since using 96 for the
43 // PCM counts, here we just choose a greater number
44 static constexpr uint32_t kRtpFrameCount = 10;
45 static constexpr uint32_t kBufferSize = kRtpFrameSize * kRtpFrameCount;
46 static constexpr uint32_t kBufferCount = 2; // double buffer
47 static constexpr uint32_t kDataMqSize = kBufferSize * kBufferCount;
48
A2dpSoftwareAudioProvider()49 A2dpSoftwareAudioProvider::A2dpSoftwareAudioProvider()
50 : BluetoothAudioProvider(), mDataMQ(nullptr) {
51 LOG(INFO) << __func__ << " - size of audio buffer " << kDataMqSize
52 << " byte(s)";
53 std::unique_ptr<DataMQ> tempDataMQ(
54 new DataMQ(kDataMqSize, /* EventFlag */ true));
55 if (tempDataMQ && tempDataMQ->isValid()) {
56 mDataMQ = std::move(tempDataMQ);
57 session_type_ = SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH;
58 } else {
59 ALOGE_IF(!tempDataMQ, "failed to allocate data MQ");
60 ALOGE_IF(tempDataMQ && !tempDataMQ->isValid(), "data MQ is invalid");
61 }
62 }
63
isValid(const V2_0::SessionType & sessionType)64 bool A2dpSoftwareAudioProvider::isValid(const V2_0::SessionType& sessionType) {
65 return isValid(static_cast<SessionType>(sessionType));
66 }
67
isValid(const SessionType & sessionType)68 bool A2dpSoftwareAudioProvider::isValid(const SessionType& sessionType) {
69 return (sessionType == session_type_ && mDataMQ && mDataMQ->isValid());
70 }
71
startSession(const sp<IBluetoothAudioPort> & hostIf,const V2_0::AudioConfiguration & audioConfig,startSession_cb _hidl_cb)72 Return<void> A2dpSoftwareAudioProvider::startSession(
73 const sp<IBluetoothAudioPort>& hostIf,
74 const V2_0::AudioConfiguration& audioConfig, startSession_cb _hidl_cb) {
75 /**
76 * Initialize the audio platform if audioConfiguration is supported.
77 * Save the IBluetoothAudioPort interface, so that it can be used
78 * later to send stream control commands to the HAL client, based on
79 * interaction with Audio framework.
80 */
81 if (audioConfig.getDiscriminator() !=
82 AudioConfiguration::hidl_discriminator::pcmConfig) {
83 LOG(WARNING) << __func__
84 << " - Invalid Audio Configuration=" << toString(audioConfig);
85 _hidl_cb(BluetoothAudioStatus::UNSUPPORTED_CODEC_CONFIGURATION,
86 DataMQ::Descriptor());
87 return Void();
88 } else if (!android::bluetooth::audio::IsSoftwarePcmConfigurationValid(
89 audioConfig.pcmConfig())) {
90 LOG(WARNING) << __func__ << " - Unsupported PCM Configuration="
91 << toString(audioConfig.pcmConfig());
92 _hidl_cb(BluetoothAudioStatus::UNSUPPORTED_CODEC_CONFIGURATION,
93 DataMQ::Descriptor());
94 return Void();
95 }
96
97 return BluetoothAudioProvider::startSession(hostIf, audioConfig, _hidl_cb);
98 }
99
onSessionReady(startSession_cb _hidl_cb)100 Return<void> A2dpSoftwareAudioProvider::onSessionReady(
101 startSession_cb _hidl_cb) {
102 if (mDataMQ && mDataMQ->isValid()) {
103 BluetoothAudioSessionReport_2_1::OnSessionStarted(
104 session_type_, stack_iface_, mDataMQ->getDesc(), audio_config_);
105 _hidl_cb(BluetoothAudioStatus::SUCCESS, *mDataMQ->getDesc());
106 } else {
107 _hidl_cb(BluetoothAudioStatus::FAILURE, DataMQ::Descriptor());
108 }
109 return Void();
110 }
111
112 } // namespace implementation
113 } // namespace V2_1
114 } // namespace audio
115 } // namespace bluetooth
116 } // namespace hardware
117 } // namespace android
118