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 "BTAudioProviderHearingAid" |
| 18 | |
| 19 | #include "HearingAidAudioProvider.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 | static constexpr uint32_t kPcmFrameSize = 4; // 16 bits per sample / stereo |
| 32 | static constexpr uint32_t kPcmFrameCount = 128; |
| 33 | static constexpr uint32_t kRtpFrameSize = kPcmFrameSize * kPcmFrameCount; |
| 34 | static constexpr uint32_t kRtpFrameCount = 7; // max counts by 1 tick (20ms) |
| 35 | static constexpr uint32_t kBufferSize = kRtpFrameSize * kRtpFrameCount; |
| 36 | static constexpr uint32_t kBufferCount = 1; // single buffer |
| 37 | static constexpr uint32_t kDataMqSize = kBufferSize * kBufferCount; |
| 38 | |
| 39 | HearingAidAudioProvider::HearingAidAudioProvider() |
| 40 | : BluetoothAudioProvider(), data_mq_(nullptr) { |
| 41 | LOG(INFO) << __func__ << " - size of audio buffer " << kDataMqSize |
| 42 | << " byte(s)"; |
| 43 | std::unique_ptr<DataMQ> data_mq( |
| 44 | new DataMQ(kDataMqSize, /* EventFlag */ true)); |
| 45 | if (data_mq && data_mq->isValid()) { |
| 46 | data_mq_ = std::move(data_mq); |
| 47 | session_type_ = SessionType::HEARING_AID_SOFTWARE_ENCODING_DATAPATH; |
| 48 | } else { |
| 49 | ALOGE_IF(!data_mq, "failed to allocate data MQ"); |
| 50 | ALOGE_IF(data_mq && !data_mq->isValid(), "data MQ is invalid"); |
| 51 | } |
| 52 | } |
| 53 | bool HearingAidAudioProvider::isValid(const SessionType& sessionType) { |
| 54 | return (sessionType == session_type_ && data_mq_ && data_mq_->isValid()); |
| 55 | } |
| 56 | |
| 57 | ndk::ScopedAStatus HearingAidAudioProvider::startSession( |
| 58 | const std::shared_ptr<IBluetoothAudioPort>& host_if, |
| 59 | const AudioConfiguration& audio_config, DataMQDesc* _aidl_return) { |
| 60 | if (audio_config.getTag() != AudioConfiguration::pcmConfig) { |
| 61 | LOG(WARNING) << __func__ << " - Invalid Audio Configuration=" |
| 62 | << audio_config.toString(); |
| 63 | *_aidl_return = DataMQDesc(); |
| 64 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 65 | } |
| 66 | const auto& pcm_config = audio_config.get<AudioConfiguration::pcmConfig>(); |
| 67 | if (!BluetoothAudioCodecs::IsSoftwarePcmConfigurationValid(pcm_config)) { |
| 68 | LOG(WARNING) << __func__ << " - Unsupported PCM Configuration=" |
| 69 | << pcm_config.toString(); |
| 70 | *_aidl_return = DataMQDesc(); |
| 71 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 72 | } |
| 73 | |
| 74 | return BluetoothAudioProvider::startSession(host_if, audio_config, |
| 75 | _aidl_return); |
| 76 | } |
| 77 | |
| 78 | ndk::ScopedAStatus HearingAidAudioProvider::onSessionReady( |
| 79 | DataMQDesc* _aidl_return) { |
| 80 | if (data_mq_ == nullptr || !data_mq_->isValid()) { |
| 81 | *_aidl_return = DataMQDesc(); |
| 82 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 83 | } |
Josh Wu | 98d7e08 | 2022-01-21 03:04:21 -0800 | [diff] [blame] | 84 | *_aidl_return = data_mq_->dupeDesc(); |
Josh Wu | 6ab53e7 | 2021-12-29 23:53:33 -0800 | [diff] [blame] | 85 | auto desc = data_mq_->dupeDesc(); |
| 86 | BluetoothAudioSessionReport::OnSessionStarted(session_type_, stack_iface_, |
| 87 | &desc, *audio_config_); |
Josh Wu | 6ab53e7 | 2021-12-29 23:53:33 -0800 | [diff] [blame] | 88 | return ndk::ScopedAStatus::ok(); |
| 89 | } |
| 90 | |
| 91 | } // namespace audio |
| 92 | } // namespace bluetooth |
| 93 | } // namespace hardware |
| 94 | } // namespace android |
| 95 | } // namespace aidl |