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 1 commit into
base: main
Choose a base branch
from
Open
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
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,67 @@ private void processSingleNalUnitPacket(ParsableByteArray data) {
bufferFlags = getBufferFlagsFromNalType(nalHeaderType);
}

/**
* Processes Single NAL Unit 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.

int endOfData = data.bytesLeft();
int currentPosition = 2; // skipping payload header (2 bytes)
do {
int nalUnitSize = ((data.getData()[currentPosition] & 0xFF) << 8)
| (data.getData()[currentPosition + 1] & 0xFF); //2 bytes of NAL unit size
currentPosition += 2;
if (currentPosition + nalUnitSize > endOfData) {
throw ParserException.createForMalformedManifest(
"Malformed Aggregation Packet. NAL unit size exceeds packet size.",
/* cause= */ null
);
}

data.setPosition(currentPosition);
fragmentedSampleSizeBytes += writeStartCode();
trackOutput.sampleData(data, nalUnitSize);
fragmentedSampleSizeBytes += nalUnitSize;

int nalHeaderType = (data.getData()[currentPosition] >> 1) & 0x3F;
bufferFlags = getBufferFlagsFromNalType(nalHeaderType);
currentPosition += nalUnitSize;
} while (currentPosition < endOfData);
}

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