1 /* 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 17 package android.media; 18 19 import android.annotation.IntDef; 20 import android.annotation.NonNull; 21 import android.annotation.SystemApi; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 25 import java.lang.annotation.Retention; 26 import java.lang.annotation.RetentionPolicy; 27 import java.util.Arrays; 28 import java.util.Objects; 29 30 /** 31 * The AudioDescriptor contains the information to describe the audio playback/capture 32 * capabilities. The capabilities are described by a byte array, which is defined by a 33 * particular standard. This is used when the format is unrecognized to the platform. 34 */ 35 public class AudioDescriptor implements Parcelable { 36 /** 37 * The audio standard is not specified. 38 */ 39 public static final int STANDARD_NONE = 0; 40 /** 41 * The Extended Display Identification Data (EDID) standard for a short audio descriptor. 42 */ 43 public static final int STANDARD_EDID = 1; 44 45 /** @hide */ 46 @IntDef({ 47 STANDARD_NONE, 48 STANDARD_EDID, 49 }) 50 @Retention(RetentionPolicy.SOURCE) 51 public @interface AudioDescriptorStandard {} 52 53 private final int mStandard; 54 private final byte[] mDescriptor; 55 private final int mEncapsulationType; 56 57 /** 58 * @hide 59 * Constructor from standard, encapsulation type and descriptor 60 * @param standard the standard of the audio descriptor 61 * @param encapsulationType the encapsulation type of the audio descriptor 62 * @param descriptor the audio descriptor 63 */ 64 @SystemApi AudioDescriptor(int standard, int encapsulationType, @NonNull byte[] descriptor)65 public AudioDescriptor(int standard, int encapsulationType, @NonNull byte[] descriptor) { 66 mStandard = standard; 67 mEncapsulationType = encapsulationType; 68 mDescriptor = descriptor; 69 } 70 71 /** 72 * @return the standard that defines audio playback/capture capabilities. 73 */ getStandard()74 public @AudioDescriptorStandard int getStandard() { 75 return mStandard; 76 } 77 78 /** 79 * @return a byte array that describes audio playback/capture capabilities as encoded by the 80 * standard for this AudioDescriptor. 81 */ getDescriptor()82 public @NonNull byte[] getDescriptor() { 83 return mDescriptor; 84 } 85 86 /** 87 * The encapsulation type indicates what encapsulation type is required when the framework is 88 * using this extra audio descriptor for playing to a device exposing this audio profile. 89 * When encapsulation is required, only playback with {@link android.media.AudioTrack} API is 90 * supported. But playback with {@link android.media.MediaPlayer} is not. 91 * When an encapsulation type is required, the {@link AudioFormat} encoding selected when 92 * creating the {@link AudioTrack} must match the encapsulation type, e.g 93 * AudioFormat#ENCODING_IEC61937 for AudioProfile.AUDIO_ENCAPSULATION_TYPE_IEC61937. 94 * 95 * @return an integer representing the encapsulation type 96 * 97 * @see AudioProfile#AUDIO_ENCAPSULATION_TYPE_NONE 98 * @see AudioProfile#AUDIO_ENCAPSULATION_TYPE_IEC61937 99 */ getEncapsulationType()100 public @AudioProfile.EncapsulationType int getEncapsulationType() { 101 return mEncapsulationType; 102 } 103 104 @Override hashCode()105 public int hashCode() { 106 return Objects.hash(mStandard, mEncapsulationType, Arrays.hashCode(mDescriptor)); 107 } 108 109 @Override equals(Object o)110 public boolean equals(Object o) { 111 if (this == o) return true; 112 if (o == null || getClass() != o.getClass()) return false; 113 114 AudioDescriptor that = (AudioDescriptor) o; 115 return ((mStandard == that.mStandard) 116 && (mEncapsulationType == that.mEncapsulationType) 117 && (Arrays.equals(mDescriptor, that.mDescriptor))); 118 } 119 120 @Override toString()121 public String toString() { 122 StringBuilder sb = new StringBuilder("{"); 123 sb.append("standard=" + mStandard); 124 sb.append(", encapsulation type=" + mEncapsulationType); 125 if (mDescriptor != null && mDescriptor.length > 0) { 126 sb.append(", descriptor=").append(Arrays.toString(mDescriptor)); 127 } 128 sb.append("}"); 129 return sb.toString(); 130 } 131 132 @Override describeContents()133 public int describeContents() { 134 return 0; 135 } 136 137 @Override writeToParcel(@onNull Parcel dest, int flags)138 public void writeToParcel(@NonNull Parcel dest, int flags) { 139 dest.writeInt(mStandard); 140 dest.writeInt(mEncapsulationType); 141 dest.writeByteArray(mDescriptor); 142 } 143 AudioDescriptor(@onNull Parcel in)144 private AudioDescriptor(@NonNull Parcel in) { 145 mStandard = in.readInt(); 146 mEncapsulationType = in.readInt(); 147 mDescriptor = in.createByteArray(); 148 } 149 150 public static final @NonNull Parcelable.Creator<AudioDescriptor> CREATOR = 151 new Parcelable.Creator<AudioDescriptor>() { 152 /** 153 * Rebuilds an AudioDescriptor previously stored with writeToParcel(). 154 * @param p Parcel object to read the AudioDescriptor from 155 * @return a new AudioDescriptor created from the data in the parcel 156 */ 157 public AudioDescriptor createFromParcel(Parcel p) { 158 return new AudioDescriptor(p); 159 } 160 161 public AudioDescriptor[] newArray(int size) { 162 return new AudioDescriptor[size]; 163 } 164 }; 165 } 166