blob: 712bd4f7181e11b3dba5ad388283b6c7babd2f8a [file] [log] [blame]
Grzegorz Kolodziejczykb5f2d232019-10-24 12:31:20 +02001/*
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
Jakub Pawlowski3c8dc612021-02-03 20:43:20 +010023#include "BluetoothAudioSessionReport_2_1.h"
24#include "BluetoothAudioSupportedCodecsDB_2_1.h"
Grzegorz Kolodziejczykb5f2d232019-10-24 12:31:20 +020025
26namespace android {
27namespace hardware {
28namespace bluetooth {
29namespace audio {
30namespace V2_1 {
31namespace implementation {
32
Jakub Pawlowski3c8dc612021-02-03 20:43:20 +010033using ::android::bluetooth::audio::BluetoothAudioSessionReport_2_1;
Grzegorz Kolodziejczykb5f2d232019-10-24 12:31:20 +020034using ::android::hardware::Void;
35using ::android::hardware::bluetooth::audio::V2_0::AudioConfiguration;
36
37static constexpr uint32_t kPcmFrameSize = 4; // 16 bits per sample / stereo
38static constexpr uint32_t kPcmFrameCount = 128;
39static constexpr uint32_t kRtpFrameSize = kPcmFrameSize * kPcmFrameCount;
40static constexpr uint32_t kRtpFrameCount = 7; // max counts by 1 tick (20ms)
41static constexpr uint32_t kBufferSize = kRtpFrameSize * kRtpFrameCount;
42static constexpr uint32_t kBufferCount = 1; // single buffer
43static constexpr uint32_t kDataMqSize = kBufferSize * kBufferCount;
44
45HearingAidAudioProvider::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
60bool HearingAidAudioProvider::isValid(const V2_0::SessionType& sessionType) {
61 return isValid(static_cast<SessionType>(sessionType));
62}
63
64bool HearingAidAudioProvider::isValid(const SessionType& sessionType) {
65 return (sessionType == session_type_ && mDataMQ && mDataMQ->isValid());
66}
67
68Return<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
96Return<void> HearingAidAudioProvider::onSessionReady(startSession_cb _hidl_cb) {
97 if (mDataMQ && mDataMQ->isValid()) {
Jakub Pawlowski3c8dc612021-02-03 20:43:20 +010098 BluetoothAudioSessionReport_2_1::OnSessionStarted(
Grzegorz Kolodziejczykb5f2d232019-10-24 12:31:20 +020099 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