1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-26 02:25:58 +01:00

Clean up keyframe handling in rtpwriter.

This commit is contained in:
Juliusz Chroboczek 2020-10-27 19:15:56 +01:00
parent 6c6b0b39c9
commit aae6b460f7

View file

@ -230,6 +230,13 @@ func sendKeyframe(kf []uint16, track conn.DownTrack, cache *packetcache.Cache) {
} }
} }
const (
kfUnneeded = iota
kfNeededPLI
kfNeededFIR
kfNeededNewFIR
)
// rtpWriterLoop is the main loop of an rtpWriter. // rtpWriterLoop is the main loop of an rtpWriter.
func rtpWriterLoop(writer *rtpWriter, up *rtpUpConnection, track *rtpUpTrack) { func rtpWriterLoop(writer *rtpWriter, up *rtpUpConnection, track *rtpUpTrack) {
defer close(writer.done) defer close(writer.done)
@ -241,9 +248,7 @@ func rtpWriterLoop(writer *rtpWriter, up *rtpUpConnection, track *rtpUpTrack) {
local := make([]conn.DownTrack, 0) local := make([]conn.DownTrack, 0)
// 3 means we want a new keyframe, 2 means we already sent FIR but kfNeeded := kfUnneeded
// haven't gotten a keyframe yet, 1 means we want a PLI.
kfNeeded := 0
for { for {
select { select {
@ -283,7 +288,7 @@ func rtpWriterLoop(writer *rtpWriter, up *rtpUpConnection, track *rtpUpTrack) {
) )
} else { } else {
// Request a new keyframe // Request a new keyframe
kfNeeded = 3 kfNeeded = kfNeededNewFIR
} }
} else { } else {
// no keyframe yet, one should // no keyframe yet, one should
@ -328,7 +333,7 @@ func rtpWriterLoop(writer *rtpWriter, up *rtpUpConnection, track *rtpUpTrack) {
err := l.WriteRTP(&packet) err := l.WriteRTP(&packet)
if err != nil { if err != nil {
if err == conn.ErrKeyframeNeeded { if err == conn.ErrKeyframeNeeded {
kfNeeded = 1 kfNeeded = kfNeededPLI
} else { } else {
continue continue
} }
@ -336,27 +341,39 @@ func rtpWriterLoop(writer *rtpWriter, up *rtpUpConnection, track *rtpUpTrack) {
l.Accumulate(uint32(bytes)) l.Accumulate(uint32(bytes))
} }
if kfNeeded > 0 { if kfNeeded > kfUnneeded {
kf := false kf := false
kfValid := false
switch codec { switch codec {
case webrtc.VP8: case webrtc.VP8:
kf = isVP8Keyframe(&packet) kf = isVP8Keyframe(&packet)
default: kfValid = true
kf = true
} }
if kf { if kf {
kfNeeded = 0 kfNeeded = kfUnneeded
} }
}
if kfNeeded >= 2 { if kfNeeded >= kfNeededFIR {
err := up.sendFIR(track, kfNeeded >= 3) err := up.sendFIR(
if err == ErrUnsupportedFeedback { track,
kfNeeded >= kfNeededNewFIR,
)
if err == ErrUnsupportedFeedback {
kfNeeded = kfNeededPLI
} else {
kfNeeded = kfNeededFIR
}
}
if kfNeeded == kfNeededPLI {
up.sendPLI(track) up.sendPLI(track)
} }
kfNeeded = 2
} else if kfNeeded > 0 { if !kfValid {
up.sendPLI(track) // we cannot detect keyframes for
// this codec, reset our state
kfNeeded = kfUnneeded
}
} }
} }
} }