blob: ba31d39f2c1b2d2de1788610d37708385b72813f [file] [log] [blame]
Alice Kuo84e87672021-10-28 12:53:38 +08001/*
2 * Copyright 2021 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 "BTAudioProviderA2dpSoftware"
18
19#include "A2dpSoftwareAudioProvider.h"
20
21#include <android-base/logging.h>
22
23#include "BluetoothAudioSessionReport_2_2.h"
24#include "BluetoothAudioSupportedCodecsDB_2_1.h"
25
26namespace android {
27namespace hardware {
28namespace bluetooth {
29namespace audio {
30namespace V2_2 {
31namespace implementation {
32
33using ::android::bluetooth::audio::BluetoothAudioSessionReport_2_2;
34using ::android::hardware::Void;
35using ::android::hardware::bluetooth::audio::V2_0::AudioConfiguration;
36
37// Here the buffer size is based on SBC
38static constexpr uint32_t kPcmFrameSize = 4; // 16 bits per sample / stereo
39// SBC is 128, and here we choose the LCM of 16, 24, and 32
40static constexpr uint32_t kPcmFrameCount = 96;
41static constexpr uint32_t kRtpFrameSize = kPcmFrameSize * kPcmFrameCount;
42// The max counts by 1 tick (20ms) for SBC is about 7. Since using 96 for the
43// PCM counts, here we just choose a greater number
44static constexpr uint32_t kRtpFrameCount = 10;
45static constexpr uint32_t kBufferSize = kRtpFrameSize * kRtpFrameCount;
46static constexpr uint32_t kBufferCount = 2; // double buffer
47static constexpr uint32_t kDataMqSize = kBufferSize * kBufferCount;
48
49A2dpSoftwareAudioProvider::A2dpSoftwareAudioProvider()
50 : BluetoothAudioProvider(), mDataMQ(nullptr) {
51 LOG(INFO) << __func__ << " - size of audio buffer " << kDataMqSize
52 << " byte(s)";
53 std::unique_ptr<DataMQ> tempDataMQ(
54 new DataMQ(kDataMqSize, /* EventFlag */ true));
55 if (tempDataMQ && tempDataMQ->isValid()) {
56 mDataMQ = std::move(tempDataMQ);
57 session_type_ = V2_1::SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH;
58 } else {
59 ALOGE_IF(!tempDataMQ, "failed to allocate data MQ");
60 ALOGE_IF(tempDataMQ && !tempDataMQ->isValid(), "data MQ is invalid");
61 }
62}
63
64bool A2dpSoftwareAudioProvider::isValid(const V2_0::SessionType& sessionType) {
65 return isValid(static_cast<V2_1::SessionType>(sessionType));
66}
67
68bool A2dpSoftwareAudioProvider::isValid(const V2_1::SessionType& sessionType) {
69 return (sessionType == session_type_ && mDataMQ && mDataMQ->isValid());
70}
71
72Return<void> A2dpSoftwareAudioProvider::startSession(
Jakub Pawlowski8d87eb72021-12-06 15:22:03 +010073 const sp<V2_0::IBluetoothAudioPort>& hostIf,
Alice Kuo84e87672021-10-28 12:53:38 +080074 const AudioConfiguration& audioConfig, startSession_cb _hidl_cb) {
75 /**
76 * Initialize the audio platform if audioConfiguration is supported.
77 * Save the IBluetoothAudioPort interface, so that it can be used
78 * later to send stream control commands to the HAL client, based on
79 * interaction with Audio framework.
80 */
81 if (audioConfig.getDiscriminator() !=
82 AudioConfiguration::hidl_discriminator::pcmConfig) {
83 LOG(WARNING) << __func__
84 << " - Invalid Audio Configuration=" << toString(audioConfig);
85 _hidl_cb(BluetoothAudioStatus::UNSUPPORTED_CODEC_CONFIGURATION,
86 DataMQ::Descriptor());
87 return Void();
88 } else if (!android::bluetooth::audio::IsSoftwarePcmConfigurationValid(
89 audioConfig.pcmConfig())) {
90 LOG(WARNING) << __func__ << " - Unsupported PCM Configuration="
91 << toString(audioConfig.pcmConfig());
92 _hidl_cb(BluetoothAudioStatus::UNSUPPORTED_CODEC_CONFIGURATION,
93 DataMQ::Descriptor());
94 return Void();
95 }
96
97 return BluetoothAudioProvider::startSession(hostIf, audioConfig, _hidl_cb);
98}
99
100Return<void> A2dpSoftwareAudioProvider::onSessionReady(
101 startSession_cb _hidl_cb) {
102 if (mDataMQ && mDataMQ->isValid()) {
103 BluetoothAudioSessionReport_2_2::OnSessionStarted(
104 session_type_, stack_iface_, mDataMQ->getDesc(), audio_config_);
105 _hidl_cb(BluetoothAudioStatus::SUCCESS, *mDataMQ->getDesc());
106 } else {
107 _hidl_cb(BluetoothAudioStatus::FAILURE, DataMQ::Descriptor());
108 }
109 return Void();
110}
111
112} // namespace implementation
113} // namespace V2_2
114} // namespace audio
115} // namespace bluetooth
116} // namespace hardware
117} // namespace android