1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-25 18:15:57 +01:00
galene/rtpconn/rtpreader.go

209 lines
4.6 KiB
Go
Raw Normal View History

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-19 17:37:48 +01:00
"github.com/jech/galene/packetcache"
"github.com/jech/galene/rtptime"
2020-10-03 12:56:16 +02:00
)
2020-12-25 16:39:12 +01:00
// isKeyframe determines if packet is the start of a keyframe.
// It returns (true, true) if that is the case, (false, true) if that is
// definitely not the case, and (false, false) if the information cannot
// be determined.
func isKeyframe(codec string, packet *rtp.Packet) (bool, bool) {
switch strings.ToLower(codec) {
case "video/vp8":
var vp8 codecs.VP8Packet
_, err := vp8.Unmarshal(packet.Payload)
if err != nil || len(vp8.Payload) < 1 {
return false, false
}
2020-10-03 12:54:17 +02:00
2020-12-25 16:39:12 +01:00
if vp8.S != 0 && vp8.PID == 0 && (vp8.Payload[0]&0x1) == 0 {
return true, true
}
return false, true
case "video/vp9":
var vp9 codecs.VP9Packet
_, err := vp9.Unmarshal(packet.Payload)
if err != nil || len(vp9.Payload) < 1 {
return false, false
}
if !vp9.B {
return false, true
}
if (vp9.Payload[0] & 0xc0) != 0x80 {
return false, false
}
profile := (vp9.Payload[0] >> 4) & 0x3
if profile != 3 {
2021-01-03 20:55:35 +01:00
return (vp9.Payload[0] & 0xC) == 0, true
}
2021-01-03 20:55:35 +01:00
return (vp9.Payload[0] & 0x6) == 0, true
case "video/h264":
if len(packet.Payload) < 1 {
return false, false
}
nalu := packet.Payload[0] & 0x1F
if nalu == 0 {
// reserved
return false, false
} else if nalu <= 23 {
// simple NALU
return nalu == 5, true
} else if nalu == 24 || nalu == 25 || nalu == 26 || nalu == 27 {
// STAP-A, STAP-B, MTAP16 or MTAP24
i := 1
if nalu == 25 || nalu == 26 || nalu == 27 {
// skip DON
i += 2
}
for i < len(packet.Payload) {
if i+2 > len(packet.Payload) {
return false, false
}
length := uint16(packet.Payload[i])<<8 |
uint16(packet.Payload[i+1])
i += 2
if i+int(length) > len(packet.Payload) {
return false, false
}
offset := 0
if nalu == 26 {
offset = 3
} else if nalu == 27 {
offset = 4
}
if offset >= int(length) {
return false, false
}
n := packet.Payload[i + offset] & 0x1F
if n == 5 {
return true, true
} else if n >= 24 {
// is this legal?
return false, false
}
i += int(length)
}
if i == len(packet.Payload) {
return false, true
}
return false, false
} else if nalu == 28 || nalu == 29 {
// FU-A or FU-B
if len(packet.Payload) < 2 {
return false, false
}
if (packet.Payload[1] & 0x80) == 0 {
// not a starting fragment
return false, true
}
return (packet.Payload[1]&0x1F == 5), true
}
return false, false
2020-12-25 16:39:12 +01:00
default:
return false, false
}
2020-10-03 12:54:17 +02:00
}
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()
sendNACK := track.hasRtcpFb("nack", "")
2020-10-03 12:56:16 +02:00
buf := make([]byte, packetcache.BufSize)
var packet rtp.Packet
for {
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)
2020-12-25 16:39:12 +01:00
kf, _ := isKeyframe(codec.MimeType, &packet)
2020-10-03 12:54:17 +02:00
first, index := track.cache.Store(
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
)
_, rate := track.rate.Estimate()
delta := packet.SequenceNumber - first
if (delta & 0x8000) != 0 {
delta = 0
}
// 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
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,
)
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:
}
}
}