blob: f5df9e382309d417136e6cbac8472b0211cc0495 [file] [log] [blame]
Patty5c05c2f2021-11-04 21:03:32 +08001/*
2 * Copyright (C) 2021 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
17package android.bluetooth;
18
Sagar Vermacda6dbf2024-01-19 11:32:47 +053019import android.annotation.FlaggedApi;
Patty5c05c2f2021-11-04 21:03:32 +080020import android.annotation.IntDef;
21import android.annotation.NonNull;
Anton Hanssonfa927a52023-11-09 09:08:19 +000022import android.annotation.Nullable;
Pattyd8c49272022-01-19 16:26:00 +080023import android.os.Parcel;
24import android.os.Parcelable;
Patty5c05c2f2021-11-04 21:03:32 +080025
Sagar Vermacda6dbf2024-01-19 11:32:47 +053026import com.android.bluetooth.flags.Flags;
27
Patty5c05c2f2021-11-04 21:03:32 +080028import java.lang.annotation.Retention;
29import java.lang.annotation.RetentionPolicy;
Patty5aa4bd22022-01-25 19:58:37 +080030import java.util.Objects;
Patty5c05c2f2021-11-04 21:03:32 +080031
32/**
33 * Represents the codec configuration for a Bluetooth LE Audio source device.
Patty5c05c2f2021-11-04 21:03:32 +080034 *
David Duarteee52b7e2023-12-02 01:32:11 +000035 * <p>Contains the source codec type.
36 *
37 * <p>The source codec type values are the same as those supported by the device hardware.
38 *
David Duarte5a02bb42023-12-04 23:07:42 +000039 * @see BluetoothLeAudioCodecConfig
Patty5c05c2f2021-11-04 21:03:32 +080040 */
Pattyd8c49272022-01-19 16:26:00 +080041public final class BluetoothLeAudioCodecConfig implements Parcelable {
Patty5c05c2f2021-11-04 21:03:32 +080042 // Add an entry for each source codec here.
43
44 /** @hide */
David Duarteee52b7e2023-12-02 01:32:11 +000045 @IntDef(
46 prefix = "SOURCE_CODEC_TYPE_",
Jakub Tyszkowski676bd922025-04-10 11:02:14 +000047 value = {
48 SOURCE_CODEC_TYPE_LC3,
49 SOURCE_CODEC_TYPE_OPUS,
50 SOURCE_CODEC_TYPE_OPUS_HI_RES,
51 SOURCE_CODEC_TYPE_INVALID
52 })
Patty5c05c2f2021-11-04 21:03:32 +080053 @Retention(RetentionPolicy.SOURCE)
54 public @interface SourceCodecType {};
55
56 public static final int SOURCE_CODEC_TYPE_LC3 = 0;
Łukasz Rymanowski61e11882024-11-20 14:13:27 +000057
58 @FlaggedApi(Flags.FLAG_LEAUDIO_ADD_OPUS_CODEC_TYPE)
59 public static final int SOURCE_CODEC_TYPE_OPUS = 1;
60
Jakub Tyszkowski676bd922025-04-10 11:02:14 +000061 /** @hide */
62 public static final int SOURCE_CODEC_TYPE_OPUS_HI_RES = 2;
63
Patty5c05c2f2021-11-04 21:03:32 +080064 public static final int SOURCE_CODEC_TYPE_INVALID = 1000 * 1000;
65
Pattyd8c49272022-01-19 16:26:00 +080066 /** @hide */
David Duarteee52b7e2023-12-02 01:32:11 +000067 @IntDef(
68 prefix = "CODEC_PRIORITY_",
Pattyd8c49272022-01-19 16:26:00 +080069 value = {CODEC_PRIORITY_DISABLED, CODEC_PRIORITY_DEFAULT, CODEC_PRIORITY_HIGHEST})
70 @Retention(RetentionPolicy.SOURCE)
71 public @interface CodecPriority {}
72
73 /**
David Duarteee52b7e2023-12-02 01:32:11 +000074 * Codec priority disabled. Used to indicate that this codec is disabled and should not be used.
Pattyd8c49272022-01-19 16:26:00 +080075 */
76 public static final int CODEC_PRIORITY_DISABLED = -1;
77
David Duarteee52b7e2023-12-02 01:32:11 +000078 /** Codec priority default. Default value used for codec priority. */
Pattyd8c49272022-01-19 16:26:00 +080079 public static final int CODEC_PRIORITY_DEFAULT = 0;
80
David Duarteee52b7e2023-12-02 01:32:11 +000081 /** Codec priority highest. Used to indicate the highest priority a codec can have. */
Pattyd8c49272022-01-19 16:26:00 +080082 public static final int CODEC_PRIORITY_HIGHEST = 1000 * 1000;
83
84 /** @hide */
David Duarteee52b7e2023-12-02 01:32:11 +000085 @IntDef(
86 flag = true,
87 prefix = "SAMPLE_RATE_",
88 value = {
89 SAMPLE_RATE_NONE,
90 SAMPLE_RATE_8000,
Sagar Vermacda6dbf2024-01-19 11:32:47 +053091 SAMPLE_RATE_11025,
David Duarteee52b7e2023-12-02 01:32:11 +000092 SAMPLE_RATE_16000,
Sagar Vermacda6dbf2024-01-19 11:32:47 +053093 SAMPLE_RATE_22050,
David Duarteee52b7e2023-12-02 01:32:11 +000094 SAMPLE_RATE_24000,
95 SAMPLE_RATE_32000,
96 SAMPLE_RATE_44100,
Sagar Vermacda6dbf2024-01-19 11:32:47 +053097 SAMPLE_RATE_48000,
98 SAMPLE_RATE_88200,
99 SAMPLE_RATE_96000,
100 SAMPLE_RATE_176400,
101 SAMPLE_RATE_192000,
102 SAMPLE_RATE_384000
David Duarteee52b7e2023-12-02 01:32:11 +0000103 })
Pattyd8c49272022-01-19 16:26:00 +0800104 @Retention(RetentionPolicy.SOURCE)
105 public @interface SampleRate {}
106
107 /**
David Duarteee52b7e2023-12-02 01:32:11 +0000108 * Codec sample rate 0 Hz. Default value used for codec sample rate. Values are the bit mask as
109 * defined in the Bluetooth Assigned Numbers, Generic Audio, Supported_Sampling_Frequencies
Sagar Vermacda6dbf2024-01-19 11:32:47 +0530110 * table.
Pattyd8c49272022-01-19 16:26:00 +0800111 */
112 public static final int SAMPLE_RATE_NONE = 0;
113
David Duarteee52b7e2023-12-02 01:32:11 +0000114 /** Codec sample rate 8000 Hz. */
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000115 public static final int SAMPLE_RATE_8000 = 0x01 << 0;
Pattyd8c49272022-01-19 16:26:00 +0800116
Sagar Vermacda6dbf2024-01-19 11:32:47 +0530117 /** Codec sample rate 11025 Hz. */
Sagar Vermacda6dbf2024-01-19 11:32:47 +0530118 public static final int SAMPLE_RATE_11025 = 0x01 << 1;
119
David Duarteee52b7e2023-12-02 01:32:11 +0000120 /** Codec sample rate 16000 Hz. */
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000121 public static final int SAMPLE_RATE_16000 = 0x01 << 2;
Pattyd8c49272022-01-19 16:26:00 +0800122
Sagar Vermacda6dbf2024-01-19 11:32:47 +0530123 /** Codec sample rate 22050 Hz. */
Sagar Vermacda6dbf2024-01-19 11:32:47 +0530124 public static final int SAMPLE_RATE_22050 = 0x01 << 3;
125
David Duarteee52b7e2023-12-02 01:32:11 +0000126 /** Codec sample rate 24000 Hz. */
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000127 public static final int SAMPLE_RATE_24000 = 0x01 << 4;
Pattyd8c49272022-01-19 16:26:00 +0800128
David Duarteee52b7e2023-12-02 01:32:11 +0000129 /** Codec sample rate 32000 Hz. */
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000130 public static final int SAMPLE_RATE_32000 = 0x01 << 5;
Pattyd8c49272022-01-19 16:26:00 +0800131
David Duarteee52b7e2023-12-02 01:32:11 +0000132 /** Codec sample rate 44100 Hz. */
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000133 public static final int SAMPLE_RATE_44100 = 0x01 << 6;
Pattyd8c49272022-01-19 16:26:00 +0800134
David Duarteee52b7e2023-12-02 01:32:11 +0000135 /** Codec sample rate 48000 Hz. */
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000136 public static final int SAMPLE_RATE_48000 = 0x01 << 7;
Pattyd8c49272022-01-19 16:26:00 +0800137
Sagar Vermacda6dbf2024-01-19 11:32:47 +0530138 /** Codec sample rate 88200 Hz. */
Sagar Vermacda6dbf2024-01-19 11:32:47 +0530139 public static final int SAMPLE_RATE_88200 = 0x01 << 8;
140
141 /** Codec sample rate 96000 Hz. */
Sagar Vermacda6dbf2024-01-19 11:32:47 +0530142 public static final int SAMPLE_RATE_96000 = 0x01 << 9;
143
144 /** Codec sample rate 176400 Hz. */
Sagar Vermacda6dbf2024-01-19 11:32:47 +0530145 public static final int SAMPLE_RATE_176400 = 0x01 << 10;
146
147 /** Codec sample rate 192000 Hz. */
Sagar Vermacda6dbf2024-01-19 11:32:47 +0530148 public static final int SAMPLE_RATE_192000 = 0x01 << 11;
149
150 /** Codec sample rate 384000 Hz. */
Sagar Vermacda6dbf2024-01-19 11:32:47 +0530151 public static final int SAMPLE_RATE_384000 = 0x01 << 12;
152
Pattyd8c49272022-01-19 16:26:00 +0800153 /** @hide */
David Duarteee52b7e2023-12-02 01:32:11 +0000154 @IntDef(
155 flag = true,
156 prefix = "BITS_PER_SAMPLE_",
157 value = {
158 BITS_PER_SAMPLE_NONE,
159 BITS_PER_SAMPLE_16,
160 BITS_PER_SAMPLE_24,
161 BITS_PER_SAMPLE_32
162 })
Pattyd8c49272022-01-19 16:26:00 +0800163 @Retention(RetentionPolicy.SOURCE)
164 public @interface BitsPerSample {}
165
David Duarteee52b7e2023-12-02 01:32:11 +0000166 /** Codec bits per sample 0. Default value of the codec bits per sample. */
Pattyd8c49272022-01-19 16:26:00 +0800167 public static final int BITS_PER_SAMPLE_NONE = 0;
168
David Duarteee52b7e2023-12-02 01:32:11 +0000169 /** Codec bits per sample 16. */
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000170 public static final int BITS_PER_SAMPLE_16 = 0x01 << 0;
Pattyd8c49272022-01-19 16:26:00 +0800171
David Duarteee52b7e2023-12-02 01:32:11 +0000172 /** Codec bits per sample 24. */
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000173 public static final int BITS_PER_SAMPLE_24 = 0x01 << 1;
Pattyd8c49272022-01-19 16:26:00 +0800174
David Duarteee52b7e2023-12-02 01:32:11 +0000175 /** Codec bits per sample 32. */
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000176 public static final int BITS_PER_SAMPLE_32 = 0x01 << 3;
Pattyd8c49272022-01-19 16:26:00 +0800177
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000178 /**
David Duarteee52b7e2023-12-02 01:32:11 +0000179 * Values are the bit mask as defined in the Bluetooth Assigned Numbers, Generic Audio,
180 * Supported_Audio_Channel_Counts table Note: We use only part of it.
181 *
182 * @hide
183 */
184 @IntDef(
185 flag = true,
186 prefix = "CHANNEL_COUNT_",
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000187 value = {CHANNEL_COUNT_NONE, CHANNEL_COUNT_1, CHANNEL_COUNT_2})
Pattyd8c49272022-01-19 16:26:00 +0800188 @Retention(RetentionPolicy.SOURCE)
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000189 public @interface ChannelCount {}
Pattyd8c49272022-01-19 16:26:00 +0800190
David Duarteee52b7e2023-12-02 01:32:11 +0000191 /** Codec channel mode NONE. Default value of the codec channel mode. */
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000192 public static final int CHANNEL_COUNT_NONE = 0;
Pattyd8c49272022-01-19 16:26:00 +0800193
David Duarteee52b7e2023-12-02 01:32:11 +0000194 /** Codec channel mode MONO. */
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000195 public static final int CHANNEL_COUNT_1 = 0x01 << 0;
Pattyd8c49272022-01-19 16:26:00 +0800196
David Duarteee52b7e2023-12-02 01:32:11 +0000197 /** Codec channel mode STEREO. */
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000198 public static final int CHANNEL_COUNT_2 = 0x01 << 1;
Pattyd8c49272022-01-19 16:26:00 +0800199
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000200 /**
David Duarteee52b7e2023-12-02 01:32:11 +0000201 * Values are the bit mask as defined in the Bluetooth Assigned Numbers, Generic Audio,
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000202 * Supported_Frame_Durations table
203 *
David Duarteee52b7e2023-12-02 01:32:11 +0000204 * @hide
205 */
206 @IntDef(
207 flag = true,
208 prefix = "FRAME_DURATION_",
Pattyd8c49272022-01-19 16:26:00 +0800209 value = {FRAME_DURATION_NONE, FRAME_DURATION_7500, FRAME_DURATION_10000})
210 @Retention(RetentionPolicy.SOURCE)
211 public @interface FrameDuration {}
212
David Duarteee52b7e2023-12-02 01:32:11 +0000213 /** Frame duration 0. Default value of the frame duration. */
Pattyd8c49272022-01-19 16:26:00 +0800214 public static final int FRAME_DURATION_NONE = 0;
215
David Duarteee52b7e2023-12-02 01:32:11 +0000216 /** Frame duration 7500 us. */
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000217 public static final int FRAME_DURATION_7500 = 0x01 << 0;
Pattyd8c49272022-01-19 16:26:00 +0800218
David Duarteee52b7e2023-12-02 01:32:11 +0000219 /** Frame duration 10000 us. */
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000220 public static final int FRAME_DURATION_10000 = 0x01 << 1;
Pattyd8c49272022-01-19 16:26:00 +0800221
Patty5c05c2f2021-11-04 21:03:32 +0800222 private final @SourceCodecType int mCodecType;
Pattyd8c49272022-01-19 16:26:00 +0800223 private final @CodecPriority int mCodecPriority;
224 private final @SampleRate int mSampleRate;
225 private final @BitsPerSample int mBitsPerSample;
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000226 private final @ChannelCount int mChannelCount;
Pattyd8c49272022-01-19 16:26:00 +0800227 private final @FrameDuration int mFrameDuration;
228 private final int mOctetsPerFrame;
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000229 private final int mMinOctetsPerFrame;
230 private final int mMaxOctetsPerFrame;
231
Patty5c05c2f2021-11-04 21:03:32 +0800232 /**
233 * Creates a new BluetoothLeAudioCodecConfig.
234 *
235 * @param codecType the source codec type
Pattyd8c49272022-01-19 16:26:00 +0800236 * @param codecPriority the priority of this codec
237 * @param sampleRate the codec sample rate
238 * @param bitsPerSample the bits per sample of this codec
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000239 * @param channelCount the channel count of this codec
Pattyd8c49272022-01-19 16:26:00 +0800240 * @param frameDuration the frame duration of this codec
241 * @param octetsPerFrame the octets per frame of this codec
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000242 * @param minOctetsPerFrame the minimum octets per frame of this codec
243 * @param maxOctetsPerFrame the maximum octets per frame of this codec
Patty5c05c2f2021-11-04 21:03:32 +0800244 */
David Duarteee52b7e2023-12-02 01:32:11 +0000245 private BluetoothLeAudioCodecConfig(
246 @SourceCodecType int codecType,
247 @CodecPriority int codecPriority,
248 @SampleRate int sampleRate,
249 @BitsPerSample int bitsPerSample,
250 @ChannelCount int channelCount,
251 @FrameDuration int frameDuration,
252 int octetsPerFrame,
253 int minOctetsPerFrame,
254 int maxOctetsPerFrame) {
Patty5c05c2f2021-11-04 21:03:32 +0800255 mCodecType = codecType;
Pattyd8c49272022-01-19 16:26:00 +0800256 mCodecPriority = codecPriority;
257 mSampleRate = sampleRate;
258 mBitsPerSample = bitsPerSample;
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000259 mChannelCount = channelCount;
Pattyd8c49272022-01-19 16:26:00 +0800260 mFrameDuration = frameDuration;
261 mOctetsPerFrame = octetsPerFrame;
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000262 mMinOctetsPerFrame = minOctetsPerFrame;
263 mMaxOctetsPerFrame = maxOctetsPerFrame;
Pattyd8c49272022-01-19 16:26:00 +0800264 }
265
266 @Override
267 public int describeContents() {
268 return 0;
269 }
270
David Duarteee52b7e2023-12-02 01:32:11 +0000271 /** {@link Parcelable.Creator} interface implementation. */
Ömer Faruk Yılmaz9d13b8f2025-04-10 05:01:39 +0000272 public static final @NonNull Parcelable.Creator<BluetoothLeAudioCodecConfig> CREATOR =
273 new Parcelable.Creator<BluetoothLeAudioCodecConfig>() {
274 public BluetoothLeAudioCodecConfig createFromParcel(Parcel in) {
275 int codecType = in.readInt();
276 int codecPriority = in.readInt();
277 int sampleRate = in.readInt();
278 int bitsPerSample = in.readInt();
279 int channelCount = in.readInt();
280 int frameDuration = in.readInt();
281 int octetsPerFrame = in.readInt();
282 int minOctetsPerFrame = in.readInt();
283 int maxOctetsPerFrame = in.readInt();
284 return new BluetoothLeAudioCodecConfig(
285 codecType,
286 codecPriority,
287 sampleRate,
288 bitsPerSample,
289 channelCount,
290 frameDuration,
291 octetsPerFrame,
292 minOctetsPerFrame,
293 maxOctetsPerFrame);
294 }
Pattyd8c49272022-01-19 16:26:00 +0800295
Ömer Faruk Yılmaz9d13b8f2025-04-10 05:01:39 +0000296 public BluetoothLeAudioCodecConfig[] newArray(int size) {
297 return new BluetoothLeAudioCodecConfig[size];
298 }
299 };
Pattyd8c49272022-01-19 16:26:00 +0800300
301 @Override
302 public void writeToParcel(@NonNull Parcel out, int flags) {
303 out.writeInt(mCodecType);
304 out.writeInt(mCodecPriority);
305 out.writeInt(mSampleRate);
306 out.writeInt(mBitsPerSample);
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000307 out.writeInt(mChannelCount);
Pattyd8c49272022-01-19 16:26:00 +0800308 out.writeInt(mFrameDuration);
309 out.writeInt(mOctetsPerFrame);
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000310 out.writeInt(mMinOctetsPerFrame);
311 out.writeInt(mMaxOctetsPerFrame);
Patty5c05c2f2021-11-04 21:03:32 +0800312 }
313
William Escande0f45ca02025-02-20 18:21:32 -0800314 private static String sampleRateToString(@SampleRate int sampleRateBit) {
Łukasz Rymanowski4b6c0d02024-04-22 15:02:02 +0000315 switch (sampleRateBit) {
316 case SAMPLE_RATE_NONE:
317 return "None";
318 case SAMPLE_RATE_8000:
319 return "8 kHz";
320 case SAMPLE_RATE_11025:
321 return "11.025 kHz";
322 case SAMPLE_RATE_16000:
323 return "16 kHz";
324 case SAMPLE_RATE_22050:
325 return "22.05 kHz";
326 case SAMPLE_RATE_24000:
327 return "24 kHz";
328 case SAMPLE_RATE_32000:
329 return "32 kHz";
330 case SAMPLE_RATE_44100:
331 return "44.1 kHz";
332 case SAMPLE_RATE_48000:
333 return "48 kHz";
334 case SAMPLE_RATE_88200:
335 return "88.2 kHz";
336 case SAMPLE_RATE_96000:
337 return "96 kHz";
338 case SAMPLE_RATE_176400:
339 return "176.4 kHz";
340 case SAMPLE_RATE_192000:
341 return "192 kHz";
342 case SAMPLE_RATE_384000:
343 return "384 kHz";
344 default:
345 return "Unknown bit " + sampleRateBit;
346 }
347 }
348
William Escande0f45ca02025-02-20 18:21:32 -0800349 private static String frameDurationToString(@FrameDuration int frameDurationBit) {
Łukasz Rymanowski4b6c0d02024-04-22 15:02:02 +0000350 switch (frameDurationBit) {
351 case FRAME_DURATION_NONE:
352 return "None";
353 case FRAME_DURATION_7500:
354 return "7.5 ms";
355 case FRAME_DURATION_10000:
356 return "10 ms";
357 default:
358 return "Unknown bit " + frameDurationBit;
359 }
360 }
361
Patty5c05c2f2021-11-04 21:03:32 +0800362 @Override
363 public String toString() {
David Duarteee52b7e2023-12-02 01:32:11 +0000364 return "{codecName:"
365 + getCodecName()
366 + ",mCodecType:"
367 + mCodecType
368 + ",mCodecPriority:"
369 + mCodecPriority
370 + ",mSampleRate:"
Łukasz Rymanowski4b6c0d02024-04-22 15:02:02 +0000371 + sampleRateToString(mSampleRate)
David Duarteee52b7e2023-12-02 01:32:11 +0000372 + ",mBitsPerSample:"
373 + mBitsPerSample
Łukasz Rymanowski4b6c0d02024-04-22 15:02:02 +0000374 + ",mChannelCountBitMask:"
David Duarteee52b7e2023-12-02 01:32:11 +0000375 + mChannelCount
376 + ",mFrameDuration:"
Łukasz Rymanowski4b6c0d02024-04-22 15:02:02 +0000377 + frameDurationToString(mFrameDuration)
David Duarteee52b7e2023-12-02 01:32:11 +0000378 + ",mOctetsPerFrame:"
379 + mOctetsPerFrame
380 + ",mMinOctetsPerFrame:"
381 + mMinOctetsPerFrame
382 + ",mMaxOctetsPerFrame:"
383 + mMaxOctetsPerFrame
384 + "}";
Patty5c05c2f2021-11-04 21:03:32 +0800385 }
386
387 /**
388 * Gets the codec type.
389 *
390 * @return the codec type
391 */
392 public @SourceCodecType int getCodecType() {
393 return mCodecType;
394 }
395
396 /**
Patty5c05c2f2021-11-04 21:03:32 +0800397 * Gets the codec name.
398 *
399 * @return the codec name
400 */
401 public @NonNull String getCodecName() {
402 switch (mCodecType) {
403 case SOURCE_CODEC_TYPE_LC3:
404 return "LC3";
405 case SOURCE_CODEC_TYPE_INVALID:
406 return "INVALID CODEC";
Jakub Tyszkowski676bd922025-04-10 11:02:14 +0000407 case SOURCE_CODEC_TYPE_OPUS_HI_RES:
408 if (Flags.leaudioAddOpusHiResCodecType()) {
409 return "Opus Hi-Res";
410 }
411 // Fall-through intended
Patty5c05c2f2021-11-04 21:03:32 +0800412 default:
Łukasz Rymanowski61e11882024-11-20 14:13:27 +0000413 if (Flags.leaudioAddOpusCodecType()) {
414 if (mCodecType == SOURCE_CODEC_TYPE_OPUS) {
415 return "Opus";
416 }
417 }
Patty5c05c2f2021-11-04 21:03:32 +0800418 break;
419 }
420 return "UNKNOWN CODEC(" + mCodecType + ")";
421 }
422
423 /**
Pattyd8c49272022-01-19 16:26:00 +0800424 * Returns the codec selection priority.
David Duarteee52b7e2023-12-02 01:32:11 +0000425 *
426 * <p>The codec selection priority is relative to other codecs: larger value means higher
427 * priority.
Pattyd8c49272022-01-19 16:26:00 +0800428 */
429 public @CodecPriority int getCodecPriority() {
430 return mCodecPriority;
431 }
432
David Duarteee52b7e2023-12-02 01:32:11 +0000433 /** Returns the codec sample rate. */
Pattyd8c49272022-01-19 16:26:00 +0800434 public @SampleRate int getSampleRate() {
435 return mSampleRate;
436 }
437
David Duarteee52b7e2023-12-02 01:32:11 +0000438 /** Returns the codec bits per sample. */
Pattyd8c49272022-01-19 16:26:00 +0800439 public @BitsPerSample int getBitsPerSample() {
440 return mBitsPerSample;
441 }
442
David Duarteee52b7e2023-12-02 01:32:11 +0000443 /** Returns the codec channel mode. */
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000444 public @ChannelCount int getChannelCount() {
445 return mChannelCount;
Pattyd8c49272022-01-19 16:26:00 +0800446 }
447
David Duarteee52b7e2023-12-02 01:32:11 +0000448 /** Returns the frame duration. */
Patty3a61c472022-02-08 12:16:46 +0800449 public @FrameDuration int getFrameDuration() {
Pattyd8c49272022-01-19 16:26:00 +0800450 return mFrameDuration;
451 }
452
David Duarteee52b7e2023-12-02 01:32:11 +0000453 /** Returns the octets per frame */
Patty3a61c472022-02-08 12:16:46 +0800454 public int getOctetsPerFrame() {
Pattyd8c49272022-01-19 16:26:00 +0800455 return mOctetsPerFrame;
456 }
457
David Duarteee52b7e2023-12-02 01:32:11 +0000458 /** Returns the minimum octets per frame */
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000459 public int getMinOctetsPerFrame() {
460 return mMinOctetsPerFrame;
461 }
462
David Duarteee52b7e2023-12-02 01:32:11 +0000463 /** Returns the maximum octets per frame */
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000464 public int getMaxOctetsPerFrame() {
465 return mMaxOctetsPerFrame;
466 }
467
Patty5aa4bd22022-01-25 19:58:37 +0800468 @Override
Anton Hanssonfa927a52023-11-09 09:08:19 +0000469 public boolean equals(@Nullable Object o) {
Patty5aa4bd22022-01-25 19:58:37 +0800470 if (o instanceof BluetoothLeAudioCodecConfig) {
471 BluetoothLeAudioCodecConfig other = (BluetoothLeAudioCodecConfig) o;
472 return (other.getCodecType() == mCodecType
473 && other.getCodecPriority() == mCodecPriority
474 && other.getSampleRate() == mSampleRate
475 && other.getBitsPerSample() == mBitsPerSample
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000476 && other.getChannelCount() == mChannelCount
Patty5aa4bd22022-01-25 19:58:37 +0800477 && other.getFrameDuration() == mFrameDuration
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000478 && other.getOctetsPerFrame() == mOctetsPerFrame
479 && other.getMinOctetsPerFrame() == mMinOctetsPerFrame
480 && other.getMaxOctetsPerFrame() == mMaxOctetsPerFrame);
Patty5aa4bd22022-01-25 19:58:37 +0800481 }
482 return false;
483 }
484
485 /**
David Duarteee52b7e2023-12-02 01:32:11 +0000486 * Returns a hash representation of this BluetoothLeAudioCodecConfig based on all the config
487 * values.
Patty5aa4bd22022-01-25 19:58:37 +0800488 */
489 @Override
490 public int hashCode() {
David Duarteee52b7e2023-12-02 01:32:11 +0000491 return Objects.hash(
492 mCodecType,
493 mCodecPriority,
494 mSampleRate,
495 mBitsPerSample,
496 mChannelCount,
497 mFrameDuration,
498 mOctetsPerFrame,
499 mMinOctetsPerFrame,
500 mMaxOctetsPerFrame);
Patty5aa4bd22022-01-25 19:58:37 +0800501 }
502
Pattyd8c49272022-01-19 16:26:00 +0800503 /**
Patty5c05c2f2021-11-04 21:03:32 +0800504 * Builder for {@link BluetoothLeAudioCodecConfig}.
David Duarteee52b7e2023-12-02 01:32:11 +0000505 *
506 * <p>By default, the codec type will be set to {@link
507 * BluetoothLeAudioCodecConfig#SOURCE_CODEC_TYPE_INVALID}
Patty5c05c2f2021-11-04 21:03:32 +0800508 */
509 public static final class Builder {
510 private int mCodecType = BluetoothLeAudioCodecConfig.SOURCE_CODEC_TYPE_INVALID;
Pattyd8c49272022-01-19 16:26:00 +0800511 private int mCodecPriority = BluetoothLeAudioCodecConfig.CODEC_PRIORITY_DEFAULT;
512 private int mSampleRate = BluetoothLeAudioCodecConfig.SAMPLE_RATE_NONE;
513 private int mBitsPerSample = BluetoothLeAudioCodecConfig.BITS_PER_SAMPLE_NONE;
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000514 private int mChannelCount = BluetoothLeAudioCodecConfig.CHANNEL_COUNT_NONE;
Pattyd8c49272022-01-19 16:26:00 +0800515 private int mFrameDuration = BluetoothLeAudioCodecConfig.FRAME_DURATION_NONE;
516 private int mOctetsPerFrame = 0;
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000517 private int mMinOctetsPerFrame = 0;
518 private int mMaxOctetsPerFrame = 0;
Pattyd8c49272022-01-19 16:26:00 +0800519
520 public Builder() {}
521
522 public Builder(@NonNull BluetoothLeAudioCodecConfig config) {
523 mCodecType = config.getCodecType();
524 mCodecPriority = config.getCodecPriority();
525 mSampleRate = config.getSampleRate();
526 mBitsPerSample = config.getBitsPerSample();
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000527 mChannelCount = config.getChannelCount();
Pattyd8c49272022-01-19 16:26:00 +0800528 mFrameDuration = config.getFrameDuration();
529 mOctetsPerFrame = config.getOctetsPerFrame();
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000530 mMinOctetsPerFrame = config.getMinOctetsPerFrame();
531 mMaxOctetsPerFrame = config.getMaxOctetsPerFrame();
Pattyd8c49272022-01-19 16:26:00 +0800532 }
Patty5c05c2f2021-11-04 21:03:32 +0800533
534 /**
Pattyd8c49272022-01-19 16:26:00 +0800535 * Set codec type for Bluetooth LE audio codec config.
Patty5c05c2f2021-11-04 21:03:32 +0800536 *
537 * @param codecType of this codec
538 * @return the same Builder instance
539 */
540 public @NonNull Builder setCodecType(@SourceCodecType int codecType) {
541 mCodecType = codecType;
542 return this;
543 }
544
545 /**
Pattyd8c49272022-01-19 16:26:00 +0800546 * Set codec priority for Bluetooth LE audio codec config.
547 *
548 * @param codecPriority of this codec
549 * @return the same Builder instance
550 */
551 public @NonNull Builder setCodecPriority(@CodecPriority int codecPriority) {
552 mCodecPriority = codecPriority;
553 return this;
554 }
555
556 /**
557 * Set sample rate for Bluetooth LE audio codec config.
558 *
559 * @param sampleRate of this codec
560 * @return the same Builder instance
561 */
562 public @NonNull Builder setSampleRate(@SampleRate int sampleRate) {
563 mSampleRate = sampleRate;
564 return this;
565 }
566
567 /**
568 * Set the bits per sample for LE audio codec config.
569 *
570 * @param bitsPerSample of this codec
571 * @return the same Builder instance
572 */
573 public @NonNull Builder setBitsPerSample(@BitsPerSample int bitsPerSample) {
574 mBitsPerSample = bitsPerSample;
575 return this;
576 }
577
578 /**
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000579 * Set the channel count for Bluetooth LE audio codec config.
Pattyd8c49272022-01-19 16:26:00 +0800580 *
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000581 * @param channelCount of this codec
Pattyd8c49272022-01-19 16:26:00 +0800582 * @return the same Builder instance
583 */
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000584 public @NonNull Builder setChannelCount(@ChannelCount int channelCount) {
585 mChannelCount = channelCount;
Pattyd8c49272022-01-19 16:26:00 +0800586 return this;
587 }
588
589 /**
590 * Set the frame duration for Bluetooth LE audio codec config.
591 *
592 * @param frameDuration of this codec
593 * @return the same Builder instance
594 */
595 public @NonNull Builder setFrameDuration(@FrameDuration int frameDuration) {
596 mFrameDuration = frameDuration;
597 return this;
598 }
599
600 /**
601 * Set the octets per frame for Bluetooth LE audio codec config.
602 *
603 * @param octetsPerFrame of this codec
604 * @return the same Builder instance
605 */
606 public @NonNull Builder setOctetsPerFrame(int octetsPerFrame) {
607 mOctetsPerFrame = octetsPerFrame;
608 return this;
609 }
610
611 /**
Łukasz Rymanowski3c979452022-03-10 10:28:12 +0000612 * Set the minimum octets per frame for Bluetooth LE audio codec config.
613 *
614 * @param minOctetsPerFrame of this codec
615 * @return the same Builder instance
616 */
617 public @NonNull Builder setMinOctetsPerFrame(int minOctetsPerFrame) {
618 mMinOctetsPerFrame = minOctetsPerFrame;
619 return this;
620 }
621
622 /**
623 * Set the maximum octets per frame for Bluetooth LE audio codec config.
624 *
625 * @param maxOctetsPerFrame of this codec
626 * @return the same Builder instance
627 */
628 public @NonNull Builder setMaxOctetsPerFrame(int maxOctetsPerFrame) {
629 mMaxOctetsPerFrame = maxOctetsPerFrame;
630 return this;
631 }
632
633 /**
Patty5c05c2f2021-11-04 21:03:32 +0800634 * Build {@link BluetoothLeAudioCodecConfig}.
David Duarteee52b7e2023-12-02 01:32:11 +0000635 *
Patty5c05c2f2021-11-04 21:03:32 +0800636 * @return new BluetoothLeAudioCodecConfig built
637 */
638 public @NonNull BluetoothLeAudioCodecConfig build() {
David Duarteee52b7e2023-12-02 01:32:11 +0000639 return new BluetoothLeAudioCodecConfig(
640 mCodecType,
641 mCodecPriority,
642 mSampleRate,
643 mBitsPerSample,
644 mChannelCount,
645 mFrameDuration,
646 mOctetsPerFrame,
647 mMinOctetsPerFrame,
648 mMaxOctetsPerFrame);
Patty5c05c2f2021-11-04 21:03:32 +0800649 }
650 }
651}