2020-05-22 22:29:31 +02:00
|
|
|
// Copyright (c) 2020 by Juliusz Chroboczek.
|
|
|
|
|
|
|
|
// This is not open source software. Copy it, and I'll break into your
|
|
|
|
// house and tell your three year-old that Santa doesn't exist.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-05-22 23:07:38 +02:00
|
|
|
"errors"
|
2020-05-22 22:29:31 +02:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
"sfu/estimator"
|
|
|
|
"sfu/jitter"
|
|
|
|
"sfu/packetcache"
|
|
|
|
|
2020-05-26 16:50:20 +02:00
|
|
|
"github.com/pion/rtp"
|
2020-05-22 22:29:31 +02:00
|
|
|
"github.com/pion/webrtc/v2"
|
|
|
|
)
|
|
|
|
|
2020-05-26 23:14:34 +02:00
|
|
|
type localTrackAction struct {
|
|
|
|
add bool
|
|
|
|
track downTrack
|
|
|
|
}
|
|
|
|
|
2020-05-22 22:29:31 +02:00
|
|
|
type upTrack struct {
|
|
|
|
track *webrtc.Track
|
|
|
|
label string
|
|
|
|
rate *estimator.Estimator
|
|
|
|
cache *packetcache.Cache
|
|
|
|
jitter *jitter.Estimator
|
|
|
|
maxBitrate uint64
|
|
|
|
lastPLI uint64
|
2020-05-21 00:24:10 +02:00
|
|
|
lastFIR uint64
|
|
|
|
firSeqno uint32
|
2020-05-22 22:29:31 +02:00
|
|
|
lastSenderReport uint32
|
|
|
|
lastSenderReportTime uint32
|
|
|
|
|
2020-05-26 23:14:34 +02:00
|
|
|
localCh chan localTrackAction // signals that local has changed
|
|
|
|
writerDone chan struct{} // closed when the loop dies
|
2020-05-22 22:29:31 +02:00
|
|
|
|
|
|
|
mu sync.Mutex
|
2020-05-26 17:44:21 +02:00
|
|
|
local []downTrack
|
2020-05-22 22:29:31 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 23:14:34 +02:00
|
|
|
func (up *upTrack) notifyLocal(add bool, track downTrack) {
|
2020-05-22 22:29:31 +02:00
|
|
|
select {
|
2020-05-26 23:14:34 +02:00
|
|
|
case up.localCh <- localTrackAction{add, track}:
|
2020-05-22 22:29:31 +02:00
|
|
|
case <-up.writerDone:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 03:36:15 +02:00
|
|
|
func (up *upTrack) addLocal(local downTrack) error {
|
2020-05-22 22:29:31 +02:00
|
|
|
up.mu.Lock()
|
|
|
|
for _, t := range up.local {
|
|
|
|
if t == local {
|
|
|
|
up.mu.Unlock()
|
2020-05-30 03:36:15 +02:00
|
|
|
return nil
|
2020-05-22 22:29:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
up.local = append(up.local, local)
|
|
|
|
up.mu.Unlock()
|
2020-05-30 03:36:15 +02:00
|
|
|
|
2020-05-26 23:14:34 +02:00
|
|
|
up.notifyLocal(true, local)
|
2020-05-30 03:36:15 +02:00
|
|
|
return nil
|
2020-05-22 22:29:31 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 17:44:21 +02:00
|
|
|
func (up *upTrack) delLocal(local downTrack) bool {
|
2020-05-22 22:29:31 +02:00
|
|
|
up.mu.Lock()
|
|
|
|
for i, l := range up.local {
|
|
|
|
if l == local {
|
|
|
|
up.local = append(up.local[:i], up.local[i+1:]...)
|
|
|
|
up.mu.Unlock()
|
2020-05-26 23:14:34 +02:00
|
|
|
up.notifyLocal(false, l)
|
2020-05-22 22:29:31 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
up.mu.Unlock()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:44:21 +02:00
|
|
|
func (up *upTrack) getLocal() []downTrack {
|
2020-05-22 22:29:31 +02:00
|
|
|
up.mu.Lock()
|
|
|
|
defer up.mu.Unlock()
|
2020-05-26 17:44:21 +02:00
|
|
|
local := make([]downTrack, len(up.local))
|
2020-05-22 22:29:31 +02:00
|
|
|
copy(local, up.local)
|
|
|
|
return local
|
|
|
|
}
|
|
|
|
|
|
|
|
func (up *upTrack) hasRtcpFb(tpe, parameter string) bool {
|
|
|
|
for _, fb := range up.track.Codec().RTCPFeedback {
|
|
|
|
if fb.Type == tpe && fb.Parameter == parameter {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-26 16:44:39 +02:00
|
|
|
type iceConnection interface {
|
|
|
|
addICECandidate(candidate *webrtc.ICECandidateInit) error
|
|
|
|
flushICECandidates() error
|
|
|
|
}
|
|
|
|
|
2020-05-22 22:29:31 +02:00
|
|
|
type upConnection struct {
|
|
|
|
id string
|
|
|
|
label string
|
|
|
|
pc *webrtc.PeerConnection
|
|
|
|
tracks []*upTrack
|
|
|
|
labels map[string]string
|
|
|
|
iceCandidates []*webrtc.ICECandidateInit
|
2020-05-23 02:22:43 +02:00
|
|
|
|
2020-05-30 03:36:15 +02:00
|
|
|
mu sync.Mutex
|
|
|
|
closed bool
|
|
|
|
local []downConnection
|
2020-05-22 22:29:31 +02:00
|
|
|
}
|
|
|
|
|
2020-05-30 03:36:15 +02:00
|
|
|
var ErrConnectionClosed = errors.New("connection is closed")
|
|
|
|
|
|
|
|
func (up *upConnection) addLocal(local downConnection) error {
|
2020-05-23 02:22:43 +02:00
|
|
|
up.mu.Lock()
|
|
|
|
defer up.mu.Unlock()
|
2020-05-30 03:36:15 +02:00
|
|
|
if up.closed {
|
|
|
|
return ErrConnectionClosed
|
|
|
|
}
|
2020-05-23 02:22:43 +02:00
|
|
|
for _, t := range up.local {
|
|
|
|
if t == local {
|
2020-05-30 03:36:15 +02:00
|
|
|
return nil
|
2020-05-23 02:22:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
up.local = append(up.local, local)
|
2020-05-30 03:36:15 +02:00
|
|
|
return nil
|
2020-05-23 02:22:43 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 17:44:21 +02:00
|
|
|
func (up *upConnection) delLocal(local downConnection) bool {
|
2020-05-23 02:22:43 +02:00
|
|
|
up.mu.Lock()
|
|
|
|
defer up.mu.Unlock()
|
|
|
|
for i, l := range up.local {
|
|
|
|
if l == local {
|
|
|
|
up.local = append(up.local[:i], up.local[i+1:]...)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:44:21 +02:00
|
|
|
func (up *upConnection) getLocal() []downConnection {
|
2020-05-23 02:22:43 +02:00
|
|
|
up.mu.Lock()
|
|
|
|
defer up.mu.Unlock()
|
2020-05-26 17:44:21 +02:00
|
|
|
local := make([]downConnection, len(up.local))
|
2020-05-23 02:22:43 +02:00
|
|
|
copy(local, up.local)
|
|
|
|
return local
|
|
|
|
}
|
|
|
|
|
2020-05-30 03:36:15 +02:00
|
|
|
func (up *upConnection) Close() error {
|
|
|
|
up.mu.Lock()
|
|
|
|
defer up.mu.Unlock()
|
|
|
|
|
|
|
|
go func(local []downConnection) {
|
|
|
|
for _, l := range local {
|
|
|
|
l.Close()
|
|
|
|
}
|
|
|
|
}(up.local)
|
|
|
|
|
|
|
|
up.local = nil
|
|
|
|
up.closed = true
|
|
|
|
return up.pc.Close()
|
|
|
|
}
|
|
|
|
|
2020-05-22 23:07:38 +02:00
|
|
|
func (up *upConnection) addICECandidate(candidate *webrtc.ICECandidateInit) error {
|
|
|
|
if up.pc.RemoteDescription() != nil {
|
|
|
|
return up.pc.AddICECandidate(*candidate)
|
|
|
|
}
|
|
|
|
up.iceCandidates = append(up.iceCandidates, candidate)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func flushICECandidates(pc *webrtc.PeerConnection, candidates []*webrtc.ICECandidateInit) error {
|
|
|
|
if pc.RemoteDescription() == nil {
|
|
|
|
return errors.New("flushICECandidates called in bad state")
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
for _, candidate := range candidates {
|
|
|
|
err2 := pc.AddICECandidate(*candidate)
|
|
|
|
if err == nil {
|
|
|
|
err = err2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (up *upConnection) flushICECandidates() error {
|
|
|
|
err := flushICECandidates(up.pc, up.iceCandidates)
|
|
|
|
up.iceCandidates = nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-22 22:29:31 +02:00
|
|
|
func getUpMid(pc *webrtc.PeerConnection, track *webrtc.Track) string {
|
|
|
|
for _, t := range pc.GetTransceivers() {
|
|
|
|
if t.Receiver() != nil && t.Receiver().Track() == track {
|
|
|
|
return t.Mid()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (up *upConnection) complete() bool {
|
|
|
|
for mid, _ := range up.labels {
|
|
|
|
found := false
|
|
|
|
for _, t := range up.tracks {
|
|
|
|
m := getUpMid(up.pc, t.track)
|
|
|
|
if m == mid {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type bitrate struct {
|
|
|
|
bitrate uint64
|
|
|
|
microseconds uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
const receiverReportTimeout = 8000000
|
|
|
|
|
|
|
|
func (br *bitrate) Set(bitrate uint64, now uint64) {
|
|
|
|
// this is racy -- a reader might read the
|
|
|
|
// data between the two writes. This shouldn't
|
|
|
|
// matter, we'll recover at the next sample.
|
|
|
|
atomic.StoreUint64(&br.bitrate, bitrate)
|
|
|
|
atomic.StoreUint64(&br.microseconds, now)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *bitrate) Get(now uint64) uint64 {
|
|
|
|
ts := atomic.LoadUint64(&br.microseconds)
|
|
|
|
if now < ts || now > ts+receiverReportTimeout {
|
|
|
|
return ^uint64(0)
|
|
|
|
}
|
|
|
|
return atomic.LoadUint64(&br.bitrate)
|
|
|
|
}
|
|
|
|
|
|
|
|
type receiverStats struct {
|
|
|
|
loss uint32
|
|
|
|
jitter uint32
|
|
|
|
microseconds uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *receiverStats) Set(loss uint8, jitter uint32, now uint64) {
|
|
|
|
atomic.StoreUint32(&s.loss, uint32(loss))
|
|
|
|
atomic.StoreUint32(&s.jitter, jitter)
|
|
|
|
atomic.StoreUint64(&s.microseconds, now)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *receiverStats) Get(now uint64) (uint8, uint32) {
|
|
|
|
ts := atomic.LoadUint64(&s.microseconds)
|
|
|
|
if now < ts || now > ts+receiverReportTimeout {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
return uint8(atomic.LoadUint32(&s.loss)), atomic.LoadUint32(&s.jitter)
|
|
|
|
}
|
|
|
|
|
2020-05-21 01:14:54 +02:00
|
|
|
var ErrKeyframeNeeded = errors.New("keyframe needed")
|
|
|
|
|
2020-05-26 17:44:21 +02:00
|
|
|
type downTrack interface {
|
|
|
|
WriteRTP(packat *rtp.Packet) error
|
|
|
|
Accumulate(bytes uint32)
|
|
|
|
GetMaxBitrate(now uint64) uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
type rtpDownTrack struct {
|
2020-05-22 22:29:31 +02:00
|
|
|
track *webrtc.Track
|
|
|
|
remote *upTrack
|
|
|
|
maxLossBitrate *bitrate
|
|
|
|
maxREMBBitrate *bitrate
|
|
|
|
rate *estimator.Estimator
|
|
|
|
stats *receiverStats
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:44:21 +02:00
|
|
|
func (down *rtpDownTrack) WriteRTP(packet *rtp.Packet) error {
|
2020-05-26 16:50:20 +02:00
|
|
|
return down.track.WriteRTP(packet)
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:44:21 +02:00
|
|
|
func (down *rtpDownTrack) Accumulate(bytes uint32) {
|
2020-05-31 23:47:12 +02:00
|
|
|
down.rate.Accumulate(bytes)
|
2020-05-26 16:50:20 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 17:44:21 +02:00
|
|
|
func (down *rtpDownTrack) GetMaxBitrate(now uint64) uint64 {
|
2020-05-22 22:29:31 +02:00
|
|
|
br1 := down.maxLossBitrate.Get(now)
|
|
|
|
br2 := down.maxREMBBitrate.Get(now)
|
|
|
|
if br1 < br2 {
|
|
|
|
return br1
|
|
|
|
}
|
|
|
|
return br2
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:44:21 +02:00
|
|
|
type downConnection interface {
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type rtpDownConnection struct {
|
2020-05-22 22:29:31 +02:00
|
|
|
id string
|
2020-05-27 11:44:49 +02:00
|
|
|
client *webClient
|
2020-05-22 22:29:31 +02:00
|
|
|
pc *webrtc.PeerConnection
|
|
|
|
remote *upConnection
|
2020-05-26 17:44:21 +02:00
|
|
|
tracks []*rtpDownTrack
|
2020-05-22 22:29:31 +02:00
|
|
|
iceCandidates []*webrtc.ICECandidateInit
|
|
|
|
}
|
2020-05-22 22:36:47 +02:00
|
|
|
|
2020-05-26 17:44:21 +02:00
|
|
|
func (down *rtpDownConnection) Close() error {
|
2020-05-23 02:22:43 +02:00
|
|
|
return down.client.action(delConnAction{down.id})
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:44:21 +02:00
|
|
|
func (down *rtpDownConnection) addICECandidate(candidate *webrtc.ICECandidateInit) error {
|
2020-05-22 23:07:38 +02:00
|
|
|
if down.pc.RemoteDescription() != nil {
|
|
|
|
return down.pc.AddICECandidate(*candidate)
|
|
|
|
}
|
|
|
|
down.iceCandidates = append(down.iceCandidates, candidate)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:44:21 +02:00
|
|
|
func (down *rtpDownConnection) flushICECandidates() error {
|
2020-05-22 23:07:38 +02:00
|
|
|
err := flushICECandidates(down.pc, down.iceCandidates)
|
|
|
|
down.iceCandidates = nil
|
|
|
|
return err
|
|
|
|
}
|