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 RTP VP9 Reader Test #114

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The f in fragment should be in upper case.

Copy link
Contributor

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 fields static final too, thanks!

createRtpPacket(
/* timestamp= */ 2599168056L,
/* sequenceNumber= */ 40289,
/* marker= */ false,
/* payloadData= */ getBytesFromHexString("08000102030405060708090A"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you extend this into something like this for better readability?

byte[] frame1Fragment1Data = getBytesFromHexString("000102...");
RtpPacket frame1fragment1 = new RtpPacket.Builder().
                                                       // ...other setters
                                                       .setPayloadData(Bytes.concat(
                                                         // header
                                                         getBytesFromHexString("0800"),
                                                         frame1Fragment1Data))
                                                       .build();
byte[] frame1Data = Bytes.concat(frame1Fragment1Data, frame1Fragment2Data);

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's a need to keep the vp9Reader as a field, you can just instantiate one in each test. Having it as a test creates the illusion that there's shared state.

See also my comment about the consume method.

private FakeTrackOutput trackOutput;
@Mock
private ExtractorOutput extractorOutput;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to mock ExtractorOutput, you can use FakeExtractorOutput.


@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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unnecessary to wrap a class that already has a Builder in a method with many parameters, no? Please consider removing this method.

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this method into static, and it takes a vp9Reader as parameter? You can also just create a new ParsablyByteArray every time this method is called.

This way you can eliminate the need for vp9Reader and packetData as field.

vp9Reader.consume(
packetData,
rtpPacket.timestamp,
rtpPacket.sequenceNumber,
/* isFrameBoundary= */ rtpPacket.marker);
}
}