-
Notifications
You must be signed in to change notification settings - Fork 429
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 RTP VP9 Reader Test #114
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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.Util.getBytesFromHexString; | ||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.mockito.ArgumentMatchers.anyInt; | ||
import static org.mockito.Mockito.when; | ||
|
||
import androidx.media3.common.Format; | ||
import androidx.media3.common.MimeTypes; | ||
import androidx.media3.common.util.ParsableByteArray; | ||
import androidx.media3.exoplayer.rtsp.RtpPacket; | ||
import androidx.media3.exoplayer.rtsp.RtpPayloadFormat; | ||
import androidx.media3.extractor.ExtractorOutput; | ||
import androidx.media3.test.utils.FakeTrackOutput; | ||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
import com.google.common.collect.ImmutableMap; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnit; | ||
import org.mockito.junit.MockitoRule; | ||
|
||
/** | ||
* Unit test for {@link RtpVp9Reader}. | ||
*/ | ||
@RunWith(AndroidJUnit4.class) | ||
public final class RtpVp9ReaderTest { | ||
|
||
private final RtpPacket frame1fragment1 = | ||
createRtpPacket( | ||
/* timestamp= */ 2599168056L, | ||
/* sequenceNumber= */ 40289, | ||
/* marker= */ false, | ||
/* payloadData= */ getBytesFromHexString("08000102030405060708090A")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you extend this into something like this for better readability?
|
||
private final RtpPacket frame1fragment2 = | ||
createRtpPacket( | ||
/* timestamp= */ 2599168056L, | ||
/* sequenceNumber= */ 40290, | ||
/* marker= */ true, | ||
/* payloadData= */ getBytesFromHexString("000B0C0D0E")); | ||
private final byte[] frame1Data = getBytesFromHexString("000102030405060708090A0B0C0D0E"); | ||
private final RtpPacket frame2fragment1 = | ||
createRtpPacket( | ||
/* timestamp= */ 2599168344L, | ||
/* sequenceNumber= */ 40291, | ||
/* marker= */ false, | ||
/* payloadData= */ getBytesFromHexString("080D0C0B0A090807060504")); | ||
// Add optional headers | ||
private final RtpPacket frame2fragment2 = | ||
createRtpPacket( | ||
/* timestamp= */ 2599168344L, | ||
/* sequenceNumber= */ 40292, | ||
/* marker= */ true, | ||
/* payloadData= */ getBytesFromHexString("0003020100")); | ||
private final byte[] frame2Data = getBytesFromHexString("0D0C0B0A09080706050403020100"); | ||
|
||
private static final RtpPayloadFormat VP9_FORMAT = | ||
new RtpPayloadFormat( | ||
new Format.Builder() | ||
.setSampleMimeType(MimeTypes.VIDEO_VP9) | ||
.build(), | ||
/* rtpPayloadType= */ 98, | ||
/* clockRate= */ 48_000, | ||
/* fmtpParameters= */ ImmutableMap.of()); | ||
|
||
@Rule | ||
public final MockitoRule mockito = MockitoJUnit.rule(); | ||
|
||
private ParsableByteArray packetData; | ||
|
||
private RtpVp9Reader vp9Reader; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think there's a need to keep the See also my comment about the |
||
private FakeTrackOutput trackOutput; | ||
@Mock | ||
private ExtractorOutput extractorOutput; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no need to mock |
||
|
||
@Before | ||
public void setUp() { | ||
packetData = new ParsableByteArray(); | ||
trackOutput = new FakeTrackOutput(/* deduplicateConsecutiveFormats= */ true); | ||
when(extractorOutput.track(anyInt(), anyInt())).thenReturn(trackOutput); | ||
vp9Reader = new RtpVp9Reader(VP9_FORMAT); | ||
vp9Reader.createTracks(extractorOutput, /* trackId= */ 0); | ||
} | ||
|
||
@Test | ||
public void consume_validPackets() { | ||
vp9Reader.onReceivingFirstPacket(frame1fragment1.timestamp, frame1fragment1.sequenceNumber); | ||
consume(frame1fragment1); | ||
consume(frame1fragment2); | ||
consume(frame2fragment1); | ||
consume(frame2fragment2); | ||
|
||
assertThat(trackOutput.getSampleCount()).isEqualTo(2); | ||
assertThat(trackOutput.getSampleData(0)).isEqualTo(frame1Data); | ||
assertThat(trackOutput.getSampleTimeUs(0)).isEqualTo(0); | ||
assertThat(trackOutput.getSampleData(1)).isEqualTo(frame2Data); | ||
assertThat(trackOutput.getSampleTimeUs(1)).isEqualTo(3200); | ||
} | ||
|
||
@Test | ||
public void consume_fragmentedFrameMissingFirstFragment() { | ||
// First packet timing information is transmitted over RTSP, not RTP. | ||
vp9Reader.onReceivingFirstPacket(frame1fragment1.timestamp, frame1fragment1.sequenceNumber); | ||
consume(frame1fragment2); | ||
consume(frame2fragment1); | ||
consume(frame2fragment2); | ||
|
||
assertThat(trackOutput.getSampleCount()).isEqualTo(1); | ||
assertThat(trackOutput.getSampleData(0)).isEqualTo(frame2Data); | ||
assertThat(trackOutput.getSampleTimeUs(0)).isEqualTo(3200); | ||
} | ||
|
||
@Test | ||
public void consume_fragmentedFrameMissingBoundaryFragment() { | ||
vp9Reader.onReceivingFirstPacket(frame1fragment1.timestamp, frame1fragment1.sequenceNumber); | ||
consume(frame1fragment1); | ||
consume(frame2fragment1); | ||
consume(frame2fragment2); | ||
|
||
assertThat(trackOutput.getSampleCount()).isEqualTo(2); | ||
assertThat(trackOutput.getSampleData(0)) | ||
.isEqualTo(getBytesFromHexString("000102030405060708090A")); | ||
assertThat(trackOutput.getSampleTimeUs(0)).isEqualTo(0); | ||
assertThat(trackOutput.getSampleData(1)).isEqualTo(frame2Data); | ||
assertThat(trackOutput.getSampleTimeUs(1)).isEqualTo(3200); | ||
} | ||
|
||
@Test | ||
public void consume_outOfOrderFragmentedFrame() { | ||
vp9Reader.onReceivingFirstPacket(frame1fragment1.timestamp, frame1fragment1.sequenceNumber); | ||
consume(frame1fragment1); | ||
consume(frame2fragment1); | ||
consume(frame1fragment2); | ||
consume(frame2fragment2); | ||
|
||
assertThat(trackOutput.getSampleCount()).isEqualTo(2); | ||
assertThat(trackOutput.getSampleData(0)) | ||
.isEqualTo(getBytesFromHexString("000102030405060708090A")); | ||
assertThat(trackOutput.getSampleTimeUs(0)).isEqualTo(0); | ||
assertThat(trackOutput.getSampleData(1)).isEqualTo(frame2Data); | ||
assertThat(trackOutput.getSampleTimeUs(1)).isEqualTo(3200); | ||
} | ||
|
||
private static RtpPacket createRtpPacket( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's unnecessary to wrap a class that already has a |
||
long timestamp, int sequenceNumber, boolean marker, byte[] payloadData) { | ||
return new RtpPacket.Builder() | ||
.setTimestamp((int) timestamp) | ||
.setSequenceNumber(sequenceNumber) | ||
.setMarker(marker) | ||
.setPayloadData(payloadData) | ||
.build(); | ||
} | ||
|
||
private void consume(RtpPacket rtpPacket) { | ||
packetData.reset(rtpPacket.payloadData); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you make this method into This way you can eliminate the need for |
||
vp9Reader.consume( | ||
packetData, | ||
rtpPacket.timestamp, | ||
rtpPacket.sequenceNumber, | ||
/* isFrameBoundary= */ rtpPacket.marker); | ||
} | ||
} |
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
f
infragment
should be in upper case.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.
My bad, the names should be
FRAME_1_FRAGMENT_1
, and please make these fieldsstatic final
too, thanks!