blob: 59df3a99017131cc8f7991ccc4dd3fd98e2344fc [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 "BTAudioProviderA2dpSW"
18
19#include "A2dpSoftwareAudioProvider.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
31// Here the buffer size is based on SBC
32static constexpr uint32_t kPcmFrameSize = 4; // 16 bits per sample / stereo
33// SBC is 128, and here we choose the LCM of 16, 24, and 32
34static constexpr uint32_t kPcmFrameCount = 96;
35static constexpr uint32_t kRtpFrameSize = kPcmFrameSize * kPcmFrameCount;
36// The max counts by 1 tick (20ms) for SBC is about 7. Since using 96 for the
37// PCM counts, here we just choose a greater number
38static constexpr uint32_t kRtpFrameCount = 10;
39static constexpr uint32_t kBufferSize = kRtpFrameSize * kRtpFrameCount;
40static constexpr uint32_t kBufferCount = 2; // double buffer
41static constexpr uint32_t kDataMqSize = kBufferSize * kBufferCount;
42
43A2dpSoftwareAudioProvider::A2dpSoftwareAudioProvider()
44 : BluetoothAudioProvider(), data_mq_(nullptr) {
45 LOG(INFO) << __func__ << " - size of audio buffer " << kDataMqSize
46 << " byte(s)";
47 std::unique_ptr<DataMQ> data_mq(
48 new DataMQ(kDataMqSize, /* EventFlag */ true));
49 if (data_mq && data_mq->isValid()) {
50 data_mq_ = std::move(data_mq);
51 session_type_ = SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH;
52 } else {
53 ALOGE_IF(!data_mq, "failed to allocate data MQ");
54 ALOGE_IF(data_mq && !data_mq->isValid(), "data MQ is invalid");
55 }
56}
57
58bool A2dpSoftwareAudioProvider::isValid(const SessionType& sessionType) {
59 return (sessionType == session_type_ && data_mq_ && data_mq_->isValid());
60}
61
62ndk::ScopedAStatus A2dpSoftwareAudioProvider::startSession(
63 const std::shared_ptr<IBluetoothAudioPort>& host_if,
Chen Chenc92270e2022-02-14 18:29:52 -080064 const AudioConfiguration& audio_config,
Cheney Ni6ecbc762022-03-03 00:12:48 +080065 const std::vector<LatencyMode>& latency_modes, DataMQDesc* _aidl_return) {
Josh Wu6ab53e72021-12-29 23:53:33 -080066 if (audio_config.getTag() != AudioConfiguration::pcmConfig) {
67 LOG(WARNING) << __func__ << " - Invalid Audio Configuration="
68 << audio_config.toString();
69 *_aidl_return = DataMQDesc();
70 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
71 }
72 const PcmConfiguration& pcm_config =
73 audio_config.get<AudioConfiguration::pcmConfig>();
74 if (!BluetoothAudioCodecs::IsSoftwarePcmConfigurationValid(pcm_config)) {
75 LOG(WARNING) << __func__ << " - Unsupported PCM Configuration="
76 << pcm_config.toString();
77 *_aidl_return = DataMQDesc();
78 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
79 }
80
Chen Chenc92270e2022-02-14 18:29:52 -080081 return BluetoothAudioProvider::startSession(
82 host_if, audio_config, latency_modes, _aidl_return);
Josh Wu6ab53e72021-12-29 23:53:33 -080083}
84
85ndk::ScopedAStatus A2dpSoftwareAudioProvider::onSessionReady(
86 DataMQDesc* _aidl_return) {
87 if (data_mq_ == nullptr || !data_mq_->isValid()) {
88 *_aidl_return = DataMQDesc();
89 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
90 }
91 *_aidl_return = data_mq_->dupeDesc();
Josh Wu98d7e082022-01-21 03:04:21 -080092 auto desc = data_mq_->dupeDesc();
Cheney Ni6ecbc762022-03-03 00:12:48 +080093 BluetoothAudioSessionReport::OnSessionStarted(
94 session_type_, stack_iface_, &desc, *audio_config_, latency_modes_);
Josh Wu6ab53e72021-12-29 23:53:33 -080095 return ndk::ScopedAStatus::ok();
96}
97
98} // namespace audio
99} // namespace bluetooth
100} // namespace hardware
101} // namespace android
Cheney Ni6ecbc762022-03-03 00:12:48 +0800102} // namespace aidl