Josh Wu | 6ab53e7 | 2021-12-29 23:53:33 -0800 | [diff] [blame] | 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 | |
| 43 | A2dpSoftwareAudioProvider::A2dpSoftwareAudioProvider() |
| 44 | : BluetoothAudioProvider(), data_mq_(nullptr) { |
| 45 | LOG(INFO) << __func__ << " - size of audio buffer " << kDataMqSize |
| 46 | << " byte(s)"; |
| 47 | std::unique_ptr<DataMQ> data_mq( |
| 48 | new DataMQ(kDataMqSize, /* EventFlag */ true)); |
| 49 | if (data_mq && data_mq->isValid()) { |
| 50 | data_mq_ = std::move(data_mq); |
| 51 | session_type_ = SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH; |
| 52 | } else { |
| 53 | ALOGE_IF(!data_mq, "failed to allocate data MQ"); |
| 54 | ALOGE_IF(data_mq && !data_mq->isValid(), "data MQ is invalid"); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | bool A2dpSoftwareAudioProvider::isValid(const SessionType& sessionType) { |
| 59 | return (sessionType == session_type_ && data_mq_ && data_mq_->isValid()); |
| 60 | } |
| 61 | |
| 62 | ndk::ScopedAStatus A2dpSoftwareAudioProvider::startSession( |
| 63 | const std::shared_ptr<IBluetoothAudioPort>& host_if, |
Chen Chen | c92270e | 2022-02-14 18:29:52 -0800 | [diff] [blame] | 64 | const AudioConfiguration& audio_config, |
Cheney Ni | 6ecbc76 | 2022-03-03 00:12:48 +0800 | [diff] [blame^] | 65 | const std::vector<LatencyMode>& latency_modes, DataMQDesc* _aidl_return) { |
Josh Wu | 6ab53e7 | 2021-12-29 23:53:33 -0800 | [diff] [blame] | 66 | if (audio_config.getTag() != AudioConfiguration::pcmConfig) { |
| 67 | LOG(WARNING) << __func__ << " - Invalid Audio Configuration=" |
| 68 | << audio_config.toString(); |
| 69 | *_aidl_return = DataMQDesc(); |
| 70 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 71 | } |
| 72 | const PcmConfiguration& pcm_config = |
| 73 | audio_config.get<AudioConfiguration::pcmConfig>(); |
| 74 | if (!BluetoothAudioCodecs::IsSoftwarePcmConfigurationValid(pcm_config)) { |
| 75 | LOG(WARNING) << __func__ << " - Unsupported PCM Configuration=" |
| 76 | << pcm_config.toString(); |
| 77 | *_aidl_return = DataMQDesc(); |
| 78 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 79 | } |
| 80 | |
Chen Chen | c92270e | 2022-02-14 18:29:52 -0800 | [diff] [blame] | 81 | return BluetoothAudioProvider::startSession( |
| 82 | host_if, audio_config, latency_modes, _aidl_return); |
Josh Wu | 6ab53e7 | 2021-12-29 23:53:33 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | ndk::ScopedAStatus A2dpSoftwareAudioProvider::onSessionReady( |
| 86 | DataMQDesc* _aidl_return) { |
| 87 | if (data_mq_ == nullptr || !data_mq_->isValid()) { |
| 88 | *_aidl_return = DataMQDesc(); |
| 89 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 90 | } |
| 91 | *_aidl_return = data_mq_->dupeDesc(); |
Josh Wu | 98d7e08 | 2022-01-21 03:04:21 -0800 | [diff] [blame] | 92 | auto desc = data_mq_->dupeDesc(); |
Cheney Ni | 6ecbc76 | 2022-03-03 00:12:48 +0800 | [diff] [blame^] | 93 | BluetoothAudioSessionReport::OnSessionStarted( |
| 94 | session_type_, stack_iface_, &desc, *audio_config_, latency_modes_); |
Josh Wu | 6ab53e7 | 2021-12-29 23:53:33 -0800 | [diff] [blame] | 95 | return ndk::ScopedAStatus::ok(); |
| 96 | } |
| 97 | |
| 98 | } // namespace audio |
| 99 | } // namespace bluetooth |
| 100 | } // namespace hardware |
| 101 | } // namespace android |
Cheney Ni | 6ecbc76 | 2022-03-03 00:12:48 +0800 | [diff] [blame^] | 102 | } // namespace aidl |