Skip to content

Commit

Permalink
Implement TrackLocalStatic.WriteRTX
Browse files Browse the repository at this point in the history
The methods WriteRTX and WriteRTPRTX are just like Write and
WriteRTP, but they attempt to use an associated RTX SSID if
one has been negotiated.
  • Loading branch information
jech committed Jan 7, 2025
1 parent 1ee0299 commit 67d2dbc
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions track_local_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,25 @@ func (s *TrackLocalStaticRTP) WriteRTP(p *rtp.Packet) error {

*packet = *p

return s.writeRTP(packet)
return s.writeRTP(packet, false)
}

// WriteRTPRTX is like WriteRTP, but attempts to use the associated RTX
// SSID if one exists
func (s *TrackLocalStaticRTP) WriteRTPRTX(p *rtp.Packet) error {
packet := getPacketAllocationFromPool()

defer resetPacketPoolAllocation(packet)

*packet = *p

return s.writeRTP(packet, true)

Check warning on line 179 in track_local_static.go

View check run for this annotation

Codecov / codecov/patch

track_local_static.go#L172-L179

Added lines #L172 - L179 were not covered by tests
}


// writeRTP is like WriteRTP, except that it may modify the packet p
func (s *TrackLocalStaticRTP) writeRTP(p *rtp.Packet) error {
// if rtx is true, it attempts to use the associated RTX track.
func (s *TrackLocalStaticRTP) writeRTP(p *rtp.Packet, rtx bool) error {
s.mu.RLock()
defer s.mu.RUnlock()

Expand All @@ -177,6 +191,10 @@ func (s *TrackLocalStaticRTP) writeRTP(p *rtp.Packet) error {
for _, b := range s.bindings {
p.Header.SSRC = uint32(b.ssrc)
p.Header.PayloadType = uint8(b.payloadType)
if rtx && b.ssrcRTX != 0 {
p.Header.SSRC = uint32(b.ssrcRTX)
p.Header.PayloadType = uint8(b.payloadTypeRTX)
}

Check warning on line 197 in track_local_static.go

View check run for this annotation

Codecov / codecov/patch

track_local_static.go#L195-L197

Added lines #L195 - L197 were not covered by tests
if _, err := b.writeStream.WriteRTP(&p.Header, p.Payload); err != nil {
writeErrs = append(writeErrs, err)
}
Expand All @@ -190,6 +208,16 @@ 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) {
return s.write(b, false)
}

// WriteRTX is like Write, but attempts to use the associated RTX SSID
func (s *TrackLocalStaticRTP) WriteRTX(b []byte) (n int, err error) {
return s.write(b, true)

Check warning on line 216 in track_local_static.go

View check run for this annotation

Codecov / codecov/patch

track_local_static.go#L215-L216

Added lines #L215 - L216 were not covered by tests
}

// write does the work of Write and WriteRTX
func (s *TrackLocalStaticRTP) write(b []byte, rtx bool) (n int, err error) {
packet := getPacketAllocationFromPool()

defer resetPacketPoolAllocation(packet)
Expand All @@ -198,7 +226,7 @@ func (s *TrackLocalStaticRTP) Write(b []byte) (n int, err error) {
return 0, err
}

return len(b), s.writeRTP(packet)
return len(b), s.writeRTP(packet, rtx)
}

// TrackLocalStaticSample is a TrackLocal that has a pre-set codec and accepts Samples.
Expand Down

0 comments on commit 67d2dbc

Please sign in to comment.