-
Notifications
You must be signed in to change notification settings - Fork 484
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
Rtp mpeg4 #35
Merged
Merged
Rtp mpeg4 #35
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d2f807e
Add support for RTSP MPEG4
ManishaJajoo 743437e
Clean up RtpMpeg4Reader
ManishaJajoo dfef2d1
Some minor cleanup in RTPMpeg4Reader
ManishaJajoo e7567d2
Fix review comments in RtpMPEG4Reader
ManishaJajoo 706d5ac
Merge branch 'main' into rtp-mpeg4
ManishaJajoo ef9393a
Fix review comments on RtpMPEG4Reader
ManishaJajoo 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 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 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 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 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
147 changes: 147 additions & 0 deletions
147
...es/exoplayer_rtsp/src/main/java/androidx/media3/exoplayer/rtsp/reader/RtpMPEG4Reader.java
This file contains 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,147 @@ | ||
/* | ||
* 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.checkStateNotNull; | ||
import static androidx.media3.common.util.Util.castNonNull; | ||
|
||
import androidx.media3.common.C; | ||
import androidx.media3.common.ParserException; | ||
import androidx.media3.common.util.Log; | ||
import androidx.media3.common.util.ParsableByteArray; | ||
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.primitives.Bytes; | ||
import org.checkerframework.checker.nullness.qual.MonotonicNonNull; | ||
|
||
/** | ||
* Parses an MPEG4 byte stream carried on RTP packets, and extracts MPEG4 Access Units. Refer to | ||
* RFC6416 for more details. | ||
*/ | ||
/* package */ final class RtpMPEG4Reader implements RtpPayloadReader { | ||
private static final String TAG = "RtpMPEG4Reader"; | ||
|
||
private static final long MEDIA_CLOCK_FREQUENCY = 90_000; | ||
|
||
/** VOP unit type. */ | ||
private static final int I_VOP = 0; | ||
|
||
private final RtpPayloadFormat payloadFormat; | ||
private @MonotonicNonNull TrackOutput trackOutput; | ||
@C.BufferFlags private int bufferFlags; | ||
private long firstReceivedTimestamp; | ||
private int previousSequenceNumber; | ||
private long startTimeOffsetUs; | ||
private int sampleLength; | ||
|
||
/** Creates an instance. */ | ||
public RtpMPEG4Reader(RtpPayloadFormat payloadFormat) { | ||
this.payloadFormat = payloadFormat; | ||
firstReceivedTimestamp = C.TIME_UNSET; | ||
previousSequenceNumber = C.INDEX_UNSET; | ||
sampleLength = 0; | ||
} | ||
|
||
@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) {} | ||
|
||
@Override | ||
public void consume(ParsableByteArray data, long timestamp, int sequenceNumber, boolean rtpMarker) | ||
throws ParserException { | ||
checkStateNotNull(trackOutput); | ||
// Check that this packet is in the sequence of the previous packet. | ||
if (previousSequenceNumber != C.INDEX_UNSET) { | ||
int expectedSequenceNumber = RtpPacket.getNextSequenceNumber(previousSequenceNumber); | ||
if (sequenceNumber != expectedSequenceNumber) { | ||
Log.w( | ||
TAG, | ||
Util.formatInvariant( | ||
"Received RTP packet with unexpected sequence number. Expected: %d; received: %d." | ||
+ " Dropping packet.", | ||
expectedSequenceNumber, sequenceNumber)); | ||
return; | ||
} | ||
} | ||
|
||
// Parse VOP Type and get the buffer flags | ||
int limit = data.bytesLeft(); | ||
trackOutput.sampleData(data, limit); | ||
if (sampleLength == 0) { | ||
bufferFlags = getBufferFlagsFromVop(data); | ||
} | ||
sampleLength += limit; | ||
|
||
// Marker (M) bit: The marker bit is set to 1 to indicate the last RTP | ||
// packet(or only RTP packet) of a VOP. When multiple VOPs are carried | ||
// in the same RTP packet, the marker bit is set to 1. | ||
if (rtpMarker) { | ||
if (firstReceivedTimestamp == C.TIME_UNSET) { | ||
firstReceivedTimestamp = timestamp; | ||
} | ||
|
||
long timeUs = toSampleUs(startTimeOffsetUs, timestamp, firstReceivedTimestamp); | ||
trackOutput.sampleMetadata(timeUs, bufferFlags, sampleLength, 0, null); | ||
sampleLength = 0; | ||
} | ||
previousSequenceNumber = sequenceNumber; | ||
} | ||
|
||
@Override | ||
public void seek(long nextRtpTimestamp, long timeUs) { | ||
firstReceivedTimestamp = nextRtpTimestamp; | ||
startTimeOffsetUs = timeUs; | ||
sampleLength = 0; | ||
} | ||
|
||
// Internal methods. | ||
|
||
/** | ||
* Parses VOP Coding type. | ||
* | ||
* Sets {@link #bufferFlags} according to the VOP Coding type. | ||
*/ | ||
@C.BufferFlags | ||
private static int getBufferFlagsFromVop(ParsableByteArray data) { | ||
// search for VOP_START_CODE (00 00 01 B6) | ||
byte[] inputData = data.getData(); | ||
byte[] startCode = new byte[] {0x0, 0x0, 0x1, (byte) 0xB6}; | ||
int vopStartCodePos = Bytes.indexOf(inputData, startCode); | ||
if (vopStartCodePos != -1) { | ||
data.setPosition(vopStartCodePos + 4); | ||
int vopType = data.peekUnsignedByte() >> 6; | ||
return (vopType == I_VOP ? C.BUFFER_FLAG_KEY_FRAME : 0); | ||
} | ||
return 0; | ||
} | ||
|
||
private static long toSampleUs( | ||
long startTimeOffsetUs, long rtpTimestamp, long firstReceivedRtpTimestamp) { | ||
return startTimeOffsetUs | ||
+ Util.scaleLargeTimestamp( | ||
(rtpTimestamp - firstReceivedRtpTimestamp), | ||
/* multiplier= */ C.MICROS_PER_SECOND, | ||
/* divisor= */ MEDIA_CLOCK_FREQUENCY); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the default width and height 352x288? Is it in an RFC? If so please add a link
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default resolution is not mentioned in RFC.
For non-standard fmtp where configinput is missing, we use 352X288 as default since CIF is the default resolution used by Android Software decoder. Adding the link for your reference
https://cs.android.com/android/platform/superproject/+/master:frameworks/av/media/codec2/components/mpeg4_h263/C2SoftMpeg4Dec.cpp;l=130