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