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 | |
Josh Wu | 98d7e08 | 2022-01-21 03:04:21 -0800 | [diff] [blame] | 17 | #define LOG_TAG "BTAudioProviderFactoryAIDL" |
Josh Wu | 6ab53e7 | 2021-12-29 23:53:33 -0800 | [diff] [blame] | 18 | |
| 19 | #include "BluetoothAudioProviderFactory.h" |
| 20 | |
| 21 | #include <BluetoothAudioCodecs.h> |
| 22 | #include <android-base/logging.h> |
| 23 | |
| 24 | #include "A2dpOffloadAudioProvider.h" |
| 25 | #include "A2dpSoftwareAudioProvider.h" |
| 26 | #include "BluetoothAudioProvider.h" |
| 27 | #include "HearingAidAudioProvider.h" |
| 28 | #include "LeAudioOffloadAudioProvider.h" |
| 29 | #include "LeAudioSoftwareAudioProvider.h" |
| 30 | |
| 31 | namespace aidl { |
| 32 | namespace android { |
| 33 | namespace hardware { |
| 34 | namespace bluetooth { |
| 35 | namespace audio { |
| 36 | |
| 37 | BluetoothAudioProviderFactory::BluetoothAudioProviderFactory() {} |
| 38 | |
| 39 | ndk::ScopedAStatus BluetoothAudioProviderFactory::openProvider( |
| 40 | const SessionType session_type, |
| 41 | std::shared_ptr<IBluetoothAudioProvider>* _aidl_return) { |
| 42 | LOG(INFO) << __func__ << " - SessionType=" << toString(session_type); |
| 43 | std::shared_ptr<BluetoothAudioProvider> provider = nullptr; |
| 44 | |
| 45 | switch (session_type) { |
| 46 | case SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH: |
| 47 | provider = ndk::SharedRefBase::make<A2dpSoftwareAudioProvider>(); |
| 48 | break; |
| 49 | case SessionType::A2DP_HARDWARE_OFFLOAD_ENCODING_DATAPATH: |
| 50 | provider = ndk::SharedRefBase::make<A2dpOffloadAudioProvider>(); |
| 51 | break; |
| 52 | case SessionType::HEARING_AID_SOFTWARE_ENCODING_DATAPATH: |
| 53 | provider = ndk::SharedRefBase::make<HearingAidAudioProvider>(); |
| 54 | break; |
| 55 | case SessionType::LE_AUDIO_SOFTWARE_ENCODING_DATAPATH: |
| 56 | provider = ndk::SharedRefBase::make<LeAudioSoftwareOutputAudioProvider>(); |
| 57 | break; |
| 58 | case SessionType::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH: |
| 59 | provider = ndk::SharedRefBase::make<LeAudioOffloadOutputAudioProvider>(); |
| 60 | break; |
| 61 | case SessionType::LE_AUDIO_SOFTWARE_DECODING_DATAPATH: |
| 62 | provider = ndk::SharedRefBase::make<LeAudioSoftwareInputAudioProvider>(); |
| 63 | break; |
| 64 | case SessionType::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH: |
| 65 | provider = ndk::SharedRefBase::make<LeAudioOffloadInputAudioProvider>(); |
| 66 | break; |
| 67 | default: |
| 68 | provider = nullptr; |
| 69 | break; |
| 70 | } |
| 71 | |
| 72 | if (provider == nullptr || !provider->isValid(session_type)) { |
| 73 | provider = nullptr; |
| 74 | LOG(ERROR) << __func__ << " - SessionType=" << toString(session_type); |
| 75 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 76 | } |
| 77 | *_aidl_return = provider; |
| 78 | |
| 79 | return ndk::ScopedAStatus::ok(); |
| 80 | } |
| 81 | |
| 82 | ndk::ScopedAStatus BluetoothAudioProviderFactory::getProviderCapabilities( |
| 83 | const SessionType session_type, |
| 84 | std::vector<AudioCapabilities>* _aidl_return) { |
| 85 | if (session_type == SessionType::A2DP_HARDWARE_OFFLOAD_ENCODING_DATAPATH) { |
| 86 | auto codec_capabilities = |
| 87 | BluetoothAudioCodecs::GetA2dpOffloadCodecCapabilities(session_type); |
| 88 | _aidl_return->resize(codec_capabilities.size()); |
| 89 | for (int i = 0; i < codec_capabilities.size(); i++) { |
| 90 | _aidl_return->at(i).set<AudioCapabilities::a2dpCapabilities>( |
| 91 | codec_capabilities[i]); |
| 92 | } |
| 93 | } else if (session_type == |
| 94 | SessionType::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH || |
| 95 | session_type == |
| 96 | SessionType::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH) { |
| 97 | std::vector<LeAudioCodecCapabilitiesSetting> db_codec_capabilities = |
| 98 | BluetoothAudioCodecs::GetLeAudioOffloadCodecCapabilities(session_type); |
| 99 | if (db_codec_capabilities.size()) { |
| 100 | _aidl_return->resize(db_codec_capabilities.size()); |
| 101 | for (int i = 0; i < db_codec_capabilities.size(); ++i) { |
| 102 | _aidl_return->at(i).set<AudioCapabilities::leAudioCapabilities>( |
| 103 | db_codec_capabilities[i]); |
| 104 | } |
| 105 | } |
| 106 | } else if (session_type != SessionType::UNKNOWN) { |
| 107 | auto pcm_capabilities = BluetoothAudioCodecs::GetSoftwarePcmCapabilities(); |
| 108 | _aidl_return->resize(pcm_capabilities.size()); |
| 109 | for (int i = 0; i < pcm_capabilities.size(); i++) { |
| 110 | _aidl_return->at(i).set<AudioCapabilities::pcmCapabilities>( |
| 111 | pcm_capabilities[i]); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | LOG(INFO) << __func__ << " - SessionType=" << toString(session_type) |
| 116 | << " supports " << _aidl_return->size() << " codecs"; |
| 117 | return ndk::ScopedAStatus::ok(); |
| 118 | } |
| 119 | |
| 120 | } // namespace audio |
| 121 | } // namespace bluetooth |
| 122 | } // namespace hardware |
| 123 | } // namespace android |
| 124 | } // namespace aidl |