blob: 66ce93bd35d68a942530da2159195967fb07a59f [file] [log] [blame]
Josh Wu6ab53e72021-12-29 23:53:33 -08001/*
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
25namespace aidl {
26namespace android {
27namespace hardware {
28namespace bluetooth {
29namespace audio {
30
31static constexpr uint32_t kPcmFrameSize = 4; // 16 bits per sample / stereo
32static constexpr uint32_t kPcmFrameCount = 128;
33static constexpr uint32_t kRtpFrameSize = kPcmFrameSize * kPcmFrameCount;
34static constexpr uint32_t kRtpFrameCount = 7; // max counts by 1 tick (20ms)
35static constexpr uint32_t kBufferSize = kRtpFrameSize * kRtpFrameCount;
36static constexpr uint32_t kBufferCount = 1; // single buffer
37static constexpr uint32_t kDataMqSize = kBufferSize * kBufferCount;
38
39HearingAidAudioProvider::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}
53bool HearingAidAudioProvider::isValid(const SessionType& sessionType) {
54 return (sessionType == session_type_ && data_mq_ && data_mq_->isValid());
55}
56
57ndk::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
78ndk::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 Wu98d7e082022-01-21 03:04:21 -080084 *_aidl_return = data_mq_->dupeDesc();
Josh Wu6ab53e72021-12-29 23:53:33 -080085 auto desc = data_mq_->dupeDesc();
86 BluetoothAudioSessionReport::OnSessionStarted(session_type_, stack_iface_,
87 &desc, *audio_config_);
Josh Wu6ab53e72021-12-29 23:53:33 -080088 return ndk::ScopedAStatus::ok();
89}
90
91} // namespace audio
92} // namespace bluetooth
93} // namespace hardware
94} // namespace android
95} // namespace aidl