1 /*
2  * Copyright (C) 2009 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 android.bluetooth;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 /**
25  * Represents the audio configuration for a Bluetooth A2DP source device.
26  *
27  * @see BluetoothA2dpSink
28  * @hide
29  */
30 public final class BluetoothAudioConfig implements Parcelable {
31 
32     private final int mSampleRate;
33     private final int mChannelConfig;
34     private final int mAudioFormat;
35 
BluetoothAudioConfig(int sampleRate, int channelConfig, int audioFormat)36     public BluetoothAudioConfig(int sampleRate, int channelConfig, int audioFormat) {
37         mSampleRate = sampleRate;
38         mChannelConfig = channelConfig;
39         mAudioFormat = audioFormat;
40     }
41 
42     @Override
equals(@ullable Object o)43     public boolean equals(@Nullable Object o) {
44         if (o instanceof BluetoothAudioConfig) {
45             BluetoothAudioConfig bac = (BluetoothAudioConfig) o;
46             return (bac.mSampleRate == mSampleRate
47                     && bac.mChannelConfig == mChannelConfig
48                     && bac.mAudioFormat == mAudioFormat);
49         }
50         return false;
51     }
52 
53     @Override
hashCode()54     public int hashCode() {
55         return mSampleRate | (mChannelConfig << 24) | (mAudioFormat << 28);
56     }
57 
58     @Override
toString()59     public String toString() {
60         return "{mSampleRate:"
61                 + mSampleRate
62                 + ",mChannelConfig:"
63                 + mChannelConfig
64                 + ",mAudioFormat:"
65                 + mAudioFormat
66                 + "}";
67     }
68 
69     @Override
describeContents()70     public int describeContents() {
71         return 0;
72     }
73 
74     public static final @NonNull Creator<BluetoothAudioConfig> CREATOR =
75             new Creator<>() {
76                 public BluetoothAudioConfig createFromParcel(Parcel in) {
77                     int sampleRate = in.readInt();
78                     int channelConfig = in.readInt();
79                     int audioFormat = in.readInt();
80                     return new BluetoothAudioConfig(sampleRate, channelConfig, audioFormat);
81                 }
82 
83                 public BluetoothAudioConfig[] newArray(int size) {
84                     return new BluetoothAudioConfig[size];
85                 }
86             };
87 
88     @Override
writeToParcel(Parcel out, int flags)89     public void writeToParcel(Parcel out, int flags) {
90         out.writeInt(mSampleRate);
91         out.writeInt(mChannelConfig);
92         out.writeInt(mAudioFormat);
93     }
94 
95     /**
96      * Returns the sample rate in samples per second
97      *
98      * @return sample rate
99      */
getSampleRate()100     public int getSampleRate() {
101         return mSampleRate;
102     }
103 
104     /**
105      * Returns the channel configuration (either {@link android.media.AudioFormat#CHANNEL_IN_MONO}
106      * or {@link android.media.AudioFormat#CHANNEL_IN_STEREO})
107      *
108      * @return channel configuration
109      */
getChannelConfig()110     public int getChannelConfig() {
111         return mChannelConfig;
112     }
113 
114     /**
115      * Returns the channel audio format (either {@link android.media.AudioFormat#ENCODING_PCM_16BIT}
116      * or {@link android.media.AudioFormat#ENCODING_PCM_8BIT}
117      *
118      * @return audio format
119      */
getAudioFormat()120     public int getAudioFormat() {
121         return mAudioFormat;
122     }
123 }
124