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 "BTAudioProviderSessionCodecsDB_2_1"
18 
19 #include "BluetoothAudioSupportedCodecsDB_2_1.h"
20 
21 #include <android-base/logging.h>
22 
23 namespace android {
24 namespace bluetooth {
25 namespace audio {
26 
27 using ::android::hardware::bluetooth::audio::V2_0::BitsPerSample;
28 using ::android::hardware::bluetooth::audio::V2_0::ChannelMode;
29 
30 using SampleRate_2_0 = ::android::hardware::bluetooth::audio::V2_0::SampleRate;
31 using SampleRate_2_1 = ::android::hardware::bluetooth::audio::V2_1::SampleRate;
32 
33 using SessionType_2_1 =
34     ::android::hardware::bluetooth::audio::V2_1::SessionType;
35 using SessionType_2_0 =
36     ::android::hardware::bluetooth::audio::V2_0::SessionType;
37 
38 namespace {
is_2_0_session_type(const::android::hardware::bluetooth::audio::V2_1::SessionType & session_type)39 bool is_2_0_session_type(
40     const ::android::hardware::bluetooth::audio::V2_1::SessionType&
41         session_type) {
42   if (session_type == SessionType_2_1::A2DP_SOFTWARE_ENCODING_DATAPATH ||
43       session_type == SessionType_2_1::A2DP_HARDWARE_OFFLOAD_DATAPATH ||
44       session_type == SessionType_2_1::HEARING_AID_SOFTWARE_ENCODING_DATAPATH) {
45     return true;
46   } else {
47     return false;
48   }
49 }
50 }  // namespace
51 
52 static const ::android::hardware::bluetooth::audio::V2_1::PcmParameters
53     kDefaultSoftwarePcmCapabilities_2_1 = {
54         .sampleRate = static_cast<SampleRate_2_1>(
55             SampleRate_2_1::RATE_44100 | SampleRate_2_1::RATE_48000 |
56             SampleRate_2_1::RATE_88200 | SampleRate_2_1::RATE_96000 |
57             SampleRate_2_1::RATE_16000 | SampleRate_2_1::RATE_24000),
58         .channelMode =
59             static_cast<ChannelMode>(ChannelMode::MONO | ChannelMode::STEREO),
60         .bitsPerSample = static_cast<BitsPerSample>(BitsPerSample::BITS_16 |
61                                                     BitsPerSample::BITS_24 |
62                                                     BitsPerSample::BITS_32)};
63 
64 std::vector<::android::hardware::bluetooth::audio::V2_1::PcmParameters>
GetSoftwarePcmCapabilities_2_1()65 GetSoftwarePcmCapabilities_2_1() {
66   return std::vector<
67       ::android::hardware::bluetooth::audio::V2_1::PcmParameters>(
68       1, kDefaultSoftwarePcmCapabilities_2_1);
69 }
70 
GetOffloadCodecCapabilities(const::android::hardware::bluetooth::audio::V2_1::SessionType & session_type)71 std::vector<CodecCapabilities> GetOffloadCodecCapabilities(
72     const ::android::hardware::bluetooth::audio::V2_1::SessionType&
73         session_type) {
74   if (is_2_0_session_type(session_type)) {
75     return GetOffloadCodecCapabilities(
76         static_cast<SessionType_2_0>(session_type));
77   }
78   return std::vector<CodecCapabilities>(0);
79 }
80 
IsSoftwarePcmConfigurationValid_2_1(const::android::hardware::bluetooth::audio::V2_1::PcmParameters & pcm_config)81 bool IsSoftwarePcmConfigurationValid_2_1(
82     const ::android::hardware::bluetooth::audio::V2_1::PcmParameters&
83         pcm_config) {
84   if ((pcm_config.sampleRate != SampleRate_2_1::RATE_44100 &&
85        pcm_config.sampleRate != SampleRate_2_1::RATE_48000 &&
86        pcm_config.sampleRate != SampleRate_2_1::RATE_88200 &&
87        pcm_config.sampleRate != SampleRate_2_1::RATE_96000 &&
88        pcm_config.sampleRate != SampleRate_2_1::RATE_16000 &&
89        pcm_config.sampleRate != SampleRate_2_1::RATE_24000) ||
90       (pcm_config.bitsPerSample != BitsPerSample::BITS_16 &&
91        pcm_config.bitsPerSample != BitsPerSample::BITS_24 &&
92        pcm_config.bitsPerSample != BitsPerSample::BITS_32) ||
93       (pcm_config.channelMode != ChannelMode::MONO &&
94        pcm_config.channelMode != ChannelMode::STEREO)) {
95     LOG(WARNING) << __func__
96                  << ": Invalid PCM Configuration=" << toString(pcm_config);
97     return false;
98   } else if (pcm_config.sampleRate &
99                  kDefaultSoftwarePcmCapabilities_2_1.sampleRate &&
100              pcm_config.bitsPerSample &
101                  kDefaultSoftwarePcmCapabilities_2_1.bitsPerSample &&
102              pcm_config.channelMode &
103                  kDefaultSoftwarePcmCapabilities_2_1.channelMode &&
104              pcm_config.dataIntervalUs != 0) {
105     return true;
106   }
107   LOG(WARNING) << __func__
108                << ": Unsupported PCM Configuration=" << toString(pcm_config);
109   return false;
110 }
111 
IsOffloadCodecConfigurationValid(const::android::hardware::bluetooth::audio::V2_1::SessionType & session_type,const::android::hardware::bluetooth::audio::V2_0::CodecConfiguration & codec_config)112 bool IsOffloadCodecConfigurationValid(
113     const ::android::hardware::bluetooth::audio::V2_1::SessionType&
114         session_type,
115     const ::android::hardware::bluetooth::audio::V2_0::CodecConfiguration&
116         codec_config) {
117   if (is_2_0_session_type(session_type)) {
118     return IsOffloadCodecConfigurationValid(
119         static_cast<SessionType_2_0>(session_type), codec_config);
120   }
121 
122   return false;
123 }
124 
125 }  // namespace audio
126 }  // namespace bluetooth
127 }  // namespace android
128