In my SFU, packets go through a number of data structures, and finally end up in a static buffer that is passed to Track.WriteRTP. This works fine with ordinary tracks, but fails when trying to write data to disk: SampleBuilder.Push requires the packet to never be overwritten. This does not appear to be documented. (My personal opinion is that Push should clone the packet, but as a second choice I'd be okay if the requirement that the Packet and its underlying buffer never be reusied were clearly documented.)
What is more, there is no easy way to clone a packet. I'm currently using the following code:
func clonePacket(packet *rtp.Packet) *rtp.Packet {
buf, err := packet.Marshal()
if err != nil {
return nil
}
var p rtp.Packet
err = p.Unmarshal(buf)
if err != nil {
return nil
}
return &p
}
My suggestions are therefore:
- document that
SampleBuilder.Push requires its parameter to never be modified;
- provide a function
Packet.Clone() that clones a packet efficiently;
- in v3, avoid the complication by having
SampleBuilder.Push copy the packet internally.
In my SFU, packets go through a number of data structures, and finally end up in a static buffer that is passed to
Track.WriteRTP. This works fine with ordinary tracks, but fails when trying to write data to disk:SampleBuilder.Pushrequires the packet to never be overwritten. This does not appear to be documented. (My personal opinion is thatPushshould clone the packet, but as a second choice I'd be okay if the requirement that thePacketand its underlying buffer never be reusied were clearly documented.)What is more, there is no easy way to clone a packet. I'm currently using the following code:
My suggestions are therefore:
SampleBuilder.Pushrequires its parameter to never be modified;Packet.Clone()that clones a packet efficiently;SampleBuilder.Pushcopy the packet internally.