Skip to content

bound aac frame to the 13-bit adts frame_length in TsWriter::Write - #3744

Open
sahvx655-wq wants to merge 1 commit into
apache:masterfrom
sahvx655-wq:ts-adts-frame-length
Open

sahvx655-wq wants to merge 1 commit into
apache:masterfrom
sahvx655-wq:ts-adts-frame-length

Conversation

@sahvx655-wq

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: N/A

Problem Summary:

TsWriter::Write(const RtmpAudioMessage&) in src/brpc/ts.cpp wraps each raw AAC frame in a 7-byte ADTS header. The ADTS frame_length field is 13 bits and counts the header as well as the payload, but the guard only rejects payloads above 0x1fff. A raw AAC frame of 8185 to 8191 bytes gives frame_length 8192 to 8198, and the & 0x03 / & 0xff / & 0xe0 masks truncate that to 0 to 6. The muxer then writes an ADTS header claiming the frame is shorter than the header itself, and every TS/HLS consumer reads the publisher's payload bytes that follow as further ADTS frames.

The frame size comes straight from the RTMP publisher, so any client that can publish to a server muxing with TsWriter can trigger it with a single oversized audio message. I found it by reading how the size guard lines up with the bit packing below it, then confirmed it with a test that writes an AAC sequence header followed by raw frames of increasing size. On master the emitted headers decode as:

payload=8185 adts_frame_length=0
payload=8190 adts_frame_length=5
payload=8191 adts_frame_length=6

What is changed and the side effects?

Changed:

  • Lower the bound in TsWriter::Write to 0x1fff - 7, so the largest accepted raw frame is 8184 bytes and frame_length always fits its 13 bits. Oversized frames get the same EINVAL status the existing check already returns, and nothing is appended to the output buffer.
  • Add RtmpTest.ts_writer_aac_frame_length_fits_adts_header to test/brpc_rtmp_unittest.cpp. It checks that 8184 bytes still muxes with frame_length 8191 and that 8185 and 0x1fff bytes are rejected. It fails on master and passes with the fix; the full brpc_rtmp_unittest suite (34 tests) passes locally on macOS.

Side effects:

  • Performance effects: none, the change is one constant in an existing comparison.

  • Breaking backward compatibility: AAC frames of 8185 to 8191 bytes are now rejected instead of being written with a corrupt ADTS header. A single ADTS frame cannot legally carry them, so no valid stream is affected.


Check List:

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain, and the change includes boundary-focused regression coverage for valid and oversized AAC payload handling.

No open issues 🎉

What changed in this PR

This pull request fixes AAC-to-ADTS muxing in TsWriter::Write by ensuring the 13-bit ADTS frame_length field includes the 7-byte header without overflow.

Changes:

  • Restricts raw AAC payloads to 8184 bytes.
  • Rejects oversized payloads before writing output.
  • Adds regression coverage for valid and rejected frame-size boundaries.
File Description
test/​brpc_rtmp_unittest.cpp Adds regression tests and supporting helpers for AAC ADTS frame-length boundary encoding and oversized-payload rejection.
src/​brpc/​ts.cpp Adjusts the AAC payload-size guard so the complete ADTS frame length fits within the 13-bit field.

Review effort: Lite

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants