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