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

AAC RTP packets with multiple AU units per packet #81

Open
belveder79 opened this issue Jan 25, 2023 · 4 comments
Open

AAC RTP packets with multiple AU units per packet #81

belveder79 opened this issue Jan 25, 2023 · 4 comments

Comments

@belveder79
Copy link

belveder79 commented Jan 25, 2023

Thx for your continued effort to improve this asset...

I was wondering how you tested the AACPayload, but at least for me it does not work with e.g. ffmpeg.

while (true)

Overall the code only works if there is just one AU unit present, as the AU units are usually lined up sequentially at the beginning of the payload and the header length (in byte / 2) gives the number of AUs present. The code assumes there is one AU unit, but as there are usually more, it might loose 2-3 other AU units and the outcome is choppy...

instead of using the while loop, I use this one, which seems to work fine:

int ptr = 0;
if (ptr + 2 > rtp_payload.Length) return audio_data; // 2 bytes for AU Header Length

// Get Size of the AU Header
int au_headers_length_bits = (((rtp_payload[ptr] << 8) + (rtp_payload[ptr + 1] << 0))); // 16 bits
int au_headers_length = (int)Math.Ceiling((double)au_headers_length_bits / 8.0);
ptr += 2;

int cnt = 0;
for(int p = 0; p < au_headers_length>>1; p++)
{
  int aac_frame_size = (((rtp_payload[ptr + 2 * p] << 8) + (rtp_payload[ptr + 2 * p + 1] << 0)) >> 3); // 13 bits
  int aac_index_delta = rtp_payload[ptr + 2 * p + 1] & 0x03; // 3 bits

  // extract the AAC block
  if ((ptr + au_headers_length + cnt ) + aac_frame_size > rtp_payload.Length) break; // not enough data to copy
  byte[] aac_data = new byte[aac_frame_size];
  Array.Copy(rtp_payload, (ptr + au_headers_length + cnt), aac_data, 0, aac_frame_size);
  audio_data.Add(aac_data);

  cnt += aac_frame_size;
}
return audio_data;

Any feedback would be great... cheers...

@RogerHardiman
Copy link
Collaborator

Many thanks for this.

When I wrote the code to extract AAC, all I had to use was an online Big Buck Bunny test RTSP stream.
I don't think I tested it on any RTSP source.

Many thanks for the code contribution.
If you want to do it as a Pull Request then you will be listed in the Contributors section by github.

@belveder79
Copy link
Author

thx - will create a pull request....

@RogerHardiman
Copy link
Collaborator

Pull Request has been added. Many thanks for that. It is nice to keep your name in the history.

I had a quick question. What device was sending AAC that showed up the problem?
For example, was it a particular make of IP camera?

@belveder79
Copy link
Author

thx... I was just testing with ffmpeg and streaming directly from an RTSP client like PHZ76/RtspServer
I also passed it through a server like aler9/rtsp-simple-server, so the implementation just did not consider the case of more than one aac packet per payload...

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

No branches or pull requests

2 participants