Skip to content

Add Clone to rtp.Packet #88

@jech

Description

@jech

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions