Skip to content

Commit

Permalink
Moved duplicate operation to function
Browse files Browse the repository at this point in the history
No functional changes
  • Loading branch information
stephanrotolante authored and ashellunts committed Nov 3, 2022
1 parent 6b1e684 commit 257c9e5
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions track_local_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,27 @@ var rtpPacketPool = sync.Pool{
},
}

func resetPacketPoolAllocation(localPacket *rtp.Packet) {
*localPacket = rtp.Packet{}
rtpPacketPool.Put(localPacket)
}

func getPacketAllocationFromPool() *rtp.Packet {
ipacket := rtpPacketPool.Get()
return ipacket.(*rtp.Packet) //nolint:forcetypeassert
}

// WriteRTP writes a RTP Packet to the TrackLocalStaticRTP
// If one PeerConnection fails the packets will still be sent to
// all PeerConnections. The error message will contain the ID of the failed
// PeerConnections so you can remove them
func (s *TrackLocalStaticRTP) WriteRTP(p *rtp.Packet) error {
ipacket := rtpPacketPool.Get()
packet := ipacket.(*rtp.Packet) //nolint:forcetypeassert
defer func() {
*packet = rtp.Packet{}
rtpPacketPool.Put(ipacket)
}()
packet := getPacketAllocationFromPool()

defer resetPacketPoolAllocation(packet)

*packet = *p

return s.writeRTP(packet)
}

Expand All @@ -166,12 +175,9 @@ func (s *TrackLocalStaticRTP) writeRTP(p *rtp.Packet) error {
// all PeerConnections. The error message will contain the ID of the failed
// PeerConnections so you can remove them
func (s *TrackLocalStaticRTP) Write(b []byte) (n int, err error) {
ipacket := rtpPacketPool.Get()
packet := ipacket.(*rtp.Packet) //nolint:forcetypeassert
defer func() {
*packet = rtp.Packet{}
rtpPacketPool.Put(ipacket)
}()
packet := getPacketAllocationFromPool()

defer resetPacketPoolAllocation(packet)

if err = packet.Unmarshal(b); err != nil {
return 0, err
Expand Down

0 comments on commit 257c9e5

Please sign in to comment.