2020-04-24 19:38:21 +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 (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"log"
|
2020-05-09 12:06:13 +02:00
|
|
|
"math"
|
2020-05-01 04:08:26 +02:00
|
|
|
"math/bits"
|
2020-04-24 19:38:21 +02:00
|
|
|
"os"
|
2020-04-25 19:59:53 +02:00
|
|
|
"strings"
|
2020-04-24 19:38:21 +02:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2020-04-30 20:15:52 +02:00
|
|
|
"sfu/estimator"
|
2020-05-02 16:21:48 +02:00
|
|
|
"sfu/jitter"
|
2020-05-02 15:27:47 +02:00
|
|
|
"sfu/mono"
|
2020-04-29 11:06:39 +02:00
|
|
|
"sfu/packetcache"
|
2020-04-27 21:43:29 +02:00
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/pion/rtcp"
|
2020-04-27 03:28:45 +02:00
|
|
|
"github.com/pion/rtp"
|
2020-04-24 19:38:21 +02:00
|
|
|
"github.com/pion/webrtc/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var iceConf webrtc.Configuration
|
|
|
|
var iceOnce sync.Once
|
|
|
|
|
|
|
|
func iceConfiguration() webrtc.Configuration {
|
|
|
|
iceOnce.Do(func() {
|
|
|
|
var iceServers []webrtc.ICEServer
|
|
|
|
file, err := os.Open(iceFilename)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Open %v: %v", iceFilename, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
d := json.NewDecoder(file)
|
|
|
|
err = d.Decode(&iceServers)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Get ICE configuration: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
iceConf = webrtc.Configuration{
|
|
|
|
ICEServers: iceServers,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return iceConf
|
|
|
|
}
|
|
|
|
|
|
|
|
type protocolError string
|
|
|
|
|
|
|
|
func (err protocolError) Error() string {
|
|
|
|
return string(err)
|
|
|
|
}
|
|
|
|
|
2020-04-25 02:09:11 +02:00
|
|
|
type userError string
|
|
|
|
|
|
|
|
func (err userError) Error() string {
|
|
|
|
return string(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func errorToWSCloseMessage(err error) (string, []byte) {
|
2020-04-24 19:38:21 +02:00
|
|
|
var code int
|
|
|
|
var text string
|
|
|
|
switch e := err.(type) {
|
|
|
|
case *websocket.CloseError:
|
|
|
|
code = websocket.CloseNormalClosure
|
|
|
|
case protocolError:
|
|
|
|
code = websocket.CloseProtocolError
|
|
|
|
text = string(e)
|
2020-04-25 02:09:11 +02:00
|
|
|
case userError:
|
|
|
|
code = websocket.CloseNormalClosure
|
|
|
|
text = string(e)
|
2020-04-24 19:38:21 +02:00
|
|
|
default:
|
|
|
|
code = websocket.CloseInternalServerErr
|
|
|
|
}
|
2020-04-26 19:15:02 +02:00
|
|
|
return text, websocket.FormatCloseMessage(code, text)
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func isWSNormalError(err error) bool {
|
|
|
|
return websocket.IsCloseError(err,
|
|
|
|
websocket.CloseNormalClosure,
|
|
|
|
websocket.CloseGoingAway)
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
type webClient struct {
|
|
|
|
group *group
|
|
|
|
id string
|
|
|
|
username string
|
|
|
|
permissions userPermission
|
|
|
|
requested map[string]uint32
|
|
|
|
done chan struct{}
|
|
|
|
writeCh chan interface{}
|
|
|
|
writerDone chan struct{}
|
|
|
|
actionCh chan interface{}
|
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
down map[string]*rtpDownConnection
|
|
|
|
up map[string]*upConnection
|
|
|
|
}
|
|
|
|
|
2020-05-28 02:35:09 +02:00
|
|
|
func (c *webClient) getGroup() *group {
|
|
|
|
return c.group
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *webClient) getId() string {
|
|
|
|
return c.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *webClient) getUsername() string {
|
|
|
|
return c.username
|
|
|
|
}
|
|
|
|
|
2020-05-31 20:41:17 +02:00
|
|
|
func (c *webClient) pushClient(id, username string, add bool) error {
|
|
|
|
return c.write(clientMessage{
|
|
|
|
Type: "user",
|
|
|
|
Id: id,
|
|
|
|
Username: username,
|
|
|
|
Del: !add,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-18 00:35:58 +02:00
|
|
|
type rateMap map[string]uint32
|
|
|
|
|
|
|
|
func (v *rateMap) UnmarshalJSON(b []byte) error {
|
|
|
|
var m map[string]interface{}
|
|
|
|
|
|
|
|
err := json.Unmarshal(b, &m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
n := make(map[string]uint32, len(m))
|
|
|
|
for k, w := range m {
|
|
|
|
switch w := w.(type) {
|
|
|
|
case bool:
|
|
|
|
if w {
|
|
|
|
n[k] = ^uint32(0)
|
|
|
|
} else {
|
|
|
|
n[k] = 0
|
|
|
|
}
|
|
|
|
case float64:
|
|
|
|
if w < 0 || w >= float64(^uint32(0)) {
|
|
|
|
return errors.New("overflow")
|
|
|
|
}
|
|
|
|
n[k] = uint32(w)
|
|
|
|
default:
|
|
|
|
return errors.New("unexpected type in JSON map")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*v = n
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v rateMap) MarshalJSON() ([]byte, error) {
|
|
|
|
m := make(map[string]interface{}, len(v))
|
|
|
|
for k, w := range v {
|
|
|
|
switch w {
|
|
|
|
case 0:
|
|
|
|
m[k] = false
|
|
|
|
case ^uint32(0):
|
|
|
|
m[k] = true
|
|
|
|
default:
|
|
|
|
m[k] = w
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return json.Marshal(m)
|
|
|
|
}
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
type clientMessage struct {
|
2020-04-25 02:09:11 +02:00
|
|
|
Type string `json:"type"`
|
|
|
|
Id string `json:"id,omitempty"`
|
|
|
|
Username string `json:"username,omitempty"`
|
|
|
|
Password string `json:"password,omitempty"`
|
2020-04-25 02:25:51 +02:00
|
|
|
Permissions userPermission `json:"permissions,omitempty"`
|
2020-04-25 02:09:11 +02:00
|
|
|
Group string `json:"group,omitempty"`
|
|
|
|
Value string `json:"value,omitempty"`
|
2020-04-25 20:30:33 +02:00
|
|
|
Me bool `json:"me,omitempty"`
|
2020-04-25 02:09:11 +02:00
|
|
|
Offer *webrtc.SessionDescription `json:"offer,omitempty"`
|
|
|
|
Answer *webrtc.SessionDescription `json:"answer,omitempty"`
|
|
|
|
Candidate *webrtc.ICECandidateInit `json:"candidate,omitempty"`
|
2020-05-17 22:31:29 +02:00
|
|
|
Labels map[string]string `json:"labels,omitempty"`
|
2020-04-25 02:09:11 +02:00
|
|
|
Del bool `json:"del,omitempty"`
|
2020-05-18 00:35:58 +02:00
|
|
|
Request rateMap `json:"request,omitempty"`
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type closeMessage struct {
|
|
|
|
data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func startClient(conn *websocket.Conn) (err error) {
|
|
|
|
var m clientMessage
|
2020-04-25 22:49:07 +02:00
|
|
|
|
|
|
|
err = conn.SetReadDeadline(time.Now().Add(15 * time.Second))
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
err = conn.ReadJSON(&m)
|
|
|
|
if err != nil {
|
2020-04-25 19:58:54 +02:00
|
|
|
conn.Close()
|
2020-04-24 19:38:21 +02:00
|
|
|
return
|
|
|
|
}
|
2020-04-25 22:49:07 +02:00
|
|
|
err = conn.SetReadDeadline(time.Time{})
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
if m.Type != "handshake" {
|
2020-04-25 19:58:54 +02:00
|
|
|
conn.Close()
|
2020-04-24 19:38:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-30 13:23:31 +02:00
|
|
|
if strings.ContainsRune(m.Username, ' ') {
|
|
|
|
err = userError("don't put spaces in your username")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
c := &webClient{
|
2020-04-24 19:38:21 +02:00
|
|
|
id: m.Id,
|
|
|
|
username: m.Username,
|
|
|
|
actionCh: make(chan interface{}, 10),
|
|
|
|
done: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
defer close(c.done)
|
|
|
|
|
2020-04-25 21:16:49 +02:00
|
|
|
c.writeCh = make(chan interface{}, 25)
|
2020-04-24 19:38:21 +02:00
|
|
|
defer func() {
|
|
|
|
if isWSNormalError(err) {
|
|
|
|
err = nil
|
|
|
|
} else {
|
2020-04-25 02:09:11 +02:00
|
|
|
m, e := errorToWSCloseMessage(err)
|
|
|
|
if m != "" {
|
|
|
|
c.write(clientMessage{
|
2020-04-25 20:47:49 +02:00
|
|
|
Type: "error",
|
|
|
|
Value: m,
|
2020-04-25 02:09:11 +02:00
|
|
|
})
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
select {
|
2020-04-25 02:09:11 +02:00
|
|
|
case c.writeCh <- closeMessage{e}:
|
2020-04-24 19:38:21 +02:00
|
|
|
case <-c.writerDone:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(c.writeCh)
|
|
|
|
c.writeCh = nil
|
|
|
|
}()
|
|
|
|
|
|
|
|
c.writerDone = make(chan struct{})
|
|
|
|
go clientWriter(conn, c.writeCh, c.writerDone)
|
|
|
|
|
2020-05-31 20:41:17 +02:00
|
|
|
g, err := addClient(m.Group, c, m.Username, m.Password)
|
2020-04-24 19:38:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.group = g
|
|
|
|
defer delClient(c)
|
|
|
|
|
|
|
|
return clientLoop(c, conn)
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func getUpConn(c *webClient, id string) *upConnection {
|
2020-04-28 14:54:50 +02:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2020-04-24 19:38:21 +02:00
|
|
|
|
|
|
|
if c.up == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
conn := c.up[id]
|
|
|
|
if conn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return conn
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func getUpConns(c *webClient) []string {
|
2020-04-28 14:54:50 +02:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2020-04-25 18:35:32 +02:00
|
|
|
up := make([]string, 0, len(c.up))
|
|
|
|
for id := range c.up {
|
|
|
|
up = append(up, id)
|
|
|
|
}
|
|
|
|
return up
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func addUpConn(c *webClient, id string) (*upConnection, error) {
|
2020-04-24 19:38:21 +02:00
|
|
|
pc, err := groups.api.NewPeerConnection(iceConfiguration())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = pc.AddTransceiverFromKind(webrtc.RTPCodecTypeAudio,
|
|
|
|
webrtc.RtpTransceiverInit{
|
|
|
|
Direction: webrtc.RTPTransceiverDirectionRecvonly,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
pc.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = pc.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo,
|
|
|
|
webrtc.RtpTransceiverInit{
|
|
|
|
Direction: webrtc.RTPTransceiverDirectionRecvonly,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
pc.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-28 23:41:18 +02:00
|
|
|
conn := &upConnection{id: id, pc: pc}
|
|
|
|
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
if c.up == nil {
|
|
|
|
c.up = make(map[string]*upConnection)
|
|
|
|
}
|
2020-05-22 22:38:40 +02:00
|
|
|
if c.up[id] != nil || (c.down != nil && c.down[id] != nil) {
|
2020-04-28 23:41:18 +02:00
|
|
|
conn.pc.Close()
|
|
|
|
return nil, errors.New("Adding duplicate connection")
|
|
|
|
}
|
|
|
|
c.up[id] = conn
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
pc.OnICECandidate(func(candidate *webrtc.ICECandidate) {
|
|
|
|
sendICE(c, id, candidate)
|
|
|
|
})
|
|
|
|
|
|
|
|
pc.OnTrack(func(remote *webrtc.Track, receiver *webrtc.RTPReceiver) {
|
2020-04-28 14:54:50 +02:00
|
|
|
c.mu.Lock()
|
2020-04-24 19:38:21 +02:00
|
|
|
u, ok := c.up[id]
|
|
|
|
if !ok {
|
|
|
|
log.Printf("Unknown connection")
|
2020-04-28 14:54:50 +02:00
|
|
|
c.mu.Unlock()
|
2020-04-24 19:38:21 +02:00
|
|
|
return
|
|
|
|
}
|
2020-05-21 22:30:31 +02:00
|
|
|
|
|
|
|
mid := getUpMid(pc, remote)
|
|
|
|
if mid == "" {
|
|
|
|
log.Printf("Couldn't get track's mid")
|
|
|
|
c.mu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
label, ok := u.labels[mid]
|
|
|
|
if !ok {
|
|
|
|
log.Printf("Couldn't get track's label")
|
|
|
|
isvideo := remote.Kind() == webrtc.RTPCodecTypeVideo
|
|
|
|
if isvideo {
|
|
|
|
label = "video"
|
|
|
|
} else {
|
|
|
|
label = "audio"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:54:50 +02:00
|
|
|
track := &upTrack{
|
|
|
|
track: remote,
|
2020-05-21 22:30:31 +02:00
|
|
|
label: label,
|
2020-04-29 20:57:26 +02:00
|
|
|
cache: packetcache.New(96),
|
2020-04-30 20:15:52 +02:00
|
|
|
rate: estimator.New(time.Second),
|
2020-05-02 16:21:48 +02:00
|
|
|
jitter: jitter.New(remote.Codec().ClockRate),
|
2020-04-26 01:33:18 +02:00
|
|
|
maxBitrate: ^uint64(0),
|
2020-05-26 23:14:34 +02:00
|
|
|
localCh: make(chan localTrackAction, 2),
|
2020-05-20 22:28:30 +02:00
|
|
|
writerDone: make(chan struct{}),
|
2020-04-28 14:54:50 +02:00
|
|
|
}
|
|
|
|
u.tracks = append(u.tracks, track)
|
2020-05-17 23:51:17 +02:00
|
|
|
var tracks []*upTrack
|
|
|
|
if u.complete() {
|
|
|
|
tracks = make([]*upTrack, len(u.tracks))
|
|
|
|
copy(tracks, u.tracks)
|
|
|
|
}
|
2020-05-09 12:06:13 +02:00
|
|
|
if remote.Kind() == webrtc.RTPCodecTypeVideo {
|
|
|
|
atomic.AddUint32(&c.group.videoCount, 1)
|
|
|
|
}
|
2020-04-28 14:54:50 +02:00
|
|
|
c.mu.Unlock()
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2020-05-26 23:14:34 +02:00
|
|
|
go readLoop(conn, track)
|
|
|
|
|
|
|
|
go rtcpUpListener(conn, track, receiver)
|
|
|
|
|
2020-05-17 23:51:17 +02:00
|
|
|
if tracks != nil {
|
|
|
|
clients := c.group.getClients(c)
|
|
|
|
for _, cc := range clients {
|
2020-05-28 02:35:09 +02:00
|
|
|
cc.pushConn(u, tracks, u.label)
|
2020-05-17 23:51:17 +02:00
|
|
|
}
|
2020-05-31 23:20:05 +02:00
|
|
|
go rtcpUpSender(conn)
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-04-29 01:57:37 +02:00
|
|
|
})
|
2020-04-28 16:50:43 +02:00
|
|
|
|
2020-04-29 01:57:37 +02:00
|
|
|
return conn, nil
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2020-05-20 21:45:48 +02:00
|
|
|
type packetIndex struct {
|
|
|
|
seqno uint16
|
|
|
|
index uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
func readLoop(conn *upConnection, track *upTrack) {
|
2020-05-21 13:17:52 +02:00
|
|
|
isvideo := track.track.Kind() == webrtc.RTPCodecTypeVideo
|
2020-05-20 21:45:48 +02:00
|
|
|
ch := make(chan packetIndex, 32)
|
|
|
|
defer close(ch)
|
|
|
|
go writeLoop(conn, track, ch)
|
|
|
|
|
2020-04-29 11:06:39 +02:00
|
|
|
buf := make([]byte, packetcache.BufSize)
|
2020-04-29 01:57:37 +02:00
|
|
|
var packet rtp.Packet
|
2020-05-21 13:17:52 +02:00
|
|
|
drop := 0
|
2020-04-29 01:57:37 +02:00
|
|
|
for {
|
2020-04-30 20:15:52 +02:00
|
|
|
bytes, err := track.track.Read(buf)
|
2020-04-29 01:57:37 +02:00
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Printf("%v", err)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2020-05-31 23:47:12 +02:00
|
|
|
track.rate.Accumulate(uint32(bytes))
|
2020-04-28 23:41:18 +02:00
|
|
|
|
2020-04-30 20:15:52 +02:00
|
|
|
err = packet.Unmarshal(buf[:bytes])
|
2020-04-29 01:57:37 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("%v", err)
|
|
|
|
continue
|
|
|
|
}
|
2020-04-27 21:43:29 +02:00
|
|
|
|
2020-05-02 16:21:48 +02:00
|
|
|
track.jitter.Accumulate(packet.Timestamp)
|
|
|
|
|
2020-05-20 21:45:48 +02:00
|
|
|
first, index :=
|
|
|
|
track.cache.Store(packet.SequenceNumber, buf[:bytes])
|
2020-04-29 11:06:39 +02:00
|
|
|
if packet.SequenceNumber-first > 24 {
|
2020-05-02 22:26:09 +02:00
|
|
|
found, first, bitmap := track.cache.BitmapGet()
|
|
|
|
if found {
|
|
|
|
err := conn.sendNACK(track, first, bitmap)
|
2020-04-29 01:57:37 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("%v", err)
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-29 01:57:37 +02:00
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2020-05-21 13:17:52 +02:00
|
|
|
if drop > 0 {
|
|
|
|
if packet.Marker {
|
|
|
|
// last packet in frame
|
|
|
|
drop = 0
|
|
|
|
} else {
|
|
|
|
drop--
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-05-20 21:45:48 +02:00
|
|
|
select {
|
|
|
|
case ch <- packetIndex{packet.SequenceNumber, index}:
|
|
|
|
default:
|
2020-05-21 13:17:52 +02:00
|
|
|
if isvideo {
|
|
|
|
// the writer is congested. Drop until
|
|
|
|
// the end of the frame.
|
|
|
|
if isvideo && !packet.Marker {
|
|
|
|
drop = 7
|
|
|
|
}
|
|
|
|
}
|
2020-05-20 21:45:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeLoop(conn *upConnection, track *upTrack, ch <-chan packetIndex) {
|
2020-05-20 22:28:30 +02:00
|
|
|
defer close(track.writerDone)
|
2020-05-20 21:45:48 +02:00
|
|
|
|
|
|
|
buf := make([]byte, packetcache.BufSize)
|
|
|
|
var packet rtp.Packet
|
|
|
|
|
2020-05-26 23:14:34 +02:00
|
|
|
local := make([]downTrack, 0)
|
2020-05-20 22:28:30 +02:00
|
|
|
|
2020-05-21 01:14:54 +02:00
|
|
|
firSent := false
|
|
|
|
|
2020-05-20 21:45:48 +02:00
|
|
|
for {
|
2020-05-20 22:28:30 +02:00
|
|
|
select {
|
2020-05-26 23:14:34 +02:00
|
|
|
case action := <-track.localCh:
|
|
|
|
if action.add {
|
|
|
|
local = append(local, action.track)
|
2020-05-21 01:14:54 +02:00
|
|
|
firSent = false
|
2020-05-26 23:14:34 +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 {
|
|
|
|
log.Printf("Deleting unknown track!")
|
|
|
|
}
|
|
|
|
}
|
2020-05-20 22:28:30 +02:00
|
|
|
case pi, ok := <-ch:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2020-05-20 21:45:48 +02:00
|
|
|
|
2020-05-20 22:28:30 +02:00
|
|
|
bytes := track.cache.GetAt(pi.seqno, pi.index, buf)
|
|
|
|
if bytes == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2020-05-20 21:45:48 +02:00
|
|
|
|
2020-05-20 22:28:30 +02:00
|
|
|
err := packet.Unmarshal(buf[:bytes])
|
|
|
|
if err != nil {
|
2020-04-29 01:57:37 +02:00
|
|
|
log.Printf("%v", err)
|
2020-05-20 22:28:30 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-05-21 01:14:54 +02:00
|
|
|
kfNeeded := false
|
|
|
|
|
2020-05-20 22:28:30 +02:00
|
|
|
for _, l := range local {
|
2020-05-26 16:50:20 +02:00
|
|
|
err := l.WriteRTP(&packet)
|
2020-05-21 12:29:41 +02:00
|
|
|
if err != nil {
|
2020-05-21 01:14:54 +02:00
|
|
|
if err == ErrKeyframeNeeded {
|
|
|
|
kfNeeded = true
|
|
|
|
} else if err != io.ErrClosedPipe {
|
2020-05-21 12:29:41 +02:00
|
|
|
log.Printf("WriteRTP: %v", err)
|
|
|
|
}
|
|
|
|
continue
|
2020-05-20 22:28:30 +02:00
|
|
|
}
|
2020-05-26 16:50:20 +02:00
|
|
|
l.Accumulate(uint32(bytes))
|
2020-04-29 01:57:37 +02:00
|
|
|
}
|
2020-05-21 01:14:54 +02:00
|
|
|
|
|
|
|
if kfNeeded {
|
|
|
|
err := conn.sendFIR(track, !firSent)
|
|
|
|
if err == ErrUnsupportedFeedback {
|
|
|
|
err := conn.sendPLI(track)
|
|
|
|
if err != nil &&
|
|
|
|
err != ErrUnsupportedFeedback {
|
|
|
|
log.Printf("sendPLI: %v", err)
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
log.Printf("sendFIR: %v", err)
|
|
|
|
}
|
|
|
|
firSent = true
|
|
|
|
}
|
2020-04-29 01:57:37 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-04-29 03:03:47 +02:00
|
|
|
func rtcpUpListener(conn *upConnection, track *upTrack, r *webrtc.RTPReceiver) {
|
|
|
|
for {
|
|
|
|
ps, err := r.ReadRTCP()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Printf("ReadRTCP: %v", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range ps {
|
|
|
|
switch p := p.(type) {
|
|
|
|
case *rtcp.SenderReport:
|
|
|
|
atomic.StoreUint32(&track.lastSenderReport,
|
|
|
|
uint32(p.NTPTime>>16))
|
2020-05-02 16:21:48 +02:00
|
|
|
atomic.StoreUint32(&track.lastSenderReportTime,
|
|
|
|
uint32(mono.Now(0x10000)))
|
2020-04-29 03:03:47 +02:00
|
|
|
case *rtcp.SourceDescription:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 23:20:05 +02:00
|
|
|
func sendRR(conn *upConnection) error {
|
2020-04-29 03:03:47 +02:00
|
|
|
if len(conn.tracks) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-02 16:21:48 +02:00
|
|
|
now := uint32(mono.Now(0x10000))
|
2020-04-29 03:03:47 +02:00
|
|
|
|
|
|
|
reports := make([]rtcp.ReceptionReport, 0, len(conn.tracks))
|
|
|
|
for _, t := range conn.tracks {
|
2020-05-01 04:41:15 +02:00
|
|
|
expected, lost, totalLost, eseqno := t.cache.GetStats(true)
|
2020-04-29 03:03:47 +02:00
|
|
|
if expected == 0 {
|
|
|
|
expected = 1
|
|
|
|
}
|
|
|
|
if lost >= expected {
|
|
|
|
lost = expected - 1
|
|
|
|
}
|
2020-05-02 16:21:48 +02:00
|
|
|
lastSR := atomic.LoadUint32(&t.lastSenderReport)
|
|
|
|
delay := now - atomic.LoadUint32(&t.lastSenderReportTime)
|
|
|
|
|
2020-04-29 03:03:47 +02:00
|
|
|
reports = append(reports, rtcp.ReceptionReport{
|
|
|
|
SSRC: t.track.SSRC(),
|
|
|
|
FractionLost: uint8((lost * 256) / expected),
|
2020-05-01 04:41:15 +02:00
|
|
|
TotalLost: totalLost,
|
2020-04-29 03:03:47 +02:00
|
|
|
LastSequenceNumber: eseqno,
|
2020-05-02 16:21:48 +02:00
|
|
|
Jitter: t.jitter.Jitter(),
|
|
|
|
LastSenderReport: lastSR,
|
|
|
|
Delay: delay,
|
2020-04-29 03:03:47 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn.pc.WriteRTCP([]rtcp.Packet{
|
|
|
|
&rtcp.ReceiverReport{
|
|
|
|
Reports: reports,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-31 23:20:05 +02:00
|
|
|
func rtcpUpSender(conn *upConnection) {
|
2020-04-29 03:03:47 +02:00
|
|
|
for {
|
|
|
|
time.Sleep(time.Second)
|
2020-05-31 23:20:05 +02:00
|
|
|
err := sendRR(conn)
|
2020-04-29 03:03:47 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF || err == io.ErrClosedPipe {
|
|
|
|
return
|
|
|
|
}
|
2020-05-31 23:20:05 +02:00
|
|
|
log.Printf("sendRR: %v", err)
|
2020-04-29 03:03:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func delUpConn(c *webClient, id string) bool {
|
2020-04-28 14:54:50 +02:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2020-04-24 19:38:21 +02:00
|
|
|
|
|
|
|
if c.up == nil {
|
2020-05-02 23:41:47 +02:00
|
|
|
return false
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
conn := c.up[id]
|
|
|
|
if conn == nil {
|
2020-05-02 23:41:47 +02:00
|
|
|
return false
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 12:06:13 +02:00
|
|
|
for _, track := range conn.tracks {
|
|
|
|
if track.track.Kind() == webrtc.RTPCodecTypeVideo {
|
|
|
|
count := atomic.AddUint32(&c.group.videoCount,
|
|
|
|
^uint32(0))
|
|
|
|
if count == ^uint32(0) {
|
|
|
|
log.Printf("Negative track count!")
|
|
|
|
atomic.StoreUint32(&c.group.videoCount, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 03:36:15 +02:00
|
|
|
conn.Close()
|
2020-04-24 19:38:21 +02:00
|
|
|
delete(c.up, id)
|
2020-05-02 23:41:47 +02:00
|
|
|
return true
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func getDownConn(c *webClient, id string) *rtpDownConnection {
|
2020-04-24 19:38:21 +02:00
|
|
|
if c.down == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:54:50 +02:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2020-04-24 19:38:21 +02:00
|
|
|
conn := c.down[id]
|
|
|
|
if conn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return conn
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func getConn(c *webClient, id string) iceConnection {
|
2020-05-22 22:36:47 +02:00
|
|
|
up := getUpConn(c, id)
|
|
|
|
if up != nil {
|
|
|
|
return up
|
|
|
|
}
|
|
|
|
down := getDownConn(c, id)
|
|
|
|
if down != nil {
|
|
|
|
return down
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func addDownConn(c *webClient, id string, remote *upConnection) (*rtpDownConnection, error) {
|
2020-04-24 19:38:21 +02:00
|
|
|
pc, err := groups.api.NewPeerConnection(iceConfiguration())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pc.OnICECandidate(func(candidate *webrtc.ICECandidate) {
|
|
|
|
sendICE(c, id, candidate)
|
|
|
|
})
|
|
|
|
|
|
|
|
pc.OnTrack(func(remote *webrtc.Track, receiver *webrtc.RTPReceiver) {
|
|
|
|
log.Printf("Got track on downstream connection")
|
|
|
|
})
|
|
|
|
|
|
|
|
if c.down == nil {
|
2020-05-26 17:44:21 +02:00
|
|
|
c.down = make(map[string]*rtpDownConnection)
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-05-26 17:44:21 +02:00
|
|
|
conn := &rtpDownConnection{
|
2020-05-20 22:28:30 +02:00
|
|
|
id: id,
|
2020-05-23 02:22:43 +02:00
|
|
|
client: c,
|
2020-05-20 22:28:30 +02:00
|
|
|
pc: pc,
|
|
|
|
remote: remote,
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2020-04-28 14:54:50 +02:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2020-05-23 02:22:43 +02:00
|
|
|
|
2020-05-22 22:38:40 +02:00
|
|
|
if c.down[id] != nil || (c.up != nil && c.up[id] != nil) {
|
2020-04-24 19:38:21 +02:00
|
|
|
conn.pc.Close()
|
|
|
|
return nil, errors.New("Adding duplicate connection")
|
|
|
|
}
|
2020-05-30 03:36:15 +02:00
|
|
|
err = remote.addLocal(conn)
|
|
|
|
if err != nil {
|
|
|
|
conn.pc.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-23 02:22:43 +02:00
|
|
|
|
2020-05-30 03:36:15 +02:00
|
|
|
c.down[id] = conn
|
2020-04-24 19:38:21 +02:00
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func delDownConn(c *webClient, id string) bool {
|
2020-04-28 14:54:50 +02:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2020-04-24 19:38:21 +02:00
|
|
|
|
|
|
|
if c.down == nil {
|
2020-05-02 23:41:47 +02:00
|
|
|
return false
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-04-25 02:25:51 +02:00
|
|
|
conn := c.down[id]
|
2020-04-24 19:38:21 +02:00
|
|
|
if conn == nil {
|
2020-05-02 23:41:47 +02:00
|
|
|
return false
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-04-28 14:54:50 +02:00
|
|
|
|
2020-05-23 02:22:43 +02:00
|
|
|
conn.remote.delLocal(conn)
|
2020-04-28 14:54:50 +02:00
|
|
|
for _, track := range conn.tracks {
|
2020-05-30 12:33:30 +02:00
|
|
|
// we only insert the track after we get an answer, so
|
|
|
|
// ignore errors here.
|
|
|
|
track.remote.delLocal(track)
|
2020-04-28 14:54:50 +02:00
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
conn.pc.Close()
|
|
|
|
delete(c.down, id)
|
2020-05-02 23:41:47 +02:00
|
|
|
return true
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func addDownTrack(c *webClient, conn *rtpDownConnection, remoteTrack *upTrack, remoteConn *upConnection) (*webrtc.RTPSender, error) {
|
2020-04-28 14:54:50 +02:00
|
|
|
local, err := conn.pc.NewTrack(
|
|
|
|
remoteTrack.track.PayloadType(),
|
|
|
|
remoteTrack.track.SSRC(),
|
|
|
|
remoteTrack.track.ID(),
|
|
|
|
remoteTrack.track.Label(),
|
|
|
|
)
|
2020-04-24 19:38:21 +02:00
|
|
|
if err != nil {
|
2020-05-17 23:51:17 +02:00
|
|
|
return nil, err
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 14:54:50 +02:00
|
|
|
s, err := conn.pc.AddTrack(local)
|
|
|
|
if err != nil {
|
2020-05-17 23:51:17 +02:00
|
|
|
return nil, err
|
2020-04-28 14:54:50 +02:00
|
|
|
}
|
2020-04-26 01:33:18 +02:00
|
|
|
|
2020-05-26 17:44:21 +02:00
|
|
|
track := &rtpDownTrack{
|
2020-05-03 12:25:10 +02:00
|
|
|
track: local,
|
|
|
|
remote: remoteTrack,
|
|
|
|
maxLossBitrate: new(bitrate),
|
|
|
|
maxREMBBitrate: new(bitrate),
|
|
|
|
stats: new(receiverStats),
|
|
|
|
rate: estimator.New(time.Second),
|
2020-04-28 15:26:50 +02:00
|
|
|
}
|
2020-04-28 14:54:50 +02:00
|
|
|
conn.tracks = append(conn.tracks, track)
|
|
|
|
|
2020-05-07 10:29:48 +02:00
|
|
|
go rtcpDownListener(conn, track, s)
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2020-05-17 23:51:17 +02:00
|
|
|
return s, nil
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-03 12:25:10 +02:00
|
|
|
const (
|
|
|
|
minLossRate = 9600
|
|
|
|
initLossRate = 512 * 1000
|
|
|
|
maxLossRate = 1 << 30
|
|
|
|
)
|
|
|
|
|
2020-05-26 17:44:21 +02:00
|
|
|
func (track *rtpDownTrack) updateRate(loss uint8, now uint64) {
|
2020-05-03 12:25:10 +02:00
|
|
|
rate := track.maxLossBitrate.Get(now)
|
|
|
|
if rate > maxLossRate {
|
|
|
|
// no recent feedback, reset
|
|
|
|
rate = initLossRate
|
|
|
|
}
|
|
|
|
if loss < 5 {
|
|
|
|
// if our actual rate is low, then we're not probing the
|
|
|
|
// bottleneck
|
|
|
|
actual := 8 * uint64(track.rate.Estimate())
|
|
|
|
if actual >= (rate*7)/8 {
|
|
|
|
// loss < 0.02, multiply by 1.05
|
|
|
|
rate = rate * 269 / 256
|
|
|
|
if rate > maxLossRate {
|
|
|
|
rate = maxLossRate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if loss > 25 {
|
|
|
|
// loss > 0.1, multiply by (1 - loss/2)
|
|
|
|
rate = rate * (512 - uint64(loss)) / 512
|
|
|
|
if rate < minLossRate {
|
|
|
|
rate = minLossRate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update unconditionally, to set the timestamp
|
|
|
|
track.maxLossBitrate.Set(rate, now)
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:44:21 +02:00
|
|
|
func rtcpDownListener(conn *rtpDownConnection, track *rtpDownTrack, s *webrtc.RTPSender) {
|
2020-05-29 18:38:44 +02:00
|
|
|
var gotFir bool
|
|
|
|
lastFirSeqno := uint8(0)
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
for {
|
|
|
|
ps, err := s.ReadRTCP()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Printf("ReadRTCP: %v", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range ps {
|
|
|
|
switch p := p.(type) {
|
|
|
|
case *rtcp.PictureLossIndication:
|
2020-04-28 20:15:24 +02:00
|
|
|
err := conn.remote.sendPLI(track.remote)
|
2020-04-24 19:38:21 +02:00
|
|
|
if err != nil {
|
2020-04-26 00:25:22 +02:00
|
|
|
log.Printf("sendPLI: %v", err)
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-05-29 18:38:44 +02:00
|
|
|
case *rtcp.FullIntraRequest:
|
|
|
|
found := false
|
|
|
|
var seqno uint8
|
|
|
|
for _, entry := range p.FIR {
|
|
|
|
if entry.SSRC == track.track.SSRC() {
|
|
|
|
found = true
|
|
|
|
seqno = entry.SequenceNumber
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
log.Printf("Misdirected FIR")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
increment := true
|
|
|
|
if gotFir {
|
|
|
|
increment = seqno != lastFirSeqno
|
|
|
|
}
|
|
|
|
gotFir = true
|
|
|
|
lastFirSeqno = seqno
|
|
|
|
|
|
|
|
err := conn.remote.sendFIR(
|
|
|
|
track.remote, increment,
|
|
|
|
)
|
|
|
|
if err == ErrUnsupportedFeedback {
|
|
|
|
err := conn.remote.sendPLI(track.remote)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("sendPLI: %v", err)
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
log.Printf("sendFIR: %v", err)
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
case *rtcp.ReceiverEstimatedMaximumBitrate:
|
2020-05-03 12:25:10 +02:00
|
|
|
track.maxREMBBitrate.Set(
|
|
|
|
p.Bitrate, mono.Microseconds(),
|
2020-05-02 15:27:47 +02:00
|
|
|
)
|
2020-04-24 19:38:21 +02:00
|
|
|
case *rtcp.ReceiverReport:
|
2020-04-29 16:03:35 +02:00
|
|
|
for _, r := range p.Reports {
|
|
|
|
if r.SSRC == track.track.SSRC() {
|
2020-05-03 12:25:10 +02:00
|
|
|
now := mono.Microseconds()
|
|
|
|
track.stats.Set(
|
|
|
|
r.FractionLost,
|
|
|
|
r.Jitter,
|
|
|
|
now,
|
|
|
|
)
|
|
|
|
track.updateRate(
|
|
|
|
r.FractionLost,
|
|
|
|
now,
|
2020-04-29 16:03:35 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2020-04-27 21:43:29 +02:00
|
|
|
case *rtcp.TransportLayerNack:
|
2020-05-03 12:25:10 +02:00
|
|
|
maxBitrate := track.GetMaxBitrate(
|
2020-05-02 15:27:47 +02:00
|
|
|
mono.Microseconds(),
|
|
|
|
)
|
2020-04-30 21:28:08 +02:00
|
|
|
bitrate := track.rate.Estimate()
|
2020-05-21 13:20:51 +02:00
|
|
|
if uint64(bitrate)*7/8 < maxBitrate {
|
2020-04-30 21:28:08 +02:00
|
|
|
sendRecovery(p, track)
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:44:21 +02:00
|
|
|
func trackKinds(down *rtpDownConnection) (audio bool, video bool) {
|
2020-04-24 19:38:21 +02:00
|
|
|
if down.pc == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range down.pc.GetSenders() {
|
|
|
|
track := s.Track()
|
|
|
|
if track == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch track.Kind() {
|
|
|
|
case webrtc.RTPCodecTypeAudio:
|
|
|
|
audio = true
|
|
|
|
case webrtc.RTPCodecTypeVideo:
|
|
|
|
video = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-09 12:06:13 +02:00
|
|
|
func updateUpBitrate(up *upConnection, maxVideoRate uint64) {
|
2020-05-02 15:27:47 +02:00
|
|
|
now := mono.Microseconds()
|
2020-04-26 01:33:18 +02:00
|
|
|
|
2020-04-28 14:54:50 +02:00
|
|
|
for _, track := range up.tracks {
|
2020-05-09 12:06:13 +02:00
|
|
|
isvideo := track.track.Kind() == webrtc.RTPCodecTypeVideo
|
|
|
|
minrate := uint64(minAudioRate)
|
|
|
|
rate := ^uint64(0)
|
|
|
|
if isvideo {
|
|
|
|
minrate = minVideoRate
|
|
|
|
rate = maxVideoRate
|
|
|
|
if rate < minrate {
|
|
|
|
rate = minrate
|
|
|
|
}
|
|
|
|
}
|
2020-04-28 14:54:50 +02:00
|
|
|
local := track.getLocal()
|
|
|
|
for _, l := range local {
|
2020-05-03 12:25:10 +02:00
|
|
|
bitrate := l.GetMaxBitrate(now)
|
2020-04-30 21:22:00 +02:00
|
|
|
if bitrate == ^uint64(0) {
|
2020-04-28 14:54:50 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-05-09 12:06:13 +02:00
|
|
|
if bitrate <= minrate {
|
|
|
|
rate = minrate
|
|
|
|
break
|
2020-04-28 15:26:50 +02:00
|
|
|
}
|
2020-05-09 12:06:13 +02:00
|
|
|
if rate > bitrate {
|
|
|
|
rate = bitrate
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-09 12:06:13 +02:00
|
|
|
track.maxBitrate = rate
|
2020-04-28 14:54:50 +02:00
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 18:07:46 +02:00
|
|
|
var ErrUnsupportedFeedback = errors.New("unsupported feedback type")
|
|
|
|
var ErrRateLimited = errors.New("rate limited")
|
|
|
|
|
2020-04-28 20:15:24 +02:00
|
|
|
func (up *upConnection) sendPLI(track *upTrack) error {
|
2020-05-09 18:49:22 +02:00
|
|
|
if !track.hasRtcpFb("nack", "pli") {
|
2020-05-29 18:07:46 +02:00
|
|
|
return ErrUnsupportedFeedback
|
2020-05-09 18:49:22 +02:00
|
|
|
}
|
2020-04-28 20:15:24 +02:00
|
|
|
last := atomic.LoadUint64(&track.lastPLI)
|
2020-05-02 15:27:47 +02:00
|
|
|
now := mono.Microseconds()
|
|
|
|
if now >= last && now-last < 200000 {
|
2020-05-29 18:07:46 +02:00
|
|
|
return ErrRateLimited
|
2020-04-28 20:15:24 +02:00
|
|
|
}
|
|
|
|
atomic.StoreUint64(&track.lastPLI, now)
|
|
|
|
return sendPLI(up.pc, track.track.SSRC())
|
|
|
|
}
|
|
|
|
|
2020-04-26 01:33:18 +02:00
|
|
|
func sendPLI(pc *webrtc.PeerConnection, ssrc uint32) error {
|
|
|
|
return pc.WriteRTCP([]rtcp.Packet{
|
2020-04-26 00:25:22 +02:00
|
|
|
&rtcp.PictureLossIndication{MediaSSRC: ssrc},
|
2020-04-24 19:38:21 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-21 00:24:10 +02:00
|
|
|
func (up *upConnection) sendFIR(track *upTrack, increment bool) error {
|
|
|
|
// we need to reliably increment the seqno, even if we are going
|
|
|
|
// to drop the packet due to rate limiting.
|
|
|
|
var seqno uint8
|
|
|
|
if increment {
|
|
|
|
seqno = uint8(atomic.AddUint32(&track.firSeqno, 1) & 0xFF)
|
|
|
|
} else {
|
|
|
|
seqno = uint8(atomic.LoadUint32(&track.firSeqno) & 0xFF)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !track.hasRtcpFb("ccm", "fir") {
|
|
|
|
return ErrUnsupportedFeedback
|
|
|
|
}
|
|
|
|
last := atomic.LoadUint64(&track.lastFIR)
|
|
|
|
now := mono.Microseconds()
|
|
|
|
if now >= last && now-last < 200000 {
|
|
|
|
return ErrRateLimited
|
|
|
|
}
|
|
|
|
atomic.StoreUint64(&track.lastFIR, now)
|
|
|
|
return sendFIR(up.pc, track.track.SSRC(), seqno)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendFIR(pc *webrtc.PeerConnection, ssrc uint32, seqno uint8) error {
|
|
|
|
return pc.WriteRTCP([]rtcp.Packet{
|
|
|
|
&rtcp.FullIntraRequest{
|
|
|
|
FIR: []rtcp.FIREntry{
|
|
|
|
rtcp.FIREntry{
|
|
|
|
SSRC: ssrc,
|
|
|
|
SequenceNumber: seqno,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-26 01:33:18 +02:00
|
|
|
func sendREMB(pc *webrtc.PeerConnection, ssrc uint32, bitrate uint64) error {
|
|
|
|
return pc.WriteRTCP([]rtcp.Packet{
|
|
|
|
&rtcp.ReceiverEstimatedMaximumBitrate{
|
|
|
|
Bitrate: bitrate,
|
2020-04-27 03:08:03 +02:00
|
|
|
SSRCs: []uint32{ssrc},
|
2020-04-26 01:33:18 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-28 23:41:18 +02:00
|
|
|
func (up *upConnection) sendNACK(track *upTrack, first uint16, bitmap uint16) error {
|
2020-05-09 18:49:22 +02:00
|
|
|
if !track.hasRtcpFb("nack", "") {
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-01 04:08:26 +02:00
|
|
|
err := sendNACK(up.pc, track.track.SSRC(), first, bitmap)
|
|
|
|
if err == nil {
|
|
|
|
track.cache.Expect(1 + bits.OnesCount16(bitmap))
|
|
|
|
}
|
|
|
|
return err
|
2020-04-28 23:41:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func sendNACK(pc *webrtc.PeerConnection, ssrc uint32, first uint16, bitmap uint16) error {
|
|
|
|
packet := rtcp.Packet(
|
|
|
|
&rtcp.TransportLayerNack{
|
|
|
|
MediaSSRC: ssrc,
|
|
|
|
Nacks: []rtcp.NackPair{
|
|
|
|
rtcp.NackPair{
|
|
|
|
first,
|
|
|
|
rtcp.PacketBitmap(bitmap),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return pc.WriteRTCP([]rtcp.Packet{packet})
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:44:21 +02:00
|
|
|
func sendRecovery(p *rtcp.TransportLayerNack, track *rtpDownTrack) {
|
2020-04-27 21:43:29 +02:00
|
|
|
var packet rtp.Packet
|
2020-05-20 20:32:30 +02:00
|
|
|
buf := make([]byte, packetcache.BufSize)
|
2020-04-27 21:43:29 +02:00
|
|
|
for _, nack := range p.Nacks {
|
|
|
|
for _, seqno := range nack.PacketList() {
|
2020-05-20 20:32:30 +02:00
|
|
|
l := track.remote.cache.Get(seqno, buf)
|
|
|
|
if l == 0 {
|
2020-04-30 21:28:08 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-05-20 20:32:30 +02:00
|
|
|
err := packet.Unmarshal(buf[:l])
|
2020-04-30 21:28:08 +02:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = track.track.WriteRTP(&packet)
|
|
|
|
if err != nil {
|
2020-05-21 12:29:41 +02:00
|
|
|
log.Printf("WriteRTP: %v", err)
|
2020-04-30 21:28:08 +02:00
|
|
|
continue
|
2020-04-27 21:43:29 +02:00
|
|
|
}
|
2020-05-31 23:47:12 +02:00
|
|
|
track.rate.Accumulate(uint32(l))
|
2020-04-27 21:43:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func negotiate(c *webClient, down *rtpDownConnection) error {
|
2020-05-17 22:31:29 +02:00
|
|
|
offer, err := down.pc.CreateOffer(nil)
|
2020-04-24 19:38:21 +02:00
|
|
|
if err != nil {
|
2020-05-17 22:31:29 +02:00
|
|
|
return err
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 22:31:29 +02:00
|
|
|
err = down.pc.SetLocalDescription(offer)
|
2020-04-24 19:38:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-17 22:31:29 +02:00
|
|
|
|
|
|
|
labels := make(map[string]string)
|
2020-05-21 22:30:31 +02:00
|
|
|
for _, t := range down.pc.GetTransceivers() {
|
|
|
|
var track *webrtc.Track
|
|
|
|
if t.Sender() != nil {
|
|
|
|
track = t.Sender().Track()
|
|
|
|
}
|
|
|
|
if track == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tr := range down.tracks {
|
|
|
|
if tr.track == track {
|
|
|
|
labels[t.Mid()] = tr.remote.label
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-05-17 22:31:29 +02:00
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
return c.write(clientMessage{
|
2020-05-17 22:31:29 +02:00
|
|
|
Type: "offer",
|
2020-05-17 23:51:17 +02:00
|
|
|
Id: down.id,
|
2020-05-17 22:31:29 +02:00
|
|
|
Offer: &offer,
|
|
|
|
Labels: labels,
|
2020-04-24 19:38:21 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func sendICE(c *webClient, id string, candidate *webrtc.ICECandidate) error {
|
2020-04-24 19:38:21 +02:00
|
|
|
if candidate == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
cand := candidate.ToJSON()
|
|
|
|
return c.write(clientMessage{
|
|
|
|
Type: "ice",
|
|
|
|
Id: id,
|
|
|
|
Candidate: &cand,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func gotOffer(c *webClient, id string, offer webrtc.SessionDescription, labels map[string]string) error {
|
2020-04-24 19:38:21 +02:00
|
|
|
var err error
|
|
|
|
up, ok := c.up[id]
|
|
|
|
if !ok {
|
|
|
|
up, err = addUpConn(c, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.username != "" {
|
|
|
|
up.label = c.username
|
|
|
|
}
|
|
|
|
err = up.pc.SetRemoteDescription(offer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
answer, err := up.pc.CreateAnswer(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = up.pc.SetLocalDescription(answer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-17 22:31:29 +02:00
|
|
|
up.labels = labels
|
|
|
|
|
2020-05-22 23:07:38 +02:00
|
|
|
err = up.flushICECandidates()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ICE: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
return c.write(clientMessage{
|
|
|
|
Type: "answer",
|
|
|
|
Id: id,
|
|
|
|
Answer: &answer,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func gotAnswer(c *webClient, id string, answer webrtc.SessionDescription) error {
|
2020-05-22 23:07:38 +02:00
|
|
|
down := getDownConn(c, id)
|
|
|
|
if down == nil {
|
2020-04-24 19:38:21 +02:00
|
|
|
return protocolError("unknown id in answer")
|
|
|
|
}
|
2020-05-22 23:07:38 +02:00
|
|
|
err := down.pc.SetRemoteDescription(answer)
|
2020-04-24 19:38:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-21 00:55:00 +02:00
|
|
|
|
2020-05-22 23:07:38 +02:00
|
|
|
err = down.flushICECandidates()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ICE: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range down.tracks {
|
2020-05-26 17:01:29 +02:00
|
|
|
t.remote.addLocal(t)
|
2020-05-21 00:55:00 +02:00
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func gotICE(c *webClient, candidate *webrtc.ICECandidateInit, id string) error {
|
2020-05-22 22:36:47 +02:00
|
|
|
conn := getConn(c, id)
|
|
|
|
if conn == nil {
|
|
|
|
return errors.New("unknown id in ICE")
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-05-22 23:07:38 +02:00
|
|
|
return conn.addICECandidate(candidate)
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func (c *webClient) setRequested(requested map[string]uint32) error {
|
2020-05-09 19:39:34 +02:00
|
|
|
if c.down != nil {
|
|
|
|
for id := range c.down {
|
|
|
|
c.write(clientMessage{
|
|
|
|
Type: "close",
|
|
|
|
Id: id,
|
|
|
|
})
|
|
|
|
delDownConn(c, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 22:31:29 +02:00
|
|
|
c.requested = requested
|
2020-05-09 19:39:34 +02:00
|
|
|
|
2020-05-30 00:23:54 +02:00
|
|
|
go pushConns(c)
|
2020-05-09 19:39:34 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-30 00:23:54 +02:00
|
|
|
func pushConns(c client) {
|
|
|
|
clients := c.getGroup().getClients(c)
|
|
|
|
for _, cc := range clients {
|
|
|
|
ccc, ok := cc.(*webClient)
|
|
|
|
if ok {
|
|
|
|
ccc.action(pushConnsAction{c})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func (c *webClient) isRequested(label string) bool {
|
2020-05-18 00:35:58 +02:00
|
|
|
return c.requested[label] != 0
|
2020-05-09 19:39:34 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func addDownConnTracks(c *webClient, remote *upConnection, tracks []*upTrack) (*rtpDownConnection, error) {
|
2020-05-17 23:51:17 +02:00
|
|
|
requested := false
|
|
|
|
for _, t := range tracks {
|
|
|
|
if c.isRequested(t.label) {
|
|
|
|
requested = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !requested {
|
|
|
|
return nil, nil
|
2020-05-12 18:27:40 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:51:17 +02:00
|
|
|
down, err := addDownConn(c, remote.id, remote)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-12 18:27:40 +02:00
|
|
|
|
2020-05-17 23:51:17 +02:00
|
|
|
for _, t := range tracks {
|
|
|
|
if !c.isRequested(t.label) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, err = addDownTrack(c, down, t, remote)
|
|
|
|
if err != nil {
|
|
|
|
delDownConn(c, down.id)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return down, nil
|
|
|
|
}
|
|
|
|
|
2020-05-28 02:35:09 +02:00
|
|
|
func (c *webClient) pushConn(conn *upConnection, tracks []*upTrack, label string) error {
|
|
|
|
err := c.action(addConnAction{conn, tracks})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-17 23:51:17 +02:00
|
|
|
if label != "" {
|
2020-05-28 02:35:09 +02:00
|
|
|
err := c.action(addLabelAction{conn.id, conn.label})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-12 18:27:40 +02:00
|
|
|
}
|
2020-05-28 02:35:09 +02:00
|
|
|
return nil
|
2020-05-12 18:27:40 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func clientLoop(c *webClient, conn *websocket.Conn) error {
|
2020-04-24 19:38:21 +02:00
|
|
|
read := make(chan interface{}, 1)
|
|
|
|
go clientReader(conn, read, c.done)
|
|
|
|
|
|
|
|
defer func() {
|
2020-05-18 00:35:58 +02:00
|
|
|
c.setRequested(map[string]uint32{})
|
2020-04-24 19:38:21 +02:00
|
|
|
if c.up != nil {
|
|
|
|
for id := range c.up {
|
|
|
|
delUpConn(c, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-04-25 02:25:51 +02:00
|
|
|
c.write(clientMessage{
|
|
|
|
Type: "permissions",
|
|
|
|
Permissions: c.permissions,
|
|
|
|
})
|
|
|
|
|
2020-04-25 21:16:49 +02:00
|
|
|
h := c.group.getChatHistory()
|
|
|
|
for _, m := range h {
|
|
|
|
err := c.write(clientMessage{
|
|
|
|
Type: "chat",
|
|
|
|
Id: m.id,
|
|
|
|
Username: m.user,
|
|
|
|
Value: m.value,
|
|
|
|
Me: m.me,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 22:44:24 +02:00
|
|
|
readTime := time.Now()
|
|
|
|
|
2020-04-26 01:33:18 +02:00
|
|
|
ticker := time.NewTicker(time.Second)
|
2020-04-24 19:38:21 +02:00
|
|
|
defer ticker.Stop()
|
2020-04-25 22:44:24 +02:00
|
|
|
slowTicker := time.NewTicker(10 * time.Second)
|
|
|
|
defer slowTicker.Stop()
|
2020-04-24 19:38:21 +02:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case m, ok := <-read:
|
|
|
|
if !ok {
|
|
|
|
return errors.New("reader died")
|
|
|
|
}
|
|
|
|
switch m := m.(type) {
|
|
|
|
case clientMessage:
|
2020-04-25 22:44:24 +02:00
|
|
|
readTime = time.Now()
|
2020-04-24 19:38:21 +02:00
|
|
|
err := handleClientMessage(c, m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case error:
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
case a := <-c.actionCh:
|
|
|
|
switch a := a.(type) {
|
2020-05-17 23:51:17 +02:00
|
|
|
case addConnAction:
|
|
|
|
down, err := addDownConnTracks(
|
|
|
|
c, a.conn, a.tracks,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-05-17 23:51:17 +02:00
|
|
|
if down != nil {
|
|
|
|
err = negotiate(c, down)
|
2020-04-24 19:38:21 +02:00
|
|
|
if err != nil {
|
2020-05-24 13:36:42 +02:00
|
|
|
log.Printf("Negotiate: %v", err)
|
|
|
|
delDownConn(c, down.id)
|
|
|
|
err = failConnection(
|
|
|
|
c, down.id,
|
|
|
|
"negotiation failed",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-28 14:54:50 +02:00
|
|
|
case delConnAction:
|
2020-05-02 23:41:47 +02:00
|
|
|
found := delDownConn(c, a.id)
|
|
|
|
if found {
|
|
|
|
c.write(clientMessage{
|
|
|
|
Type: "close",
|
|
|
|
Id: a.id,
|
|
|
|
})
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
case addLabelAction:
|
|
|
|
c.write(clientMessage{
|
|
|
|
Type: "label",
|
|
|
|
Id: a.id,
|
|
|
|
Value: a.label,
|
|
|
|
})
|
2020-05-17 23:51:17 +02:00
|
|
|
case pushConnsAction:
|
2020-04-24 19:38:21 +02:00
|
|
|
for _, u := range c.up {
|
2020-05-12 18:27:40 +02:00
|
|
|
tracks := make([]*upTrack, len(u.tracks))
|
|
|
|
copy(tracks, u.tracks)
|
2020-05-28 02:35:09 +02:00
|
|
|
go a.c.pushConn(u, tracks, u.label)
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-05-02 23:41:47 +02:00
|
|
|
case connectionFailedAction:
|
|
|
|
found := delUpConn(c, a.id)
|
|
|
|
if found {
|
2020-05-24 13:36:42 +02:00
|
|
|
err := failConnection(c, a.id,
|
2020-05-30 12:53:58 +02:00
|
|
|
"connection failed")
|
2020-05-24 13:36:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-02 23:41:47 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// What should we do if a downstream
|
|
|
|
// connection fails? Renegotiate?
|
2020-04-25 18:35:32 +02:00
|
|
|
case permissionsChangedAction:
|
2020-04-25 17:36:35 +02:00
|
|
|
c.write(clientMessage{
|
|
|
|
Type: "permissions",
|
|
|
|
Permissions: c.permissions,
|
|
|
|
})
|
2020-04-25 19:58:54 +02:00
|
|
|
if !c.permissions.Present {
|
2020-04-25 18:35:32 +02:00
|
|
|
ids := getUpConns(c)
|
|
|
|
for _, id := range ids {
|
2020-05-24 13:36:42 +02:00
|
|
|
found := delUpConn(c, id)
|
|
|
|
if found {
|
|
|
|
failConnection(
|
|
|
|
c, id,
|
|
|
|
"permission denied",
|
|
|
|
)
|
|
|
|
}
|
2020-04-25 18:35:32 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-25 17:36:35 +02:00
|
|
|
case kickAction:
|
|
|
|
return userError("you have been kicked")
|
2020-04-24 19:38:21 +02:00
|
|
|
default:
|
|
|
|
log.Printf("unexpected action %T", a)
|
|
|
|
return errors.New("unexpected action")
|
|
|
|
}
|
|
|
|
case <-ticker.C:
|
|
|
|
sendRateUpdate(c)
|
2020-04-25 22:44:24 +02:00
|
|
|
case <-slowTicker.C:
|
|
|
|
if time.Since(readTime) > 90*time.Second {
|
|
|
|
return errors.New("client is dead")
|
|
|
|
}
|
|
|
|
if time.Since(readTime) > 60*time.Second {
|
|
|
|
err := c.write(clientMessage{
|
|
|
|
Type: "ping",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func failConnection(c *webClient, id string, message string) error {
|
2020-05-24 13:36:42 +02:00
|
|
|
if id != "" {
|
|
|
|
err := c.write(clientMessage{
|
|
|
|
Type: "abort",
|
|
|
|
Id: id,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if message != "" {
|
|
|
|
err := c.error(userError(message))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-28 02:35:09 +02:00
|
|
|
func setPermissions(g *group, id string, perm string) error {
|
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
|
|
|
|
|
|
|
client := g.getClientUnlocked(id)
|
|
|
|
if client == nil {
|
|
|
|
return userError("no such user")
|
|
|
|
}
|
|
|
|
|
|
|
|
c, ok := client.(*webClient)
|
|
|
|
if !ok {
|
|
|
|
return userError("this is not a real user")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch perm {
|
|
|
|
case "op":
|
|
|
|
c.permissions.Op = true
|
2020-05-30 00:23:54 +02:00
|
|
|
if g.description.AllowRecording {
|
|
|
|
c.permissions.Record = true
|
|
|
|
}
|
2020-05-28 02:35:09 +02:00
|
|
|
case "unop":
|
|
|
|
c.permissions.Op = false
|
2020-05-30 00:23:54 +02:00
|
|
|
c.permissions.Record = false
|
2020-05-28 02:35:09 +02:00
|
|
|
case "present":
|
|
|
|
c.permissions.Present = true
|
|
|
|
case "unpresent":
|
|
|
|
c.permissions.Present = false
|
|
|
|
default:
|
|
|
|
return userError("unknown permission")
|
|
|
|
}
|
|
|
|
return c.action(permissionsChangedAction{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func kickClient(g *group, id string) error {
|
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
|
|
|
|
|
|
|
client := g.getClientUnlocked(id)
|
|
|
|
if client == nil {
|
|
|
|
return userError("no such user")
|
|
|
|
}
|
|
|
|
|
|
|
|
c, ok := client.(*webClient)
|
|
|
|
if !ok {
|
|
|
|
return userError("this is not a real user")
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.action(kickAction{})
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func handleClientMessage(c *webClient, m clientMessage) error {
|
2020-04-24 19:38:21 +02:00
|
|
|
switch m.Type {
|
2020-05-09 19:39:34 +02:00
|
|
|
case "request":
|
2020-05-17 22:31:29 +02:00
|
|
|
err := c.setRequested(m.Request)
|
2020-05-09 19:39:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
case "offer":
|
2020-04-25 02:25:51 +02:00
|
|
|
if !c.permissions.Present {
|
2020-04-25 18:29:44 +02:00
|
|
|
c.write(clientMessage{
|
|
|
|
Type: "abort",
|
|
|
|
Id: m.Id,
|
|
|
|
})
|
|
|
|
return c.error(userError("not authorised"))
|
2020-04-25 02:25:51 +02:00
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
if m.Offer == nil {
|
|
|
|
return protocolError("null offer")
|
|
|
|
}
|
2020-05-17 22:31:29 +02:00
|
|
|
err := gotOffer(c, m.Id, *m.Offer, m.Labels)
|
2020-04-24 19:38:21 +02:00
|
|
|
if err != nil {
|
2020-05-24 13:36:42 +02:00
|
|
|
log.Printf("gotOffer: %v", err)
|
|
|
|
return failConnection(c, m.Id, "negotiation failed")
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
case "answer":
|
|
|
|
if m.Answer == nil {
|
|
|
|
return protocolError("null answer")
|
|
|
|
}
|
2020-05-17 22:31:29 +02:00
|
|
|
err := gotAnswer(c, m.Id, *m.Answer)
|
2020-04-24 19:38:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case "close":
|
2020-05-02 23:41:47 +02:00
|
|
|
found := delUpConn(c, m.Id)
|
|
|
|
if !found {
|
|
|
|
log.Printf("Deleting unknown up connection %v", m.Id)
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
case "ice":
|
|
|
|
if m.Candidate == nil {
|
|
|
|
return protocolError("null candidate")
|
|
|
|
}
|
|
|
|
err := gotICE(c, m.Candidate, m.Id)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ICE: %v", err)
|
|
|
|
}
|
|
|
|
case "chat":
|
2020-04-25 21:16:49 +02:00
|
|
|
c.group.addToChatHistory(m.Id, m.Username, m.Value, m.Me)
|
2020-04-24 19:38:21 +02:00
|
|
|
clients := c.group.getClients(c)
|
|
|
|
for _, cc := range clients {
|
2020-05-28 02:35:09 +02:00
|
|
|
cc, ok := cc.(*webClient)
|
|
|
|
if ok {
|
|
|
|
cc.write(m)
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-04-30 19:13:10 +02:00
|
|
|
case "clearchat":
|
|
|
|
c.group.clearChatHistory()
|
|
|
|
m := clientMessage{Type: "clearchat"}
|
|
|
|
clients := c.group.getClients(nil)
|
|
|
|
for _, cc := range clients {
|
2020-05-28 02:35:09 +02:00
|
|
|
cc, ok := cc.(*webClient)
|
|
|
|
if ok {
|
|
|
|
cc.write(m)
|
|
|
|
}
|
2020-04-30 19:13:10 +02:00
|
|
|
}
|
2020-04-25 17:36:35 +02:00
|
|
|
case "op", "unop", "present", "unpresent":
|
2020-04-25 18:09:31 +02:00
|
|
|
if !c.permissions.Op {
|
2020-05-28 04:27:15 +02:00
|
|
|
return c.error(userError("not authorised"))
|
2020-04-25 17:36:35 +02:00
|
|
|
}
|
2020-05-28 02:35:09 +02:00
|
|
|
err := setPermissions(c.group, m.Id, m.Type)
|
2020-04-25 17:36:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return c.error(err)
|
|
|
|
}
|
2020-05-18 15:24:04 +02:00
|
|
|
case "lock", "unlock":
|
|
|
|
if !c.permissions.Op {
|
2020-05-28 04:27:15 +02:00
|
|
|
return c.error(userError("not authorised"))
|
2020-05-18 15:24:04 +02:00
|
|
|
}
|
|
|
|
var locked uint32
|
|
|
|
if m.Type == "lock" {
|
|
|
|
locked = 1
|
|
|
|
}
|
|
|
|
atomic.StoreUint32(&c.group.locked, locked)
|
2020-05-30 00:23:54 +02:00
|
|
|
case "record":
|
|
|
|
if !c.permissions.Record {
|
|
|
|
return c.error(userError("not authorised"))
|
|
|
|
}
|
|
|
|
for _, cc := range c.group.getClients(c) {
|
|
|
|
_, ok := cc.(*diskClient)
|
|
|
|
if ok {
|
|
|
|
return c.error(userError("already recording"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
disk := &diskClient{
|
|
|
|
group: c.group,
|
|
|
|
id: "recording",
|
|
|
|
}
|
2020-05-31 20:41:17 +02:00
|
|
|
_, err := addClient(c.group.name, disk, "", "")
|
2020-05-30 00:23:54 +02:00
|
|
|
if err != nil {
|
|
|
|
disk.Close()
|
|
|
|
return c.error(err)
|
|
|
|
}
|
|
|
|
go pushConns(disk)
|
|
|
|
case "unrecord":
|
|
|
|
if !c.permissions.Record {
|
|
|
|
return c.error(userError("not authorised"))
|
|
|
|
}
|
|
|
|
for _, cc := range c.group.getClients(c) {
|
|
|
|
disk, ok := cc.(*diskClient)
|
|
|
|
if ok {
|
|
|
|
disk.Close()
|
|
|
|
delClient(disk)
|
|
|
|
}
|
|
|
|
}
|
2020-04-25 17:36:35 +02:00
|
|
|
case "kick":
|
2020-04-25 18:09:31 +02:00
|
|
|
if !c.permissions.Op {
|
2020-05-28 04:27:15 +02:00
|
|
|
return c.error(userError("not authorised"))
|
2020-04-25 17:36:35 +02:00
|
|
|
}
|
|
|
|
err := kickClient(c.group, m.Id)
|
|
|
|
if err != nil {
|
|
|
|
return c.error(err)
|
|
|
|
}
|
2020-04-25 22:44:24 +02:00
|
|
|
case "pong":
|
|
|
|
// nothing
|
|
|
|
case "ping":
|
|
|
|
c.write(clientMessage{
|
|
|
|
Type: "pong",
|
|
|
|
})
|
2020-04-24 19:38:21 +02:00
|
|
|
default:
|
|
|
|
log.Printf("unexpected message: %v", m.Type)
|
|
|
|
return protocolError("unexpected message")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func sendRateUpdate(c *webClient) {
|
2020-04-28 14:54:50 +02:00
|
|
|
type remb struct {
|
|
|
|
pc *webrtc.PeerConnection
|
|
|
|
ssrc uint32
|
|
|
|
bitrate uint64
|
|
|
|
}
|
|
|
|
rembs := make([]remb, 0)
|
|
|
|
|
2020-05-09 12:06:13 +02:00
|
|
|
maxVideoRate := ^uint64(0)
|
|
|
|
count := atomic.LoadUint32(&c.group.videoCount)
|
|
|
|
if count >= 3 {
|
|
|
|
maxVideoRate = uint64(2000000 / math.Sqrt(float64(count)))
|
|
|
|
if maxVideoRate < minVideoRate {
|
|
|
|
maxVideoRate = minVideoRate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:54:50 +02:00
|
|
|
c.mu.Lock()
|
2020-04-24 19:38:21 +02:00
|
|
|
for _, u := range c.up {
|
2020-05-09 12:06:13 +02:00
|
|
|
updateUpBitrate(u, maxVideoRate)
|
2020-04-28 14:54:50 +02:00
|
|
|
for _, t := range u.tracks {
|
2020-05-09 18:49:22 +02:00
|
|
|
if !t.hasRtcpFb("goog-remb", "") {
|
|
|
|
continue
|
|
|
|
}
|
2020-04-28 14:54:50 +02:00
|
|
|
bitrate := t.maxBitrate
|
2020-04-28 15:26:50 +02:00
|
|
|
if bitrate == ^uint64(0) {
|
|
|
|
continue
|
2020-04-26 01:33:18 +02:00
|
|
|
}
|
2020-04-28 15:26:50 +02:00
|
|
|
rembs = append(rembs,
|
|
|
|
remb{u.pc, t.track.SSRC(), bitrate})
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-28 14:54:50 +02:00
|
|
|
c.mu.Unlock()
|
|
|
|
|
|
|
|
for _, r := range rembs {
|
|
|
|
err := sendREMB(r.pc, r.ssrc, r.bitrate)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("sendREMB: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func clientReader(conn *websocket.Conn, read chan<- interface{}, done <-chan struct{}) {
|
|
|
|
defer close(read)
|
|
|
|
for {
|
|
|
|
var m clientMessage
|
|
|
|
err := conn.ReadJSON(&m)
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case read <- err:
|
|
|
|
return
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case read <- m:
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func clientWriter(conn *websocket.Conn, ch <-chan interface{}, done chan<- struct{}) {
|
|
|
|
defer func() {
|
|
|
|
close(done)
|
|
|
|
conn.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
m, ok := <-ch
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err := conn.SetWriteDeadline(
|
|
|
|
time.Now().Add(2 * time.Second))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch m := m.(type) {
|
|
|
|
case clientMessage:
|
|
|
|
err := conn.WriteJSON(m)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case closeMessage:
|
|
|
|
err := conn.WriteMessage(websocket.CloseMessage, m.data)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
log.Printf("clientWiter: unexpected message %T", m)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-28 02:35:09 +02:00
|
|
|
|
2020-05-30 12:38:13 +02:00
|
|
|
var ErrWriterDead = errors.New("client writer died")
|
|
|
|
var ErrClientDead = errors.New("client dead")
|
|
|
|
|
2020-05-28 02:35:09 +02:00
|
|
|
func (c *webClient) action(m interface{}) error {
|
|
|
|
select {
|
|
|
|
case c.actionCh <- m:
|
|
|
|
return nil
|
|
|
|
case <-c.done:
|
2020-05-30 12:38:13 +02:00
|
|
|
return ErrClientDead
|
2020-05-28 02:35:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *webClient) write(m clientMessage) error {
|
|
|
|
select {
|
|
|
|
case c.writeCh <- m:
|
|
|
|
return nil
|
|
|
|
case <-c.writerDone:
|
2020-05-30 12:38:13 +02:00
|
|
|
return ErrWriterDead
|
2020-05-28 02:35:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *webClient) error(err error) error {
|
|
|
|
switch e := err.(type) {
|
|
|
|
case userError:
|
|
|
|
return c.write(clientMessage{
|
|
|
|
Type: "error",
|
|
|
|
Value: string(e),
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|