1 /* 2 * Copyright (C) 2010 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.net.rtp; 18 19 import java.util.Arrays; 20 21 /** 22 * This class defines a collection of audio codecs to be used with 23 * {@link AudioStream}s. Their parameters are designed to be exchanged using 24 * Session Description Protocol (SDP). Most of the values listed here can be 25 * found in RFC 3551, while others are described in separated standards. 26 * 27 * <p>Few simple configurations are defined as public static instances for the 28 * convenience of direct uses. More complicated ones could be obtained using 29 * {@link #getCodec(int, String, String)}. For example, one can use the 30 * following snippet to create a mode-1-only AMR codec.</p> 31 * <pre> 32 * AudioCodec codec = AudioCodec.getCodec(100, "AMR/8000", "mode-set=1"); 33 * </pre> 34 * 35 * @see AudioStream 36 * @hide 37 */ 38 public class AudioCodec { 39 /** 40 * The RTP payload type of the encoding. 41 */ 42 public final int type; 43 44 /** 45 * The encoding parameters to be used in the corresponding SDP attribute. 46 */ 47 public final String rtpmap; 48 49 /** 50 * The format parameters to be used in the corresponding SDP attribute. 51 */ 52 public final String fmtp; 53 54 /** 55 * G.711 u-law audio codec. 56 */ 57 public static final AudioCodec PCMU = new AudioCodec(0, "PCMU/8000", null); 58 59 /** 60 * G.711 a-law audio codec. 61 */ 62 public static final AudioCodec PCMA = new AudioCodec(8, "PCMA/8000", null); 63 64 /** 65 * GSM Full-Rate audio codec, also known as GSM-FR, GSM 06.10, GSM, or 66 * simply FR. 67 */ 68 public static final AudioCodec GSM = new AudioCodec(3, "GSM/8000", null); 69 70 /** 71 * GSM Enhanced Full-Rate audio codec, also known as GSM-EFR, GSM 06.60, or 72 * simply EFR. 73 */ 74 public static final AudioCodec GSM_EFR = new AudioCodec(96, "GSM-EFR/8000", null); 75 76 /** 77 * Adaptive Multi-Rate narrowband audio codec, also known as AMR or AMR-NB. 78 * Currently CRC, robust sorting, and interleaving are not supported. See 79 * more details about these features in RFC 4867. 80 */ 81 public static final AudioCodec AMR = new AudioCodec(97, "AMR/8000", null); 82 83 private static final AudioCodec[] sCodecs = {GSM_EFR, AMR, GSM, PCMU, PCMA}; 84 85 private AudioCodec(int type, String rtpmap, String fmtp) { 86 this.type = type; 87 this.rtpmap = rtpmap; 88 this.fmtp = fmtp; 89 } 90 91 /** 92 * Returns system supported audio codecs. 93 */ 94 public static AudioCodec[] getCodecs() { 95 return Arrays.copyOf(sCodecs, sCodecs.length); 96 } 97 98 /** 99 * Creates an AudioCodec according to the given configuration. 100 * 101 * @param type The payload type of the encoding defined in RTP/AVP. 102 * @param rtpmap The encoding parameters specified in the corresponding SDP 103 * attribute, or null if it is not available. 104 * @param fmtp The format parameters specified in the corresponding SDP 105 * attribute, or null if it is not available. 106 * @return The configured AudioCodec or {@code null} if it is not supported. 107 */ 108 public static AudioCodec getCodec(int type, String rtpmap, String fmtp) { 109 if (type < 0 || type > 127) { 110 return null; 111 } 112 113 AudioCodec hint = null; 114 if (rtpmap != null) { 115 String clue = rtpmap.trim().toUpperCase(); 116 for (AudioCodec codec : sCodecs) { 117 if (clue.startsWith(codec.rtpmap)) { 118 String channels = clue.substring(codec.rtpmap.length()); 119 if (channels.length() == 0 || channels.equals("/1")) { 120 hint = codec; 121 } 122 break; 123 } 124 } 125 } else if (type < 96) { 126 for (AudioCodec codec : sCodecs) { 127 if (type == codec.type) { 128 hint = codec; 129 rtpmap = codec.rtpmap; 130 break; 131 } 132 } 133 } 134 135 if (hint == null) { 136 return null; 137 } 138 if (hint == AMR && fmtp != null) { 139 String clue = fmtp.toLowerCase(); 140 if (clue.contains("crc=1") || clue.contains("robust-sorting=1") || 141 clue.contains("interleaving=")) { 142 return null; 143 } 144 } 145 return new AudioCodec(type, rtpmap, fmtp); 146 } 147 } 148