Search code examples
webrtccodecopus

Calculate OPUS packet length


I have a simple application where I send OPUS packets from one client to other say A to B.

A reads one packet from a OPUS file, and send to B. Again after 20ms or 30ms reads one more packet and send to B, so on.. Till now I was using RTP over UDP, so on receiving side at B, when I receive the packet, I receive complete packet. After receiving complete packet I write to a new file.

This works fine.

Now I am planning to support RTP over TCP. A will read a complete packet from OPUS file and send to B. When packet is received at B, it may be received as a single packet or multiple packet (tcp behaviour). My requirement is, I should buffer the data till I receive complete packet. Once I receive complete packet, I will write it to a file.

Now my question is, how do I determine the length of OPUS packet at B while I receiving, so that I can buffer it.

Do not want to use libopus etc if somehow I can avoid it. If by any means from received data, can I find out length of packet?


Solution

  • TCP is a stream protocol. You have two primary choices: add a length word (16 bits is enough) before each Opus packet (read length, then read packet, dealing with buffering (wait to get enough bytes for the read), or pad every Opus packet to a specific size. Opus doesn't use fixed-size packets; they depend on the content and the bitrate and quality settings.