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
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";
rakeshnitb marked this conversation as resolved.
Show resolved Hide resolved

/** 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;
rakeshnitb marked this conversation as resolved.
Show resolved Hide resolved
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) {
rakeshnitb marked this conversation as resolved.
Show resolved Hide resolved
Pair<Integer, Integer> configParameters = getSampleRateAndChannelCount(configInput);
channelCount = configParameters.first;
clockRate = configParameters.second;
formatBuilder.setSampleRate(clockRate).setChannelCount(channelCount);
claincly marked this conversation as resolved.
Show resolved Hide resolved
}
}
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;
rakeshnitb marked this conversation as resolved.
Show resolved Hide resolved
byte[] configBuffer = Util.getBytesFromHexString(configInput);
ParsableBitArray scratchBits = new ParsableBitArray(configBuffer);
rakeshnitb marked this conversation as resolved.
Show resolved Hide resolved
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.");
rakeshnitb marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Up @@ -54,7 +54,7 @@
private int fragmentedSampleSizeBytes;
private long startTimeOffsetUs;
private long sampleTimeUsOfFragmentedSample;
private int numSubFrames;
private int numberOfSubframes;

/** Creates an instance. */
public RtpMp4aReader(RtpPayloadFormat payloadFormat) {
Expand All @@ -78,9 +78,9 @@ public void onReceivingFirstPacket(long timestamp, int sequenceNumber) {
checkState(firstReceivedTimestamp == C.TIME_UNSET);
firstReceivedTimestamp = timestamp;
try {
numSubFrames = getNumOfSubframesFromMpeg4AudioConfig(payloadFormat.fmtpParameters);
numberOfSubframes = getNumOfSubframesFromMpeg4AudioConfig(payloadFormat.fmtpParameters);
rakeshnitb marked this conversation as resolved.
Show resolved Hide resolved
} catch (ParserException e) {
e.printStackTrace();
throw new IllegalArgumentException(e);
}
}

Expand All @@ -94,23 +94,30 @@ public void consume(
if(fragmentedSampleSizeBytes > 0 && expectedSequenceNumber < sequenceNumber) {
outputSampleMetadataForFragmentedPackets();
}
int sampleOffset = 0;
for (int subFrame = 0; subFrame <= numSubFrames; subFrame++) {
int audioPayloadOffset = 0;
for (int subFrame = 0; subFrame < numberOfSubframes; subFrame++) {
int sampleLength = 0;

/* Each subframe starts with a variable length encoding */
for (; sampleOffset < data.bytesLeft(); sampleOffset++) {
sampleLength += data.getData()[sampleOffset] & 0xff;
if ((data.getData()[sampleOffset] & 0xff) != 0xff) {
/**
* This implements PayloadLengthInfo() in Chapter 1.7.3.1, it's only support one program and
* one layer.
* Each subframe starts with a variable length encoding.
*/
for (; audioPayloadOffset < data.bytesLeft(); audioPayloadOffset++) {
int payloadMuxLength = data.peekUnsignedByte();
rakeshnitb marked this conversation as resolved.
Show resolved Hide resolved
sampleLength += payloadMuxLength;
if (payloadMuxLength != 0xff) {
break;
} else {
data.setPosition(audioPayloadOffset + 1);
}
}
sampleOffset++;
data.setPosition(sampleOffset);
audioPayloadOffset++;
data.setPosition(audioPayloadOffset);

// Write the audio sample
/* Write the audio sample */
rakeshnitb marked this conversation as resolved.
Show resolved Hide resolved
trackOutput.sampleData(data, sampleLength);
sampleOffset += sampleLength;
audioPayloadOffset+= sampleLength;
rakeshnitb marked this conversation as resolved.
Show resolved Hide resolved
fragmentedSampleSizeBytes += sampleLength;
}
sampleTimeUsOfFragmentedSample = toSampleTimeUs(startTimeOffsetUs, timestamp,
Expand Down Expand Up @@ -140,25 +147,25 @@ public void seek(long nextRtpTimestamp, long timeUs) {
* @throws ParserException If the MPEG-4 Audio Stream Mux configuration cannot be parsed due to
* unsupported audioMuxVersion.
*/
private static Integer getNumOfSubframesFromMpeg4AudioConfig(
private static int getNumOfSubframesFromMpeg4AudioConfig(
ImmutableMap<String, String> fmtpAttributes) throws ParserException {
@Nullable String configInput = fmtpAttributes.get(PARAMETER_MP4A_CONFIG);
int numSubFrames = 0;
int numberOfSubframes = 0;
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.");
numSubFrames = scratchBits.readBits(6);
numberOfSubframes = scratchBits.readBits(6);
checkArgument(scratchBits.readBits(4) == 0, "Invalid numProgram.");
checkArgument(scratchBits.readBits(3) == 0, "Invalid numLayer.");
} else {
throw ParserException.createForMalformedDataOfUnknownType(
"unsupported audio mux version: " + audioMuxVersion, null);
}
}
return numSubFrames;
return numberOfSubframes + 1;
}

/**
Expand Down