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"
|
|
|
|
"os"
|
2020-04-25 19:59:53 +02:00
|
|
|
"strings"
|
2020-04-24 19:38:21 +02:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
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/sdp"
|
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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"`
|
|
|
|
Del bool `json:"del,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
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &client{
|
|
|
|
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-04-25 19:59:53 +02:00
|
|
|
if strings.ContainsRune(m.Username, ' ') {
|
|
|
|
err = userError("don't put spaces in your username")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-25 02:25:51 +02:00
|
|
|
g, users, 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)
|
|
|
|
|
|
|
|
for _, u := range users {
|
|
|
|
c.write(clientMessage{
|
|
|
|
Type: "user",
|
|
|
|
Id: u.id,
|
|
|
|
Username: u.username,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
clients := g.getClients(nil)
|
|
|
|
u := clientMessage{
|
|
|
|
Type: "user",
|
|
|
|
Id: c.id,
|
|
|
|
Username: c.username,
|
|
|
|
}
|
|
|
|
for _, c := range clients {
|
|
|
|
c.write(u)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
clients := g.getClients(c)
|
|
|
|
u := clientMessage{
|
|
|
|
Type: "user",
|
|
|
|
Id: c.id,
|
|
|
|
Username: c.username,
|
|
|
|
Del: true,
|
|
|
|
}
|
|
|
|
for _, c := range clients {
|
|
|
|
c.write(u)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return clientLoop(c, conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUpConn(c *client, 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-04-25 18:35:32 +02:00
|
|
|
func getUpConns(c *client) []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-04-24 19:38:21 +02:00
|
|
|
func addUpConn(c *client, id string) (*upConnection, error) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if c.up[id] != nil {
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2020-04-29 03:03:47 +02:00
|
|
|
go rtcpUpSender(c, conn)
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
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-04-28 14:54:50 +02:00
|
|
|
track := &upTrack{
|
|
|
|
track: remote,
|
2020-04-29 20:57:26 +02:00
|
|
|
cache: packetcache.New(96),
|
2020-04-26 01:33:18 +02:00
|
|
|
maxBitrate: ^uint64(0),
|
2020-04-28 14:54:50 +02:00
|
|
|
}
|
|
|
|
u.tracks = append(u.tracks, track)
|
|
|
|
done := len(u.tracks) >= u.trackCount
|
|
|
|
c.mu.Unlock()
|
2020-04-24 19:38:21 +02:00
|
|
|
|
|
|
|
clients := c.group.getClients(c)
|
|
|
|
for _, cc := range clients {
|
2020-04-28 14:54:50 +02:00
|
|
|
cc.action(addTrackAction{track, u, done})
|
2020-04-25 02:25:51 +02:00
|
|
|
if done && u.label != "" {
|
2020-04-24 19:38:21 +02:00
|
|
|
cc.action(addLabelAction{id, u.label})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 01:57:37 +02:00
|
|
|
go upLoop(conn, track)
|
2020-04-29 03:03:47 +02:00
|
|
|
|
|
|
|
go rtcpUpListener(conn, track, receiver)
|
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-04-29 01:57:37 +02:00
|
|
|
func upLoop(conn *upConnection, track *upTrack) {
|
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
|
|
|
|
var local []*downTrack
|
|
|
|
var localTime time.Time
|
|
|
|
for {
|
|
|
|
now := time.Now()
|
|
|
|
if now.Sub(localTime) > time.Second/2 {
|
|
|
|
local = track.getLocal()
|
|
|
|
localTime = now
|
|
|
|
}
|
2020-04-27 03:28:45 +02:00
|
|
|
|
2020-04-29 01:57:37 +02:00
|
|
|
i, err := track.track.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Printf("%v", err)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2020-04-28 23:41:18 +02:00
|
|
|
|
2020-04-29 01:57:37 +02:00
|
|
|
err = packet.Unmarshal(buf[:i])
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("%v", err)
|
|
|
|
continue
|
|
|
|
}
|
2020-04-27 21:43:29 +02:00
|
|
|
|
2020-04-29 11:06:39 +02:00
|
|
|
first := track.cache.Store(packet.SequenceNumber, buf[:i])
|
|
|
|
if packet.SequenceNumber-first > 24 {
|
|
|
|
first, bitmap := track.cache.BitmapGet()
|
2020-04-29 01:57:37 +02:00
|
|
|
if bitmap != ^uint16(0) {
|
|
|
|
err := conn.sendNACK(track, first, ^bitmap)
|
|
|
|
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-04-29 01:57:37 +02:00
|
|
|
for _, l := range local {
|
|
|
|
if l.muted() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err := l.track.WriteRTP(&packet)
|
|
|
|
if err != nil && err != io.ErrClosedPipe {
|
|
|
|
log.Printf("%v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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))
|
|
|
|
case *rtcp.SourceDescription:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendRR(c *client, conn *upConnection) error {
|
|
|
|
c.mu.Lock()
|
|
|
|
if len(conn.tracks) == 0 {
|
|
|
|
c.mu.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ssrc := conn.tracks[0].track.SSRC()
|
|
|
|
|
|
|
|
reports := make([]rtcp.ReceptionReport, 0, len(conn.tracks))
|
|
|
|
for _, t := range conn.tracks {
|
|
|
|
expected, lost, eseqno := t.cache.GetStats(true)
|
|
|
|
if expected == 0 {
|
|
|
|
expected = 1
|
|
|
|
}
|
|
|
|
if lost >= expected {
|
|
|
|
lost = expected - 1
|
|
|
|
}
|
|
|
|
reports = append(reports, rtcp.ReceptionReport{
|
|
|
|
SSRC: t.track.SSRC(),
|
|
|
|
LastSenderReport: atomic.LoadUint32(&t.lastSenderReport),
|
|
|
|
FractionLost: uint8((lost * 256) / expected),
|
|
|
|
TotalLost: lost,
|
|
|
|
LastSequenceNumber: eseqno,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
c.mu.Unlock()
|
|
|
|
|
|
|
|
return conn.pc.WriteRTCP([]rtcp.Packet{
|
|
|
|
&rtcp.ReceiverReport{
|
|
|
|
SSRC: ssrc,
|
|
|
|
Reports: reports,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func rtcpUpSender(c *client, conn *upConnection) {
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
err := sendRR(c, conn)
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF || err == io.ErrClosedPipe {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("WriteRTCP: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
func delUpConn(c *client, id string) {
|
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 {
|
|
|
|
log.Printf("Deleting unknown connection")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := c.up[id]
|
|
|
|
if conn == nil {
|
|
|
|
log.Printf("Deleting unknown connection")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type clientId struct {
|
|
|
|
client *client
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
cids := make([]clientId, 0)
|
2020-04-28 14:54:50 +02:00
|
|
|
|
|
|
|
clients := c.group.getClients(c)
|
|
|
|
for _, cc := range clients {
|
|
|
|
cc.mu.Lock()
|
2020-04-24 19:38:21 +02:00
|
|
|
for _, otherconn := range cc.down {
|
|
|
|
if otherconn.remote == conn {
|
|
|
|
cids = append(cids, clientId{cc, otherconn.id})
|
|
|
|
}
|
|
|
|
}
|
2020-04-28 14:54:50 +02:00
|
|
|
cc.mu.Unlock()
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, cid := range cids {
|
2020-04-28 14:54:50 +02:00
|
|
|
cid.client.action(delConnAction{cid.id})
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
conn.pc.Close()
|
|
|
|
delete(c.up, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDownConn(c *client, id string) *downConnection {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func addDownConn(c *client, id string, remote *upConnection) (*downConnection, error) {
|
|
|
|
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 {
|
|
|
|
c.down = make(map[string]*downConnection)
|
|
|
|
}
|
|
|
|
conn := &downConnection{id: id, pc: pc, remote: remote}
|
|
|
|
|
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[id] != nil {
|
|
|
|
conn.pc.Close()
|
|
|
|
return nil, errors.New("Adding duplicate connection")
|
|
|
|
}
|
|
|
|
c.down[id] = conn
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func delDownConn(c *client, id string) {
|
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 {
|
|
|
|
log.Printf("Deleting unknown connection")
|
|
|
|
return
|
|
|
|
}
|
2020-04-25 02:25:51 +02:00
|
|
|
conn := c.down[id]
|
2020-04-24 19:38:21 +02:00
|
|
|
if conn == nil {
|
|
|
|
log.Printf("Deleting unknown connection")
|
|
|
|
return
|
|
|
|
}
|
2020-04-28 14:54:50 +02:00
|
|
|
|
|
|
|
for _, track := range conn.tracks {
|
|
|
|
found := track.remote.delLocal(track)
|
|
|
|
if !found {
|
|
|
|
log.Printf("Couldn't find remote track")
|
|
|
|
}
|
|
|
|
track.remote = nil
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
conn.pc.Close()
|
|
|
|
delete(c.down, id)
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:54:50 +02:00
|
|
|
func addDownTrack(c *client, id string, remoteTrack *upTrack, remoteConn *upConnection) (*downConnection, *webrtc.RTPSender, error) {
|
2020-04-24 19:38:21 +02:00
|
|
|
conn := getDownConn(c, id)
|
|
|
|
if conn == nil {
|
|
|
|
var err error
|
2020-04-28 14:54:50 +02:00
|
|
|
conn, err = addDownConn(c, id, remoteConn)
|
2020-04-24 19:38:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:54:50 +02:00
|
|
|
s, err := conn.pc.AddTrack(local)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-04-26 01:33:18 +02:00
|
|
|
|
2020-04-28 15:26:50 +02:00
|
|
|
track := &downTrack{
|
|
|
|
track: local,
|
|
|
|
remote: remoteTrack,
|
|
|
|
maxBitrate: new(timeStampedBitrate),
|
|
|
|
}
|
2020-04-28 14:54:50 +02:00
|
|
|
conn.tracks = append(conn.tracks, track)
|
|
|
|
remoteTrack.addLocal(track)
|
|
|
|
|
2020-04-29 03:03:47 +02:00
|
|
|
go rtcpDownListener(c.group, conn, track, s)
|
2020-04-24 19:38:21 +02:00
|
|
|
|
|
|
|
return conn, s, nil
|
|
|
|
}
|
|
|
|
|
2020-04-26 01:33:18 +02:00
|
|
|
var epoch = time.Now()
|
|
|
|
|
|
|
|
func msSinceEpoch() uint64 {
|
|
|
|
return uint64(time.Since(epoch) / time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2020-04-29 03:03:47 +02:00
|
|
|
func rtcpDownListener(g *group, conn *downConnection, track *downTrack, s *webrtc.RTPSender) {
|
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-29 20:31:49 +02:00
|
|
|
if track.muted() {
|
|
|
|
continue
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
case *rtcp.ReceiverEstimatedMaximumBitrate:
|
2020-04-26 01:33:18 +02:00
|
|
|
ms := msSinceEpoch()
|
|
|
|
// 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(
|
2020-04-28 17:17:52 +02:00
|
|
|
&track.maxBitrate.bitrate,
|
2020-04-26 01:33:18 +02:00
|
|
|
p.Bitrate,
|
|
|
|
)
|
|
|
|
atomic.StoreUint64(
|
2020-04-28 17:17:52 +02:00
|
|
|
&track.maxBitrate.timestamp,
|
2020-04-26 01:33:18 +02:00
|
|
|
uint64(ms),
|
|
|
|
)
|
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() {
|
|
|
|
atomic.StoreUint32(
|
|
|
|
&track.loss,
|
|
|
|
uint32(r.FractionLost),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2020-04-27 21:43:29 +02:00
|
|
|
case *rtcp.TransportLayerNack:
|
2020-04-29 20:31:49 +02:00
|
|
|
if track.muted() {
|
|
|
|
continue
|
|
|
|
}
|
2020-04-27 21:43:29 +02:00
|
|
|
sendRecovery(p, track)
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func trackKinds(down *downConnection) (audio bool, video bool) {
|
|
|
|
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-04-28 14:54:50 +02:00
|
|
|
func updateUpBitrate(up *upConnection) {
|
2020-04-26 01:33:18 +02:00
|
|
|
now := msSinceEpoch()
|
|
|
|
|
2020-04-28 14:54:50 +02:00
|
|
|
for _, track := range up.tracks {
|
2020-04-28 15:26:50 +02:00
|
|
|
track.maxBitrate = ^uint64(0)
|
2020-04-28 14:54:50 +02:00
|
|
|
local := track.getLocal()
|
|
|
|
for _, l := range local {
|
|
|
|
ms := atomic.LoadUint64(&l.maxBitrate.timestamp)
|
|
|
|
bitrate := atomic.LoadUint64(&l.maxBitrate.bitrate)
|
2020-04-29 18:49:09 +02:00
|
|
|
loss := atomic.LoadUint32(&l.loss)
|
2020-04-28 23:41:18 +02:00
|
|
|
if now < ms || now > ms+5000 || bitrate == 0 {
|
2020-04-29 18:49:09 +02:00
|
|
|
// no rate information
|
2020-04-28 15:26:50 +02:00
|
|
|
l.setMuted(false)
|
2020-04-28 14:54:50 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-04-29 18:49:09 +02:00
|
|
|
|
|
|
|
isvideo := l.track.Kind() == webrtc.RTPCodecTypeVideo
|
|
|
|
minrate1 := uint64(9600)
|
|
|
|
minrate2 := uint64(19200)
|
|
|
|
if isvideo {
|
|
|
|
minrate1 = 256000
|
|
|
|
minrate2 = 512000
|
|
|
|
}
|
|
|
|
if bitrate < minrate2 {
|
|
|
|
if loss <= 13 {
|
|
|
|
// less than 10% loss, go ahead
|
|
|
|
bitrate = minrate2
|
|
|
|
} else if loss <= 64 || !isvideo {
|
|
|
|
if bitrate < minrate1 {
|
|
|
|
bitrate = minrate1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// video track with dramatic loss
|
|
|
|
l.setMuted(true)
|
|
|
|
continue
|
|
|
|
}
|
2020-04-28 15:26:50 +02:00
|
|
|
}
|
|
|
|
l.setMuted(false)
|
2020-04-28 14:54:50 +02:00
|
|
|
if track.maxBitrate > bitrate {
|
|
|
|
track.maxBitrate = bitrate
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-28 14:54:50 +02:00
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 20:15:24 +02:00
|
|
|
func (up *upConnection) sendPLI(track *upTrack) error {
|
|
|
|
last := atomic.LoadUint64(&track.lastPLI)
|
|
|
|
now := msSinceEpoch()
|
2020-04-28 23:41:18 +02:00
|
|
|
if now >= last && now-last < 200 {
|
2020-04-28 20:15:24 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
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-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 {
|
|
|
|
return sendNACK(up.pc, track.track.SSRC(), first, bitmap)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-27 21:43:29 +02:00
|
|
|
func sendRecovery(p *rtcp.TransportLayerNack, track *downTrack) {
|
|
|
|
var packet rtp.Packet
|
|
|
|
for _, nack := range p.Nacks {
|
|
|
|
for _, seqno := range nack.PacketList() {
|
2020-04-29 11:06:39 +02:00
|
|
|
raw := track.remote.cache.Get(seqno)
|
2020-04-27 21:43:29 +02:00
|
|
|
if raw != nil {
|
|
|
|
err := packet.Unmarshal(raw)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = track.track.WriteRTP(&packet)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("%v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
func countMediaStreams(data string) (int, error) {
|
|
|
|
desc := sdp.NewJSEPSessionDescription(false)
|
|
|
|
err := desc.Unmarshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return len(desc.MediaDescriptions), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func negotiate(c *client, id string, pc *webrtc.PeerConnection) error {
|
|
|
|
offer, err := pc.CreateOffer(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = pc.SetLocalDescription(offer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.write(clientMessage{
|
|
|
|
Type: "offer",
|
|
|
|
Id: id,
|
|
|
|
Offer: &offer,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendICE(c *client, id string, candidate *webrtc.ICECandidate) error {
|
|
|
|
if candidate == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
cand := candidate.ToJSON()
|
|
|
|
return c.write(clientMessage{
|
|
|
|
Type: "ice",
|
|
|
|
Id: id,
|
|
|
|
Candidate: &cand,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func gotOffer(c *client, offer webrtc.SessionDescription, id string) error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
n, err := countMediaStreams(offer.SDP)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Couldn't parse SDP: %v", err)
|
|
|
|
n = 2
|
|
|
|
}
|
2020-04-26 00:39:07 +02:00
|
|
|
up.trackCount = n
|
2020-04-24 19:38:21 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.write(clientMessage{
|
|
|
|
Type: "answer",
|
|
|
|
Id: id,
|
|
|
|
Answer: &answer,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func gotAnswer(c *client, answer webrtc.SessionDescription, id string) error {
|
|
|
|
conn := getDownConn(c, id)
|
|
|
|
if conn == nil {
|
|
|
|
return protocolError("unknown id in answer")
|
|
|
|
}
|
|
|
|
err := conn.pc.SetRemoteDescription(answer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func gotICE(c *client, candidate *webrtc.ICECandidateInit, id string) error {
|
|
|
|
var pc *webrtc.PeerConnection
|
|
|
|
down := getDownConn(c, id)
|
|
|
|
if down != nil {
|
|
|
|
pc = down.pc
|
|
|
|
} else {
|
|
|
|
up := getUpConn(c, id)
|
|
|
|
if up == nil {
|
|
|
|
return errors.New("unknown id in ICE")
|
|
|
|
}
|
|
|
|
pc = up.pc
|
|
|
|
}
|
|
|
|
return pc.AddICECandidate(*candidate)
|
|
|
|
}
|
|
|
|
|
|
|
|
func clientLoop(c *client, conn *websocket.Conn) error {
|
|
|
|
read := make(chan interface{}, 1)
|
|
|
|
go clientReader(conn, read, c.done)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if c.down != nil {
|
|
|
|
for id := range c.down {
|
|
|
|
c.write(clientMessage{
|
|
|
|
Type: "close",
|
|
|
|
Id: id,
|
|
|
|
})
|
|
|
|
delDownConn(c, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.up != nil {
|
|
|
|
for id := range c.up {
|
|
|
|
delUpConn(c, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
g := c.group
|
|
|
|
|
2020-04-25 02:25:51 +02:00
|
|
|
c.write(clientMessage{
|
|
|
|
Type: "permissions",
|
|
|
|
Permissions: c.permissions,
|
|
|
|
})
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
for _, cc := range g.getClients(c) {
|
|
|
|
cc.action(pushTracksAction{c})
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
case addTrackAction:
|
|
|
|
down, _, err :=
|
|
|
|
addDownTrack(
|
2020-04-28 14:54:50 +02:00
|
|
|
c, a.remote.id, a.track,
|
2020-04-24 19:38:21 +02:00
|
|
|
a.remote)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if a.done {
|
2020-04-28 14:54:50 +02:00
|
|
|
err = negotiate(c, a.remote.id, down.pc)
|
2020-04-24 19:38:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-04-28 14:54:50 +02:00
|
|
|
case delConnAction:
|
2020-04-24 19:38:21 +02:00
|
|
|
c.write(clientMessage{
|
|
|
|
Type: "close",
|
|
|
|
Id: a.id,
|
|
|
|
})
|
|
|
|
delDownConn(c, a.id)
|
|
|
|
case addLabelAction:
|
|
|
|
c.write(clientMessage{
|
|
|
|
Type: "label",
|
|
|
|
Id: a.id,
|
|
|
|
Value: a.label,
|
|
|
|
})
|
|
|
|
case pushTracksAction:
|
|
|
|
for _, u := range c.up {
|
|
|
|
var done bool
|
2020-04-28 14:54:50 +02:00
|
|
|
for i, t := range u.tracks {
|
2020-04-26 00:39:07 +02:00
|
|
|
done = i >= u.trackCount-1
|
2020-04-24 19:38:21 +02:00
|
|
|
a.c.action(addTrackAction{
|
2020-04-28 14:54:50 +02:00
|
|
|
t, u, done,
|
2020-04-24 19:38:21 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if done && u.label != "" {
|
|
|
|
a.c.action(addLabelAction{
|
|
|
|
u.id, u.label,
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
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 {
|
|
|
|
c.write(clientMessage{
|
|
|
|
Type: "abort",
|
2020-04-25 19:58:54 +02:00
|
|
|
Id: id,
|
2020-04-25 18:35:32 +02:00
|
|
|
})
|
|
|
|
delUpConn(c, id)
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleClientMessage(c *client, m clientMessage) error {
|
|
|
|
switch m.Type {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
err := gotOffer(c, *m.Offer, m.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case "answer":
|
|
|
|
if m.Answer == nil {
|
|
|
|
return protocolError("null answer")
|
|
|
|
}
|
|
|
|
err := gotAnswer(c, *m.Answer, m.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case "close":
|
|
|
|
delUpConn(c, m.Id)
|
|
|
|
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 {
|
|
|
|
cc.write(m)
|
|
|
|
}
|
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-04-25 17:36:35 +02:00
|
|
|
c.error(userError("not authorised"))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := setPermission(c.group, m.Id, m.Type)
|
|
|
|
if err != nil {
|
|
|
|
return c.error(err)
|
|
|
|
}
|
|
|
|
case "kick":
|
2020-04-25 18:09:31 +02:00
|
|
|
if !c.permissions.Op {
|
2020-04-25 17:36:35 +02:00
|
|
|
c.error(userError("not authorised"))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendRateUpdate(c *client) {
|
2020-04-28 14:54:50 +02:00
|
|
|
type remb struct {
|
|
|
|
pc *webrtc.PeerConnection
|
|
|
|
ssrc uint32
|
|
|
|
bitrate uint64
|
|
|
|
}
|
|
|
|
rembs := make([]remb, 0)
|
|
|
|
|
|
|
|
c.mu.Lock()
|
2020-04-24 19:38:21 +02:00
|
|
|
for _, u := range c.up {
|
2020-04-28 14:54:50 +02:00
|
|
|
updateUpBitrate(u)
|
|
|
|
for _, t := range u.tracks {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|