Skip to content

RTSP H265 Aggregation packet support #2413

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
* DASH extension:
* Smooth Streaming extension:
* RTSP extension:
* Add support for RTP Aggregation Packet for H265 in accordance with RFC
7798#4.4.2 ([#2413](https://github.com/androidx/media/pull/2413)).
* Decoder extensions (FFmpeg, VP9, AV1, etc.):
* MIDI extension:
* Leanback extension:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ public void consume(ParsableByteArray data, long timestamp, int sequenceNumber,
if (payloadType >= 0 && payloadType < RTP_PACKET_TYPE_AP) {
processSingleNalUnitPacket(data);
} else if (payloadType == RTP_PACKET_TYPE_AP) {
// TODO: Support AggregationPacket mode.
throw new UnsupportedOperationException("need to implement processAggregationPacket");
processAggregationPacket(data);
} else if (payloadType == RTP_PACKET_TYPE_FU) {
processFragmentationUnitPacket(data, sequenceNumber);
} else {
Expand Down Expand Up @@ -167,6 +166,58 @@ private void processSingleNalUnitPacket(ParsableByteArray data) {
bufferFlags = getBufferFlagsFromNalType(nalHeaderType);
}

/**
* Processes Aggregation packet (RFC7798 Section 4.4.2).
*
* <p>Outputs 2 or more NAL Unit (with start code prepended) to {@link #trackOutput}. Sets {@link
* #bufferFlags} and {@link #fragmentedSampleSizeBytes} accordingly.
*/
@RequiresNonNull("trackOutput")
private void processAggregationPacket(ParsableByteArray data) throws ParserException {
// The structure of an Aggregation Packet.
// 0 1 2 3
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | PayloadHdr (Type=48) | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
// | |
// | two or more aggregation units |
// | |
// | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | :...OPTIONAL RTP padding |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

// The structure of aggregation unit
// 0 1 2 3
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// : DONL (conditional) | NALU size |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | NALU size | |
// +-+-+-+-+-+-+-+-+ NAL unit |
// | |
// | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | :
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

// Since sprop-max-don-diff != 0 is not supported, DONL won't present in the packet.

data.setPosition(2); // skipping payload header (2 bytes)
do {
short nalUnitSize = data.readShort(); // 2 bytes of NAL unit size
int nalHeaderType = (data.getData()[data.getPosition()] >> 1) & 0x3F;
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe the data.getData()[data.getPosition()] would be data.peekUnsignedByte().

Or the whole line as NalUnitUtil.getH265NalUnitType(data.getData(), -3).

if (data.bytesLeft() < nalUnitSize) {
throw ParserException.createForMalformedManifest(
"Malformed Aggregation Packet. NAL unit size exceeds packet size.", /* cause= */ null);
}

fragmentedSampleSizeBytes += writeStartCode();
trackOutput.sampleData(data, nalUnitSize);
fragmentedSampleSizeBytes += nalUnitSize;
bufferFlags = getBufferFlagsFromNalType(nalHeaderType);
} while (data.bytesLeft() > 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

You could also make this a while loop such that while(data.bytesLeft() >= 3)

Copy link
Author

Choose a reason for hiding this comment

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

That is correct, however in this case we may miss "invalid" packet with 1-2 bytes "extra"

}

/**
* Processes Fragmentation Unit packet (RFC7798 Section 4.4.3).
*
Expand Down