1 /*
2  * Copyright (C) 2022 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 "BTAudioProviderA2dpSW"
18 
19 #include "A2dpSoftwareAudioProvider.h"
20 
21 #include <BluetoothAudioCodecs.h>
22 #include <BluetoothAudioSessionReport.h>
23 #include <android-base/logging.h>
24 
25 namespace aidl {
26 namespace android {
27 namespace hardware {
28 namespace bluetooth {
29 namespace audio {
30 
31 // Here the buffer size is based on SBC
32 static constexpr uint32_t kPcmFrameSize = 4;  // 16 bits per sample / stereo
33 // SBC is 128, and here we choose the LCM of 16, 24, and 32
34 static constexpr uint32_t kPcmFrameCount = 96;
35 static constexpr uint32_t kRtpFrameSize = kPcmFrameSize * kPcmFrameCount;
36 // The max counts by 1 tick (20ms) for SBC is about 7. Since using 96 for the
37 // PCM counts, here we just choose a greater number
38 static constexpr uint32_t kRtpFrameCount = 10;
39 static constexpr uint32_t kBufferSize = kRtpFrameSize * kRtpFrameCount;
40 static constexpr uint32_t kBufferCount = 2;  // double buffer
41 static constexpr uint32_t kDataMqSize = kBufferSize * kBufferCount;
42 
A2dpSoftwareEncodingAudioProvider()43 A2dpSoftwareEncodingAudioProvider::A2dpSoftwareEncodingAudioProvider()
44     : A2dpSoftwareAudioProvider() {
45   session_type_ = SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH;
46 }
47 
A2dpSoftwareDecodingAudioProvider()48 A2dpSoftwareDecodingAudioProvider::A2dpSoftwareDecodingAudioProvider()
49     : A2dpSoftwareAudioProvider() {
50   session_type_ = SessionType::A2DP_SOFTWARE_DECODING_DATAPATH;
51 }
52 
A2dpSoftwareAudioProvider()53 A2dpSoftwareAudioProvider::A2dpSoftwareAudioProvider()
54     : BluetoothAudioProvider(), data_mq_(nullptr) {
55   LOG(INFO) << __func__ << " - size of audio buffer " << kDataMqSize
56             << " byte(s)";
57   std::unique_ptr<DataMQ> data_mq(
58       new DataMQ(kDataMqSize, /* EventFlag */ true));
59   if (data_mq && data_mq->isValid()) {
60     data_mq_ = std::move(data_mq);
61   } else {
62     ALOGE_IF(!data_mq, "failed to allocate data MQ");
63     ALOGE_IF(data_mq && !data_mq->isValid(), "data MQ is invalid");
64   }
65 }
66 
isValid(const SessionType & sessionType)67 bool A2dpSoftwareAudioProvider::isValid(const SessionType& sessionType) {
68   return (sessionType == session_type_ && data_mq_ && data_mq_->isValid());
69 }
70 
startSession(const std::shared_ptr<IBluetoothAudioPort> & host_if,const AudioConfiguration & audio_config,const std::vector<LatencyMode> & latency_modes,DataMQDesc * _aidl_return)71 ndk::ScopedAStatus A2dpSoftwareAudioProvider::startSession(
72     const std::shared_ptr<IBluetoothAudioPort>& host_if,
73     const AudioConfiguration& audio_config,
74     const std::vector<LatencyMode>& latency_modes, DataMQDesc* _aidl_return) {
75   if (audio_config.getTag() != AudioConfiguration::pcmConfig) {
76     LOG(WARNING) << __func__ << " - Invalid Audio Configuration="
77                  << audio_config.toString();
78     *_aidl_return = DataMQDesc();
79     return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
80   }
81   const PcmConfiguration& pcm_config =
82       audio_config.get<AudioConfiguration::pcmConfig>();
83   if (!BluetoothAudioCodecs::IsSoftwarePcmConfigurationValid(pcm_config)) {
84     LOG(WARNING) << __func__ << " - Unsupported PCM Configuration="
85                  << pcm_config.toString();
86     *_aidl_return = DataMQDesc();
87     return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
88   }
89 
90   return BluetoothAudioProvider::startSession(
91       host_if, audio_config, latency_modes, _aidl_return);
92 }
93 
onSessionReady(DataMQDesc * _aidl_return)94 ndk::ScopedAStatus A2dpSoftwareAudioProvider::onSessionReady(
95     DataMQDesc* _aidl_return) {
96   if (data_mq_ == nullptr || !data_mq_->isValid()) {
97     *_aidl_return = DataMQDesc();
98     return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
99   }
100   *_aidl_return = data_mq_->dupeDesc();
101   auto desc = data_mq_->dupeDesc();
102   BluetoothAudioSessionReport::OnSessionStarted(
103       session_type_, stack_iface_, &desc, *audio_config_, latency_modes_);
104   return ndk::ScopedAStatus::ok();
105 }
106 
107 }  // namespace audio
108 }  // namespace bluetooth
109 }  // namespace hardware
110 }  // namespace android
111 }  // namespace aidl
112