1 /*
2  * Copyright 2018 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 "BTAudioProvidersFactory"
18 
19 #include <android-base/logging.h>
20 
21 #include "BluetoothAudioProvidersFactory.h"
22 #include "BluetoothAudioSupportedCodecsDB.h"
23 
24 namespace android {
25 namespace hardware {
26 namespace bluetooth {
27 namespace audio {
28 namespace V2_0 {
29 namespace implementation {
30 
31 using ::android::hardware::hidl_vec;
32 using ::android::hardware::Void;
33 
34 A2dpSoftwareAudioProvider
35     BluetoothAudioProvidersFactory::a2dp_software_provider_instance_;
36 A2dpOffloadAudioProvider
37     BluetoothAudioProvidersFactory::a2dp_offload_provider_instance_;
38 HearingAidAudioProvider
39     BluetoothAudioProvidersFactory::hearing_aid_provider_instance_;
40 
openProvider(const SessionType sessionType,openProvider_cb _hidl_cb)41 Return<void> BluetoothAudioProvidersFactory::openProvider(
42     const SessionType sessionType, openProvider_cb _hidl_cb) {
43   LOG(INFO) << __func__ << " - SessionType=" << toString(sessionType);
44   BluetoothAudioStatus status = BluetoothAudioStatus::SUCCESS;
45   BluetoothAudioProvider* provider = nullptr;
46   switch (sessionType) {
47     case SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH:
48       provider = &a2dp_software_provider_instance_;
49       break;
50     case SessionType::A2DP_HARDWARE_OFFLOAD_DATAPATH:
51       provider = &a2dp_offload_provider_instance_;
52       break;
53     case SessionType::HEARING_AID_SOFTWARE_ENCODING_DATAPATH:
54       provider = &hearing_aid_provider_instance_;
55       break;
56     default:
57       status = BluetoothAudioStatus::FAILURE;
58   }
59   if (provider == nullptr || !provider->isValid(sessionType)) {
60     provider = nullptr;
61     status = BluetoothAudioStatus::FAILURE;
62     LOG(ERROR) << __func__ << " - SessionType=" << toString(sessionType)
63                << ", status=" << toString(status);
64   }
65   _hidl_cb(status, provider);
66   return Void();
67 }
68 
getProviderCapabilities(const SessionType sessionType,getProviderCapabilities_cb _hidl_cb)69 Return<void> BluetoothAudioProvidersFactory::getProviderCapabilities(
70     const SessionType sessionType, getProviderCapabilities_cb _hidl_cb) {
71   hidl_vec<AudioCapabilities> audio_capabilities =
72       hidl_vec<AudioCapabilities>(0);
73   if (sessionType == SessionType::A2DP_HARDWARE_OFFLOAD_DATAPATH) {
74     std::vector<CodecCapabilities> db_codec_capabilities =
75         android::bluetooth::audio::GetOffloadCodecCapabilities(sessionType);
76     if (db_codec_capabilities.size()) {
77       audio_capabilities.resize(db_codec_capabilities.size());
78       for (int i = 0; i < db_codec_capabilities.size(); ++i) {
79         audio_capabilities[i].codecCapabilities(db_codec_capabilities[i]);
80       }
81     }
82   } else if (sessionType != SessionType::UNKNOWN) {
83     std::vector<PcmParameters> db_pcm_capabilities =
84         android::bluetooth::audio::GetSoftwarePcmCapabilities();
85     if (db_pcm_capabilities.size() == 1) {
86       audio_capabilities.resize(1);
87       audio_capabilities[0].pcmCapabilities(db_pcm_capabilities[0]);
88     }
89   }
90   LOG(INFO) << __func__ << " - SessionType=" << toString(sessionType)
91             << " supports " << audio_capabilities.size() << " codecs";
92   _hidl_cb(audio_capabilities);
93   return Void();
94 }
95 
HIDL_FETCH_IBluetoothAudioProvidersFactory(const char *)96 IBluetoothAudioProvidersFactory* HIDL_FETCH_IBluetoothAudioProvidersFactory(
97     const char* /* name */) {
98   return new BluetoothAudioProvidersFactory();
99 }
100 
101 }  // namespace implementation
102 }  // namespace V2_0
103 }  // namespace audio
104 }  // namespace bluetooth
105 }  // namespace hardware
106 }  // namespace android
107