blob: 5cc127766e83c16d53157006a162e31c196060d5 [file] [log] [blame]
Pavlin Radoslavov178a3d22016-12-21 12:05:51 -08001/*
2 * Copyright (C) 2016 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
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import java.util.Objects;
23
24/**
25 * Represents the codec configuration for a Bluetooth A2DP source device.
26 *
27 * {@see BluetoothA2dp}
28 *
29 * {@hide}
30 */
31public final class BluetoothCodecConfig implements Parcelable {
32
33 /**
34 * Extra for the codec configuration intents of the individual profiles.
35 *
36 * This extra represents the current codec configuration of the A2DP
37 * profile.
38 */
39 public static final String EXTRA_CODEC_CONFIG = "android.bluetooth.codec.extra.CODEC_CONFIG";
40
41 /**
42 * Extra for the codec configuration intents of the individual profiles.
43 *
44 * This extra represents the previous codec configuration of the A2DP
45 * profile.
46 */
47 public static final String EXTRA_PREVIOUS_CODEC_CONFIG =
48 "android.bluetooth.codec.extra.PREVIOUS_CODEC_CONFIG";
49
50 public static final int SOURCE_CODEC_TYPE_SBC = 0;
51 public static final int SOURCE_CODEC_TYPE_INVALID = 1000 * 1000;
52
53 public static final int CODEC_PRIORITY_DEFAULT = 0;
54 public static final int CODEC_PRIORITY_HIGHEST = 1000 * 1000;
55
56 public static final int SAMPLE_RATE_NONE = 0;
57 public static final int SAMPLE_RATE_44100 = 0x1 << 0;
58 public static final int SAMPLE_RATE_48000 = 0x1 << 1;
59 public static final int SAMPLE_RATE_88200 = 0x1 << 2;
60 public static final int SAMPLE_RATE_96000 = 0x1 << 3;
61 public static final int SAMPLE_RATE_176400 = 0x1 << 4;
62 public static final int SAMPLE_RATE_192000 = 0x1 << 5;
63
64 public static final int BITS_PER_SAMPLE_NONE = 0;
65 public static final int BITS_PER_SAMPLE_16 = 0x1 << 0;
66 public static final int BITS_PER_SAMPLE_24 = 0x1 << 1;
67 public static final int BITS_PER_SAMPLE_32 = 0x1 << 2;
68
69 public static final int CHANNEL_MODE_NONE = 0;
70 public static final int CHANNEL_MODE_MONO = 0x1 << 0;
71 public static final int CHANNEL_MODE_STEREO = 0x1 << 1;
72
73 private final int mCodecType;
74 private final int mCodecPriority;
75 private final int mSampleRate;
76 private final int mBitsPerSample;
77 private final int mChannelMode;
78 private final long mCodecSpecific1;
79 private final long mCodecSpecific2;
80 private final long mCodecSpecific3;
81 private final long mCodecSpecific4;
82
83 public BluetoothCodecConfig(int codecType, int codecPriority,
84 int sampleRate, int bitsPerSample,
85 int channelMode,long codecSpecific1,
86 long codecSpecific2, long codecSpecific3,
87 long codecSpecific4) {
88 mCodecType = codecType;
89 mCodecPriority = codecPriority;
90 mSampleRate = sampleRate;
91 mBitsPerSample = bitsPerSample;
92 mChannelMode = channelMode;
93 mCodecSpecific1 = codecSpecific1;
94 mCodecSpecific2 = codecSpecific2;
95 mCodecSpecific3 = codecSpecific3;
96 mCodecSpecific4 = codecSpecific4;
97 }
98
99 @Override
100 public boolean equals(Object o) {
101 if (o instanceof BluetoothCodecConfig) {
102 BluetoothCodecConfig other = (BluetoothCodecConfig)o;
103 return (other.mCodecType == mCodecType &&
104 other.mCodecPriority == mCodecPriority &&
105 other.mSampleRate == mSampleRate &&
106 other.mBitsPerSample == mBitsPerSample &&
107 other.mChannelMode == mChannelMode &&
108 other.mCodecSpecific1 == mCodecSpecific1 &&
109 other.mCodecSpecific2 == mCodecSpecific2 &&
110 other.mCodecSpecific3 == mCodecSpecific3 &&
111 other.mCodecSpecific4 == mCodecSpecific4);
112 }
113 return false;
114 }
115
116 @Override
117 public int hashCode() {
118 return Objects.hash(mCodecType, mCodecPriority, mSampleRate,
119 mBitsPerSample, mChannelMode, mCodecSpecific1,
120 mCodecSpecific2, mCodecSpecific3, mCodecSpecific4);
121 }
122
123 @Override
124 public String toString() {
125 return "{mCodecType:" + mCodecType +
126 ",mCodecPriority:" + mCodecPriority +
127 ",mSampleRate:" + String.format("0x%x", mSampleRate) +
128 ",mBitsPerSample:" + String.format("0x%x", mBitsPerSample) +
129 ",mChannelMode:" + String.format("0x%x", mChannelMode) +
130 ",mCodecSpecific1:" + mCodecSpecific1 +
131 ",mCodecSpecific2:" + mCodecSpecific2 +
132 ",mCodecSpecific3:" + mCodecSpecific3 +
133 ",mCodecSpecific4:" + mCodecSpecific4 + "}";
134 }
135
136 public int describeContents() {
137 return 0;
138 }
139
140 public static final Parcelable.Creator<BluetoothCodecConfig> CREATOR =
141 new Parcelable.Creator<BluetoothCodecConfig>() {
142 public BluetoothCodecConfig createFromParcel(Parcel in) {
143 final int codecType = in.readInt();
144 final int codecPriority = in.readInt();
145 final int sampleRate = in.readInt();
146 final int bitsPerSample = in.readInt();
147 final int channelMode = in.readInt();
148 final long codecSpecific1 = in.readLong();
149 final long codecSpecific2 = in.readLong();
150 final long codecSpecific3 = in.readLong();
151 final long codecSpecific4 = in.readLong();
152 return new BluetoothCodecConfig(codecType, codecPriority,
153 sampleRate, bitsPerSample,
154 channelMode, codecSpecific1,
155 codecSpecific2, codecSpecific3,
156 codecSpecific4);
157 }
158 public BluetoothCodecConfig[] newArray(int size) {
159 return new BluetoothCodecConfig[size];
160 }
161 };
162
163 public void writeToParcel(Parcel out, int flags) {
164 out.writeInt(mCodecType);
165 out.writeInt(mCodecPriority);
166 out.writeInt(mSampleRate);
167 out.writeInt(mBitsPerSample);
168 out.writeInt(mChannelMode);
169 out.writeLong(mCodecSpecific1);
170 out.writeLong(mCodecSpecific2);
171 out.writeLong(mCodecSpecific3);
172 out.writeLong(mCodecSpecific4);
173 }
174
175 /**
176 * Returns the codec type.
177 * See {@link android.bluetooth.BluetoothCodecConfig#SOURCE_CODEC_TYPE_SBC}.
178 *
179 * @return the codec type
180 */
181 public int getCodecType() {
182 return mCodecType;
183 }
184
185 /**
186 * Returns the codec selection priority.
187 * The codec selection priority is relative to other codecs: larger value
188 * means higher priority. If 0, reset to default.
189 *
190 * @return the codec priority
191 */
192 public int getCodecPriority() {
193 return mCodecPriority;
194 }
195
196 /**
197 * Returns the codec sample rate. The value can be a bitmask with all
198 * supported sample rates:
199 * {@link android.bluetooth.BluetoothCodecConfig#SAMPLE_RATE_NONE} or
200 * {@link android.bluetooth.BluetoothCodecConfig#SAMPLE_RATE_44100} or
201 * {@link android.bluetooth.BluetoothCodecConfig#SAMPLE_RATE_48000} or
202 * {@link android.bluetooth.BluetoothCodecConfig#SAMPLE_RATE_88200} or
203 * {@link android.bluetooth.BluetoothCodecConfig#SAMPLE_RATE_96000} or
204 * {@link android.bluetooth.BluetoothCodecConfig#SAMPLE_RATE_176400} or
205 * {@link android.bluetooth.BluetoothCodecConfig#SAMPLE_RATE_192000}
206 *
207 * @return the codec sample rate
208 */
209 public int getSampleRate() {
210 return mSampleRate;
211 }
212
213 /**
214 * Returns the codec bits per sample. The value can be a bitmask with all
215 * bits per sample supported:
216 * {@link android.bluetooth.BluetoothCodecConfig#BITS_PER_SAMPLE_NONE} or
217 * {@link android.bluetooth.BluetoothCodecConfig#BITS_PER_SAMPLE_16} or
218 * {@link android.bluetooth.BluetoothCodecConfig#BITS_PER_SAMPLE_24} or
219 * {@link android.bluetooth.BluetoothCodecConfig#BITS_PER_SAMPLE_32}
220 *
221 * @return the codec bits per sample
222 */
223 public int getBitsPerSample() {
224 return mBitsPerSample;
225 }
226
227 /**
228 * Returns the codec channel mode. The value can be a bitmask with all
229 * supported channel modes:
230 * {@link android.bluetooth.BluetoothCodecConfig#CHANNEL_MODE_NONE} or
231 * {@link android.bluetooth.BluetoothCodecConfig#CHANNEL_MODE_MONO} or
232 * {@link android.bluetooth.BluetoothCodecConfig#CHANNEL_MODE_STEREO}
233 *
234 * @return the codec channel mode
235 */
236 public int getChannelMode() {
237 return mChannelMode;
238 }
239
240 /**
241 * Returns a codec specific value1.
242 *
243 * @return a codec specific value1.
244 */
245 public long getCodecSpecific1() {
246 return mCodecSpecific1;
247 }
248
249 /**
250 * Returns a codec specific value2.
251 *
252 * @return a codec specific value2
253 */
254 public long getCodecSpecific2() {
255 return mCodecSpecific2;
256 }
257
258 /**
259 * Returns a codec specific value3.
260 *
261 * @return a codec specific value3
262 */
263 public long getCodecSpecific3() {
264 return mCodecSpecific3;
265 }
266
267 /**
268 * Returns a codec specific value4.
269 *
270 * @return a codec specific value4
271 */
272 public long getCodecSpecific4() {
273 return mCodecSpecific4;
274 }
275
276 /**
277 * Checks whether the audio feeding parameters are same.
278 *
279 * @param other the codec config to compare against
280 * @return true if the audio feeding parameters are same, otherwise false
281 */
282 public boolean sameAudioFeedingParameters(BluetoothCodecConfig other) {
283 return (other != null && other.mSampleRate == mSampleRate &&
284 other.mBitsPerSample == mBitsPerSample &&
285 other.mChannelMode == mChannelMode);
286 }
287}