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 
HearingAidAudioProvider()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 }
isValid(const SessionType & sessionType)53 bool HearingAidAudioProvider::isValid(const SessionType& sessionType) {
54   return (sessionType == session_type_ && data_mq_ && data_mq_->isValid());
55 }
56 
startSession(const std::shared_ptr<IBluetoothAudioPort> & host_if,const AudioConfiguration & audio_config,const std::vector<LatencyMode> & latency_modes,DataMQDesc * _aidl_return)57 ndk::ScopedAStatus HearingAidAudioProvider::startSession(
58     const std::shared_ptr<IBluetoothAudioPort>& host_if,
59     const AudioConfiguration& audio_config,
60     const std::vector<LatencyMode>& latency_modes, DataMQDesc* _aidl_return) {
61   if (audio_config.getTag() != AudioConfiguration::pcmConfig) {
62     LOG(WARNING) << __func__ << " - Invalid Audio Configuration="
63                  << audio_config.toString();
64     *_aidl_return = DataMQDesc();
65     return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
66   }
67   const auto& pcm_config = audio_config.get<AudioConfiguration::pcmConfig>();
68   if (!BluetoothAudioCodecs::IsSoftwarePcmConfigurationValid(pcm_config)) {
69     LOG(WARNING) << __func__ << " - Unsupported PCM Configuration="
70                  << pcm_config.toString();
71     *_aidl_return = DataMQDesc();
72     return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
73   }
74 
75   return BluetoothAudioProvider::startSession(
76       host_if, audio_config, latency_modes, _aidl_return);
77 }
78 
onSessionReady(DataMQDesc * _aidl_return)79 ndk::ScopedAStatus HearingAidAudioProvider::onSessionReady(
80     DataMQDesc* _aidl_return) {
81   if (data_mq_ == nullptr || !data_mq_->isValid()) {
82     *_aidl_return = DataMQDesc();
83     return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
84   }
85   *_aidl_return = data_mq_->dupeDesc();
86   auto desc = data_mq_->dupeDesc();
87   BluetoothAudioSessionReport::OnSessionStarted(
88       session_type_, stack_iface_, &desc, *audio_config_, latency_modes_);
89   return ndk::ScopedAStatus::ok();
90 }
91 
92 }  // namespace audio
93 }  // namespace bluetooth
94 }  // namespace hardware
95 }  // namespace android
96 }  // namespace aidl
97