2020-10-03 12:56:16 +02:00
|
|
|
package rtpconn
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"log"
|
2020-12-04 01:15:52 +01:00
|
|
|
"strings"
|
2020-10-03 12:56:16 +02:00
|
|
|
|
|
|
|
"github.com/pion/rtp"
|
2020-10-03 12:54:17 +02:00
|
|
|
"github.com/pion/rtp/codecs"
|
2020-10-03 12:56:16 +02:00
|
|
|
"github.com/pion/webrtc/v3"
|
|
|
|
|
2020-12-06 19:43:17 +01:00
|
|
|
"galene/packetcache"
|
|
|
|
"galene/rtptime"
|
2020-10-03 12:56:16 +02:00
|
|
|
)
|
|
|
|
|
2020-10-03 12:54:17 +02:00
|
|
|
func isVP8Keyframe(packet *rtp.Packet) bool {
|
|
|
|
var vp8 codecs.VP8Packet
|
|
|
|
_, err := vp8.Unmarshal(packet.Payload)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return vp8.S != 0 && vp8.PID == 0 &&
|
|
|
|
len(vp8.Payload) > 0 && (vp8.Payload[0]&0x1) == 0
|
|
|
|
}
|
|
|
|
|
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", "")
|
2020-10-03 12:56:16 +02:00
|
|
|
buf := make([]byte, packetcache.BufSize)
|
|
|
|
var packet rtp.Packet
|
|
|
|
for {
|
|
|
|
bytes, err := track.track.Read(buf)
|
|
|
|
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)
|
|
|
|
|
2020-10-03 12:54:17 +02:00
|
|
|
kf := false
|
2020-12-04 01:15:52 +01:00
|
|
|
if isvideo && strings.ToLower(codec.MimeType) == "video/vp8" {
|
2020-10-03 12:54:17 +02:00
|
|
|
kf = isVP8Keyframe(&packet)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
// send a NACK if a packet is late by 65ms or 2 packets,
|
|
|
|
// whichever is more
|
|
|
|
packets := rate / 16
|
|
|
|
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)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case action := <-track.localCh:
|
|
|
|
err := writers.add(action.track, action.add)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("add/remove track: %v", err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|