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, |
| 65 | const std::vector<LatencyMode>& latency_modes, |
| 66 | DataMQDesc* _aidl_return) { |
| 67 | latency_modes_ = latency_modes; |
Josh Wu | 6ab53e7 | 2021-12-29 23:53:33 -0800 | [diff] [blame] | 68 | if (audio_config.getTag() != AudioConfiguration::pcmConfig) { |
| 69 | LOG(WARNING) << __func__ << " - Invalid Audio Configuration=" |
| 70 | << audio_config.toString(); |
| 71 | *_aidl_return = DataMQDesc(); |
| 72 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 73 | } |
| 74 | const PcmConfiguration& pcm_config = |
| 75 | audio_config.get<AudioConfiguration::pcmConfig>(); |
| 76 | if (!BluetoothAudioCodecs::IsSoftwarePcmConfigurationValid(pcm_config)) { |
| 77 | LOG(WARNING) << __func__ << " - Unsupported PCM Configuration=" |
| 78 | << pcm_config.toString(); |
| 79 | *_aidl_return = DataMQDesc(); |
| 80 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 81 | } |
| 82 | |
Chen Chen | c92270e | 2022-02-14 18:29:52 -0800 | [diff] [blame^] | 83 | return BluetoothAudioProvider::startSession( |
| 84 | host_if, audio_config, latency_modes, _aidl_return); |
Josh Wu | 6ab53e7 | 2021-12-29 23:53:33 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | ndk::ScopedAStatus A2dpSoftwareAudioProvider::onSessionReady( |
| 88 | DataMQDesc* _aidl_return) { |
| 89 | if (data_mq_ == nullptr || !data_mq_->isValid()) { |
| 90 | *_aidl_return = DataMQDesc(); |
| 91 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 92 | } |
| 93 | *_aidl_return = data_mq_->dupeDesc(); |
Josh Wu | 98d7e08 | 2022-01-21 03:04:21 -0800 | [diff] [blame] | 94 | auto desc = data_mq_->dupeDesc(); |
Josh Wu | 6ab53e7 | 2021-12-29 23:53:33 -0800 | [diff] [blame] | 95 | BluetoothAudioSessionReport::OnSessionStarted(session_type_, stack_iface_, |
Josh Wu | 98d7e08 | 2022-01-21 03:04:21 -0800 | [diff] [blame] | 96 | &desc, *audio_config_); |
Josh Wu | 6ab53e7 | 2021-12-29 23:53:33 -0800 | [diff] [blame] | 97 | return ndk::ScopedAStatus::ok(); |
| 98 | } |
| 99 | |
| 100 | } // namespace audio |
| 101 | } // namespace bluetooth |
| 102 | } // namespace hardware |
| 103 | } // namespace android |
| 104 | } // namespace aidl |