• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.android.bluetooth.a2dp;
18 
19 import android.bluetooth.BluetoothCodecConfig;
20 import android.bluetooth.BluetoothDevice;
21 import android.content.Context;
22 import android.content.res.Resources;
23 import android.content.res.Resources.NotFoundException;
24 
25 import com.android.bluetooth.R;
26 
27 /*
28  * A2DP Codec Configuration setup.
29  */
30 class A2dpCodecConfig {
31     private static final boolean DBG = true;
32     private static final String TAG = "A2dpCodecConfig";
33 
34     private Context mContext;
35     private A2dpNativeInterface mA2dpNativeInterface;
36 
37     private BluetoothCodecConfig[] mCodecConfigPriorities;
38     private int mA2dpSourceCodecPrioritySbc = BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT;
39     private int mA2dpSourceCodecPriorityAac = BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT;
40     private int mA2dpSourceCodecPriorityAptx = BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT;
41     private int mA2dpSourceCodecPriorityAptxHd = BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT;
42     private int mA2dpSourceCodecPriorityLdac = BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT;
43 
A2dpCodecConfig(Context context, A2dpNativeInterface a2dpNativeInterface)44     A2dpCodecConfig(Context context, A2dpNativeInterface a2dpNativeInterface) {
45         mContext = context;
46         mA2dpNativeInterface = a2dpNativeInterface;
47         mCodecConfigPriorities = assignCodecConfigPriorities();
48     }
49 
codecConfigPriorities()50     BluetoothCodecConfig[] codecConfigPriorities() {
51         return mCodecConfigPriorities;
52     }
53 
setCodecConfigPreference(BluetoothDevice device, BluetoothCodecConfig codecConfig)54     void setCodecConfigPreference(BluetoothDevice device,
55                                   BluetoothCodecConfig codecConfig) {
56         BluetoothCodecConfig[] codecConfigArray = new BluetoothCodecConfig[1];
57         codecConfigArray[0] = codecConfig;
58         mA2dpNativeInterface.setCodecConfigPreference(device, codecConfigArray);
59     }
60 
enableOptionalCodecs(BluetoothDevice device)61     void enableOptionalCodecs(BluetoothDevice device) {
62         BluetoothCodecConfig[] codecConfigArray = assignCodecConfigPriorities();
63         if (codecConfigArray == null) {
64             return;
65         }
66 
67         // Set the mandatory codec's priority to default, and remove the rest
68         for (int i = 0; i < codecConfigArray.length; i++) {
69             BluetoothCodecConfig codecConfig = codecConfigArray[i];
70             if (!codecConfig.isMandatoryCodec()) {
71                 codecConfigArray[i] = null;
72             }
73         }
74 
75         mA2dpNativeInterface.setCodecConfigPreference(device, codecConfigArray);
76     }
77 
disableOptionalCodecs(BluetoothDevice device)78     void disableOptionalCodecs(BluetoothDevice device) {
79         BluetoothCodecConfig[] codecConfigArray = assignCodecConfigPriorities();
80         if (codecConfigArray == null) {
81             return;
82         }
83         // Set the mandatory codec's priority to highest, and remove the rest
84         for (int i = 0; i < codecConfigArray.length; i++) {
85             BluetoothCodecConfig codecConfig = codecConfigArray[i];
86             if (codecConfig.isMandatoryCodec()) {
87                 codecConfig.setCodecPriority(BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST);
88             } else {
89                 codecConfigArray[i] = null;
90             }
91         }
92         mA2dpNativeInterface.setCodecConfigPreference(device, codecConfigArray);
93     }
94 
95     // Assign the A2DP Source codec config priorities
assignCodecConfigPriorities()96     private BluetoothCodecConfig[] assignCodecConfigPriorities() {
97         Resources resources = mContext.getResources();
98         if (resources == null) {
99             return null;
100         }
101 
102         int value;
103         try {
104             value = resources.getInteger(R.integer.a2dp_source_codec_priority_sbc);
105         } catch (NotFoundException e) {
106             value = BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT;
107         }
108         if ((value >= BluetoothCodecConfig.CODEC_PRIORITY_DISABLED) && (value
109                 < BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST)) {
110             mA2dpSourceCodecPrioritySbc = value;
111         }
112 
113         try {
114             value = resources.getInteger(R.integer.a2dp_source_codec_priority_aac);
115         } catch (NotFoundException e) {
116             value = BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT;
117         }
118         if ((value >= BluetoothCodecConfig.CODEC_PRIORITY_DISABLED) && (value
119                 < BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST)) {
120             mA2dpSourceCodecPriorityAac = value;
121         }
122 
123         try {
124             value = resources.getInteger(R.integer.a2dp_source_codec_priority_aptx);
125         } catch (NotFoundException e) {
126             value = BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT;
127         }
128         if ((value >= BluetoothCodecConfig.CODEC_PRIORITY_DISABLED) && (value
129                 < BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST)) {
130             mA2dpSourceCodecPriorityAptx = value;
131         }
132 
133         try {
134             value = resources.getInteger(R.integer.a2dp_source_codec_priority_aptx_hd);
135         } catch (NotFoundException e) {
136             value = BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT;
137         }
138         if ((value >= BluetoothCodecConfig.CODEC_PRIORITY_DISABLED) && (value
139                 < BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST)) {
140             mA2dpSourceCodecPriorityAptxHd = value;
141         }
142 
143         try {
144             value = resources.getInteger(R.integer.a2dp_source_codec_priority_ldac);
145         } catch (NotFoundException e) {
146             value = BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT;
147         }
148         if ((value >= BluetoothCodecConfig.CODEC_PRIORITY_DISABLED) && (value
149                 < BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST)) {
150             mA2dpSourceCodecPriorityLdac = value;
151         }
152 
153         BluetoothCodecConfig codecConfig;
154         BluetoothCodecConfig[] codecConfigArray =
155                 new BluetoothCodecConfig[BluetoothCodecConfig.SOURCE_CODEC_TYPE_MAX];
156         codecConfig = new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
157                 mA2dpSourceCodecPrioritySbc, BluetoothCodecConfig.SAMPLE_RATE_NONE,
158                 BluetoothCodecConfig.BITS_PER_SAMPLE_NONE, BluetoothCodecConfig
159                 .CHANNEL_MODE_NONE, 0 /* codecSpecific1 */,
160                 0 /* codecSpecific2 */, 0 /* codecSpecific3 */, 0 /* codecSpecific4 */);
161         codecConfigArray[0] = codecConfig;
162         codecConfig = new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_AAC,
163                 mA2dpSourceCodecPriorityAac, BluetoothCodecConfig.SAMPLE_RATE_NONE,
164                 BluetoothCodecConfig.BITS_PER_SAMPLE_NONE, BluetoothCodecConfig
165                 .CHANNEL_MODE_NONE, 0 /* codecSpecific1 */,
166                 0 /* codecSpecific2 */, 0 /* codecSpecific3 */, 0 /* codecSpecific4 */);
167         codecConfigArray[1] = codecConfig;
168         codecConfig = new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX,
169                 mA2dpSourceCodecPriorityAptx, BluetoothCodecConfig.SAMPLE_RATE_NONE,
170                 BluetoothCodecConfig.BITS_PER_SAMPLE_NONE, BluetoothCodecConfig
171                 .CHANNEL_MODE_NONE, 0 /* codecSpecific1 */,
172                 0 /* codecSpecific2 */, 0 /* codecSpecific3 */, 0 /* codecSpecific4 */);
173         codecConfigArray[2] = codecConfig;
174         codecConfig = new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX_HD,
175                 mA2dpSourceCodecPriorityAptxHd, BluetoothCodecConfig.SAMPLE_RATE_NONE,
176                 BluetoothCodecConfig.BITS_PER_SAMPLE_NONE, BluetoothCodecConfig
177                 .CHANNEL_MODE_NONE, 0 /* codecSpecific1 */,
178                 0 /* codecSpecific2 */, 0 /* codecSpecific3 */, 0 /* codecSpecific4 */);
179         codecConfigArray[3] = codecConfig;
180         codecConfig = new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_LDAC,
181                 mA2dpSourceCodecPriorityLdac, BluetoothCodecConfig.SAMPLE_RATE_NONE,
182                 BluetoothCodecConfig.BITS_PER_SAMPLE_NONE, BluetoothCodecConfig
183                 .CHANNEL_MODE_NONE, 0 /* codecSpecific1 */,
184                 0 /* codecSpecific2 */, 0 /* codecSpecific3 */, 0 /* codecSpecific4 */);
185         codecConfigArray[4] = codecConfig;
186 
187         return codecConfigArray;
188     }
189 }
190 
191