2020-09-18 10:28:05 +02:00
|
|
|
package rtpconn
|
2020-09-12 22:19:38 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"log"
|
2020-10-12 12:20:55 +02:00
|
|
|
"sort"
|
2020-09-12 22:19:38 +02:00
|
|
|
"time"
|
|
|
|
|
2020-12-19 17:37:48 +01:00
|
|
|
"github.com/jech/galene/conn"
|
|
|
|
"github.com/jech/galene/packetcache"
|
|
|
|
"github.com/jech/galene/rtptime"
|
2020-09-12 22:19:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// packetIndex is a request to send a packet from the cache.
|
|
|
|
type packetIndex struct {
|
|
|
|
// the packet's seqno
|
|
|
|
seqno uint16
|
|
|
|
// the index in the cache
|
|
|
|
index uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
// An rtpWriterPool is a set of rtpWriters
|
|
|
|
type rtpWriterPool struct {
|
|
|
|
track *rtpUpTrack
|
|
|
|
writers []*rtpWriter
|
|
|
|
count int
|
|
|
|
}
|
|
|
|
|
|
|
|
// sqrt computes the integer square root
|
|
|
|
func sqrt(n int) int {
|
|
|
|
if n < 2 {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
s := sqrt(n/2) * 2
|
|
|
|
l := s + 1
|
|
|
|
if l*l > n {
|
|
|
|
return s
|
|
|
|
} else {
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add adds or removes a track from a writer pool
|
2020-09-13 11:04:16 +02:00
|
|
|
func (wp *rtpWriterPool) add(track conn.DownTrack, add bool) error {
|
2020-09-12 22:19:38 +02:00
|
|
|
n := 4
|
|
|
|
if wp.count > 16 {
|
|
|
|
n = sqrt(wp.count)
|
|
|
|
}
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for i < len(wp.writers) {
|
|
|
|
w := wp.writers[i]
|
|
|
|
err := w.add(track, add, n)
|
|
|
|
if err == nil {
|
|
|
|
if add {
|
|
|
|
wp.count++
|
|
|
|
} else {
|
|
|
|
if wp.count > 0 {
|
|
|
|
wp.count--
|
|
|
|
} else {
|
|
|
|
log.Printf("Negative writer count!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
} else if err == ErrWriterDead {
|
|
|
|
wp.del(wp.writers[i])
|
|
|
|
} else {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if add {
|
2021-07-14 14:05:23 +02:00
|
|
|
writer := newRtpWriter(wp.track)
|
2020-09-12 22:19:38 +02:00
|
|
|
wp.writers = append(wp.writers, writer)
|
|
|
|
err := writer.add(track, true, n)
|
|
|
|
if err == nil {
|
|
|
|
wp.count++
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
return errors.New("deleting unknown track")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// del deletes a writer.
|
|
|
|
func (wp *rtpWriterPool) del(w *rtpWriter) bool {
|
|
|
|
for i, ww := range wp.writers {
|
|
|
|
if ww == w {
|
|
|
|
close(w.ch)
|
|
|
|
wp.writers = append(wp.writers[:i], wp.writers[i+1:]...)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// close deletes all writers.
|
|
|
|
func (wp *rtpWriterPool) close() {
|
|
|
|
for _, w := range wp.writers {
|
|
|
|
close(w.ch)
|
|
|
|
}
|
|
|
|
wp.writers = nil
|
|
|
|
wp.count = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// write writes a packet stored in the packet cache to all local tracks
|
|
|
|
func (wp *rtpWriterPool) write(seqno uint16, index uint16, delay uint32, isvideo bool, marker bool) {
|
2020-09-13 09:40:49 +02:00
|
|
|
pi := packetIndex{seqno, index}
|
2020-09-12 22:19:38 +02:00
|
|
|
|
|
|
|
var dead []*rtpWriter
|
|
|
|
for _, w := range wp.writers {
|
|
|
|
if w.drop > 0 {
|
|
|
|
// currently dropping
|
|
|
|
if marker {
|
|
|
|
// last packet in frame
|
|
|
|
w.drop = 0
|
|
|
|
} else {
|
|
|
|
w.drop--
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case w.ch <- pi:
|
|
|
|
// all is well
|
|
|
|
case <-w.done:
|
|
|
|
// the writer is dead.
|
|
|
|
dead = append(dead, w)
|
|
|
|
default:
|
|
|
|
// the writer is congested
|
|
|
|
if isvideo {
|
|
|
|
// drop until the end of the frame
|
|
|
|
if !marker {
|
|
|
|
w.drop = 7
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// audio, try again with a delay
|
2020-10-03 12:54:17 +02:00
|
|
|
d := delay / uint32(2*len(wp.writers))
|
2020-09-12 22:19:38 +02:00
|
|
|
timer := time.NewTimer(rtptime.ToDuration(
|
2022-04-15 03:35:34 +02:00
|
|
|
int64(d), rtptime.JiffiesPerSec,
|
2020-09-12 22:19:38 +02:00
|
|
|
))
|
|
|
|
|
|
|
|
select {
|
|
|
|
case w.ch <- pi:
|
|
|
|
timer.Stop()
|
|
|
|
case <-w.done:
|
|
|
|
dead = append(dead, w)
|
|
|
|
case <-timer.C:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if dead != nil {
|
|
|
|
for _, d := range dead {
|
|
|
|
wp.del(d)
|
|
|
|
}
|
|
|
|
dead = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var ErrWriterDead = errors.New("writer is dead")
|
|
|
|
var ErrWriterBusy = errors.New("writer is busy")
|
|
|
|
var ErrUnknownTrack = errors.New("unknown track")
|
|
|
|
|
|
|
|
type writerAction struct {
|
|
|
|
add bool
|
2020-09-13 11:04:16 +02:00
|
|
|
track conn.DownTrack
|
2020-09-12 22:19:38 +02:00
|
|
|
maxTracks int
|
|
|
|
ch chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
// an rtpWriter is a thread writing to a set of tracks.
|
|
|
|
type rtpWriter struct {
|
|
|
|
ch chan packetIndex
|
|
|
|
done chan struct{}
|
|
|
|
action chan writerAction
|
|
|
|
|
|
|
|
// this is not touched by the writer loop, used by the caller
|
|
|
|
drop int
|
|
|
|
}
|
|
|
|
|
2021-07-14 14:05:23 +02:00
|
|
|
func newRtpWriter(track *rtpUpTrack) *rtpWriter {
|
2020-09-12 22:19:38 +02:00
|
|
|
writer := &rtpWriter{
|
|
|
|
ch: make(chan packetIndex, 32),
|
|
|
|
done: make(chan struct{}),
|
|
|
|
action: make(chan writerAction, 1),
|
|
|
|
}
|
2021-07-14 14:05:23 +02:00
|
|
|
go rtpWriterLoop(writer, track)
|
2020-09-12 22:19:38 +02:00
|
|
|
return writer
|
|
|
|
}
|
|
|
|
|
|
|
|
// add adds or removes a track from a writer.
|
2020-09-13 11:04:16 +02:00
|
|
|
func (writer *rtpWriter) add(track conn.DownTrack, add bool, max int) error {
|
2020-09-12 22:19:38 +02:00
|
|
|
ch := make(chan error, 1)
|
|
|
|
select {
|
|
|
|
case writer.action <- writerAction{add, track, max, ch}:
|
|
|
|
select {
|
|
|
|
case err := <-ch:
|
|
|
|
return err
|
|
|
|
case <-writer.done:
|
|
|
|
return ErrWriterDead
|
|
|
|
}
|
|
|
|
case <-writer.done:
|
|
|
|
return ErrWriterDead
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 02:25:56 +02:00
|
|
|
func sendSequence(kf, last uint16, track conn.DownTrack, cache *packetcache.Cache) {
|
2020-10-03 12:54:17 +02:00
|
|
|
buf := make([]byte, packetcache.BufSize)
|
2021-08-04 02:25:56 +02:00
|
|
|
seqno := kf
|
|
|
|
for ((last - seqno) & 0x8000) == 0 {
|
2020-10-03 12:54:17 +02:00
|
|
|
bytes := cache.Get(seqno, buf)
|
2020-10-11 22:09:20 +02:00
|
|
|
if bytes == 0 {
|
2020-10-03 12:54:17 +02:00
|
|
|
return
|
|
|
|
}
|
2021-05-11 23:48:17 +02:00
|
|
|
|
|
|
|
_, err := track.Write(buf[:bytes])
|
2021-05-11 22:36:56 +02:00
|
|
|
if err != nil {
|
2020-10-03 12:54:17 +02:00
|
|
|
return
|
|
|
|
}
|
2021-08-04 02:25:56 +02:00
|
|
|
seqno++
|
2020-10-03 12:54:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-12 22:19:38 +02:00
|
|
|
// rtpWriterLoop is the main loop of an rtpWriter.
|
2021-07-14 14:05:23 +02:00
|
|
|
func rtpWriterLoop(writer *rtpWriter, track *rtpUpTrack) {
|
2020-09-12 22:19:38 +02:00
|
|
|
defer close(writer.done)
|
|
|
|
|
|
|
|
buf := make([]byte, packetcache.BufSize)
|
2020-09-13 11:04:16 +02:00
|
|
|
local := make([]conn.DownTrack, 0)
|
2020-09-12 22:19:38 +02:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case action := <-writer.action:
|
|
|
|
if action.add {
|
|
|
|
if len(local) >= action.maxTracks {
|
|
|
|
action.ch <- ErrWriterBusy
|
|
|
|
close(action.ch)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
local = append(local, action.track)
|
|
|
|
action.ch <- nil
|
|
|
|
close(action.ch)
|
|
|
|
|
|
|
|
track.mu.Lock()
|
|
|
|
ntp := track.srNTPTime
|
|
|
|
rtp := track.srRTPTime
|
|
|
|
track.mu.Unlock()
|
|
|
|
if ntp != 0 {
|
2020-09-13 11:04:16 +02:00
|
|
|
action.track.SetTimeOffset(ntp, rtp)
|
2020-09-12 22:19:38 +02:00
|
|
|
}
|
2020-12-27 01:24:52 +01:00
|
|
|
cname, ok := track.cname.Load().(string)
|
|
|
|
if ok && cname != "" {
|
2020-09-13 11:04:16 +02:00
|
|
|
action.track.SetCname(cname)
|
2020-09-12 22:19:38 +02:00
|
|
|
}
|
2020-10-11 22:09:20 +02:00
|
|
|
|
2021-08-04 02:25:56 +02:00
|
|
|
last, foundLast := track.cache.Last()
|
|
|
|
kf, foundKf := track.cache.Keyframe()
|
|
|
|
if foundLast && foundKf {
|
|
|
|
if last-kf < 40 { // modulo 2^16
|
|
|
|
go sendSequence(
|
|
|
|
kf, last,
|
2020-10-11 22:09:20 +02:00
|
|
|
action.track,
|
|
|
|
track.cache,
|
|
|
|
)
|
|
|
|
} else {
|
2021-05-11 22:36:56 +02:00
|
|
|
track.RequestKeyframe()
|
2020-10-11 22:09:20 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// no keyframe yet, one should
|
|
|
|
// arrive soon. Do nothing.
|
|
|
|
}
|
2020-09-12 22:19:38 +02:00
|
|
|
} else {
|
|
|
|
found := false
|
|
|
|
for i, t := range local {
|
|
|
|
if t == action.track {
|
|
|
|
local = append(local[:i],
|
|
|
|
local[i+1:]...)
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
action.ch <- ErrUnknownTrack
|
|
|
|
} else {
|
|
|
|
action.ch <- nil
|
|
|
|
}
|
|
|
|
close(action.ch)
|
|
|
|
if len(local) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case pi, ok := <-writer.ch:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes := track.cache.GetAt(pi.seqno, pi.index, buf)
|
|
|
|
if bytes == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, l := range local {
|
2021-05-11 23:48:17 +02:00
|
|
|
_, err := l.Write(buf[:bytes])
|
2020-09-12 22:19:38 +02:00
|
|
|
if err != nil {
|
2021-05-11 22:36:56 +02:00
|
|
|
continue
|
2020-09-12 22:19:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 12:20:55 +02:00
|
|
|
|
|
|
|
// nackWriter is called when bufferedNACKs becomes non-empty. It decides
|
|
|
|
// which nacks to ship out.
|
2021-07-14 14:05:23 +02:00
|
|
|
func nackWriter(track *rtpUpTrack) {
|
2020-10-12 12:20:55 +02:00
|
|
|
// a client might send us a NACK for a packet that has already
|
|
|
|
// been nacked by the reader loop. Give recovery a chance.
|
2020-10-31 21:31:05 +01:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2020-10-12 12:20:55 +02:00
|
|
|
|
|
|
|
track.mu.Lock()
|
|
|
|
nacks := track.bufferedNACKs
|
|
|
|
track.bufferedNACKs = nil
|
|
|
|
track.mu.Unlock()
|
|
|
|
|
2020-10-31 21:31:05 +01:00
|
|
|
if len(nacks) == 0 || !track.hasRtcpFb("nack", "") {
|
2020-10-12 14:38:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-12 12:20:55 +02:00
|
|
|
// drop any nacks before the last keyframe
|
|
|
|
var cutoff uint16
|
2021-08-04 02:25:56 +02:00
|
|
|
seqno, found := track.cache.Keyframe()
|
2020-10-12 12:20:55 +02:00
|
|
|
if found {
|
|
|
|
cutoff = seqno
|
|
|
|
} else {
|
2021-08-04 02:25:56 +02:00
|
|
|
lastSeqno, last := track.cache.Last()
|
2020-10-12 12:20:55 +02:00
|
|
|
if !last {
|
|
|
|
// NACK on a fresh track? Give up.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// no keyframe, use an arbitrary cutoff
|
|
|
|
cutoff = lastSeqno - 256
|
|
|
|
}
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for i < len(nacks) {
|
|
|
|
if ((nacks[i] - cutoff) & 0x8000) != 0 {
|
|
|
|
// earlier than the cutoff, drop
|
|
|
|
nacks = append(nacks[:i], nacks[i+1:]...)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
l := track.cache.Get(nacks[i], nil)
|
|
|
|
if l > 0 {
|
|
|
|
// the packet arrived in the meantime
|
|
|
|
nacks = append(nacks[:i], nacks[i+1:]...)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(nacks, func(i, j int) bool {
|
|
|
|
return nacks[i]-cutoff < nacks[j]-cutoff
|
|
|
|
})
|
|
|
|
|
2020-10-12 14:38:41 +02:00
|
|
|
if len(nacks) > 0 {
|
2021-07-14 14:05:23 +02:00
|
|
|
track.sendNACKs(nacks)
|
2020-10-12 12:20:55 +02:00
|
|
|
}
|
|
|
|
}
|