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