Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for RTSP Mp4a-Latm #162

Closed
wants to merge 11 commits into from
Prev Previous commit
Next Next commit
Fix review comment in CSD parsing of Mp4a-Latm Reader
Change-Id: I6fc07d88a7dbc52fc2fbe0e5ad45a53f8f25c4fd
  • Loading branch information
rakeshnitb committed Sep 30, 2022
commit 9f8d69929df0af324eb47e5f298f90e42cc6dccc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
private static final String PARAMETER_H265_SPROP_VPS = "sprop-vps";
private static final String PARAMETER_H265_SPROP_MAX_DON_DIFF = "sprop-max-don-diff";
private static final String PARAMETER_MP4V_CONFIG = "config";
private static final String PARAMETER_MP4A_CPRESENT = "cpresent";

/** Prefix for the RFC6381 codecs string for AAC formats. */
private static final String AAC_CODECS_PREFIX = "mp4a.40.";
Expand Down Expand Up @@ -211,11 +212,18 @@ public int hashCode() {
checkArgument(channelCount != C.INDEX_UNSET);
checkArgument(!fmtpParameters.isEmpty());
if(mediaEncoding.equals(RtpPayloadFormat.RTP_MEDIA_MPEG4_AUDIO)) {
Pair<Integer, Integer> cfgOpts = getSampleRateAndChannelCountFromAudioConfig(
fmtpParameters, channelCount, clockRate);
channelCount = cfgOpts.first;
clockRate = cfgOpts.second;
formatBuilder.setSampleRate(clockRate).setChannelCount(channelCount);
boolean isCPresent = true;
if (fmtpParameters.get(PARAMETER_MP4A_CPRESENT) != null && fmtpParameters.get(
PARAMETER_MP4A_CPRESENT).equals("0")) {
isCPresent = false;
}
@Nullable String configInput = fmtpParameters.get(PARAMETER_MP4V_CONFIG);
if (!isCPresent && configInput != null && configInput.length() % 2 == 0) {
Pair<Integer, Integer> configParameters = getSampleRateAndChannelCount(configInput);
channelCount = configParameters.first;
clockRate = configParameters.second;
formatBuilder.setSampleRate(clockRate).setChannelCount(channelCount);
}
}
processAacFmtpAttribute(formatBuilder, fmtpParameters, channelCount, clockRate);
break;
Expand Down Expand Up @@ -311,33 +319,30 @@ private static void processAacFmtpAttribute(
}

/**
* Parses an MPEG-4 Audio Stream Mux configuration, as defined in ISO/IEC14496-3. FMTP attribute
* contains config which is a byte array containing the MPEG-4 Audio Stream Mux configuration to
* parse.
* Returns a {@link Pair} of sample rate and channel count, by parsing the
* MPEG4 Audio Stream Mux configuration.
*
* <p>fmtp attribute {@code config} includes the MPEG4 Audio Stream Mux
* configuration (ISO/IEC14496-3, Chapter 1.7.3).
*/
private static Pair<Integer, Integer> getSampleRateAndChannelCountFromAudioConfig(
ImmutableMap<String, String> fmtpAttributes,
int channelCount,
int sampleRate) {
@Nullable String configInput = fmtpAttributes.get(PARAMETER_MP4V_CONFIG);
if (configInput != null && configInput.length() % 2 == 0) {
byte[] configBuffer = Util.getBytesFromHexString(configInput);
ParsableBitArray scratchBits = new ParsableBitArray(configBuffer);
int audioMuxVersion = scratchBits.readBits(1);
if (audioMuxVersion == 0) {
checkArgument(scratchBits.readBits(1) == 1, "Invalid allStreamsSameTimeFraming.");
scratchBits.readBits(6);
checkArgument(scratchBits.readBits(4) == 0, "Invalid numProgram.");
checkArgument(scratchBits.readBits(3) == 0, "Invalid numLayer.");
AacUtil.Config aacConfig = null;
try {
aacConfig = AacUtil.parseAudioSpecificConfig(scratchBits, false);
} catch (ParserException e) {
throw new IllegalArgumentException(e);
}
sampleRate = aacConfig.sampleRateHz;
channelCount = aacConfig.channelCount;
private static Pair<Integer, Integer> getSampleRateAndChannelCount(String configInput) {
int channelCount = 0, sampleRate = 0;
byte[] configBuffer = Util.getBytesFromHexString(configInput);
ParsableBitArray scratchBits = new ParsableBitArray(configBuffer);
int audioMuxVersion = scratchBits.readBits(1);
if (audioMuxVersion == 0) {
checkArgument(scratchBits.readBits(1) == 1, "Invalid allStreamsSameTimeFraming.");
scratchBits.readBits(6);
checkArgument(scratchBits.readBits(4) == 0, "Invalid numProgram.");
checkArgument(scratchBits.readBits(3) == 0, "Invalid numLayer.");
@Nullable AacUtil.Config aacConfig = null;
try {
aacConfig = AacUtil.parseAudioSpecificConfig(scratchBits, false);
} catch (ParserException e) {
throw new IllegalArgumentException(e);
}
sampleRate = aacConfig.sampleRateHz;
channelCount = aacConfig.channelCount;
}
return Pair.create(channelCount, sampleRate);
}
Expand Down