1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-24 17:45:58 +01:00

Merge GetRTP and Nack into GetPacket.

The two function were always called together.  This factors out
the NACKing logic into the track.
This commit is contained in:
Juliusz Chroboczek 2021-07-14 14:20:52 +02:00
parent 36d31f0db8
commit 3d2089f40f
3 changed files with 23 additions and 37 deletions

View file

@ -25,9 +25,9 @@ type UpTrack interface {
Kind() webrtc.RTPCodecType Kind() webrtc.RTPCodecType
Label() string Label() string
Codec() webrtc.RTPCodecCapability Codec() webrtc.RTPCodecCapability
// get a recent packet. Returns 0 if the packet is not in cache. // GetPacket fetches a recent packet. Returns 0 if the packet is
GetRTP(seqno uint16, result []byte) uint16 // not in cache, and, in that case, optionally schedules a NACK.
Nack(seqnos []uint16) error GetPacket(seqno uint16, result []byte, nack bool) uint16
RequestKeyframe() error RequestKeyframe() error
} }

View file

@ -494,23 +494,18 @@ func (t *diskTrack) Write(buf []byte) (int, error) {
if ((p.SequenceNumber - lastSeqno) & 0x8000) == 0 { if ((p.SequenceNumber - lastSeqno) & 0x8000) == 0 {
count := p.SequenceNumber - lastSeqno count := p.SequenceNumber - lastSeqno
if count > 0 && count < 128 { if count > 0 && count < 128 {
var nacks []uint16
for i := lastSeqno + 1; i != p.SequenceNumber; i++ { for i := lastSeqno + 1; i != p.SequenceNumber; i++ {
// different buf each time // different buf each time
buf := make([]byte, 1504) buf := make([]byte, 1504)
n := t.remote.GetRTP(i, buf) n := t.remote.GetPacket(i, buf, true)
if n > 0 { if n == 0 {
p := new(rtp.Packet) continue
err := p.Unmarshal(buf) }
if err == nil { p := new(rtp.Packet)
t.writeRTP(p) err := p.Unmarshal(buf)
} if err == nil {
} else { t.writeRTP(p)
nacks = append(nacks, i)
} }
}
if len(nacks) > 0 {
t.remote.Nack(nacks)
} }
} }
} }

View file

@ -430,10 +430,6 @@ func (up *rtpUpTrack) getLocal() []conn.DownTrack {
return local return local
} }
func (up *rtpUpTrack) GetRTP(seqno uint16, result []byte) uint16 {
return up.cache.Get(seqno, result)
}
func (up *rtpUpTrack) Label() string { func (up *rtpUpTrack) Label() string {
return up.track.RID() return up.track.RID()
} }
@ -712,7 +708,6 @@ func sendNACKs(pc *webrtc.PeerConnection, ssrc webrtc.SSRC, nacks []rtcp.NackPai
} }
func gotNACK(track *rtpDownTrack, p *rtcp.TransportLayerNack) { func gotNACK(track *rtpDownTrack, p *rtcp.TransportLayerNack) {
var unhandled []uint16
buf := make([]byte, packetcache.BufSize) buf := make([]byte, packetcache.BufSize)
for _, nack := range p.Nacks { for _, nack := range p.Nacks {
nack.Range(func(s uint16) bool { nack.Range(func(s uint16) bool {
@ -720,9 +715,8 @@ func gotNACK(track *rtpDownTrack, p *rtcp.TransportLayerNack) {
if !ok { if !ok {
return true return true
} }
l := track.remote.GetRTP(seqno, buf) l := track.remote.GetPacket(seqno, buf, true)
if l == 0 { if l == 0 {
unhandled = append(unhandled, seqno)
return true return true
} }
_, err := track.Write(buf[:l]) _, err := track.Write(buf[:l])
@ -733,33 +727,30 @@ func gotNACK(track *rtpDownTrack, p *rtcp.TransportLayerNack) {
return true return true
}) })
} }
if len(unhandled) == 0 {
return
}
track.remote.Nack(unhandled)
} }
func (track *rtpUpTrack) Nack(nacks []uint16) error { func (track *rtpUpTrack) GetPacket(seqno uint16, result []byte, nack bool) uint16 {
n := track.cache.Get(seqno, result)
if n > 0 || !nack {
return n
}
track.mu.Lock() track.mu.Lock()
defer track.mu.Unlock() defer track.mu.Unlock()
doit := len(track.bufferedNACKs) == 0 doit := len(track.bufferedNACKs) == 0
outer: for _, s := range track.bufferedNACKs {
for _, nack := range nacks { if s == seqno {
for _, seqno := range track.bufferedNACKs { return 0
if seqno == nack {
continue outer
}
} }
track.bufferedNACKs = append(track.bufferedNACKs, nack)
} }
track.bufferedNACKs = append(track.bufferedNACKs, seqno)
if doit { if doit {
go nackWriter(track) go nackWriter(track)
} }
return nil return 0
} }
func rtcpUpListener(conn *rtpUpConnection, track *rtpUpTrack, r *webrtc.RTPReceiver) { func rtcpUpListener(conn *rtpUpConnection, track *rtpUpTrack, r *webrtc.RTPReceiver) {