-
Notifications
You must be signed in to change notification settings - Fork 543
Add support for RTSP Mp4a-Latm #162
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
048aaf3
Add support for RTSP Mp4a-Latm
rakeshnitb 9648529
Fix review comment in RTP Mp4a-latm Reader
rakeshnitb 15f9655
Added Rtp Mp4a-Latm Reader Test
rakeshnitb 97afe69
Added support for CSD parsing in RTSP Mp4a-Latm Reader
rakeshnitb 4880057
Fix some more review comment in RTP Mp4a-Latm Reader
rakeshnitb 9f8d699
Fix review comment in CSD parsing of Mp4a-Latm Reader
rakeshnitb 0ac84fe
Fix review comment in mp4a-latm Reader
rakeshnitb ce98d6d
Fix review comment in CSD parsing of Mp4a-Latm
rakeshnitb ca5bab5
Fix review comment in mp4a-latm Reader
rakeshnitb cee05d3
Fix review comment in Mp4a-Latm reader
rakeshnitb a89ed98
Fix review comments in mp4a-latm reader and test
rakeshnitb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
...ies/exoplayer_rtsp/src/main/java/androidx/media3/exoplayer/rtsp/reader/RtpMp4aReader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/* | ||
* Copyright 2022 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package androidx.media3.exoplayer.rtsp.reader; | ||
|
||
import static androidx.media3.common.util.Assertions.checkState; | ||
import static androidx.media3.common.util.Assertions.checkStateNotNull; | ||
import static androidx.media3.common.util.Assertions.checkArgument; | ||
import static androidx.media3.common.util.Util.castNonNull; | ||
import static androidx.media3.exoplayer.rtsp.reader.RtpReaderUtils.toSampleTimeUs; | ||
|
||
import androidx.annotation.Nullable; | ||
import androidx.media3.common.C; | ||
import androidx.media3.common.ParserException; | ||
import androidx.media3.common.util.ParsableBitArray; | ||
import androidx.media3.common.util.ParsableByteArray; | ||
import androidx.media3.common.util.UnstableApi; | ||
import androidx.media3.common.util.Util; | ||
import androidx.media3.exoplayer.rtsp.RtpPacket; | ||
import androidx.media3.exoplayer.rtsp.RtpPayloadFormat; | ||
import androidx.media3.extractor.ExtractorOutput; | ||
import androidx.media3.extractor.TrackOutput; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.checkerframework.checker.nullness.qual.MonotonicNonNull; | ||
|
||
/** | ||
* Parses an MP4A-LATM byte stream carried on RTP packets, and extracts MP4A-LATM Access Units. | ||
* Refer to RFC3016 for more details. | ||
*/ | ||
@UnstableApi | ||
/* package */ final class RtpMp4aReader implements RtpPayloadReader { | ||
private static final String TAG = "RtpMp4aLatmReader"; | ||
|
||
private static final String PARAMETER_MP4A_CONFIG = "config"; | ||
|
||
private final RtpPayloadFormat payloadFormat; | ||
private @MonotonicNonNull TrackOutput trackOutput; | ||
private long firstReceivedTimestamp; | ||
private int previousSequenceNumber; | ||
/** The combined size of a sample that is fragmented into multiple subFrames. */ | ||
private int fragmentedSampleSizeBytes; | ||
private long startTimeOffsetUs; | ||
private long sampleTimeUsOfFragmentedSample; | ||
private int numSubFrames; | ||
rakeshnitb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** Creates an instance. */ | ||
public RtpMp4aReader(RtpPayloadFormat payloadFormat) { | ||
this.payloadFormat = payloadFormat; | ||
firstReceivedTimestamp = C.TIME_UNSET; | ||
previousSequenceNumber = C.INDEX_UNSET; | ||
fragmentedSampleSizeBytes = 0; | ||
// The start time offset must be 0 until the first seek. | ||
startTimeOffsetUs = 0; | ||
sampleTimeUsOfFragmentedSample = C.TIME_UNSET; | ||
} | ||
|
||
@Override | ||
public void createTracks(ExtractorOutput extractorOutput, int trackId) { | ||
trackOutput = extractorOutput.track(trackId, C.TRACK_TYPE_VIDEO); | ||
castNonNull(trackOutput).format(payloadFormat.format); | ||
} | ||
|
||
@Override | ||
public void onReceivingFirstPacket(long timestamp, int sequenceNumber) { | ||
checkState(firstReceivedTimestamp == C.TIME_UNSET); | ||
firstReceivedTimestamp = timestamp; | ||
try { | ||
numSubFrames = getNumOfSubframesFromMpeg4AudioConfig(payloadFormat.fmtpParameters); | ||
rakeshnitb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch (ParserException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void consume( | ||
ParsableByteArray data, long timestamp, int sequenceNumber, boolean rtpMarker) | ||
throws ParserException { | ||
checkStateNotNull(trackOutput); | ||
|
||
int expectedSequenceNumber = RtpPacket.getNextSequenceNumber(previousSequenceNumber); | ||
if(fragmentedSampleSizeBytes > 0 && expectedSequenceNumber < sequenceNumber) { | ||
outputSampleMetadataForFragmentedPackets(); | ||
} | ||
int sampleOffset = 0; | ||
for (int subFrame = 0; subFrame <= numSubFrames; subFrame++) { | ||
rakeshnitb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int sampleLength = 0; | ||
|
||
/* Each subframe starts with a variable length encoding */ | ||
rakeshnitb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (; sampleOffset < data.bytesLeft(); sampleOffset++) { | ||
sampleLength += data.getData()[sampleOffset] & 0xff; | ||
rakeshnitb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ((data.getData()[sampleOffset] & 0xff) != 0xff) { | ||
break; | ||
} | ||
} | ||
sampleOffset++; | ||
data.setPosition(sampleOffset); | ||
|
||
// Write the audio sample | ||
trackOutput.sampleData(data, sampleLength); | ||
sampleOffset += sampleLength; | ||
fragmentedSampleSizeBytes += sampleLength; | ||
} | ||
sampleTimeUsOfFragmentedSample = toSampleTimeUs(startTimeOffsetUs, timestamp, | ||
firstReceivedTimestamp, payloadFormat.clockRate); | ||
if (rtpMarker) { | ||
outputSampleMetadataForFragmentedPackets(); | ||
} | ||
previousSequenceNumber = sequenceNumber; | ||
} | ||
|
||
@Override | ||
public void seek(long nextRtpTimestamp, long timeUs) { | ||
firstReceivedTimestamp = nextRtpTimestamp; | ||
fragmentedSampleSizeBytes = 0; | ||
startTimeOffsetUs = timeUs; | ||
} | ||
|
||
// Internal methods. | ||
|
||
/** | ||
* 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. | ||
* | ||
* @param fmtpAttributes The format parameters, mapped from the SDP FMTP attribute. | ||
* @return The number of subframes. | ||
* @throws ParserException If the MPEG-4 Audio Stream Mux configuration cannot be parsed due to | ||
* unsupported audioMuxVersion. | ||
*/ | ||
private static Integer getNumOfSubframesFromMpeg4AudioConfig( | ||
rakeshnitb marked this conversation as resolved.
Show resolved
Hide resolved
rakeshnitb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ImmutableMap |
||
@Nullable String configInput = fmtpAttributes.get(PARAMETER_MP4A_CONFIG); | ||
int numSubFrames = 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); | ||
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; | ||
rakeshnitb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* Outputs sample metadata. | ||
* | ||
* Call this method only when receiving a end of Mpeg4 partition |
||
*/ | ||
private void outputSampleMetadataForFragmentedPackets() { | ||
trackOutput.sampleMetadata( | ||
sampleTimeUsOfFragmentedSample, | ||
C.BUFFER_FLAG_KEY_FRAME, | ||
fragmentedSampleSizeBytes, | ||
/* offset= */ 0, | ||
/* cryptoData= */ null); | ||
fragmentedSampleSizeBytes = 0; | ||
sampleTimeUsOfFragmentedSample = C.TIME_UNSET; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.