blob: 8090d265a6b02e28701594c14eafe0621f82842f [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 "BTAudioProviderStub"
18
19#include "BluetoothAudioProvider.h"
20
21#include <BluetoothAudioSessionReport.h>
22#include <android-base/logging.h>
23
24namespace aidl {
25namespace android {
26namespace hardware {
27namespace bluetooth {
28namespace audio {
29
30BluetoothAudioProvider::BluetoothAudioProvider() {
31 death_recipient_ = ::ndk::ScopedAIBinder_DeathRecipient(
32 AIBinder_DeathRecipient_new(binderDiedCallbackAidl));
33}
34
35ndk::ScopedAStatus BluetoothAudioProvider::startSession(
36 const std::shared_ptr<IBluetoothAudioPort>& host_if,
37 const AudioConfiguration& audio_config, DataMQDesc* _aidl_return) {
38 if (host_if == nullptr) {
39 *_aidl_return = DataMQDesc();
40 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
41 }
42 audio_config_ = std::make_unique<AudioConfiguration>(audio_config);
43 stack_iface_ = host_if;
44
45 AIBinder_linkToDeath(stack_iface_->asBinder().get(), death_recipient_.get(),
46 this);
47
48 onSessionReady(_aidl_return);
49 return ndk::ScopedAStatus::ok();
50}
51
52ndk::ScopedAStatus BluetoothAudioProvider::endSession() {
53 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_);
54
55 if (stack_iface_ != nullptr) {
56 BluetoothAudioSessionReport::OnSessionEnded(session_type_);
57
58 AIBinder_unlinkToDeath(stack_iface_->asBinder().get(),
59 death_recipient_.get(), this);
60 } else {
61 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
62 << " has NO session";
63 }
64
65 stack_iface_ = nullptr;
66 audio_config_ = nullptr;
67
68 return ndk::ScopedAStatus::ok();
69}
70
71ndk::ScopedAStatus BluetoothAudioProvider::streamStarted(
72 BluetoothAudioStatus status) {
73 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
74 << ", status=" << toString(status);
75
76 if (stack_iface_ != nullptr) {
77 BluetoothAudioSessionReport::ReportControlStatus(session_type_, true,
78 status);
79 } else {
80 LOG(WARNING) << __func__ << " - SessionType=" << toString(session_type_)
81 << ", status=" << toString(status) << " has NO session";
82 }
83
84 return ndk::ScopedAStatus::ok();
85}
86
87ndk::ScopedAStatus BluetoothAudioProvider::streamSuspended(
88 BluetoothAudioStatus status) {
89 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
90 << ", status=" << toString(status);
91
92 if (stack_iface_ != nullptr) {
93 BluetoothAudioSessionReport::ReportControlStatus(session_type_, false,
94 status);
95 } else {
96 LOG(WARNING) << __func__ << " - SessionType=" << toString(session_type_)
97 << ", status=" << toString(status) << " has NO session";
98 }
99 return ndk::ScopedAStatus::ok();
100}
101
102ndk::ScopedAStatus BluetoothAudioProvider::updateAudioConfiguration(
103 const AudioConfiguration& audio_config) {
104 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_);
105
106 if (stack_iface_ == nullptr || audio_config_ == nullptr) {
107 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
108 << " has NO session";
109 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
110 }
111
112 if (audio_config.getTag() != audio_config_->getTag()) {
113 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
114 << " audio config type is not match";
115 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
116 }
117
118 audio_config_ = std::make_unique<AudioConfiguration>(audio_config);
119 BluetoothAudioSessionReport::ReportAudioConfigChanged(session_type_,
120 *audio_config_);
121 return ndk::ScopedAStatus::ok();
122}
123
Chen Chen7cdf8322022-02-08 13:24:11 -0800124ndk::ScopedAStatus BluetoothAudioProvider::setLowLatencyModeAllowed(
125 bool allowed) {
126 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_);
127
128 if (stack_iface_ == nullptr) {
129 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
130 << " has NO session";
131 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
132 }
133 LOG(INFO) << __func__ << " - allowed " << allowed;
Chen Chen81f38e52022-02-09 13:27:35 -0800134 BluetoothAudioSessionReport::ReportLowLatencyModeAllowedChanged(
135 session_type_, allowed);
Chen Chen7cdf8322022-02-08 13:24:11 -0800136 return ndk::ScopedAStatus::ok();
137}
138
Josh Wu6ab53e72021-12-29 23:53:33 -0800139void BluetoothAudioProvider::binderDiedCallbackAidl(void* ptr) {
140 LOG(ERROR) << __func__ << " - BluetoothAudio Service died";
141 auto provider = static_cast<BluetoothAudioProvider*>(ptr);
142 if (provider == nullptr) {
143 LOG(ERROR) << __func__ << ": Null AudioProvider HAL died";
144 return;
145 }
146 provider->endSession();
147}
148
149} // namespace audio
150} // namespace bluetooth
151} // namespace hardware
152} // namespace android
153} // namespace aidl