• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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 <android-base/logging.h>
22 
23 #include "BluetoothAudioSessionReport_2_1.h"
24 #include "BluetoothAudioSupportedCodecsDB_2_1.h"
25 
26 namespace android {
27 namespace hardware {
28 namespace bluetooth {
29 namespace audio {
30 namespace V2_1 {
31 namespace implementation {
32 
33 using ::android::bluetooth::audio::BluetoothAudioSessionReport_2_1;
34 using ::android::hardware::Void;
35 using ::android::hardware::bluetooth::audio::V2_0::AudioConfiguration;
36 
37 static constexpr uint32_t kPcmFrameSize = 4;  // 16 bits per sample / stereo
38 static constexpr uint32_t kPcmFrameCount = 128;
39 static constexpr uint32_t kRtpFrameSize = kPcmFrameSize * kPcmFrameCount;
40 static constexpr uint32_t kRtpFrameCount = 7;  // max counts by 1 tick (20ms)
41 static constexpr uint32_t kBufferSize = kRtpFrameSize * kRtpFrameCount;
42 static constexpr uint32_t kBufferCount = 1;  // single buffer
43 static constexpr uint32_t kDataMqSize = kBufferSize * kBufferCount;
44 
HearingAidAudioProvider()45 HearingAidAudioProvider::HearingAidAudioProvider()
46     : BluetoothAudioProvider(), mDataMQ(nullptr) {
47   LOG(INFO) << __func__ << " - size of audio buffer " << kDataMqSize
48             << " byte(s)";
49   std::unique_ptr<DataMQ> tempDataMQ(
50       new DataMQ(kDataMqSize, /* EventFlag */ true));
51   if (tempDataMQ && tempDataMQ->isValid()) {
52     mDataMQ = std::move(tempDataMQ);
53     session_type_ = SessionType::HEARING_AID_SOFTWARE_ENCODING_DATAPATH;
54   } else {
55     ALOGE_IF(!tempDataMQ, "failed to allocate data MQ");
56     ALOGE_IF(tempDataMQ && !tempDataMQ->isValid(), "data MQ is invalid");
57   }
58 }
59 
isValid(const V2_0::SessionType & sessionType)60 bool HearingAidAudioProvider::isValid(const V2_0::SessionType& sessionType) {
61   return isValid(static_cast<SessionType>(sessionType));
62 }
63 
isValid(const SessionType & sessionType)64 bool HearingAidAudioProvider::isValid(const SessionType& sessionType) {
65   return (sessionType == session_type_ && mDataMQ && mDataMQ->isValid());
66 }
67 
startSession(const sp<IBluetoothAudioPort> & hostIf,const V2_0::AudioConfiguration & audioConfig,startSession_cb _hidl_cb)68 Return<void> HearingAidAudioProvider::startSession(
69     const sp<IBluetoothAudioPort>& hostIf,
70     const V2_0::AudioConfiguration& audioConfig, startSession_cb _hidl_cb) {
71   /**
72    * Initialize the audio platform if audioConfiguration is supported.
73    * Save the IBluetoothAudioPort interface, so that it can be used
74    * later to send stream control commands to the HAL client, based on
75    * interaction with Audio framework.
76    */
77   if (audioConfig.getDiscriminator() !=
78       AudioConfiguration::hidl_discriminator::pcmConfig) {
79     LOG(WARNING) << __func__
80                  << " - Invalid Audio Configuration=" << toString(audioConfig);
81     _hidl_cb(BluetoothAudioStatus::UNSUPPORTED_CODEC_CONFIGURATION,
82              DataMQ::Descriptor());
83     return Void();
84   } else if (!android::bluetooth::audio::IsSoftwarePcmConfigurationValid(
85                  audioConfig.pcmConfig())) {
86     LOG(WARNING) << __func__ << " - Unsupported PCM Configuration="
87                  << toString(audioConfig.pcmConfig());
88     _hidl_cb(BluetoothAudioStatus::UNSUPPORTED_CODEC_CONFIGURATION,
89              DataMQ::Descriptor());
90     return Void();
91   }
92 
93   return BluetoothAudioProvider::startSession(hostIf, audioConfig, _hidl_cb);
94 }
95 
onSessionReady(startSession_cb _hidl_cb)96 Return<void> HearingAidAudioProvider::onSessionReady(startSession_cb _hidl_cb) {
97   if (mDataMQ && mDataMQ->isValid()) {
98     BluetoothAudioSessionReport_2_1::OnSessionStarted(
99         session_type_, stack_iface_, mDataMQ->getDesc(), audio_config_);
100     _hidl_cb(BluetoothAudioStatus::SUCCESS, *mDataMQ->getDesc());
101   } else {
102     _hidl_cb(BluetoothAudioStatus::FAILURE, DataMQ::Descriptor());
103   }
104   return Void();
105 }
106 
107 }  // namespace implementation
108 }  // namespace V2_1
109 }  // namespace audio
110 }  // namespace bluetooth
111 }  // namespace hardware
112 }  // namespace android
113