2020-10-03 12:56:16 +02:00
|
|
|
package rtpconn
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"log"
|
2021-05-17 03:54:20 +02:00
|
|
|
"time"
|
2020-10-03 12:56:16 +02:00
|
|
|
|
|
|
|
"github.com/pion/rtp"
|
|
|
|
"github.com/pion/webrtc/v3"
|
|
|
|
|
2020-12-19 17:37:48 +01:00
|
|
|
"github.com/jech/galene/packetcache"
|
|
|
|
"github.com/jech/galene/rtptime"
|
2020-10-03 12:56:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func readLoop(conn *rtpUpConnection, track *rtpUpTrack) {
|
|
|
|
writers := rtpWriterPool{conn: conn, track: track}
|
|
|
|
defer func() {
|
|
|
|
writers.close()
|
|
|
|
close(track.readerDone)
|
|
|
|
}()
|
|
|
|
|
|
|
|
isvideo := track.track.Kind() == webrtc.RTPCodecTypeVideo
|
2020-12-04 01:15:52 +01:00
|
|
|
codec := track.track.Codec()
|
2020-10-12 14:38:41 +02:00
|
|
|
sendNACK := track.hasRtcpFb("nack", "")
|
2021-05-17 03:54:20 +02:00
|
|
|
sendPLI := track.hasRtcpFb("nack", "pli")
|
2021-05-11 22:36:56 +02:00
|
|
|
var kfNeeded bool
|
2021-05-17 03:54:20 +02:00
|
|
|
var kfRequested time.Time
|
2020-10-03 12:56:16 +02:00
|
|
|
buf := make([]byte, packetcache.BufSize)
|
|
|
|
var packet rtp.Packet
|
|
|
|
for {
|
2021-05-17 03:54:20 +02:00
|
|
|
|
|
|
|
inner:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case action := <-track.localCh:
|
|
|
|
switch action.action {
|
|
|
|
case trackActionAdd, trackActionDel:
|
|
|
|
err := writers.add(
|
|
|
|
action.track,
|
|
|
|
action.action == trackActionAdd,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(
|
|
|
|
"add/remove track: %v",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
case trackActionKeyframe:
|
|
|
|
kfNeeded = true
|
|
|
|
default:
|
|
|
|
log.Printf("Unknown action")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-19 19:03:14 +01:00
|
|
|
bytes, _, err := track.track.Read(buf)
|
2020-10-03 12:56:16 +02:00
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Printf("%v", err)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
track.rate.Accumulate(uint32(bytes))
|
|
|
|
|
|
|
|
err = packet.Unmarshal(buf[:bytes])
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("%v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
track.jitter.Accumulate(packet.Timestamp)
|
|
|
|
|
2021-05-11 22:36:56 +02:00
|
|
|
kf, kfKnown := isKeyframe(codec.MimeType, &packet)
|
|
|
|
if kf || !kfKnown {
|
|
|
|
kfNeeded = false
|
|
|
|
}
|
2021-04-29 17:03:25 +02:00
|
|
|
if packet.Extension {
|
|
|
|
packet.Extension = false
|
|
|
|
packet.Extensions = nil
|
|
|
|
bytes, err = packet.MarshalTo(buf)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("%v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-03 12:54:17 +02:00
|
|
|
first, index := track.cache.Store(
|
2020-10-04 13:11:33 +02:00
|
|
|
packet.SequenceNumber, packet.Timestamp,
|
2020-10-08 17:58:58 +02:00
|
|
|
kf, packet.Marker, buf[:bytes],
|
2020-10-03 12:54:17 +02:00
|
|
|
)
|
2020-10-04 13:11:33 +02:00
|
|
|
|
|
|
|
_, rate := track.rate.Estimate()
|
|
|
|
|
|
|
|
delta := packet.SequenceNumber - first
|
|
|
|
if (delta & 0x8000) != 0 {
|
|
|
|
delta = 0
|
|
|
|
}
|
2020-12-26 21:09:14 +01:00
|
|
|
// send a NACK if a packet is late by 20ms or 2 packets,
|
|
|
|
// whichever is more. Since TCP sends a dupack after 2 packets,
|
|
|
|
// this should be safe.
|
|
|
|
packets := rate / 50
|
2020-10-04 13:11:33 +02:00
|
|
|
if packets > 24 {
|
|
|
|
packets = 24
|
|
|
|
}
|
|
|
|
if packets < 2 {
|
|
|
|
packets = 2
|
|
|
|
}
|
|
|
|
// send NACKs for more recent packets, this makes better
|
|
|
|
// use of the NACK bitmap
|
|
|
|
unnacked := uint16(4)
|
|
|
|
if unnacked > uint16(packets) {
|
|
|
|
unnacked = uint16(packets)
|
|
|
|
}
|
|
|
|
if uint32(delta) > packets {
|
|
|
|
found, first, bitmap := track.cache.BitmapGet(
|
|
|
|
packet.SequenceNumber - unnacked,
|
|
|
|
)
|
2020-10-12 14:38:41 +02:00
|
|
|
if found && sendNACK {
|
2020-10-03 12:56:16 +02:00
|
|
|
err := conn.sendNACK(track, first, bitmap)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("%v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delay := uint32(rtptime.JiffiesPerSec / 1024)
|
|
|
|
if rate > 512 {
|
|
|
|
delay = rtptime.JiffiesPerSec / rate / 2
|
|
|
|
}
|
|
|
|
|
|
|
|
writers.write(packet.SequenceNumber, index, delay,
|
|
|
|
isvideo, packet.Marker)
|
|
|
|
|
2021-05-17 03:54:20 +02:00
|
|
|
now := time.Now()
|
|
|
|
if kfNeeded && now.Sub(kfRequested) > time.Second/2 {
|
|
|
|
if sendPLI {
|
|
|
|
err := conn.sendPLI(track)
|
2021-05-11 22:36:56 +02:00
|
|
|
if err != nil {
|
2021-05-17 03:54:20 +02:00
|
|
|
log.Printf("sendPLI: %v", err)
|
|
|
|
kfNeeded = false
|
2021-05-11 22:36:56 +02:00
|
|
|
}
|
2021-05-17 03:54:20 +02:00
|
|
|
} else {
|
2021-05-11 22:36:56 +02:00
|
|
|
kfNeeded = false
|
|
|
|
}
|
2021-05-17 03:54:20 +02:00
|
|
|
kfRequested = now
|
2021-05-11 22:36:56 +02:00
|
|
|
}
|
2020-10-03 12:56:16 +02:00
|
|
|
}
|
|
|
|
}
|