• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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.media;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 import java.util.Arrays;
27 import java.util.Objects;
28 
29 /**
30  * The {@link AudioFormat} class is used to access a number of audio format and
31  * channel configuration constants. They are for instance used
32  * in {@link AudioTrack} and {@link AudioRecord}, as valid values in individual parameters of
33  * constructors like {@link AudioTrack#AudioTrack(int, int, int, int, int, int)}, where the fourth
34  * parameter is one of the <code>AudioFormat.ENCODING_*</code> constants.
35  * The <code>AudioFormat</code> constants are also used in {@link MediaFormat} to specify
36  * audio related values commonly used in media, such as for {@link MediaFormat#KEY_CHANNEL_MASK}.
37  * <p>The {@link AudioFormat.Builder} class can be used to create instances of
38  * the <code>AudioFormat</code> format class.
39  * Refer to
40  * {@link AudioFormat.Builder} for documentation on the mechanics of the configuration and building
41  * of such instances. Here we describe the main concepts that the <code>AudioFormat</code> class
42  * allow you to convey in each instance, they are:
43  * <ol>
44  * <li><a href="#sampleRate">sample rate</a>
45  * <li><a href="#encoding">encoding</a>
46  * <li><a href="#channelMask">channel masks</a>
47  * </ol>
48  * <p>Closely associated with the <code>AudioFormat</code> is the notion of an
49  * <a href="#audioFrame">audio frame</a>, which is used throughout the documentation
50  * to represent the minimum size complete unit of audio data.
51  *
52  * <h4 id="sampleRate">Sample rate</h4>
53  * <p>Expressed in Hz, the sample rate in an <code>AudioFormat</code> instance expresses the number
54  * of audio samples for each channel per second in the content you are playing or recording. It is
55  * not the sample rate
56  * at which content is rendered or produced. For instance a sound at a media sample rate of 8000Hz
57  * can be played on a device operating at a sample rate of 48000Hz; the sample rate conversion is
58  * automatically handled by the platform, it will not play at 6x speed.
59  *
60  * <p>As of API {@link android.os.Build.VERSION_CODES#M},
61  * sample rates up to 192kHz are supported
62  * for <code>AudioRecord</code> and <code>AudioTrack</code>, with sample rate conversion
63  * performed as needed.
64  * To improve efficiency and avoid lossy conversions, it is recommended to match the sample rate
65  * for <code>AudioRecord</code> and <code>AudioTrack</code> to the endpoint device
66  * sample rate, and limit the sample rate to no more than 48kHz unless there are special
67  * device capabilities that warrant a higher rate.
68  *
69  * <h4 id="encoding">Encoding</h4>
70  * <p>Audio encoding is used to describe the bit representation of audio data, which can be
71  * either linear PCM or compressed audio, such as AC3 or DTS.
72  * <p>For linear PCM, the audio encoding describes the sample size, 8 bits, 16 bits, or 32 bits,
73  * and the sample representation, integer or float.
74  * <ul>
75  * <li> {@link #ENCODING_PCM_8BIT}: The audio sample is a 8 bit unsigned integer in the
76  * range [0, 255], with a 128 offset for zero. This is typically stored as a Java byte in a
77  * byte array or ByteBuffer. Since the Java byte is <em>signed</em>,
78  * be careful with math operations and conversions as the most significant bit is inverted.
79  * </li>
80  * <li> {@link #ENCODING_PCM_16BIT}: The audio sample is a 16 bit signed integer
81  * typically stored as a Java short in a short array, but when the short
82  * is stored in a ByteBuffer, it is native endian (as compared to the default Java big endian).
83  * The short has full range from [-32768, 32767],
84  * and is sometimes interpreted as fixed point Q.15 data.
85  * </li>
86  * <li> {@link #ENCODING_PCM_FLOAT}: Introduced in
87  * API {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this encoding specifies that
88  * the audio sample is a 32 bit IEEE single precision float. The sample can be
89  * manipulated as a Java float in a float array, though within a ByteBuffer
90  * it is stored in native endian byte order.
91  * The nominal range of <code>ENCODING_PCM_FLOAT</code> audio data is [-1.0, 1.0].
92  * It is implementation dependent whether the positive maximum of 1.0 is included
93  * in the interval. Values outside of the nominal range are clamped before
94  * sending to the endpoint device. Beware that
95  * the handling of NaN is undefined; subnormals may be treated as zero; and
96  * infinities are generally clamped just like other values for <code>AudioTrack</code>
97  * &ndash; try to avoid infinities because they can easily generate a NaN.
98  * <br>
99  * To achieve higher audio bit depth than a signed 16 bit integer short,
100  * it is recommended to use <code>ENCODING_PCM_FLOAT</code> for audio capture, processing,
101  * and playback.
102  * Floats are efficiently manipulated by modern CPUs,
103  * have greater precision than 24 bit signed integers,
104  * and have greater dynamic range than 32 bit signed integers.
105  * <code>AudioRecord</code> as of API {@link android.os.Build.VERSION_CODES#M} and
106  * <code>AudioTrack</code> as of API {@link android.os.Build.VERSION_CODES#LOLLIPOP}
107  * support <code>ENCODING_PCM_FLOAT</code>.
108  * </li>
109  * </ul>
110  * <p>For compressed audio, the encoding specifies the method of compression,
111  * for example {@link #ENCODING_AC3} and {@link #ENCODING_DTS}. The compressed
112  * audio data is typically stored as bytes in
113  * a byte array or ByteBuffer. When a compressed audio encoding is specified
114  * for an <code>AudioTrack</code>, it creates a direct (non-mixed) track
115  * for output to an endpoint (such as HDMI) capable of decoding the compressed audio.
116  * For (most) other endpoints, which are not capable of decoding such compressed audio,
117  * you will need to decode the data first, typically by creating a {@link MediaCodec}.
118  * Alternatively, one may use {@link MediaPlayer} for playback of compressed
119  * audio files or streams.
120  * <p>When compressed audio is sent out through a direct <code>AudioTrack</code>,
121  * it need not be written in exact multiples of the audio access unit;
122  * this differs from <code>MediaCodec</code> input buffers.
123  *
124  * <h4 id="channelMask">Channel mask</h4>
125  * <p>Channel masks are used in <code>AudioTrack</code> and <code>AudioRecord</code> to describe
126  * the samples and their arrangement in the audio frame. They are also used in the endpoint (e.g.
127  * a USB audio interface, a DAC connected to headphones) to specify allowable configurations of a
128  * particular device.
129  * <br>As of API {@link android.os.Build.VERSION_CODES#M}, there are two types of channel masks:
130  * channel position masks and channel index masks.
131  *
132  * <h5 id="channelPositionMask">Channel position masks</h5>
133  * Channel position masks are the original Android channel masks, and are used since API
134  * {@link android.os.Build.VERSION_CODES#BASE}.
135  * For input and output, they imply a positional nature - the location of a speaker or a microphone
136  * for recording or playback.
137  * <br>For a channel position mask, each allowed channel position corresponds to a bit in the
138  * channel mask. If that channel position is present in the audio frame, that bit is set,
139  * otherwise it is zero. The order of the bits (from lsb to msb) corresponds to the order of that
140  * position's sample in the audio frame.
141  * <br>The canonical channel position masks by channel count are as follows:
142  * <br><table>
143  * <tr><td>channel count</td><td>channel position mask</td></tr>
144  * <tr><td>1</td><td>{@link #CHANNEL_OUT_MONO}</td></tr>
145  * <tr><td>2</td><td>{@link #CHANNEL_OUT_STEREO}</td></tr>
146  * <tr><td>3</td><td>{@link #CHANNEL_OUT_STEREO} | {@link #CHANNEL_OUT_FRONT_CENTER}</td></tr>
147  * <tr><td>4</td><td>{@link #CHANNEL_OUT_QUAD}</td></tr>
148  * <tr><td>5</td><td>{@link #CHANNEL_OUT_QUAD} | {@link #CHANNEL_OUT_FRONT_CENTER}</td></tr>
149  * <tr><td>6</td><td>{@link #CHANNEL_OUT_5POINT1}</td></tr>
150  * <tr><td>7</td><td>{@link #CHANNEL_OUT_5POINT1} | {@link #CHANNEL_OUT_BACK_CENTER}</td></tr>
151  * <tr><td>8</td><td>{@link #CHANNEL_OUT_7POINT1_SURROUND}</td></tr>
152  * </table>
153  * <br>These masks are an ORed composite of individual channel masks. For example
154  * {@link #CHANNEL_OUT_STEREO} is composed of {@link #CHANNEL_OUT_FRONT_LEFT} and
155  * {@link #CHANNEL_OUT_FRONT_RIGHT}.
156  *
157  * <h5 id="channelIndexMask">Channel index masks</h5>
158  * Channel index masks are introduced in API {@link android.os.Build.VERSION_CODES#M}. They allow
159  * the selection of a particular channel from the source or sink endpoint by number, i.e. the first
160  * channel, the second channel, and so forth. This avoids problems with artificially assigning
161  * positions to channels of an endpoint, or figuring what the i<sup>th</sup> position bit is within
162  * an endpoint's channel position mask etc.
163  * <br>Here's an example where channel index masks address this confusion: dealing with a 4 channel
164  * USB device. Using a position mask, and based on the channel count, this would be a
165  * {@link #CHANNEL_OUT_QUAD} device, but really one is only interested in channel 0
166  * through channel 3. The USB device would then have the following individual bit channel masks:
167  * {@link #CHANNEL_OUT_FRONT_LEFT},
168  * {@link #CHANNEL_OUT_FRONT_RIGHT}, {@link #CHANNEL_OUT_BACK_LEFT}
169  * and {@link #CHANNEL_OUT_BACK_RIGHT}. But which is channel 0 and which is
170  * channel 3?
171  * <br>For a channel index mask, each channel number is represented as a bit in the mask, from the
172  * lsb (channel 0) upwards to the msb, numerically this bit value is
173  * <code>1 << channelNumber</code>.
174  * A set bit indicates that channel is present in the audio frame, otherwise it is cleared.
175  * The order of the bits also correspond to that channel number's sample order in the audio frame.
176  * <br>For the previous 4 channel USB device example, the device would have a channel index mask
177  * <code>0xF</code>. Suppose we wanted to select only the first and the third channels; this would
178  * correspond to a channel index mask <code>0x5</code> (the first and third bits set). If an
179  * <code>AudioTrack</code> uses this channel index mask, the audio frame would consist of two
180  * samples, the first sample of each frame routed to channel 0, and the second sample of each frame
181  * routed to channel 2.
182  * The canonical channel index masks by channel count are given by the formula
183  * <code>(1 << channelCount) - 1</code>.
184  *
185  * <h5>Use cases</h5>
186  * <ul>
187  * <li><i>Channel position mask for an endpoint:</i> <code>CHANNEL_OUT_FRONT_LEFT</code>,
188  *  <code>CHANNEL_OUT_FRONT_CENTER</code>, etc. for HDMI home theater purposes.
189  * <li><i>Channel position mask for an audio stream:</i> Creating an <code>AudioTrack</code>
190  *  to output movie content, where 5.1 multichannel output is to be written.
191  * <li><i>Channel index mask for an endpoint:</i> USB devices for which input and output do not
192  *  correspond to left or right speaker or microphone.
193  * <li><i>Channel index mask for an audio stream:</i> An <code>AudioRecord</code> may only want the
194  *  third and fourth audio channels of the endpoint (i.e. the second channel pair), and not care the
195  *  about position it corresponds to, in which case the channel index mask is <code>0xC</code>.
196  *  Multichannel <code>AudioRecord</code> sessions should use channel index masks.
197  * </ul>
198  * <h4 id="audioFrame">Audio Frame</h4>
199  * <p>For linear PCM, an audio frame consists of a set of samples captured at the same time,
200  * whose count and
201  * channel association are given by the <a href="#channelMask">channel mask</a>,
202  * and whose sample contents are specified by the <a href="#encoding">encoding</a>.
203  * For example, a stereo 16 bit PCM frame consists of
204  * two 16 bit linear PCM samples, with a frame size of 4 bytes.
205  * For compressed audio, an audio frame may alternately
206  * refer to an access unit of compressed data bytes that is logically grouped together for
207  * decoding and bitstream access (e.g. {@link MediaCodec}),
208  * or a single byte of compressed data (e.g. {@link AudioTrack#getBufferSizeInFrames()
209  * AudioTrack.getBufferSizeInFrames()}),
210  * or the linear PCM frame result from decoding the compressed data
211  * (e.g.{@link AudioTrack#getPlaybackHeadPosition()
212  * AudioTrack.getPlaybackHeadPosition()}),
213  * depending on the context where audio frame is used.
214  */
215 public final class AudioFormat implements Parcelable {
216 
217     //---------------------------------------------------------
218     // Constants
219     //--------------------
220     /** Invalid audio data format */
221     public static final int ENCODING_INVALID = 0;
222     /** Default audio data format */
223     public static final int ENCODING_DEFAULT = 1;
224 
225     // These values must be kept in sync with core/jni/android_media_AudioFormat.h
226     // Also sync av/services/audiopolicy/managerdefault/ConfigParsingUtils.h
227     /** Audio data format: PCM 16 bit per sample. Guaranteed to be supported by devices. */
228     public static final int ENCODING_PCM_16BIT = 2;
229     /** Audio data format: PCM 8 bit per sample. Not guaranteed to be supported by devices. */
230     public static final int ENCODING_PCM_8BIT = 3;
231     /** Audio data format: single-precision floating-point per sample */
232     public static final int ENCODING_PCM_FLOAT = 4;
233     /** Audio data format: AC-3 compressed */
234     public static final int ENCODING_AC3 = 5;
235     /** Audio data format: E-AC-3 compressed */
236     public static final int ENCODING_E_AC3 = 6;
237     /** Audio data format: DTS compressed */
238     public static final int ENCODING_DTS = 7;
239     /** Audio data format: DTS HD compressed */
240     public static final int ENCODING_DTS_HD = 8;
241     /** Audio data format: MP3 compressed
242      * @hide
243      * */
244     public static final int ENCODING_MP3 = 9;
245     /** Audio data format: AAC LC compressed
246      * @hide
247      * */
248     public static final int ENCODING_AAC_LC = 10;
249     /** Audio data format: AAC HE V1 compressed
250      * @hide
251      * */
252     public static final int ENCODING_AAC_HE_V1 = 11;
253     /** Audio data format: AAC HE V2 compressed
254      * @hide
255      * */
256     public static final int ENCODING_AAC_HE_V2 = 12;
257     /** Audio data format: compressed audio wrapped in PCM for HDMI
258      * or S/PDIF passthrough.
259      * IEC61937 uses a stereo stream of 16-bit samples as the wrapper.
260      * So the channel mask for the track must be {@link #CHANNEL_OUT_STEREO}.
261      * Data should be written to the stream in a short[] array.
262      * If the data is written in a byte[] array then there may be endian problems
263      * on some platforms when converting to short internally.
264      */
265     public static final int ENCODING_IEC61937 = 13;
266     /** Audio data format: DOLBY TRUEHD compressed
267      **/
268     public static final int ENCODING_DOLBY_TRUEHD = 14;
269 
270     /** @hide */
toLogFriendlyEncoding(int enc)271     public static String toLogFriendlyEncoding(int enc) {
272         switch(enc) {
273             case ENCODING_INVALID:
274                 return "ENCODING_INVALID";
275             case ENCODING_PCM_16BIT:
276                 return "ENCODING_PCM_16BIT";
277             case ENCODING_PCM_8BIT:
278                 return "ENCODING_PCM_8BIT";
279             case ENCODING_PCM_FLOAT:
280                 return "ENCODING_PCM_FLOAT";
281             case ENCODING_AC3:
282                 return "ENCODING_AC3";
283             case ENCODING_E_AC3:
284                 return "ENCODING_E_AC3";
285             case ENCODING_DTS:
286                 return "ENCODING_DTS";
287             case ENCODING_DTS_HD:
288                 return "ENCODING_DTS_HD";
289             case ENCODING_MP3:
290                 return "ENCODING_MP3";
291             case ENCODING_AAC_LC:
292                 return "ENCODING_AAC_LC";
293             case ENCODING_AAC_HE_V1:
294                 return "ENCODING_AAC_HE_V1";
295             case ENCODING_AAC_HE_V2:
296                 return "ENCODING_AAC_HE_V2";
297             case ENCODING_IEC61937:
298                 return "ENCODING_IEC61937";
299             case ENCODING_DOLBY_TRUEHD:
300                 return "ENCODING_DOLBY_TRUEHD";
301             default :
302                 return "invalid encoding " + enc;
303         }
304     }
305 
306     /** Invalid audio channel configuration */
307     /** @deprecated Use {@link #CHANNEL_INVALID} instead.  */
308     @Deprecated    public static final int CHANNEL_CONFIGURATION_INVALID   = 0;
309     /** Default audio channel configuration */
310     /** @deprecated Use {@link #CHANNEL_OUT_DEFAULT} or {@link #CHANNEL_IN_DEFAULT} instead.  */
311     @Deprecated    public static final int CHANNEL_CONFIGURATION_DEFAULT   = 1;
312     /** Mono audio configuration */
313     /** @deprecated Use {@link #CHANNEL_OUT_MONO} or {@link #CHANNEL_IN_MONO} instead.  */
314     @Deprecated    public static final int CHANNEL_CONFIGURATION_MONO      = 2;
315     /** Stereo (2 channel) audio configuration */
316     /** @deprecated Use {@link #CHANNEL_OUT_STEREO} or {@link #CHANNEL_IN_STEREO} instead.  */
317     @Deprecated    public static final int CHANNEL_CONFIGURATION_STEREO    = 3;
318 
319     /** Invalid audio channel mask */
320     public static final int CHANNEL_INVALID = 0;
321     /** Default audio channel mask */
322     public static final int CHANNEL_OUT_DEFAULT = 1;
323 
324     // Output channel mask definitions below are translated to the native values defined in
325     //  in /system/media/audio/include/system/audio.h in the JNI code of AudioTrack
326     public static final int CHANNEL_OUT_FRONT_LEFT = 0x4;
327     public static final int CHANNEL_OUT_FRONT_RIGHT = 0x8;
328     public static final int CHANNEL_OUT_FRONT_CENTER = 0x10;
329     public static final int CHANNEL_OUT_LOW_FREQUENCY = 0x20;
330     public static final int CHANNEL_OUT_BACK_LEFT = 0x40;
331     public static final int CHANNEL_OUT_BACK_RIGHT = 0x80;
332     public static final int CHANNEL_OUT_FRONT_LEFT_OF_CENTER = 0x100;
333     public static final int CHANNEL_OUT_FRONT_RIGHT_OF_CENTER = 0x200;
334     public static final int CHANNEL_OUT_BACK_CENTER = 0x400;
335     public static final int CHANNEL_OUT_SIDE_LEFT =         0x800;
336     public static final int CHANNEL_OUT_SIDE_RIGHT =       0x1000;
337     /** @hide */
338     public static final int CHANNEL_OUT_TOP_CENTER =       0x2000;
339     /** @hide */
340     public static final int CHANNEL_OUT_TOP_FRONT_LEFT =   0x4000;
341     /** @hide */
342     public static final int CHANNEL_OUT_TOP_FRONT_CENTER = 0x8000;
343     /** @hide */
344     public static final int CHANNEL_OUT_TOP_FRONT_RIGHT = 0x10000;
345     /** @hide */
346     public static final int CHANNEL_OUT_TOP_BACK_LEFT =   0x20000;
347     /** @hide */
348     public static final int CHANNEL_OUT_TOP_BACK_CENTER = 0x40000;
349     /** @hide */
350     public static final int CHANNEL_OUT_TOP_BACK_RIGHT =  0x80000;
351 
352     public static final int CHANNEL_OUT_MONO = CHANNEL_OUT_FRONT_LEFT;
353     public static final int CHANNEL_OUT_STEREO = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT);
354     // aka QUAD_BACK
355     public static final int CHANNEL_OUT_QUAD = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
356             CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT);
357     /** @hide */
358     public static final int CHANNEL_OUT_QUAD_SIDE = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
359             CHANNEL_OUT_SIDE_LEFT | CHANNEL_OUT_SIDE_RIGHT);
360     public static final int CHANNEL_OUT_SURROUND = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
361             CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_BACK_CENTER);
362     // aka 5POINT1_BACK
363     public static final int CHANNEL_OUT_5POINT1 = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
364             CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY | CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT);
365     /** @hide */
366     public static final int CHANNEL_OUT_5POINT1_SIDE = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
367             CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY |
368             CHANNEL_OUT_SIDE_LEFT | CHANNEL_OUT_SIDE_RIGHT);
369     // different from AUDIO_CHANNEL_OUT_7POINT1 used internally, and not accepted by AudioRecord.
370     /** @deprecated Not the typical 7.1 surround configuration. Use {@link #CHANNEL_OUT_7POINT1_SURROUND} instead. */
371     @Deprecated    public static final int CHANNEL_OUT_7POINT1 = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
372             CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY | CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT |
373             CHANNEL_OUT_FRONT_LEFT_OF_CENTER | CHANNEL_OUT_FRONT_RIGHT_OF_CENTER);
374     // matches AUDIO_CHANNEL_OUT_7POINT1
375     public static final int CHANNEL_OUT_7POINT1_SURROUND = (
376             CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_FRONT_RIGHT |
377             CHANNEL_OUT_SIDE_LEFT | CHANNEL_OUT_SIDE_RIGHT |
378             CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT |
379             CHANNEL_OUT_LOW_FREQUENCY);
380     // CHANNEL_OUT_ALL is not yet defined; if added then it should match AUDIO_CHANNEL_OUT_ALL
381 
382     /** Minimum value for sample rate,
383      *  assuming AudioTrack and AudioRecord share the same limitations.
384      * @hide
385      */
386     // never unhide
387     public static final int SAMPLE_RATE_HZ_MIN = 4000;
388     /** Maximum value for sample rate,
389      *  assuming AudioTrack and AudioRecord share the same limitations.
390      * @hide
391      */
392     // never unhide
393     public static final int SAMPLE_RATE_HZ_MAX = 192000;
394     /** Sample rate will be a route-dependent value.
395      * For AudioTrack, it is usually the sink sample rate,
396      * and for AudioRecord it is usually the source sample rate.
397      */
398     public static final int SAMPLE_RATE_UNSPECIFIED = 0;
399 
400     /**
401      * @hide
402      * Return the input channel mask corresponding to an output channel mask.
403      * This can be used for submix rerouting for the mask of the recorder to map to that of the mix.
404      * @param outMask a combination of the CHANNEL_OUT_* definitions, but not CHANNEL_OUT_DEFAULT
405      * @return a combination of CHANNEL_IN_* definitions matching an output channel mask
406      * @throws IllegalArgumentException
407      */
inChannelMaskFromOutChannelMask(int outMask)408     public static int inChannelMaskFromOutChannelMask(int outMask) throws IllegalArgumentException {
409         if (outMask == CHANNEL_OUT_DEFAULT) {
410             throw new IllegalArgumentException(
411                     "Illegal CHANNEL_OUT_DEFAULT channel mask for input.");
412         }
413         switch (channelCountFromOutChannelMask(outMask)) {
414             case 1:
415                 return CHANNEL_IN_MONO;
416             case 2:
417                 return CHANNEL_IN_STEREO;
418             default:
419                 throw new IllegalArgumentException("Unsupported channel configuration for input.");
420         }
421     }
422 
423     /**
424      * @hide
425      * Return the number of channels from an input channel mask
426      * @param mask a combination of the CHANNEL_IN_* definitions, even CHANNEL_IN_DEFAULT
427      * @return number of channels for the mask
428      */
channelCountFromInChannelMask(int mask)429     public static int channelCountFromInChannelMask(int mask) {
430         return Integer.bitCount(mask);
431     }
432     /**
433      * @hide
434      * Return the number of channels from an output channel mask
435      * @param mask a combination of the CHANNEL_OUT_* definitions, but not CHANNEL_OUT_DEFAULT
436      * @return number of channels for the mask
437      */
channelCountFromOutChannelMask(int mask)438     public static int channelCountFromOutChannelMask(int mask) {
439         return Integer.bitCount(mask);
440     }
441     /**
442      * @hide
443      * Return a channel mask ready to be used by native code
444      * @param mask a combination of the CHANNEL_OUT_* definitions, but not CHANNEL_OUT_DEFAULT
445      * @return a native channel mask
446      */
convertChannelOutMaskToNativeMask(int javaMask)447     public static int convertChannelOutMaskToNativeMask(int javaMask) {
448         return (javaMask >> 2);
449     }
450 
451     /**
452      * @hide
453      * Return a java output channel mask
454      * @param mask a native channel mask
455      * @return a combination of the CHANNEL_OUT_* definitions
456      */
convertNativeChannelMaskToOutMask(int nativeMask)457     public static int convertNativeChannelMaskToOutMask(int nativeMask) {
458         return (nativeMask << 2);
459     }
460 
461     public static final int CHANNEL_IN_DEFAULT = 1;
462     // These directly match native
463     public static final int CHANNEL_IN_LEFT = 0x4;
464     public static final int CHANNEL_IN_RIGHT = 0x8;
465     public static final int CHANNEL_IN_FRONT = 0x10;
466     public static final int CHANNEL_IN_BACK = 0x20;
467     public static final int CHANNEL_IN_LEFT_PROCESSED = 0x40;
468     public static final int CHANNEL_IN_RIGHT_PROCESSED = 0x80;
469     public static final int CHANNEL_IN_FRONT_PROCESSED = 0x100;
470     public static final int CHANNEL_IN_BACK_PROCESSED = 0x200;
471     public static final int CHANNEL_IN_PRESSURE = 0x400;
472     public static final int CHANNEL_IN_X_AXIS = 0x800;
473     public static final int CHANNEL_IN_Y_AXIS = 0x1000;
474     public static final int CHANNEL_IN_Z_AXIS = 0x2000;
475     public static final int CHANNEL_IN_VOICE_UPLINK = 0x4000;
476     public static final int CHANNEL_IN_VOICE_DNLINK = 0x8000;
477     public static final int CHANNEL_IN_MONO = CHANNEL_IN_FRONT;
478     public static final int CHANNEL_IN_STEREO = (CHANNEL_IN_LEFT | CHANNEL_IN_RIGHT);
479     /** @hide */
480     public static final int CHANNEL_IN_FRONT_BACK = CHANNEL_IN_FRONT | CHANNEL_IN_BACK;
481     // CHANNEL_IN_ALL is not yet defined; if added then it should match AUDIO_CHANNEL_IN_ALL
482 
483     /** @hide */
getBytesPerSample(int audioFormat)484     public static int getBytesPerSample(int audioFormat)
485     {
486         switch (audioFormat) {
487         case ENCODING_PCM_8BIT:
488             return 1;
489         case ENCODING_PCM_16BIT:
490         case ENCODING_IEC61937:
491         case ENCODING_DEFAULT:
492             return 2;
493         case ENCODING_PCM_FLOAT:
494             return 4;
495         case ENCODING_INVALID:
496         default:
497             throw new IllegalArgumentException("Bad audio format " + audioFormat);
498         }
499     }
500 
501     /** @hide */
isValidEncoding(int audioFormat)502     public static boolean isValidEncoding(int audioFormat)
503     {
504         switch (audioFormat) {
505         case ENCODING_PCM_8BIT:
506         case ENCODING_PCM_16BIT:
507         case ENCODING_PCM_FLOAT:
508         case ENCODING_AC3:
509         case ENCODING_E_AC3:
510         case ENCODING_DTS:
511         case ENCODING_DTS_HD:
512         case ENCODING_MP3:
513         case ENCODING_AAC_LC:
514         case ENCODING_AAC_HE_V1:
515         case ENCODING_AAC_HE_V2:
516         case ENCODING_IEC61937:
517             return true;
518         default:
519             return false;
520         }
521     }
522 
523     /** @hide */
isPublicEncoding(int audioFormat)524     public static boolean isPublicEncoding(int audioFormat)
525     {
526         switch (audioFormat) {
527         case ENCODING_PCM_8BIT:
528         case ENCODING_PCM_16BIT:
529         case ENCODING_PCM_FLOAT:
530         case ENCODING_AC3:
531         case ENCODING_E_AC3:
532         case ENCODING_DTS:
533         case ENCODING_DTS_HD:
534         case ENCODING_IEC61937:
535             return true;
536         default:
537             return false;
538         }
539     }
540 
541     /** @hide */
isEncodingLinearPcm(int audioFormat)542     public static boolean isEncodingLinearPcm(int audioFormat)
543     {
544         switch (audioFormat) {
545         case ENCODING_PCM_8BIT:
546         case ENCODING_PCM_16BIT:
547         case ENCODING_PCM_FLOAT:
548         case ENCODING_DEFAULT:
549             return true;
550         case ENCODING_AC3:
551         case ENCODING_E_AC3:
552         case ENCODING_DTS:
553         case ENCODING_DTS_HD:
554         case ENCODING_MP3:
555         case ENCODING_AAC_LC:
556         case ENCODING_AAC_HE_V1:
557         case ENCODING_AAC_HE_V2:
558         case ENCODING_IEC61937: // wrapped in PCM but compressed
559             return false;
560         case ENCODING_INVALID:
561         default:
562             throw new IllegalArgumentException("Bad audio format " + audioFormat);
563         }
564     }
565 
566     /** @hide */
isEncodingLinearFrames(int audioFormat)567     public static boolean isEncodingLinearFrames(int audioFormat)
568     {
569         switch (audioFormat) {
570         case ENCODING_PCM_8BIT:
571         case ENCODING_PCM_16BIT:
572         case ENCODING_PCM_FLOAT:
573         case ENCODING_IEC61937: // same size as stereo PCM
574         case ENCODING_DEFAULT:
575             return true;
576         case ENCODING_AC3:
577         case ENCODING_E_AC3:
578         case ENCODING_DTS:
579         case ENCODING_DTS_HD:
580         case ENCODING_MP3:
581         case ENCODING_AAC_LC:
582         case ENCODING_AAC_HE_V1:
583         case ENCODING_AAC_HE_V2:
584             return false;
585         case ENCODING_INVALID:
586         default:
587             throw new IllegalArgumentException("Bad audio format " + audioFormat);
588         }
589     }
590     /**
591      * Returns an array of public encoding values extracted from an array of
592      * encoding values.
593      * @hide
594      */
filterPublicFormats(int[] formats)595     public static int[] filterPublicFormats(int[] formats) {
596         if (formats == null) {
597             return null;
598         }
599         int[] myCopy = Arrays.copyOf(formats, formats.length);
600         int size = 0;
601         for (int i = 0; i < myCopy.length; i++) {
602             if (isPublicEncoding(myCopy[i])) {
603                 if (size != i) {
604                     myCopy[size] = myCopy[i];
605                 }
606                 size++;
607             }
608         }
609         return Arrays.copyOf(myCopy, size);
610     }
611 
612     /** @removed */
AudioFormat()613     public AudioFormat()
614     {
615         throw new UnsupportedOperationException("There is no valid usage of this constructor");
616     }
617 
618     /**
619      * Private constructor with an ignored argument to differentiate from the removed default ctor
620      * @param ignoredArgument
621      */
AudioFormat(int ignoredArgument)622     private AudioFormat(int ignoredArgument) {
623     }
624 
625     /**
626      * Constructor used by the JNI.  Parameters are not checked for validity.
627      */
628     // Update sound trigger JNI in core/jni/android_hardware_SoundTrigger.cpp when modifying this
629     // constructor
AudioFormat(int encoding, int sampleRate, int channelMask, int channelIndexMask)630     private AudioFormat(int encoding, int sampleRate, int channelMask, int channelIndexMask) {
631         mEncoding = encoding;
632         mSampleRate = sampleRate;
633         mChannelMask = channelMask;
634         mChannelIndexMask = channelIndexMask;
635         mPropertySetMask = AUDIO_FORMAT_HAS_PROPERTY_ENCODING |
636                 AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE |
637                 AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK |
638                 AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK;
639     }
640 
641     /** @hide */
642     public final static int AUDIO_FORMAT_HAS_PROPERTY_NONE = 0x0;
643     /** @hide */
644     public final static int AUDIO_FORMAT_HAS_PROPERTY_ENCODING = 0x1 << 0;
645     /** @hide */
646     public final static int AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE = 0x1 << 1;
647     /** @hide */
648     public final static int AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK = 0x1 << 2;
649     /** @hide */
650     public final static int AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK = 0x1 << 3;
651 
652     private int mEncoding;
653     private int mSampleRate;
654     private int mChannelMask;
655     private int mChannelIndexMask;
656     private int mPropertySetMask;
657 
658     /**
659      * Return the encoding.
660      * See the section on <a href="#encoding">encodings</a> for more information about the different
661      * types of supported audio encoding.
662      * @return one of the values that can be set in {@link Builder#setEncoding(int)} or
663      * {@link AudioFormat#ENCODING_INVALID} if not set.
664      */
getEncoding()665     public int getEncoding() {
666         if ((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_ENCODING) == 0) {
667             return ENCODING_INVALID;
668         }
669         return mEncoding;
670     }
671 
672     /**
673      * Return the sample rate.
674      * @return one of the values that can be set in {@link Builder#setSampleRate(int)} or
675      * {@link #SAMPLE_RATE_UNSPECIFIED} if not set.
676      */
getSampleRate()677     public int getSampleRate() {
678         return mSampleRate;
679     }
680 
681     /**
682      * Return the channel mask.
683      * See the section on <a href="#channelMask">channel masks</a> for more information about
684      * the difference between index-based masks(as returned by {@link #getChannelIndexMask()}) and
685      * the position-based mask returned by this function.
686      * @return one of the values that can be set in {@link Builder#setChannelMask(int)} or
687      * {@link AudioFormat#CHANNEL_INVALID} if not set.
688      */
getChannelMask()689     public int getChannelMask() {
690         if ((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK) == 0) {
691             return CHANNEL_INVALID;
692         }
693         return mChannelMask;
694     }
695 
696     /**
697      * Return the channel index mask.
698      * See the section on <a href="#channelMask">channel masks</a> for more information about
699      * the difference between index-based masks, and position-based masks (as returned
700      * by {@link #getChannelMask()}).
701      * @return one of the values that can be set in {@link Builder#setChannelIndexMask(int)} or
702      * {@link AudioFormat#CHANNEL_INVALID} if not set or an invalid mask was used.
703      */
getChannelIndexMask()704     public int getChannelIndexMask() {
705         if ((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK) == 0) {
706             return CHANNEL_INVALID;
707         }
708         return mChannelIndexMask;
709     }
710 
711     /**
712      * Return the channel count.
713      * @return the channel count derived from the channel position mask or the channel index mask.
714      * Zero is returned if both the channel position mask and the channel index mask are not set.
715      */
getChannelCount()716     public int getChannelCount() {
717         final int channelIndexCount = Integer.bitCount(getChannelIndexMask());
718         int channelCount = channelCountFromOutChannelMask(getChannelMask());
719         if (channelCount == 0) {
720             channelCount = channelIndexCount;
721         } else if (channelCount != channelIndexCount && channelIndexCount != 0) {
722             channelCount = 0; // position and index channel count mismatch
723         }
724         return channelCount;
725     }
726 
727     /** @hide */
getPropertySetMask()728     public int getPropertySetMask() {
729         return mPropertySetMask;
730     }
731 
732     /** @hide */
toLogFriendlyString()733     public String toLogFriendlyString() {
734         return String.format("%dch %dHz %s",
735                 getChannelCount(), mSampleRate, toLogFriendlyEncoding(mEncoding));
736     }
737 
738     /**
739      * Builder class for {@link AudioFormat} objects.
740      * Use this class to configure and create an AudioFormat instance. By setting format
741      * characteristics such as audio encoding, channel mask or sample rate, you indicate which
742      * of those are to vary from the default behavior on this device wherever this audio format
743      * is used. See {@link AudioFormat} for a complete description of the different parameters that
744      * can be used to configure an <code>AudioFormat</code> instance.
745      * <p>{@link AudioFormat} is for instance used in
746      * {@link AudioTrack#AudioTrack(AudioAttributes, AudioFormat, int, int, int)}. In this
747      * constructor, every format characteristic set on the <code>Builder</code> (e.g. with
748      * {@link #setSampleRate(int)}) will alter the default values used by an
749      * <code>AudioTrack</code>. In this case for audio playback with <code>AudioTrack</code>, the
750      * sample rate set in the <code>Builder</code> would override the platform output sample rate
751      * which would otherwise be selected by default.
752      */
753     public static class Builder {
754         private int mEncoding = ENCODING_INVALID;
755         private int mSampleRate = SAMPLE_RATE_UNSPECIFIED;
756         private int mChannelMask = CHANNEL_INVALID;
757         private int mChannelIndexMask = 0;
758         private int mPropertySetMask = AUDIO_FORMAT_HAS_PROPERTY_NONE;
759 
760         /**
761          * Constructs a new Builder with none of the format characteristics set.
762          */
Builder()763         public Builder() {
764         }
765 
766         /**
767          * Constructs a new Builder from a given {@link AudioFormat}.
768          * @param af the {@link AudioFormat} object whose data will be reused in the new Builder.
769          */
Builder(AudioFormat af)770         public Builder(AudioFormat af) {
771             mEncoding = af.mEncoding;
772             mSampleRate = af.mSampleRate;
773             mChannelMask = af.mChannelMask;
774             mChannelIndexMask = af.mChannelIndexMask;
775             mPropertySetMask = af.mPropertySetMask;
776         }
777 
778         /**
779          * Combines all of the format characteristics that have been set and return a new
780          * {@link AudioFormat} object.
781          * @return a new {@link AudioFormat} object
782          */
build()783         public AudioFormat build() {
784             AudioFormat af = new AudioFormat(1980/*ignored*/);
785             af.mEncoding = mEncoding;
786             // not calling setSampleRate is equivalent to calling
787             // setSampleRate(SAMPLE_RATE_UNSPECIFIED)
788             af.mSampleRate = mSampleRate;
789             af.mChannelMask = mChannelMask;
790             af.mChannelIndexMask = mChannelIndexMask;
791             af.mPropertySetMask = mPropertySetMask;
792             return af;
793         }
794 
795         /**
796          * Sets the data encoding format.
797          * @param encoding one of {@link AudioFormat#ENCODING_DEFAULT},
798          *     {@link AudioFormat#ENCODING_PCM_8BIT},
799          *     {@link AudioFormat#ENCODING_PCM_16BIT},
800          *     {@link AudioFormat#ENCODING_PCM_FLOAT},
801          *     {@link AudioFormat#ENCODING_AC3},
802          *     {@link AudioFormat#ENCODING_E_AC3}.
803          *     {@link AudioFormat#ENCODING_DTS},
804          *     {@link AudioFormat#ENCODING_DTS_HD}.
805          * @return the same Builder instance.
806          * @throws java.lang.IllegalArgumentException
807          */
setEncoding(@ncoding int encoding)808         public Builder setEncoding(@Encoding int encoding) throws IllegalArgumentException {
809             switch (encoding) {
810                 case ENCODING_DEFAULT:
811                     mEncoding = ENCODING_PCM_16BIT;
812                     break;
813                 case ENCODING_PCM_8BIT:
814                 case ENCODING_PCM_16BIT:
815                 case ENCODING_PCM_FLOAT:
816                 case ENCODING_AC3:
817                 case ENCODING_E_AC3:
818                 case ENCODING_DTS:
819                 case ENCODING_DTS_HD:
820                 case ENCODING_IEC61937:
821                     mEncoding = encoding;
822                     break;
823                 case ENCODING_INVALID:
824                 default:
825                     throw new IllegalArgumentException("Invalid encoding " + encoding);
826             }
827             mPropertySetMask |= AUDIO_FORMAT_HAS_PROPERTY_ENCODING;
828             return this;
829         }
830 
831         /**
832          * Sets the channel position mask.
833          * The channel position mask specifies the association between audio samples in a frame
834          * with named endpoint channels. The samples in the frame correspond to the
835          * named set bits in the channel position mask, in ascending bit order.
836          * See {@link #setChannelIndexMask(int)} to specify channels
837          * based on endpoint numbered channels. This <a href="#channelPositionMask>description of
838          * channel position masks</a> covers the concept in more details.
839          * @param channelMask describes the configuration of the audio channels.
840          *    <p> For output, the channelMask can be an OR-ed combination of
841          *    channel position masks, e.g.
842          *    {@link AudioFormat#CHANNEL_OUT_FRONT_LEFT},
843          *    {@link AudioFormat#CHANNEL_OUT_FRONT_RIGHT},
844          *    {@link AudioFormat#CHANNEL_OUT_FRONT_CENTER},
845          *    {@link AudioFormat#CHANNEL_OUT_LOW_FREQUENCY}
846          *    {@link AudioFormat#CHANNEL_OUT_BACK_LEFT},
847          *    {@link AudioFormat#CHANNEL_OUT_BACK_RIGHT},
848          *    {@link AudioFormat#CHANNEL_OUT_BACK_CENTER},
849          *    {@link AudioFormat#CHANNEL_OUT_SIDE_LEFT},
850          *    {@link AudioFormat#CHANNEL_OUT_SIDE_RIGHT}.
851          *    <p> For a valid {@link AudioTrack} channel position mask,
852          *    the following conditions apply:
853          *    <br> (1) at most eight channel positions may be used;
854          *    <br> (2) right/left pairs should be matched.
855          *    <p> For input or {@link AudioRecord}, the mask should be
856          *    {@link AudioFormat#CHANNEL_IN_MONO} or
857          *    {@link AudioFormat#CHANNEL_IN_STEREO}.  {@link AudioFormat#CHANNEL_IN_MONO} is
858          *    guaranteed to work on all devices.
859          * @return the same <code>Builder</code> instance.
860          * @throws IllegalArgumentException if the channel mask is invalid or
861          *    if both channel index mask and channel position mask
862          *    are specified but do not have the same channel count.
863          */
setChannelMask(int channelMask)864         public @NonNull Builder setChannelMask(int channelMask) {
865             if (channelMask == CHANNEL_INVALID) {
866                 throw new IllegalArgumentException("Invalid zero channel mask");
867             } else if (/* channelMask != 0 && */ mChannelIndexMask != 0 &&
868                     Integer.bitCount(channelMask) != Integer.bitCount(mChannelIndexMask)) {
869                 throw new IllegalArgumentException("Mismatched channel count for mask " +
870                         Integer.toHexString(channelMask).toUpperCase());
871             }
872             mChannelMask = channelMask;
873             mPropertySetMask |= AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK;
874             return this;
875         }
876 
877         /**
878          * Sets the channel index mask.
879          * A channel index mask specifies the association of audio samples in the frame
880          * with numbered endpoint channels. The i-th bit in the channel index
881          * mask corresponds to the i-th endpoint channel.
882          * For example, an endpoint with four channels is represented
883          * as index mask bits 0 through 3. This <a href="#channelIndexMask>description of channel
884          * index masks</a> covers the concept in more details.
885          * See {@link #setChannelMask(int)} for a positional mask interpretation.
886          * <p> Both {@link AudioTrack} and {@link AudioRecord} support
887          * a channel index mask.
888          * If a channel index mask is specified it is used,
889          * otherwise the channel position mask specified
890          * by <code>setChannelMask</code> is used.
891          * For <code>AudioTrack</code> and <code>AudioRecord</code>,
892          * a channel position mask is not required if a channel index mask is specified.
893          *
894          * @param channelIndexMask describes the configuration of the audio channels.
895          *    <p> For output, the <code>channelIndexMask</code> is an OR-ed combination of
896          *    bits representing the mapping of <code>AudioTrack</code> write samples
897          *    to output sink channels.
898          *    For example, a mask of <code>0xa</code>, or binary <code>1010</code>,
899          *    means the <code>AudioTrack</code> write frame consists of two samples,
900          *    which are routed to the second and the fourth channels of the output sink.
901          *    Unmatched output sink channels are zero filled and unmatched
902          *    <code>AudioTrack</code> write samples are dropped.
903          *    <p> For input, the <code>channelIndexMask</code> is an OR-ed combination of
904          *    bits representing the mapping of input source channels to
905          *    <code>AudioRecord</code> read samples.
906          *    For example, a mask of <code>0x5</code>, or binary
907          *    <code>101</code>, will read from the first and third channel of the input
908          *    source device and store them in the first and second sample of the
909          *    <code>AudioRecord</code> read frame.
910          *    Unmatched input source channels are dropped and
911          *    unmatched <code>AudioRecord</code> read samples are zero filled.
912          * @return the same <code>Builder</code> instance.
913          * @throws IllegalArgumentException if the channel index mask is invalid or
914          *    if both channel index mask and channel position mask
915          *    are specified but do not have the same channel count.
916          */
setChannelIndexMask(int channelIndexMask)917         public @NonNull Builder setChannelIndexMask(int channelIndexMask) {
918             if (channelIndexMask == 0) {
919                 throw new IllegalArgumentException("Invalid zero channel index mask");
920             } else if (/* channelIndexMask != 0 && */ mChannelMask != 0 &&
921                     Integer.bitCount(channelIndexMask) != Integer.bitCount(mChannelMask)) {
922                 throw new IllegalArgumentException("Mismatched channel count for index mask " +
923                         Integer.toHexString(channelIndexMask).toUpperCase());
924             }
925             mChannelIndexMask = channelIndexMask;
926             mPropertySetMask |= AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK;
927             return this;
928         }
929 
930         /**
931          * Sets the sample rate.
932          * @param sampleRate the sample rate expressed in Hz
933          * @return the same Builder instance.
934          * @throws java.lang.IllegalArgumentException
935          */
setSampleRate(int sampleRate)936         public Builder setSampleRate(int sampleRate) throws IllegalArgumentException {
937             // TODO Consider whether to keep the MIN and MAX range checks here.
938             // It is not necessary and poses the problem of defining the limits independently from
939             // native implementation or platform capabilities.
940             if (((sampleRate < SAMPLE_RATE_HZ_MIN) || (sampleRate > SAMPLE_RATE_HZ_MAX)) &&
941                     sampleRate != SAMPLE_RATE_UNSPECIFIED) {
942                 throw new IllegalArgumentException("Invalid sample rate " + sampleRate);
943             }
944             mSampleRate = sampleRate;
945             mPropertySetMask |= AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE;
946             return this;
947         }
948     }
949 
950     @Override
equals(Object o)951     public boolean equals(Object o) {
952         if (this == o) return true;
953         if (o == null || getClass() != o.getClass()) return false;
954 
955         AudioFormat that = (AudioFormat) o;
956 
957         if (mPropertySetMask != that.mPropertySetMask) return false;
958 
959         // return false if any of the properties is set and the values differ
960         return !((((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_ENCODING) != 0)
961                             && (mEncoding != that.mEncoding))
962                     || (((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE) != 0)
963                             && (mSampleRate != that.mSampleRate))
964                     || (((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK) != 0)
965                             && (mChannelMask != that.mChannelMask))
966                     || (((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK) != 0)
967                             && (mChannelIndexMask != that.mChannelIndexMask)));
968     }
969 
970     @Override
hashCode()971     public int hashCode() {
972         return Objects.hash(mPropertySetMask, mSampleRate, mEncoding, mChannelMask,
973                 mChannelIndexMask);
974     }
975 
976     @Override
describeContents()977     public int describeContents() {
978         return 0;
979     }
980 
981     @Override
writeToParcel(Parcel dest, int flags)982     public void writeToParcel(Parcel dest, int flags) {
983         dest.writeInt(mPropertySetMask);
984         dest.writeInt(mEncoding);
985         dest.writeInt(mSampleRate);
986         dest.writeInt(mChannelMask);
987         dest.writeInt(mChannelIndexMask);
988     }
989 
AudioFormat(Parcel in)990     private AudioFormat(Parcel in) {
991         mPropertySetMask = in.readInt();
992         mEncoding = in.readInt();
993         mSampleRate = in.readInt();
994         mChannelMask = in.readInt();
995         mChannelIndexMask = in.readInt();
996     }
997 
998     public static final Parcelable.Creator<AudioFormat> CREATOR =
999             new Parcelable.Creator<AudioFormat>() {
1000         public AudioFormat createFromParcel(Parcel p) {
1001             return new AudioFormat(p);
1002         }
1003         public AudioFormat[] newArray(int size) {
1004             return new AudioFormat[size];
1005         }
1006     };
1007 
1008     @Override
toString()1009     public String toString () {
1010         return new String("AudioFormat:"
1011                 + " props=" + mPropertySetMask
1012                 + " enc=" + mEncoding
1013                 + " chan=0x" + Integer.toHexString(mChannelMask).toUpperCase()
1014                 + " chan_index=0x" + Integer.toHexString(mChannelIndexMask).toUpperCase()
1015                 + " rate=" + mSampleRate);
1016     }
1017 
1018     /** @hide */
1019     @IntDef({
1020         ENCODING_DEFAULT,
1021         ENCODING_PCM_8BIT,
1022         ENCODING_PCM_16BIT,
1023         ENCODING_PCM_FLOAT,
1024         ENCODING_AC3,
1025         ENCODING_E_AC3,
1026         ENCODING_DTS,
1027         ENCODING_DTS_HD,
1028         ENCODING_IEC61937
1029     })
1030     @Retention(RetentionPolicy.SOURCE)
1031     public @interface Encoding {}
1032 
1033 }
1034