Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
sfu
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alain Takoudjou
sfu
Commits
2347417f
Commit
2347417f
authored
Sep 18, 2020
by
Juliusz Chroboczek
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'modular' into master
parents
714a0939
709a6857
Changes
12
Show whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
585 additions
and
356 deletions
+585
-356
.gitignore
.gitignore
+1
-0
conn/conn.go
conn/conn.go
+48
-0
disk/disk.go
disk/disk.go
+39
-41
group/client.go
group/client.go
+29
-0
group/group.go
group/group.go
+124
-81
rtpconn/rtpconn.go
rtpconn/rtpconn.go
+49
-52
rtpconn/rtpstats.go
rtpconn/rtpstats.go
+74
-0
rtpconn/rtpwriter.go
rtpconn/rtpwriter.go
+12
-11
rtpconn/webclient.go
rtpconn/webclient.go
+92
-132
sfu.go
sfu.go
+7
-7
stats/stats.go
stats/stats.go
+73
-0
webserver.go
webserver.go
+37
-32
No files found.
.gitignore
View file @
2347417f
...
...
@@ -3,3 +3,4 @@ data/*.pem
sfu
passwd
groups/*.json
static/*.d.ts
conn.go
→
conn
/conn
.go
View file @
2347417f
...
...
@@ -3,7 +3,8 @@
// 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
// Package conn defines interfaces for connections and tracks.
package
conn
import
(
"errors"
...
...
@@ -15,29 +16,33 @@ import (
var
ErrConnectionClosed
=
errors
.
New
(
"connection is closed"
)
var
ErrKeyframeNeeded
=
errors
.
New
(
"keyframe needed"
)
type
upConnection
interface
{
addLocal
(
downConnection
)
error
delLocal
(
downConnection
)
bool
// Type Up represents a connection in the client to server direction.
type
Up
interface
{
AddLocal
(
Down
)
error
DelLocal
(
Down
)
bool
Id
()
string
Label
()
string
}
type
upTrack
interface
{
addLocal
(
downTrack
)
error
delLocal
(
downTrack
)
bool
// Type UpTrack represents a track in the client to server direction.
type
UpTrack
interface
{
AddLocal
(
DownTrack
)
error
DelLocal
(
DownTrack
)
bool
Label
()
string
Codec
()
*
webrtc
.
RTPCodec
// get a recent packet. Returns 0 if the packet is not in cache.
g
etRTP
(
seqno
uint16
,
result
[]
byte
)
uint16
G
etRTP
(
seqno
uint16
,
result
[]
byte
)
uint16
}
type
downConnection
interface
{
// Type Down represents a connection in the server to client direction.
type
Down
interface
{
GetMaxBitrate
(
now
uint64
)
uint64
}
type
downTrack
interface
{
// Type DownTrack represents a track in the server to client direction.
type
DownTrack
interface
{
WriteRTP
(
packat
*
rtp
.
Packet
)
error
Accumulate
(
bytes
uint32
)
s
etTimeOffset
(
ntp
uint64
,
rtp
uint32
)
s
etCname
(
string
)
S
etTimeOffset
(
ntp
uint64
,
rtp
uint32
)
S
etCname
(
string
)
}
disk.go
→
disk
/disk
.go
View file @
2347417f
package
main
package
disk
import
(
crand
"crypto/rand"
"errors"
"fmt"
"os"
"path/filepath"
"
strconv
"
"
encoding/hex
"
"sync"
"time"
...
...
@@ -14,10 +15,15 @@ import (
"github.com/pion/rtp/codecs"
"github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3/pkg/media/samplebuilder"
"sfu/conn"
"sfu/group"
)
type
diskClient
struct
{
group
*
group
var
Directory
string
type
Client
struct
{
group
*
group
.
Group
id
string
mu
sync
.
Mutex
...
...
@@ -25,45 +31,37 @@ type diskClient struct {
closed
bool
}
var
idCounter
struct
{
mu
sync
.
Mutex
counter
int
}
func
newId
()
string
{
idCounter
.
mu
.
Lock
()
defer
idCounter
.
mu
.
Unlock
()
s
:=
strconv
.
FormatInt
(
int64
(
idCounter
.
counter
),
16
)
idCounter
.
counter
++
return
s
b
:=
make
([]
byte
,
16
)
crand
.
Read
(
b
)
return
hex
.
EncodeToString
(
b
)
}
func
New
DiskClient
(
g
*
group
)
*
disk
Client
{
return
&
disk
Client
{
group
:
g
,
id
:
newId
()}
func
New
(
g
*
group
.
Group
)
*
Client
{
return
&
Client
{
group
:
g
,
id
:
newId
()}
}
func
(
client
*
diskClient
)
Group
()
*
g
roup
{
func
(
client
*
Client
)
Group
()
*
group
.
G
roup
{
return
client
.
group
}
func
(
client
*
disk
Client
)
Id
()
string
{
func
(
client
*
Client
)
Id
()
string
{
return
client
.
id
}
func
(
client
*
diskClient
)
Credentials
()
c
lientCredentials
{
return
c
lientCredentials
{
"RECORDING"
,
""
}
func
(
client
*
Client
)
Credentials
()
group
.
C
lientCredentials
{
return
group
.
C
lientCredentials
{
"RECORDING"
,
""
}
}
func
(
client
*
diskClient
)
SetPermissions
(
perms
c
lientPermissions
)
{
func
(
client
*
Client
)
SetPermissions
(
perms
group
.
C
lientPermissions
)
{
return
}
func
(
client
*
diskClient
)
p
ushClient
(
id
,
username
string
,
add
bool
)
error
{
func
(
client
*
Client
)
P
ushClient
(
id
,
username
string
,
add
bool
)
error
{
return
nil
}
func
(
client
*
disk
Client
)
Close
()
error
{
func
(
client
*
Client
)
Close
()
error
{
client
.
mu
.
Lock
()
defer
client
.
mu
.
Unlock
()
...
...
@@ -75,13 +73,13 @@ func (client *diskClient) Close() error {
return
nil
}
func
(
client
*
disk
Client
)
kick
(
message
string
)
error
{
func
(
client
*
Client
)
kick
(
message
string
)
error
{
err
:=
client
.
Close
()
d
elClient
(
client
)
group
.
D
elClient
(
client
)
return
err
}
func
(
client
*
diskClient
)
pushConn
(
id
string
,
conn
upConnection
,
tracks
[]
u
pTrack
,
label
string
)
error
{
func
(
client
*
Client
)
PushConn
(
id
string
,
up
conn
.
Up
,
tracks
[]
conn
.
U
pTrack
,
label
string
)
error
{
client
.
mu
.
Lock
()
defer
client
.
mu
.
Unlock
()
...
...
@@ -95,11 +93,11 @@ func (client *diskClient) pushConn(id string, conn upConnection, tracks []upTrac
delete
(
client
.
down
,
id
)
}
if
conn
==
nil
{
if
up
==
nil
{
return
nil
}
directory
:=
filepath
.
Join
(
recordingsDir
,
client
.
group
.
name
)
directory
:=
filepath
.
Join
(
Directory
,
client
.
group
.
Name
()
)
err
:=
os
.
MkdirAll
(
directory
,
0700
)
if
err
!=
nil
{
return
err
...
...
@@ -109,12 +107,12 @@ func (client *diskClient) pushConn(id string, conn upConnection, tracks []upTrac
client
.
down
=
make
(
map
[
string
]
*
diskConn
)
}
down
,
err
:=
newDiskConn
(
directory
,
label
,
conn
,
tracks
)
down
,
err
:=
newDiskConn
(
directory
,
label
,
up
,
tracks
)
if
err
!=
nil
{
return
err
}
client
.
down
[
conn
.
Id
()]
=
down
client
.
down
[
up
.
Id
()]
=
down
return
nil
}
...
...
@@ -125,7 +123,7 @@ type diskConn struct {
mu
sync
.
Mutex
file
*
os
.
File
remote
upConnection
remote
conn
.
Up
tracks
[]
*
diskTrack
width
,
height
uint32
}
...
...
@@ -150,7 +148,7 @@ func (conn *diskConn) reopen() error {
}
func
(
conn
*
diskConn
)
Close
()
error
{
conn
.
remote
.
d
elLocal
(
conn
)
conn
.
remote
.
D
elLocal
(
conn
)
conn
.
mu
.
Lock
()
tracks
:=
make
([]
*
diskTrack
,
0
,
len
(
conn
.
tracks
))
...
...
@@ -164,7 +162,7 @@ func (conn *diskConn) Close() error {
conn
.
mu
.
Unlock
()
for
_
,
t
:=
range
tracks
{
t
.
remote
.
d
elLocal
(
t
)
t
.
remote
.
D
elLocal
(
t
)
}
return
nil
}
...
...
@@ -196,7 +194,7 @@ func openDiskFile(directory, label string) (*os.File, error) {
}
type
diskTrack
struct
{
remote
u
pTrack
remote
conn
.
U
pTrack
conn
*
diskConn
writer
webm
.
BlockWriteCloser
...
...
@@ -206,7 +204,7 @@ type diskTrack struct {
origin
uint64
}
func
newDiskConn
(
directory
,
label
string
,
up
upConnection
,
remoteTracks
[]
u
pTrack
)
(
*
diskConn
,
error
)
{
func
newDiskConn
(
directory
,
label
string
,
up
conn
.
Up
,
remoteTracks
[]
conn
.
U
pTrack
)
(
*
diskConn
,
error
)
{
conn
:=
diskConn
{
directory
:
directory
,
label
:
label
,
...
...
@@ -231,10 +229,10 @@ func newDiskConn(directory, label string, up upConnection, remoteTracks []upTrac
conn
:
&
conn
,
}
conn
.
tracks
=
append
(
conn
.
tracks
,
track
)
remote
.
a
ddLocal
(
track
)
remote
.
A
ddLocal
(
track
)
}
err
:=
up
.
a
ddLocal
(
&
conn
)
err
:=
up
.
A
ddLocal
(
&
conn
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -242,10 +240,10 @@ func newDiskConn(directory, label string, up upConnection, remoteTracks []upTrac
return
&
conn
,
nil
}
func
(
t
*
diskTrack
)
s
etTimeOffset
(
ntp
uint64
,
rtp
uint32
)
{
func
(
t
*
diskTrack
)
S
etTimeOffset
(
ntp
uint64
,
rtp
uint32
)
{
}
func
(
t
*
diskTrack
)
s
etCname
(
string
)
{
func
(
t
*
diskTrack
)
S
etCname
(
string
)
{
}
func
clonePacket
(
packet
*
rtp
.
Packet
)
*
rtp
.
Packet
{
...
...
@@ -310,7 +308,7 @@ func (t *diskTrack) WriteRTP(packet *rtp.Packet) error {
if
t
.
writer
==
nil
{
if
!
keyframe
{
return
ErrKeyframeNeeded
return
conn
.
ErrKeyframeNeeded
}
return
nil
}
...
...
client.go
→
group/
client.go
View file @
2347417f
package
main
package
group
type
clientCredentials
struct
{
import
(
"sfu/conn"
)
type
ClientCredentials
struct
{
Username
string
`json:"username,omitempty"`
Password
string
`json:"password,omitempty"`
}
type
c
lientPermissions
struct
{
type
C
lientPermissions
struct
{
Op
bool
`json:"op,omitempty"`
Present
bool
`json:"present,omitempty"`
Record
bool
`json:"record,omitempty"`
}
type
c
lient
interface
{
Group
()
*
g
roup
type
C
lient
interface
{
Group
()
*
G
roup
Id
()
string
Credentials
()
c
lientCredentials
SetPermissions
(
c
lientPermissions
)
pushConn
(
id
string
,
conn
upConnection
,
tracks
[]
u
pTrack
,
label
string
)
error
p
ushClient
(
id
,
username
string
,
add
bool
)
error
Credentials
()
C
lientCredentials
SetPermissions
(
C
lientPermissions
)
PushConn
(
id
string
,
conn
conn
.
Up
,
tracks
[]
conn
.
U
pTrack
,
label
string
)
error
P
ushClient
(
id
,
username
string
,
add
bool
)
error
}
type
k
ickable
interface
{
k
ick
(
message
string
)
error
type
K
ickable
interface
{
K
ick
(
message
string
)
error
}
group.go
→
group
/group
.go
View file @
2347417f
...
...
@@ -3,7 +3,7 @@
// 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
package
group
import
(
"encoding/json"
...
...
@@ -18,18 +18,60 @@ import (
"github.com/pion/webrtc/v3"
)
type
chatHistoryEntry
struct
{
id
string
user
string
kind
string
value
string
var
Directory
string
type
UserError
string
func
(
err
UserError
)
Error
()
string
{
return
string
(
err
)
}
type
ProtocolError
string
func
(
err
ProtocolError
)
Error
()
string
{
return
string
(
err
)
}
var
IceFilename
string
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
ChatHistoryEntry
struct
{
Id
string
User
string
Kind
string
Value
string
}
const
(
m
inBitrate
=
200000
M
inBitrate
=
200000
)
type
g
roup
struct
{
type
G
roup
struct
{
name
string
mu
sync
.
Mutex
...
...
@@ -37,35 +79,39 @@ type group struct {
// indicates that the group no longer exists, but it still has clients
dead
bool
locked
bool
clients
map
[
string
]
client
history
[]
chatHistoryEntry
clients
map
[
string
]
Client
history
[]
ChatHistoryEntry
}
func
(
g
*
Group
)
Name
()
string
{
return
g
.
name
}
func
(
g
*
g
roup
)
Locked
()
bool
{
func
(
g
*
G
roup
)
Locked
()
bool
{
g
.
mu
.
Lock
()
defer
g
.
mu
.
Unlock
()
return
g
.
locked
}
func
(
g
*
g
roup
)
SetLocked
(
locked
bool
)
{
func
(
g
*
G
roup
)
SetLocked
(
locked
bool
)
{
g
.
mu
.
Lock
()
defer
g
.
mu
.
Unlock
()
g
.
locked
=
locked
}
func
(
g
*
g
roup
)
Public
()
bool
{
func
(
g
*
G
roup
)
Public
()
bool
{
g
.
mu
.
Lock
()
defer
g
.
mu
.
Unlock
()
return
g
.
description
.
Public
}
func
(
g
*
g
roup
)
Redirect
()
string
{
func
(
g
*
G
roup
)
Redirect
()
string
{
g
.
mu
.
Lock
()
defer
g
.
mu
.
Unlock
()
return
g
.
description
.
Redirect
}
func
(
g
*
g
roup
)
AllowRecording
()
bool
{
func
(
g
*
G
roup
)
AllowRecording
()
bool
{
g
.
mu
.
Lock
()
defer
g
.
mu
.
Unlock
()
return
g
.
description
.
AllowRecording
...
...
@@ -73,20 +119,20 @@ func (g *group) AllowRecording() bool {
var
groups
struct
{
mu
sync
.
Mutex
groups
map
[
string
]
*
g
roup
groups
map
[
string
]
*
G
roup
api
*
webrtc
.
API
}
func
(
g
*
g
roup
)
API
()
*
webrtc
.
API
{
func
(
g
*
G
roup
)
API
()
*
webrtc
.
API
{
return
groups
.
api
}
func
addGroup
(
name
string
,
desc
*
groupDescription
)
(
*
g
roup
,
error
)
{
func
Add
(
name
string
,
desc
*
groupDescription
)
(
*
G
roup
,
error
)
{
groups
.
mu
.
Lock
()
defer
groups
.
mu
.
Unlock
()
if
groups
.
groups
==
nil
{
groups
.
groups
=
make
(
map
[
string
]
*
g
roup
)
groups
.
groups
=
make
(
map
[
string
]
*
G
roup
)
s
:=
webrtc
.
SettingEngine
{}
m
:=
webrtc
.
MediaEngine
{}
m
.
RegisterCodec
(
webrtc
.
NewRTPVP8CodecExt
(
...
...
@@ -113,15 +159,15 @@ func addGroup(name string, desc *groupDescription) (*group, error) {
g
:=
groups
.
groups
[
name
]
if
g
==
nil
{
if
desc
==
nil
{
desc
,
err
=
g
etDescription
(
name
)
desc
,
err
=
G
etDescription
(
name
)
if
err
!=
nil
{
return
nil
,
err
}
}
g
=
&
g
roup
{
g
=
&
G
roup
{
name
:
name
,
description
:
desc
,
clients
:
make
(
map
[
string
]
c
lient
),
clients
:
make
(
map
[
string
]
C
lient
),
}
groups
.
groups
[
name
]
=
g
return
g
,
nil
...
...
@@ -147,7 +193,7 @@ func addGroup(name string, desc *groupDescription) (*group, error) {
return
nil
,
err
}
if
changed
{
desc
,
err
:=
g
etDescription
(
name
)
desc
,
err
:=
G
etDescription
(
name
)
if
err
!=
nil
{
if
!
os
.
IsNotExist
(
err
)
{
log
.
Printf
(
"Reading group %v: %v"
,
...
...
@@ -167,7 +213,7 @@ func addGroup(name string, desc *groupDescription) (*group, error) {
return
g
,
nil
}
func
rangeGroups
(
f
func
(
g
*
g
roup
)
bool
)
{
func
Range
(
f
func
(
g
*
G
roup
)
bool
)
{
groups
.
mu
.
Lock
()
defer
groups
.
mu
.
Unlock
()
...
...
@@ -179,17 +225,17 @@ func rangeGroups(f func(g *group) bool) {
}
}
func
getGroup
Names
()
[]
string
{
func
Get
Names
()
[]
string
{
names
:=
make
([]
string
,
0
)
rangeGroups
(
func
(
g
*
g
roup
)
bool
{
Range
(
func
(
g
*
G
roup
)
bool
{
names
=
append
(
names
,
g
.
name
)
return
true
})
return
names
}
func
getGroup
(
name
string
)
*
g
roup
{
func
Get
(
name
string
)
*
G
roup
{
groups
.
mu
.
Lock
()
defer
groups
.
mu
.
Unlock
()
...
...
@@ -210,8 +256,8 @@ func delGroupUnlocked(name string) bool {
return
true
}
func
addClient
(
name
string
,
c
client
)
(
*
g
roup
,
error
)
{
g
,
err
:=
addGroup
(
name
,
nil
)
func
AddClient
(
name
string
,
c
Client
)
(
*
G
roup
,
error
)
{
g
,
err
:=
Add
(
name
,
nil
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -219,7 +265,7 @@ func addClient(name string, c client) (*group, error) {
g
.
mu
.
Lock
()
defer
g
.
mu
.
Unlock
()
perms
,
err
:=
g
etPermission
(
g
.
description
,
c
.
Credentials
())
perms
,
err
:=
g
.
description
.
GetPermission
(
c
.
Credentials
())
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -227,37 +273,34 @@ func addClient(name string, c client) (*group, error) {
c
.
SetPermissions
(
perms
)
if
!
perms
.
Op
&&
g
.
locked
{
return
nil
,
u
serError
(
"group is locked"
)
return
nil
,
U
serError
(
"group is locked"
)
}
if
!
perms
.
Op
&&
g
.
description
.
MaxClients
>
0
{
if
len
(
g
.
clients
)
>=
g
.
description
.
MaxClients
{
return
nil
,
u
serError
(
"too many users"
)
return
nil
,
U
serError
(
"too many users"
)
}
}
if
g
.
clients
[
c
.
Id
()]
!=
nil
{
return
nil
,
p
rotocolError
(
"duplicate client id"
)
return
nil
,
P
rotocolError
(
"duplicate client id"
)
}
g
.
clients
[
c
.
Id
()]
=
c
go
func
(
clients
[]
c
lient
)
{
go
func
(
clients
[]
C
lient
)
{
u
:=
c
.
Credentials
()
.
Username
c
.
p
ushClient
(
c
.
Id
(),
u
,
true
)
c
.
P
ushClient
(
c
.
Id
(),
u
,
true
)
for
_
,
cc
:=
range
clients
{
uu
:=
cc
.
Credentials
()
.
Username
err
:=
c
.
pushClient
(
cc
.
Id
(),
uu
,
true
)
if
err
==
ErrClientDead
{
return
}
cc
.
pushClient
(
c
.
Id
(),
u
,
true
)
c
.
PushClient
(
cc
.
Id
(),
uu
,
true
)
cc
.
PushClient
(
c
.
Id
(),
u
,
true
)
}
}(
g
.
getClientsUnlocked
(
c
))
return
g
,
nil
}
func
delClient
(
c
c
lient
)
{
func
DelClient
(
c
C
lient
)
{
g
:=
c
.
Group
()
if
g
==
nil
{
return
...
...
@@ -271,21 +314,21 @@ func delClient(c client) {
}
delete
(
g
.
clients
,
c
.
Id
())
go
func
(
clients
[]
c
lient
)
{
go
func
(
clients
[]
C
lient
)
{
for
_
,
cc
:=
range
clients
{
cc
.
p
ushClient
(
c
.
Id
(),
c
.
Credentials
()
.
Username
,
false
)
cc
.
P
ushClient
(
c
.
Id
(),
c
.
Credentials
()
.
Username
,
false
)
}
}(
g
.
getClientsUnlocked
(
nil
))
}
func
(
g
*
group
)
getClients
(
except
client
)
[]
c
lient
{
func
(
g
*
Group
)
GetClients
(
except
Client
)
[]
C
lient
{
g
.
mu
.
Lock
()
defer
g
.
mu
.
Unlock
()
return
g
.
getClientsUnlocked
(
except
)
}
func
(
g
*
group
)
getClientsUnlocked
(
except
client
)
[]
c
lient
{
clients
:=
make
([]
c
lient
,
0
,
len
(
g
.
clients
))
func
(
g
*
Group
)
getClientsUnlocked
(
except
Client
)
[]
C
lient
{
clients
:=
make
([]
C
lient
,
0
,
len
(
g
.
clients
))
for
_
,
c
:=
range
g
.
clients
{
if
c
!=
except
{
clients
=
append
(
clients
,
c
)
...
...
@@ -294,13 +337,13 @@ func (g *group) getClientsUnlocked(except client) []client {
return
clients
}
func
(
g
*
group
)
getClient
(
id
string
)
c
lient
{
func
(
g
*
Group
)
GetClient
(
id
string
)
C
lient
{
g
.
mu
.
Lock
()
defer
g
.
mu
.
Unlock
()
return
g
.
getClientUnlocked
(
id
)
}
func
(
g
*
group
)
getClientUnlocked
(
id
string
)
c
lient
{
func
(
g
*
Group
)
getClientUnlocked
(
id
string
)
C
lient
{
for
idd
,
c
:=
range
g
.
clients
{
if
idd
==
id
{
return
c
...
...
@@ -309,7 +352,7 @@ func (g *group) getClientUnlocked(id string) client {
return
nil
}
func
(
g
*
group
)
Range
(
f
func
(
c
c
lient
)
bool
)
{
func
(
g
*
Group
)
Range
(
f
func
(
c
C
lient
)
bool
)
{
g
.
mu
.
Lock
()
defer
g
.
mu
.
Unlock
()
for
_
,
c
:=
range
g
.
clients
{
...
...
@@ -320,11 +363,11 @@ func (g *group) Range(f func(c client) bool) {
}
}
func
(
g
*
group
)
s
hutdown
(
message
string
)
{
g
.
Range
(
func
(
c
c
lient
)
bool
{
cc
,
ok
:=
c
.
(
k
ickable
)
func
(
g
*
Group
)
S
hutdown
(
message
string
)
{
g
.
Range
(
func
(
c
C
lient
)
bool
{
cc
,
ok
:=
c
.
(
K
ickable
)
if
ok
{
cc
.
k
ick
(
message
)
cc
.
K
ick
(
message
)
}
return
true
})
...
...
@@ -332,13 +375,13 @@ func (g *group) shutdown(message string) {
const
maxChatHistory
=
20
func
(
g
*
group
)
c
learChatHistory
()
{
func
(
g
*
Group
)
C
learChatHistory
()
{
g
.
mu
.
Lock
()
defer
g
.
mu
.
Unlock
()
g
.
history
=
nil
}
func
(
g
*
group
)
a
ddToChatHistory
(
id
,
user
,
kind
,
value
string
)
{
func
(
g
*
Group
)
A
ddToChatHistory
(
id
,
user
,
kind
,
value
string
)
{
g
.
mu
.
Lock
()
defer
g
.
mu
.
Unlock
()
...
...
@@ -347,20 +390,20 @@ func (g *group) addToChatHistory(id, user, kind, value string) {
g
.
history
=
g
.
history
[
:
len
(
g
.
history
)
-
1
]
}
g
.
history
=
append
(
g
.
history
,
chatHistoryEntry
{
id
:
id
,
user
:
user
,
kind
:
kind
,
v
alue
:
value
},
ChatHistoryEntry
{
Id
:
id
,
User
:
user
,
Kind
:
kind
,
V
alue
:
value
},
)
}
func
(
g
*
group
)
getChatHistory
()
[]
c
hatHistoryEntry
{
func
(
g
*
Group
)
GetChatHistory
()
[]
C
hatHistoryEntry
{
g
.
mu
.
Lock
()
defer
g
.
mu
.
Unlock
()
h
:=
make
([]
c
hatHistoryEntry
,
len
(
g
.
history
))
h
:=
make
([]
C
hatHistoryEntry
,
len
(
g
.
history
))
copy
(
h
,
g
.
history
)
return
h
}
func
matchUser
(
user
clientCredentials
,
users
[]
c
lientCredentials
)
(
bool
,
bool
)
{
func
matchUser
(
user
ClientCredentials
,
users
[]
C
lientCredentials
)
(
bool
,
bool
)
{
for
_
,
u
:=
range
users
{
if
u
.
Username
==
""
{
if
u
.
Password
==
""
||
u
.
Password
==
user
.
Password
{
...
...
@@ -383,13 +426,13 @@ type groupDescription struct {
MaxClients
int
`json:"max-clients,omitempty"`
AllowAnonymous
bool
`json:"allow-anonymous,omitempty"`
AllowRecording
bool
`json:"allow-recording,omitempty"`
Op
[]
c
lientCredentials
`json:"op,omitempty"`
Presenter
[]
c
lientCredentials
`json:"presenter,omitempty"`
Other
[]
c
lientCredentials
`json:"other,omitempty"`
Op
[]
C
lientCredentials
`json:"op,omitempty"`
Presenter
[]
C
lientCredentials
`json:"presenter,omitempty"`
Other
[]
C
lientCredentials
`json:"other,omitempty"`
}
func
descriptionChanged
(
name
string
,
old
*
groupDescription
)
(
bool
,
error
)
{
fi
,
err
:=
os
.
Stat
(
filepath
.
Join
(
groupsDir
,
name
+
".json"
))
fi
,
err
:=
os
.
Stat
(
filepath
.
Join
(
Directory
,
name
+
".json"
))
if
err
!=
nil
{
return
false
,
err
}
...
...
@@ -399,8 +442,8 @@ func descriptionChanged(name string, old *groupDescription) (bool, error) {
return
false
,
err
}
func
g
etDescription
(
name
string
)
(
*
groupDescription
,
error
)
{
r
,
err
:=
os
.
Open
(
filepath
.
Join
(
groupsDir
,
name
+
".json"
))
func
G
etDescription
(
name
string
)
(
*
groupDescription
,
error
)
{
r
,
err
:=
os
.
Open
(
filepath
.
Join
(
Directory
,
name
+
".json"
))
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -424,10 +467,10 @@ func getDescription(name string) (*groupDescription, error) {
return
&
desc
,
nil
}
func
getPermission
(
desc
*
groupDescription
,
creds
clientCredentials
)
(
c
lientPermissions
,
error
)
{
var
p
c
lientPermissions
func
(
desc
*
groupDescription
)
GetPermission
(
creds
ClientCredentials
)
(
C
lientPermissions
,
error
)
{
var
p
C
lientPermissions
if
!
desc
.
AllowAnonymous
&&
creds
.
Username
==
""
{
return
p
,
u
serError
(
"anonymous users not allowed in this group, please choose a username"
)
return
p
,
U
serError
(
"anonymous users not allowed in this group, please choose a username"
)
}
if
found
,
good
:=
matchUser
(
creds
,
desc
.
Op
);
found
{
if
good
{
...
...
@@ -438,34 +481,34 @@ func getPermission(desc *groupDescription, creds clientCredentials) (clientPermi
}
return
p
,
nil
}
return
p
,
u
serError
(
"not authorised"
)
return
p
,
U
serError
(
"not authorised"
)
}
if
found
,
good
:=
matchUser
(
creds
,
desc
.
Presenter
);
found
{
if
good
{
p
.
Present
=
true
return
p
,
nil
}
return
p
,
u
serError
(
"not authorised"
)
return
p
,
U
serError
(
"not authorised"
)
}
if
found
,
good
:=
matchUser
(
creds
,
desc
.
Other
);
found
{
if
good
{
return
p
,
nil
}
return
p
,
u
serError
(
"not authorised"
)
return
p
,
U
serError
(
"not authorised"
)
}
return
p
,
u
serError
(
"not authorised"
)
return
p
,
U
serError
(
"not authorised"
)
}
type
publicGroup
struct
{
type
Public
struct
{
Name
string
`json:"name"`
ClientCount
int
`json:"clientCount"`
}
func
getPublicGroups
()
[]
publicGroup
{
gs
:=
make
([]
publicGroup
,
0
)
rangeGroups
(
func
(
g
*
g
roup
)
bool
{
func
GetPublic
()
[]
Public
{
gs
:=
make
([]
Public
,
0
)
Range
(
func
(
g
*
G
roup
)
bool
{
if
g
.
Public
()
{
gs
=
append
(
gs
,
publicGroup
{
gs
=
append
(
gs
,
Public
{
Name
:
g
.
name
,
ClientCount
:
len
(
g
.
clients
),
})
...
...
@@ -478,8 +521,8 @@ func getPublicGroups() []publicGroup {
return
gs
}
func
r
eadPublicGroups
()
{
dir
,
err
:=
os
.
Open
(
groupsDir
)
func
R
eadPublicGroups
()
{
dir
,
err
:=
os
.
Open
(
Directory
)
if
err
!=
nil
{
return
}
...
...
@@ -496,7 +539,7 @@ func readPublicGroups() {
continue
}
name
:=
fi
.
Name
()[
:
len
(
fi
.
Name
())
-
5
]
desc
,
err
:=
g
etDescription
(
name
)
desc
,
err
:=
G
etDescription
(
name
)
if
err
!=
nil
{
if
!
os
.
IsNotExist
(
err
)
{
log
.
Printf
(
"Reading group %v: %v"
,
name
,
err
)
...
...
@@ -504,7 +547,7 @@ func readPublicGroups() {
continue
}
if
desc
.
Public
{
addGroup
(
name
,
desc
)
Add
(
name
,
desc
)
}
}
}
rtpconn.go
→
rtpconn
/rtpconn
.go
View file @
2347417f
// 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
package
rtpconn
import
(
"errors"
...
...
@@ -14,14 +9,16 @@ import (
"sync/atomic"
"time"
"github.com/pion/rtcp"
"github.com/pion/rtp"
"github.com/pion/webrtc/v3"
"sfu/conn"
"sfu/estimator"
"sfu/group"
"sfu/jitter"
"sfu/packetcache"
"sfu/rtptime"
"github.com/pion/rtcp"
"github.com/pion/rtp"
"github.com/pion/webrtc/v3"
)
type
bitrate
struct
{
...
...
@@ -71,7 +68,7 @@ type iceConnection interface {
type
rtpDownTrack
struct
{
track
*
webrtc
.
Track
remote
u
pTrack
remote
conn
.
U
pTrack
maxBitrate
*
bitrate
rate
*
estimator
.
Estimator
stats
*
receiverStats
...
...
@@ -91,26 +88,26 @@ func (down *rtpDownTrack) Accumulate(bytes uint32) {
down
.
rate
.
Accumulate
(
bytes
)
}
func
(
down
*
rtpDownTrack
)
s
etTimeOffset
(
ntp
uint64
,
rtp
uint32
)
{
func
(
down
*
rtpDownTrack
)
S
etTimeOffset
(
ntp
uint64
,
rtp
uint32
)
{
atomic
.
StoreUint64
(
&
down
.
remoteNTPTime
,
ntp
)
atomic
.
StoreUint32
(
&
down
.
remoteRTPTime
,
rtp
)
}
func
(
down
*
rtpDownTrack
)
s
etCname
(
cname
string
)
{
func
(
down
*
rtpDownTrack
)
S
etCname
(
cname
string
)
{
down
.
cname
.
Store
(
cname
)
}
type
rtpDownConnection
struct
{
id
string
pc
*
webrtc
.
PeerConnection
remote
upConnection
remote
conn
.
Up
tracks
[]
*
rtpDownTrack
maxREMBBitrate
*
bitrate
iceCandidates
[]
*
webrtc
.
ICECandidateInit
}
func
newDownConn
(
c
client
,
id
string
,
remote
upConnection
)
(
*
rtpDownConnection
,
error
)
{
pc
,
err
:=
c
.
Group
()
.
API
()
.
NewPeerConnection
(
i
ceConfiguration
())
func
newDownConn
(
c
group
.
Client
,
id
string
,
remote
conn
.
Up
)
(
*
rtpDownConnection
,
error
)
{
pc
,
err
:=
c
.
Group
()
.
API
()
.
NewPeerConnection
(
group
.
I
ceConfiguration
())
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -193,7 +190,7 @@ type rtpUpTrack struct {
mu
sync
.
Mutex
cname
string
local
[]
d
ownTrack
local
[]
conn
.
D
ownTrack
srTime
uint64
srNTPTime
uint64
srRTPTime
uint32
...
...
@@ -201,17 +198,17 @@ type rtpUpTrack struct {
type
localTrackAction
struct
{
add
bool
track
d
ownTrack
track
conn
.
D
ownTrack
}
func
(
up
*
rtpUpTrack
)
notifyLocal
(
add
bool
,
track
d
ownTrack
)
{
func
(
up
*
rtpUpTrack
)
notifyLocal
(
add
bool
,
track
conn
.
D
ownTrack
)
{
select
{
case
up
.
localCh
<-
localTrackAction
{
add
,
track
}
:
case
<-
up
.
readerDone
:
}
}
func
(
up
*
rtpUpTrack
)
addLocal
(
local
d
ownTrack
)
error
{
func
(
up
*
rtpUpTrack
)
AddLocal
(
local
conn
.
D
ownTrack
)
error
{
up
.
mu
.
Lock
()
for
_
,
t
:=
range
up
.
local
{
if
t
==
local
{
...
...
@@ -226,7 +223,7 @@ func (up *rtpUpTrack) addLocal(local downTrack) error {
return
nil
}
func
(
up
*
rtpUpTrack
)
delLocal
(
local
d
ownTrack
)
bool
{
func
(
up
*
rtpUpTrack
)
DelLocal
(
local
conn
.
D
ownTrack
)
bool
{
up
.
mu
.
Lock
()
for
i
,
l
:=
range
up
.
local
{
if
l
==
local
{
...
...
@@ -240,15 +237,15 @@ func (up *rtpUpTrack) delLocal(local downTrack) bool {
return
false
}
func
(
up
*
rtpUpTrack
)
getLocal
()
[]
d
ownTrack
{
func
(
up
*
rtpUpTrack
)
getLocal
()
[]
conn
.
D
ownTrack
{
up
.
mu
.
Lock
()
defer
up
.
mu
.
Unlock
()
local
:=
make
([]
d
ownTrack
,
len
(
up
.
local
))
local
:=
make
([]
conn
.
D
ownTrack
,
len
(
up
.
local
))
copy
(
local
,
up
.
local
)
return
local
}
func
(
up
*
rtpUpTrack
)
g
etRTP
(
seqno
uint16
,
result
[]
byte
)
uint16
{
func
(
up
*
rtpUpTrack
)
G
etRTP
(
seqno
uint16
,
result
[]
byte
)
uint16
{
return
up
.
cache
.
Get
(
seqno
,
result
)
}
...
...
@@ -278,7 +275,7 @@ type rtpUpConnection struct {
mu
sync
.
Mutex
tracks
[]
*
rtpUpTrack
local
[]
downConnectio
n
local
[]
conn
.
Dow
n
}
func
(
up
*
rtpUpConnection
)
getTracks
()
[]
*
rtpUpTrack
{
...
...
@@ -297,7 +294,7 @@ func (up *rtpUpConnection) Label() string {
return
up
.
label
}
func
(
up
*
rtpUpConnection
)
addLocal
(
local
downConnectio
n
)
error
{
func
(
up
*
rtpUpConnection
)
AddLocal
(
local
conn
.
Dow
n
)
error
{
up
.
mu
.
Lock
()
defer
up
.
mu
.
Unlock
()
for
_
,
t
:=
range
up
.
local
{
...
...
@@ -309,7 +306,7 @@ func (up *rtpUpConnection) addLocal(local downConnection) error {
return
nil
}
func
(
up
*
rtpUpConnection
)
delLocal
(
local
downConnectio
n
)
bool
{
func
(
up
*
rtpUpConnection
)
DelLocal
(
local
conn
.
Dow
n
)
bool
{
up
.
mu
.
Lock
()
defer
up
.
mu
.
Unlock
()
for
i
,
l
:=
range
up
.
local
{
...
...
@@ -321,10 +318,10 @@ func (up *rtpUpConnection) delLocal(local downConnection) bool {
return
false
}
func
(
up
*
rtpUpConnection
)
getLocal
()
[]
downConnectio
n
{
func
(
up
*
rtpUpConnection
)
getLocal
()
[]
conn
.
Dow
n
{
up
.
mu
.
Lock
()
defer
up
.
mu
.
Unlock
()
local
:=
make
([]
downConnectio
n
,
len
(
up
.
local
))
local
:=
make
([]
conn
.
Dow
n
,
len
(
up
.
local
))
copy
(
local
,
up
.
local
)
return
local
}
...
...
@@ -370,8 +367,8 @@ func (up *rtpUpConnection) complete() bool {
return
true
}
func
newUpConn
(
c
c
lient
,
id
string
)
(
*
rtpUpConnection
,
error
)
{
pc
,
err
:=
c
.
Group
()
.
API
()
.
NewPeerConnection
(
i
ceConfiguration
())
func
newUpConn
(
c
group
.
C
lient
,
id
string
)
(
*
rtpUpConnection
,
error
)
{
pc
,
err
:=
c
.
Group
()
.
API
()
.
NewPeerConnection
(
group
.
I
ceConfiguration
())
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -396,10 +393,10 @@ func newUpConn(c client, id string) (*rtpUpConnection, error) {
return
nil
,
err
}
conn
:=
&
rtpUpConnection
{
id
:
id
,
pc
:
pc
}
up
:=
&
rtpUpConnection
{
id
:
id
,
pc
:
pc
}
pc
.
OnTrack
(
func
(
remote
*
webrtc
.
Track
,
receiver
*
webrtc
.
RTPReceiver
)
{
conn
.
mu
.
Lock
()
up
.
mu
.
Lock
()
mid
:=
getTrackMid
(
pc
,
remote
)
if
mid
==
""
{
...
...
@@ -407,7 +404,7 @@ func newUpConn(c client, id string) (*rtpUpConnection, error) {
return
}
label
,
ok
:=
conn
.
labels
[
mid
]
label
,
ok
:=
up
.
labels
[
mid
]
if
!
ok
{
log
.
Printf
(
"Couldn't get track's label"
)
isvideo
:=
remote
.
Kind
()
==
webrtc
.
RTPCodecTypeVideo
...
...
@@ -428,34 +425,34 @@ func newUpConn(c client, id string) (*rtpUpConnection, error) {
readerDone
:
make
(
chan
struct
{}),
}
conn
.
tracks
=
append
(
conn
.
tracks
,
track
)
up
.
tracks
=
append
(
up
.
tracks
,
track
)
go
readLoop
(
conn
,
track
)
go
readLoop
(
up
,
track
)
go
rtcpUpListener
(
conn
,
track
,
receiver
)
go
rtcpUpListener
(
up
,
track
,
receiver
)
complete
:=
conn
.
complete
()
var
tracks
[]
u
pTrack
if
(
complete
)
{
tracks
=
make
([]
upTrack
,
len
(
conn
.
tracks
))
for
i
,
t
:=
range
conn
.
tracks
{
complete
:=
up
.
complete
()
var
tracks
[]
conn
.
U
pTrack
if
complete
{
tracks
=
make
([]
conn
.
UpTrack
,
len
(
up
.
tracks
))
for
i
,
t
:=
range
up
.
tracks
{
tracks
[
i
]
=
t
}
}
// pushConn might need to take the lock
conn
.
mu
.
Unlock
()
up
.
mu
.
Unlock
()
if
complete
{
clients
:=
c
.
Group
()
.
g
etClients
(
c
)
clients
:=
c
.
Group
()
.
G
etClients
(
c
)
for
_
,
cc
:=
range
clients
{
cc
.
pushConn
(
conn
.
id
,
conn
,
tracks
,
conn
.
label
)
cc
.
PushConn
(
up
.
id
,
up
,
tracks
,
up
.
label
)
}
go
rtcpUpSender
(
conn
)
go
rtcpUpSender
(
up
)
}
})
return
conn
,
nil
return
up
,
nil
}
func
readLoop
(
conn
*
rtpUpConnection
,
track
*
rtpUpTrack
)
{
...
...
@@ -606,7 +603,7 @@ func sendRecovery(p *rtcp.TransportLayerNack, track *rtpDownTrack) {
buf
:=
make
([]
byte
,
packetcache
.
BufSize
)
for
_
,
nack
:=
range
p
.
Nacks
{
for
_
,
seqno
:=
range
nack
.
PacketList
()
{
l
:=
track
.
remote
.
g
etRTP
(
seqno
,
buf
)
l
:=
track
.
remote
.
G
etRTP
(
seqno
,
buf
)
if
l
==
0
{
continue
}
...
...
@@ -650,7 +647,7 @@ func rtcpUpListener(conn *rtpUpConnection, track *rtpUpTrack, r *webrtc.RTPRecei
track
.
srRTPTime
=
p
.
RTPTime
track
.
mu
.
Unlock
()
for
_
,
l
:=
range
local
{
l
.
s
etTimeOffset
(
p
.
NTPTime
,
p
.
RTPTime
)
l
.
S
etTimeOffset
(
p
.
NTPTime
,
p
.
RTPTime
)
}
case
*
rtcp
.
SourceDescription
:
for
_
,
c
:=
range
p
.
Chunks
{
...
...
@@ -665,7 +662,7 @@ func rtcpUpListener(conn *rtpUpConnection, track *rtpUpTrack, r *webrtc.RTPRecei
track
.
cname
=
i
.
Text
track
.
mu
.
Unlock
()
for
_
,
l
:=
range
local
{
l
.
s
etCname
(
i
.
Text
)
l
.
S
etCname
(
i
.
Text
)
}
}
}
...
...
@@ -749,8 +746,8 @@ func sendUpRTCP(conn *rtpUpConnection) error {
rate
=
r
}
}
if
rate
<
m
inBitrate
{
rate
=
m
inBitrate
if
rate
<
group
.
M
inBitrate
{
rate
=
group
.
M
inBitrate
}
var
ssrcs
[]
uint32
...
...
stats.go
→
rtpconn/rtp
stats.go
View file @
2347417f
package
mai
n
package
rtpcon
n
import
(
"sort"
...
...
@@ -6,76 +6,20 @@ import (
"time"
"sfu/rtptime"
"sfu/stats"
)
type
groupStats
struct
{
name
string
clients
[]
clientStats
}
type
clientStats
struct
{
id
string
up
,
down
[]
connStats
}
type
connStats
struct
{
id
string
maxBitrate
uint64
tracks
[]
trackStats
}
type
trackStats
struct
{
bitrate
uint64
maxBitrate
uint64
loss
uint8
rtt
time
.
Duration
jitter
time
.
Duration
}
func
getGroupStats
()
[]
groupStats
{
names
:=
getGroupNames
()
gs
:=
make
([]
groupStats
,
0
,
len
(
names
))
for
_
,
name
:=
range
names
{
g
:=
getGroup
(
name
)
if
g
==
nil
{
continue
}
clients
:=
g
.
getClients
(
nil
)
stats
:=
groupStats
{
name
:
name
,
clients
:
make
([]
clientStats
,
0
,
len
(
clients
)),
}
for
_
,
c
:=
range
clients
{
c
,
ok
:=
c
.
(
*
webClient
)
if
ok
{
cs
:=
getClientStats
(
c
)
stats
.
clients
=
append
(
stats
.
clients
,
cs
)
}
}
sort
.
Slice
(
stats
.
clients
,
func
(
i
,
j
int
)
bool
{
return
stats
.
clients
[
i
]
.
id
<
stats
.
clients
[
j
]
.
id
})
gs
=
append
(
gs
,
stats
)
}
sort
.
Slice
(
gs
,
func
(
i
,
j
int
)
bool
{
return
gs
[
i
]
.
name
<
gs
[
j
]
.
name
})
return
gs
}
func
getClientStats
(
c
*
webClient
)
clientStats
{
func
(
c
*
webClient
)
GetStats
()
*
stats
.
Client
{
c
.
mu
.
Lock
()
defer
c
.
mu
.
Unlock
()
cs
:=
clientStats
{
i
d
:
c
.
id
,
cs
:=
stats
.
Client
{
I
d
:
c
.
id
,
}
for
_
,
up
:=
range
c
.
up
{
conns
:=
connStats
{
i
d
:
up
.
id
,
conns
:=
stats
.
Conn
{
I
d
:
up
.
id
,
}
tracks
:=
up
.
getTracks
()
for
_
,
t
:=
range
tracks
{
...
...
@@ -87,23 +31,23 @@ func getClientStats(c *webClient) clientStats {
jitter
:=
time
.
Duration
(
t
.
jitter
.
Jitter
())
*
(
time
.
Second
/
time
.
Duration
(
t
.
jitter
.
HZ
()))
rate
,
_
:=
t
.
rate
.
Estimate
()
conns
.
tracks
=
append
(
conns
.
tracks
,
trackStats
{
b
itrate
:
uint64
(
rate
)
*
8
,
l
oss
:
loss
,
j
itter
:
jitter
,
conns
.
Tracks
=
append
(
conns
.
Tracks
,
stats
.
Track
{
B
itrate
:
uint64
(
rate
)
*
8
,
L
oss
:
loss
,
J
itter
:
jitter
,
})
}
cs
.
up
=
append
(
cs
.
u
p
,
conns
)
cs
.
Up
=
append
(
cs
.
U
p
,
conns
)
}
sort
.
Slice
(
cs
.
u
p
,
func
(
i
,
j
int
)
bool
{
return
cs
.
up
[
i
]
.
id
<
cs
.
up
[
j
]
.
i
d
sort
.
Slice
(
cs
.
U
p
,
func
(
i
,
j
int
)
bool
{
return
cs
.
Up
[
i
]
.
Id
<
cs
.
Up
[
j
]
.
I
d
})
jiffies
:=
rtptime
.
Jiffies
()
for
_
,
down
:=
range
c
.
down
{
conns
:=
connStats
{
i
d
:
down
.
id
,
m
axBitrate
:
down
.
GetMaxBitrate
(
jiffies
),
conns
:=
stats
.
Conn
{
I
d
:
down
.
id
,
M
axBitrate
:
down
.
GetMaxBitrate
(
jiffies
),
}
for
_
,
t
:=
range
down
.
tracks
{
rate
,
_
:=
t
.
rate
.
Estimate
()
...
...
@@ -112,19 +56,19 @@ func getClientStats(c *webClient) clientStats {
loss
,
jitter
:=
t
.
stats
.
Get
(
jiffies
)
j
:=
time
.
Duration
(
jitter
)
*
time
.
Second
/
time
.
Duration
(
t
.
track
.
Codec
()
.
ClockRate
)
conns
.
tracks
=
append
(
conns
.
tracks
,
trackStats
{
b
itrate
:
uint64
(
rate
)
*
8
,
m
axBitrate
:
t
.
maxBitrate
.
Get
(
jiffies
),
l
oss
:
uint8
(
uint32
(
loss
)
*
100
/
256
),
r
tt
:
rtt
,
j
itter
:
j
,
conns
.
Tracks
=
append
(
conns
.
Tracks
,
stats
.
Track
{
B
itrate
:
uint64
(
rate
)
*
8
,
M
axBitrate
:
t
.
maxBitrate
.
Get
(
jiffies
),
L
oss
:
uint8
(
uint32
(
loss
)
*
100
/
256
),
R
tt
:
rtt
,
J
itter
:
j
,
})
}
cs
.
down
=
append
(
cs
.
d
own
,
conns
)
cs
.
Down
=
append
(
cs
.
D
own
,
conns
)
}
sort
.
Slice
(
cs
.
d
own
,
func
(
i
,
j
int
)
bool
{
return
cs
.
down
[
i
]
.
id
<
cs
.
down
[
j
]
.
i
d
sort
.
Slice
(
cs
.
D
own
,
func
(
i
,
j
int
)
bool
{
return
cs
.
Down
[
i
]
.
Id
<
cs
.
Down
[
j
]
.
I
d
})
return
cs
return
&
cs
}
rtpwriter.go
→
rtp
conn/rtp
writer.go
View file @
2347417f
package
mai
n
package
rtpcon
n
import
(
"errors"
...
...
@@ -7,6 +7,7 @@ import (
"github.com/pion/rtp"
"sfu/conn"
"sfu/packetcache"
"sfu/rtptime"
)
...
...
@@ -43,7 +44,7 @@ func sqrt(n int) int {
}
// add adds or removes a track from a writer pool
func
(
wp
*
rtpWriterPool
)
add
(
track
d
ownTrack
,
add
bool
)
error
{
func
(
wp
*
rtpWriterPool
)
add
(
track
conn
.
D
ownTrack
,
add
bool
)
error
{
n
:=
4
if
wp
.
count
>
16
{
n
=
sqrt
(
wp
.
count
)
...
...
@@ -166,7 +167,7 @@ var ErrUnknownTrack = errors.New("unknown track")
type
writerAction
struct
{
add
bool
track
d
ownTrack
track
conn
.
D
ownTrack
maxTracks
int
ch
chan
error
}
...
...
@@ -192,7 +193,7 @@ func newRtpWriter(conn *rtpUpConnection, track *rtpUpTrack) *rtpWriter {
}
// add adds or removes a track from a writer.
func
(
writer
*
rtpWriter
)
add
(
track
d
ownTrack
,
add
bool
,
max
int
)
error
{
func
(
writer
*
rtpWriter
)
add
(
track
conn
.
D
ownTrack
,
add
bool
,
max
int
)
error
{
ch
:=
make
(
chan
error
,
1
)
select
{
case
writer
.
action
<-
writerAction
{
add
,
track
,
max
,
ch
}
:
...
...
@@ -208,13 +209,13 @@ func (writer *rtpWriter) add(track downTrack, add bool, max int) error {
}
// rtpWriterLoop is the main loop of an rtpWriter.
func
rtpWriterLoop
(
writer
*
rtpWriter
,
conn
*
rtpUpConnection
,
track
*
rtpUpTrack
)
{
func
rtpWriterLoop
(
writer
*
rtpWriter
,
up
*
rtpUpConnection
,
track
*
rtpUpTrack
)
{
defer
close
(
writer
.
done
)
buf
:=
make
([]
byte
,
packetcache
.
BufSize
)
var
packet
rtp
.
Packet
local
:=
make
([]
d
ownTrack
,
0
)
local
:=
make
([]
conn
.
D
ownTrack
,
0
)
// reset whenever a new track is inserted
firSent
:=
false
...
...
@@ -239,10 +240,10 @@ func rtpWriterLoop(writer *rtpWriter, conn *rtpUpConnection, track *rtpUpTrack)
cname
:=
track
.
cname
track
.
mu
.
Unlock
()
if
ntp
!=
0
{
action
.
track
.
s
etTimeOffset
(
ntp
,
rtp
)
action
.
track
.
S
etTimeOffset
(
ntp
,
rtp
)
}
if
cname
!=
""
{
action
.
track
.
s
etCname
(
cname
)
action
.
track
.
S
etCname
(
cname
)
}
}
else
{
found
:=
false
...
...
@@ -283,7 +284,7 @@ func rtpWriterLoop(writer *rtpWriter, conn *rtpUpConnection, track *rtpUpTrack)
for
_
,
l
:=
range
local
{
err
:=
l
.
WriteRTP
(
&
packet
)
if
err
!=
nil
{
if
err
==
ErrKeyframeNeeded
{
if
err
==
conn
.
ErrKeyframeNeeded
{
kfNeeded
=
true
}
continue
...
...
@@ -292,9 +293,9 @@ func rtpWriterLoop(writer *rtpWriter, conn *rtpUpConnection, track *rtpUpTrack)
}
if
kfNeeded
{
err
:=
conn
.
sendFIR
(
track
,
!
firSent
)
err
:=
up
.
sendFIR
(
track
,
!
firSent
)
if
err
==
ErrUnsupportedFeedback
{
conn
.
sendPLI
(
track
)
up
.
sendPLI
(
track
)
}
firSent
=
true
}
...
...
webclient.go
→
rtpconn/
webclient.go
View file @
2347417f
// 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
package
rtpconn
import
(
"encoding/json"
...
...
@@ -13,49 +8,14 @@ import (
"sync"
"time"
"sfu/estimator"
"github.com/gorilla/websocket"
"github.com/pion/webrtc/v3"
)
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
)
}
type
userError
string
func
(
err
userError
)
Error
()
string
{
return
string
(
err
)
}
"sfu/conn"
"sfu/disk"
"sfu/estimator"
"sfu/group"
)
func
errorToWSCloseMessage
(
err
error
)
(
string
,
[]
byte
)
{
var
code
int
...
...
@@ -63,10 +23,10 @@ func errorToWSCloseMessage(err error) (string, []byte) {
switch
e
:=
err
.
(
type
)
{
case
*
websocket
.
CloseError
:
code
=
websocket
.
CloseNormalClosure
case
p
rotocolError
:
case
group
.
P
rotocolError
:
code
=
websocket
.
CloseProtocolError
text
=
string
(
e
)
case
u
serError
:
case
group
.
U
serError
:
code
=
websocket
.
CloseNormalClosure
text
=
string
(
e
)
default
:
...
...
@@ -82,10 +42,10 @@ func isWSNormalError(err error) bool {
}
type
webClient
struct
{
group
*
group
group
*
group
.
Group
id
string
credentials
c
lientCredentials
permissions
c
lientPermissions
credentials
group
.
C
lientCredentials
permissions
group
.
C
lientPermissions
requested
map
[
string
]
uint32
done
chan
struct
{}
writeCh
chan
interface
{}
...
...
@@ -97,7 +57,7 @@ type webClient struct {
up
map
[
string
]
*
rtpUpConnection
}
func
(
c
*
webClient
)
Group
()
*
group
{
func
(
c
*
webClient
)
Group
()
*
group
.
Group
{
return
c
.
group
}
...
...
@@ -105,15 +65,15 @@ func (c *webClient) Id() string {
return
c
.
id
}
func
(
c
*
webClient
)
Credentials
()
c
lientCredentials
{
func
(
c
*
webClient
)
Credentials
()
group
.
C
lientCredentials
{
return
c
.
credentials
}
func
(
c
*
webClient
)
SetPermissions
(
perms
c
lientPermissions
)
{
func
(
c
*
webClient
)
SetPermissions
(
perms
group
.
C
lientPermissions
)
{
c
.
permissions
=
perms
}
func
(
c
*
webClient
)
p
ushClient
(
id
,
username
string
,
add
bool
)
error
{
func
(
c
*
webClient
)
P
ushClient
(
id
,
username
string
,
add
bool
)
error
{
kind
:=
"add"
if
!
add
{
kind
=
"delete"
...
...
@@ -179,7 +139,7 @@ type clientMessage struct {
Id
string
`json:"id,omitempty"`
Username
string
`json:"username,omitempty"`
Password
string
`json:"password,omitempty"`
Permissions
clientPermissions
`json:"permissions,omitempty"`
Permissions
group
.
ClientPermissions
`json:"permissions,omitempty"`
Group
string
`json:"group,omitempty"`
Value
string
`json:"value,omitempty"`
Offer
*
webrtc
.
SessionDescription
`json:"offer,omitempty"`
...
...
@@ -263,11 +223,11 @@ func delUpConn(c *webClient, id string) bool {
delete
(
c
.
up
,
id
)
c
.
mu
.
Unlock
()
go
func
(
clients
[]
c
lient
)
{
go
func
(
clients
[]
group
.
C
lient
)
{
for
_
,
c
:=
range
clients
{
c
.
p
ushConn
(
conn
.
id
,
nil
,
nil
,
""
)
c
.
P
ushConn
(
conn
.
id
,
nil
,
nil
,
""
)
}
}(
c
.
Group
()
.
g
etClients
(
c
))
}(
c
.
Group
()
.
G
etClients
(
c
))
conn
.
pc
.
Close
()
return
true
...
...
@@ -299,7 +259,7 @@ func getConn(c *webClient, id string) iceConnection {
return
nil
}
func
addDownConn
(
c
*
webClient
,
id
string
,
remote
upConnection
)
(
*
rtpDownConnection
,
error
)
{
func
addDownConn
(
c
*
webClient
,
id
string
,
remote
conn
.
Up
)
(
*
rtpDownConnection
,
error
)
{
conn
,
err
:=
newDownConn
(
c
,
id
,
remote
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -332,7 +292,7 @@ func addDownConn(c *webClient, id string, remote upConnection) (*rtpDownConnecti
}
})
err
=
remote
.
a
ddLocal
(
conn
)
err
=
remote
.
A
ddLocal
(
conn
)
if
err
!=
nil
{
conn
.
pc
.
Close
()
return
nil
,
err
...
...
@@ -354,18 +314,18 @@ func delDownConn(c *webClient, id string) bool {
return
false
}
conn
.
remote
.
d
elLocal
(
conn
)
conn
.
remote
.
D
elLocal
(
conn
)
for
_
,
track
:=
range
conn
.
tracks
{
// we only insert the track after we get an answer, so
// ignore errors here.
track
.
remote
.
d
elLocal
(
track
)
track
.
remote
.
D
elLocal
(
track
)
}
conn
.
pc
.
Close
()
delete
(
c
.
down
,
id
)
return
true
}
func
addDownTrack
(
c
*
webClient
,
conn
*
rtpDownConnection
,
remoteTrack
upTrack
,
remoteConn
upConnection
)
(
*
webrtc
.
RTPSender
,
error
)
{
func
addDownTrack
(
c
*
webClient
,
conn
*
rtpDownConnection
,
remoteTrack
conn
.
UpTrack
,
remoteConn
conn
.
Up
)
(
*
webrtc
.
RTPSender
,
error
)
{
var
pt
uint8
var
ssrc
uint32
var
id
,
label
string
...
...
@@ -510,7 +470,7 @@ func gotOffer(c *webClient, id string, offer webrtc.SessionDescription, renegoti
func
gotAnswer
(
c
*
webClient
,
id
string
,
answer
webrtc
.
SessionDescription
)
error
{
down
:=
getDownConn
(
c
,
id
)
if
down
==
nil
{
return
p
rotocolError
(
"unknown id in answer"
)
return
group
.
P
rotocolError
(
"unknown id in answer"
)
}
err
:=
down
.
pc
.
SetRemoteDescription
(
answer
)
if
err
!=
nil
{
...
...
@@ -523,7 +483,7 @@ func gotAnswer(c *webClient, id string, answer webrtc.SessionDescription) error
}
for
_
,
t
:=
range
down
.
tracks
{
t
.
remote
.
a
ddLocal
(
t
)
t
.
remote
.
A
ddLocal
(
t
)
}
return
nil
}
...
...
@@ -553,8 +513,8 @@ func (c *webClient) setRequested(requested map[string]uint32) error {
return
nil
}
func
pushConns
(
c
c
lient
)
{
clients
:=
c
.
Group
()
.
g
etClients
(
c
)
func
pushConns
(
c
group
.
C
lient
)
{
clients
:=
c
.
Group
()
.
G
etClients
(
c
)
for
_
,
cc
:=
range
clients
{
ccc
,
ok
:=
cc
.
(
*
webClient
)
if
ok
{
...
...
@@ -567,7 +527,7 @@ func (c *webClient) isRequested(label string) bool {
return
c
.
requested
[
label
]
!=
0
}
func
addDownConnTracks
(
c
*
webClient
,
remote
upConnection
,
tracks
[]
u
pTrack
)
(
*
rtpDownConnection
,
error
)
{
func
addDownConnTracks
(
c
*
webClient
,
remote
conn
.
Up
,
tracks
[]
conn
.
U
pTrack
)
(
*
rtpDownConnection
,
error
)
{
requested
:=
false
for
_
,
t
:=
range
tracks
{
if
c
.
isRequested
(
t
.
Label
())
{
...
...
@@ -600,13 +560,13 @@ func addDownConnTracks(c *webClient, remote upConnection, tracks []upTrack) (*rt
return
down
,
nil
}
func
(
c
*
webClient
)
pushConn
(
id
string
,
conn
upConnection
,
tracks
[]
u
pTrack
,
label
string
)
error
{
err
:=
c
.
action
(
pushConnAction
{
id
,
conn
,
tracks
})
func
(
c
*
webClient
)
PushConn
(
id
string
,
up
conn
.
Up
,
tracks
[]
conn
.
U
pTrack
,
label
string
)
error
{
err
:=
c
.
action
(
pushConnAction
{
id
,
up
,
tracks
})
if
err
!=
nil
{
return
err
}
if
conn
!=
nil
&&
label
!=
""
{
err
:=
c
.
action
(
addLabelAction
{
conn
.
Id
(),
conn
.
Label
()})
if
up
!=
nil
&&
label
!=
""
{
err
:=
c
.
action
(
addLabelAction
{
up
.
Id
(),
up
.
Label
()})
if
err
!=
nil
{
return
err
}
...
...
@@ -614,7 +574,7 @@ func (c *webClient) pushConn(id string, conn upConnection, tracks []upTrack, lab
return
nil
}
func
s
tartClient
(
conn
*
websocket
.
Conn
)
(
err
error
)
{
func
S
tartClient
(
conn
*
websocket
.
Conn
)
(
err
error
)
{
var
m
clientMessage
err
=
conn
.
SetReadDeadline
(
time
.
Now
()
.
Add
(
15
*
time
.
Second
))
...
...
@@ -646,7 +606,7 @@ func startClient(conn *websocket.Conn) (err error) {
c
:=
&
webClient
{
id
:
m
.
Id
,
credentials
:
c
lientCredentials
{
credentials
:
group
.
C
lientCredentials
{
m
.
Username
,
m
.
Password
,
},
...
...
@@ -683,32 +643,32 @@ func startClient(conn *websocket.Conn) (err error) {
}
if
m
.
Type
!=
"join"
{
return
p
rotocolError
(
"you must join a group first"
)
return
group
.
P
rotocolError
(
"you must join a group first"
)
}
g
,
err
:=
a
ddClient
(
m
.
Group
,
c
)
g
,
err
:=
group
.
A
ddClient
(
m
.
Group
,
c
)
if
err
!=
nil
{
if
os
.
IsNotExist
(
err
)
{
err
=
u
serError
(
"group does not exist"
)
err
=
group
.
U
serError
(
"group does not exist"
)
}
return
}
if
redirect
:=
g
.
Redirect
();
redirect
!=
""
{
// We normally redirect at the HTTP level, but the group
// description could have been edited in the meantime.
err
=
u
serError
(
"group is now at "
+
redirect
)
err
=
group
.
U
serError
(
"group is now at "
+
redirect
)
return
}
c
.
group
=
g
defer
d
elClient
(
c
)
defer
group
.
D
elClient
(
c
)
return
clientLoop
(
c
,
conn
)
}
type
pushConnAction
struct
{
id
string
conn
upConnection
tracks
[]
u
pTrack
conn
conn
.
Up
tracks
[]
conn
.
U
pTrack
}
type
addLabelAction
struct
{
...
...
@@ -717,7 +677,7 @@ type addLabelAction struct {
}
type
pushConnsAction
struct
{
c
c
lient
c
group
.
C
lient
}
type
connectionFailedAction
struct
{
...
...
@@ -730,9 +690,9 @@ type kickAction struct {
message
string
}
func
clientLoop
(
c
*
webClient
,
conn
*
websocket
.
Conn
)
error
{
func
clientLoop
(
c
*
webClient
,
ws
*
websocket
.
Conn
)
error
{
read
:=
make
(
chan
interface
{},
1
)
go
clientReader
(
conn
,
read
,
c
.
done
)
go
clientReader
(
ws
,
read
,
c
.
done
)
defer
func
()
{
c
.
setRequested
(
map
[
string
]
uint32
{})
...
...
@@ -748,14 +708,14 @@ func clientLoop(c *webClient, conn *websocket.Conn) error {
Permissions
:
c
.
permissions
,
})
h
:=
c
.
group
.
g
etChatHistory
()
h
:=
c
.
group
.
G
etChatHistory
()
for
_
,
m
:=
range
h
{
err
:=
c
.
write
(
clientMessage
{
Type
:
"chat"
,
Id
:
m
.
i
d
,
Username
:
m
.
u
ser
,
Value
:
m
.
v
alue
,
Kind
:
m
.
k
ind
,
Id
:
m
.
I
d
,
Username
:
m
.
U
ser
,
Value
:
m
.
V
alue
,
Kind
:
m
.
K
ind
,
})
if
err
!=
nil
{
return
err
...
...
@@ -829,11 +789,11 @@ func clientLoop(c *webClient, conn *websocket.Conn) error {
case
pushConnsAction
:
for
_
,
u
:=
range
c
.
up
{
tracks
:=
u
.
getTracks
()
ts
:=
make
([]
u
pTrack
,
len
(
tracks
))
ts
:=
make
([]
conn
.
U
pTrack
,
len
(
tracks
))
for
i
,
t
:=
range
tracks
{
ts
[
i
]
=
t
}
go
a
.
c
.
p
ushConn
(
u
.
id
,
u
,
ts
,
u
.
label
)
go
a
.
c
.
P
ushConn
(
u
.
id
,
u
,
ts
,
u
.
label
)
}
case
connectionFailedAction
:
if
down
:=
getDownConn
(
c
,
a
.
id
);
down
!=
nil
{
...
...
@@ -842,12 +802,12 @@ func clientLoop(c *webClient, conn *websocket.Conn) error {
return
err
}
tracks
:=
make
(
[]
u
pTrack
,
len
(
down
.
tracks
),
[]
conn
.
U
pTrack
,
len
(
down
.
tracks
),
)
for
i
,
t
:=
range
down
.
tracks
{
tracks
[
i
]
=
t
.
remote
}
go
c
.
p
ushConn
(
go
c
.
P
ushConn
(
down
.
remote
.
Id
(),
down
.
remote
,
tracks
,
down
.
remote
.
Label
(),
)
...
...
@@ -879,7 +839,7 @@ func clientLoop(c *webClient, conn *websocket.Conn) error {
}
}
case
kickAction
:
return
u
serError
(
a
.
message
)
return
group
.
U
serError
(
a
.
message
)
default
:
log
.
Printf
(
"unexpected action %T"
,
a
)
return
errors
.
New
(
"unexpected action"
)
...
...
@@ -911,7 +871,7 @@ func failConnection(c *webClient, id string, message string) error {
}
}
if
message
!=
""
{
err
:=
c
.
error
(
u
serError
(
message
))
err
:=
c
.
error
(
group
.
U
serError
(
message
))
if
err
!=
nil
{
return
err
}
...
...
@@ -919,15 +879,15 @@ func failConnection(c *webClient, id string, message string) error {
return
nil
}
func
setPermissions
(
g
*
group
,
id
string
,
perm
string
)
error
{
client
:=
g
.
g
etClient
(
id
)
func
setPermissions
(
g
*
group
.
Group
,
id
string
,
perm
string
)
error
{
client
:=
g
.
G
etClient
(
id
)
if
client
==
nil
{
return
u
serError
(
"no such user"
)
return
group
.
U
serError
(
"no such user"
)
}
c
,
ok
:=
client
.
(
*
webClient
)
if
!
ok
{
return
u
serError
(
"this is not a real user"
)
return
group
.
U
serError
(
"this is not a real user"
)
}
switch
perm
{
...
...
@@ -944,7 +904,7 @@ func setPermissions(g *group, id string, perm string) error {
case
"unpresent"
:
c
.
permissions
.
Present
=
false
default
:
return
u
serError
(
"unknown permission"
)
return
group
.
U
serError
(
"unknown permission"
)
}
return
c
.
action
(
permissionsChangedAction
{})
}
...
...
@@ -953,18 +913,18 @@ func (c *webClient) kick(message string) error {
return
c
.
action
(
kickAction
{
message
})
}
func
kickClient
(
g
*
group
,
id
string
,
message
string
)
error
{
client
:=
g
.
g
etClient
(
id
)
func
kickClient
(
g
*
group
.
Group
,
id
string
,
message
string
)
error
{
client
:=
g
.
G
etClient
(
id
)
if
client
==
nil
{
return
u
serError
(
"no such user"
)
return
group
.
U
serError
(
"no such user"
)
}
c
,
ok
:=
client
.
(
k
ickable
)
c
,
ok
:=
client
.
(
group
.
K
ickable
)
if
!
ok
{
return
u
serError
(
"this client is not kickable"
)
return
group
.
U
serError
(
"this client is not kickable"
)
}
return
c
.
k
ick
(
message
)
return
c
.
K
ick
(
message
)
}
func
handleClientMessage
(
c
*
webClient
,
m
clientMessage
)
error
{
...
...
@@ -980,10 +940,10 @@ func handleClientMessage(c *webClient, m clientMessage) error {
Type
:
"abort"
,
Id
:
m
.
Id
,
})
return
c
.
error
(
u
serError
(
"not authorised"
))
return
c
.
error
(
group
.
U
serError
(
"not authorised"
))
}
if
m
.
Offer
==
nil
{
return
p
rotocolError
(
"null offer"
)
return
group
.
P
rotocolError
(
"null offer"
)
}
err
:=
gotOffer
(
c
,
m
.
Id
,
*
m
.
Offer
,
m
.
Kind
==
"renegotiate"
,
m
.
Labels
,
...
...
@@ -994,7 +954,7 @@ func handleClientMessage(c *webClient, m clientMessage) error {
}
case
"answer"
:
if
m
.
Answer
==
nil
{
return
p
rotocolError
(
"null answer"
)
return
group
.
P
rotocolError
(
"null answer"
)
}
err
:=
gotAnswer
(
c
,
m
.
Id
,
*
m
.
Answer
)
if
err
!=
nil
{
...
...
@@ -1017,15 +977,15 @@ func handleClientMessage(c *webClient, m clientMessage) error {
}
case
"ice"
:
if
m
.
Candidate
==
nil
{
return
p
rotocolError
(
"null candidate"
)
return
group
.
P
rotocolError
(
"null candidate"
)
}
err
:=
gotICE
(
c
,
m
.
Candidate
,
m
.
Id
)
if
err
!=
nil
{
log
.
Printf
(
"ICE: %v"
,
err
)
}
case
"chat"
:
c
.
group
.
a
ddToChatHistory
(
m
.
Id
,
m
.
Username
,
m
.
Kind
,
m
.
Value
)
clients
:=
c
.
group
.
g
etClients
(
nil
)
c
.
group
.
A
ddToChatHistory
(
m
.
Id
,
m
.
Username
,
m
.
Kind
,
m
.
Value
)
clients
:=
c
.
group
.
G
etClients
(
nil
)
for
_
,
cc
:=
range
clients
{
cc
,
ok
:=
cc
.
(
*
webClient
)
if
ok
{
...
...
@@ -1035,9 +995,9 @@ func handleClientMessage(c *webClient, m clientMessage) error {
case
"groupaction"
:
switch
m
.
Kind
{
case
"clearchat"
:
c
.
group
.
c
learChatHistory
()
c
.
group
.
C
learChatHistory
()
m
:=
clientMessage
{
Type
:
"clearchat"
}
clients
:=
c
.
group
.
g
etClients
(
nil
)
clients
:=
c
.
group
.
G
etClients
(
nil
)
for
_
,
cc
:=
range
clients
{
cc
,
ok
:=
cc
.
(
*
webClient
)
if
ok
{
...
...
@@ -1046,21 +1006,21 @@ func handleClientMessage(c *webClient, m clientMessage) error {
}
case
"lock"
,
"unlock"
:
if
!
c
.
permissions
.
Op
{
return
c
.
error
(
u
serError
(
"not authorised"
))
return
c
.
error
(
group
.
U
serError
(
"not authorised"
))
}
c
.
group
.
SetLocked
(
m
.
Kind
==
"lock"
)
case
"record"
:
if
!
c
.
permissions
.
Record
{
return
c
.
error
(
u
serError
(
"not authorised"
))
return
c
.
error
(
group
.
U
serError
(
"not authorised"
))
}
for
_
,
cc
:=
range
c
.
group
.
g
etClients
(
c
)
{
_
,
ok
:=
cc
.
(
*
diskClient
)
for
_
,
cc
:=
range
c
.
group
.
G
etClients
(
c
)
{
_
,
ok
:=
cc
.
(
*
disk
.
Client
)
if
ok
{
return
c
.
error
(
u
serError
(
"already recording"
))
return
c
.
error
(
group
.
U
serError
(
"already recording"
))
}
}
disk
:=
NewDiskClient
(
c
.
group
)
_
,
err
:=
addClient
(
c
.
group
.
name
,
disk
)
disk
:=
disk
.
New
(
c
.
group
)
_
,
err
:=
group
.
AddClient
(
c
.
group
.
Name
()
,
disk
)
if
err
!=
nil
{
disk
.
Close
()
return
c
.
error
(
err
)
...
...
@@ -1068,23 +1028,23 @@ func handleClientMessage(c *webClient, m clientMessage) error {
go
pushConns
(
disk
)
case
"unrecord"
:
if
!
c
.
permissions
.
Record
{
return
c
.
error
(
u
serError
(
"not authorised"
))
return
c
.
error
(
group
.
U
serError
(
"not authorised"
))
}
for
_
,
cc
:=
range
c
.
group
.
g
etClients
(
c
)
{
disk
,
ok
:=
cc
.
(
*
diskClient
)
for
_
,
cc
:=
range
c
.
group
.
G
etClients
(
c
)
{
disk
,
ok
:=
cc
.
(
*
disk
.
Client
)
if
ok
{
disk
.
Close
()
d
elClient
(
disk
)
group
.
D
elClient
(
disk
)
}
}
default
:
return
p
rotocolError
(
"unknown group action"
)
return
group
.
P
rotocolError
(
"unknown group action"
)
}
case
"useraction"
:
switch
m
.
Kind
{
case
"op"
,
"unop"
,
"present"
,
"unpresent"
:
if
!
c
.
permissions
.
Op
{
return
c
.
error
(
u
serError
(
"not authorised"
))
return
c
.
error
(
group
.
U
serError
(
"not authorised"
))
}
err
:=
setPermissions
(
c
.
group
,
m
.
Id
,
m
.
Kind
)
if
err
!=
nil
{
...
...
@@ -1092,7 +1052,7 @@ func handleClientMessage(c *webClient, m clientMessage) error {
}
case
"kick"
:
if
!
c
.
permissions
.
Op
{
return
c
.
error
(
u
serError
(
"not authorised"
))
return
c
.
error
(
group
.
U
serError
(
"not authorised"
))
}
message
:=
m
.
Value
if
message
==
""
{
...
...
@@ -1103,7 +1063,7 @@ func handleClientMessage(c *webClient, m clientMessage) error {
return
c
.
error
(
err
)
}
default
:
return
p
rotocolError
(
"unknown user action"
)
return
group
.
P
rotocolError
(
"unknown user action"
)
}
case
"pong"
:
// nothing
...
...
@@ -1113,7 +1073,7 @@ func handleClientMessage(c *webClient, m clientMessage) error {
})
default
:
log
.
Printf
(
"unexpected message: %v"
,
m
.
Type
)
return
p
rotocolError
(
"unexpected message"
)
return
group
.
P
rotocolError
(
"unexpected message"
)
}
return
nil
}
...
...
@@ -1207,7 +1167,7 @@ func (c *webClient) close(data []byte) error {
func
(
c
*
webClient
)
error
(
err
error
)
error
{
switch
e
:=
err
.
(
type
)
{
case
u
serError
:
case
group
.
U
serError
:
return
c
.
write
(
clientMessage
{
Type
:
"usermessage"
,
Kind
:
"error"
,
...
...
sfu.go
View file @
2347417f
...
...
@@ -14,14 +14,14 @@ import (
"runtime"
"runtime/pprof"
"syscall"
"sfu/disk"
"sfu/group"
)
var
httpAddr
string
var
staticRoot
string
var
dataDir
string
var
groupsDir
string
var
recordingsDir
string
var
iceFilename
string
func
main
()
{
var
cpuprofile
,
memprofile
,
mutexprofile
string
...
...
@@ -31,9 +31,9 @@ func main() {
"web server root `directory`"
)
flag
.
StringVar
(
&
dataDir
,
"data"
,
"./data/"
,
"data `directory`"
)
flag
.
StringVar
(
&
group
sDir
,
"groups"
,
"./groups/"
,
flag
.
StringVar
(
&
group
.
Directory
,
"groups"
,
"./groups/"
,
"group description `directory`"
)
flag
.
StringVar
(
&
recordingsDir
,
"recordings"
,
"./recordings/"
,
flag
.
StringVar
(
&
disk
.
Directory
,
"recordings"
,
"./recordings/"
,
"recordings `directory`"
)
flag
.
StringVar
(
&
cpuprofile
,
"cpuprofile"
,
""
,
"store CPU profile in `file`"
)
...
...
@@ -81,9 +81,9 @@ func main() {
}()
}
i
ceFilename
=
filepath
.
Join
(
dataDir
,
"ice-servers.json"
)
group
.
I
ceFilename
=
filepath
.
Join
(
dataDir
,
"ice-servers.json"
)
go
r
eadPublicGroups
()
go
group
.
R
eadPublicGroups
()
webserver
()
terminate
:=
make
(
chan
os
.
Signal
,
1
)
...
...
stats/stats.go
0 → 100644
View file @
2347417f
package
stats
import
(
"sort"
"time"
"sfu/group"
)
type
GroupStats
struct
{
Name
string
Clients
[]
*
Client
}
type
Client
struct
{
Id
string
Up
,
Down
[]
Conn
}
type
Statable
interface
{
GetStats
()
*
Client
}
type
Conn
struct
{
Id
string
MaxBitrate
uint64
Tracks
[]
Track
}
type
Track
struct
{
Bitrate
uint64
MaxBitrate
uint64
Loss
uint8
Rtt
time
.
Duration
Jitter
time
.
Duration
}
func
GetGroups
()
[]
GroupStats
{
names
:=
group
.
GetNames
()
gs
:=
make
([]
GroupStats
,
0
,
len
(
names
))
for
_
,
name
:=
range
names
{
g
:=
group
.
Get
(
name
)
if
g
==
nil
{
continue
}
clients
:=
g
.
GetClients
(
nil
)
stats
:=
GroupStats
{
Name
:
name
,
Clients
:
make
([]
*
Client
,
0
,
len
(
clients
)),
}
for
_
,
c
:=
range
clients
{
s
,
ok
:=
c
.
(
Statable
)
if
ok
{
cs
:=
s
.
GetStats
()
stats
.
Clients
=
append
(
stats
.
Clients
,
cs
)
}
else
{
stats
.
Clients
=
append
(
stats
.
Clients
,
&
Client
{
Id
:
c
.
Id
()},
)
}
}
sort
.
Slice
(
stats
.
Clients
,
func
(
i
,
j
int
)
bool
{
return
stats
.
Clients
[
i
]
.
Id
<
stats
.
Clients
[
j
]
.
Id
})
gs
=
append
(
gs
,
stats
)
}
sort
.
Slice
(
gs
,
func
(
i
,
j
int
)
bool
{
return
gs
[
i
]
.
Name
<
gs
[
j
]
.
Name
})
return
gs
}
webserver.go
View file @
2347417f
...
...
@@ -18,6 +18,11 @@ import (
"time"
"github.com/gorilla/websocket"
"sfu/disk"
"sfu/group"
"sfu/rtpconn"
"sfu/stats"
)
var
server
*
http
.
Server
...
...
@@ -47,8 +52,8 @@ func webserver() {
IdleTimeout
:
120
*
time
.
Second
,
}
server
.
RegisterOnShutdown
(
func
()
{
rangeGroups
(
func
(
g
*
g
roup
)
bool
{
go
g
.
s
hutdown
(
"server is shutting down"
)
group
.
Range
(
func
(
g
*
group
.
G
roup
)
bool
{
go
g
.
S
hutdown
(
"server is shutting down"
)
return
true
})
})
...
...
@@ -139,7 +144,7 @@ func groupHandler(w http.ResponseWriter, r *http.Request) {
return
}
g
,
err
:=
addGroup
(
name
,
nil
)
g
,
err
:=
group
.
Add
(
name
,
nil
)
if
err
!=
nil
{
if
os
.
IsNotExist
(
err
)
{
notFound
(
w
)
...
...
@@ -168,7 +173,7 @@ func publicHandler(w http.ResponseWriter, r *http.Request) {
return
}
g
:=
g
etPublicGroups
()
g
:=
g
roup
.
GetPublic
()
e
:=
json
.
NewEncoder
(
w
)
e
.
Encode
(
g
)
return
...
...
@@ -222,7 +227,7 @@ func statsHandler(w http.ResponseWriter, r *http.Request) {
return
}
s
tats
:=
getGroupStat
s
()
s
s
:=
stats
.
GetGroup
s
()
fmt
.
Fprintf
(
w
,
"<!DOCTYPE html>
\n
<html><head>
\n
"
)
fmt
.
Fprintf
(
w
,
"<title>Stats</title>
\n
"
)
...
...
@@ -239,51 +244,51 @@ func statsHandler(w http.ResponseWriter, r *http.Request) {
return
err
}
printTrack
:=
func
(
w
io
.
Writer
,
t
trackStats
)
{
printTrack
:=
func
(
w
io
.
Writer
,
t
stats
.
Track
)
{
fmt
.
Fprintf
(
w
,
"<tr><td></td><td></td><td></td>"
)
fmt
.
Fprintf
(
w
,
"<td>"
)
printBitrate
(
w
,
t
.
bitrate
,
t
.
m
axBitrate
)
printBitrate
(
w
,
t
.
Bitrate
,
t
.
M
axBitrate
)
fmt
.
Fprintf
(
w
,
"</td>"
)
fmt
.
Fprintf
(
w
,
"<td>%d%%</td>"
,
t
.
l
oss
,
t
.
L
oss
,
)
fmt
.
Fprintf
(
w
,
"<td>"
)
if
t
.
r
tt
>
0
{
fmt
.
Fprintf
(
w
,
"%v"
,
t
.
r
tt
)
if
t
.
R
tt
>
0
{
fmt
.
Fprintf
(
w
,
"%v"
,
t
.
R
tt
)
}
if
t
.
j
itter
>
0
{
fmt
.
Fprintf
(
w
,
"±%v"
,
t
.
j
itter
)
if
t
.
J
itter
>
0
{
fmt
.
Fprintf
(
w
,
"±%v"
,
t
.
J
itter
)
}
fmt
.
Fprintf
(
w
,
"</td>"
)
fmt
.
Fprintf
(
w
,
"</tr>"
)
}
for
_
,
gs
:=
range
s
tat
s
{
fmt
.
Fprintf
(
w
,
"<p>%v</p>
\n
"
,
html
.
EscapeString
(
gs
.
n
ame
))
for
_
,
gs
:=
range
ss
{
fmt
.
Fprintf
(
w
,
"<p>%v</p>
\n
"
,
html
.
EscapeString
(
gs
.
N
ame
))
fmt
.
Fprintf
(
w
,
"<table>"
)
for
_
,
cs
:=
range
gs
.
c
lients
{
fmt
.
Fprintf
(
w
,
"<tr><td>%v</td></tr>
\n
"
,
cs
.
i
d
)
for
_
,
up
:=
range
cs
.
u
p
{
for
_
,
cs
:=
range
gs
.
C
lients
{
fmt
.
Fprintf
(
w
,
"<tr><td>%v</td></tr>
\n
"
,
cs
.
I
d
)
for
_
,
up
:=
range
cs
.
U
p
{
fmt
.
Fprintf
(
w
,
"<tr><td></td><td>Up</td><td>%v</td>"
,
up
.
i
d
)
if
up
.
m
axBitrate
>
0
{
up
.
I
d
)
if
up
.
M
axBitrate
>
0
{
fmt
.
Fprintf
(
w
,
"<td>%v</td>"
,
up
.
m
axBitrate
)
up
.
M
axBitrate
)
}
fmt
.
Fprintf
(
w
,
"</tr>
\n
"
)
for
_
,
t
:=
range
up
.
t
racks
{
for
_
,
t
:=
range
up
.
T
racks
{
printTrack
(
w
,
t
)
}
}
for
_
,
down
:=
range
cs
.
d
own
{
for
_
,
down
:=
range
cs
.
D
own
{
fmt
.
Fprintf
(
w
,
"<tr><td></td><td>Down</td><td> %v</td>"
,
down
.
i
d
)
if
down
.
m
axBitrate
>
0
{
down
.
I
d
)
if
down
.
M
axBitrate
>
0
{
fmt
.
Fprintf
(
w
,
"<td>%v</td>"
,
down
.
m
axBitrate
)
down
.
M
axBitrate
)
}
fmt
.
Fprintf
(
w
,
"</tr>
\n
"
)
for
_
,
t
:=
range
down
.
t
racks
{
for
_
,
t
:=
range
down
.
T
racks
{
printTrack
(
w
,
t
)
}
}
...
...
@@ -302,7 +307,7 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
return
}
go
func
()
{
err
:=
s
tartClient
(
conn
)
err
:=
rtpconn
.
S
tartClient
(
conn
)
if
err
!=
nil
{
log
.
Printf
(
"client: %v"
,
err
)
}
...
...
@@ -322,7 +327,7 @@ func recordingsHandler(w http.ResponseWriter, r *http.Request) {
return
}
f
,
err
:=
os
.
Open
(
filepath
.
Join
(
recordingsDir
,
pth
))
f
,
err
:=
os
.
Open
(
filepath
.
Join
(
disk
.
Directory
,
pth
))
if
err
!=
nil
{
if
os
.
IsNotExist
(
err
)
{
notFound
(
w
)
...
...
@@ -389,7 +394,7 @@ func handleGroupAction(w http.ResponseWriter, r *http.Request, group string) {
return
}
err
:=
os
.
Remove
(
filepath
.
Join
(
recordingsDir
,
group
+
"/"
+
filename
),
filepath
.
Join
(
disk
.
Directory
,
group
+
"/"
+
filename
),
)
if
err
!=
nil
{
if
os
.
IsPermission
(
err
)
{
...
...
@@ -409,8 +414,8 @@ func handleGroupAction(w http.ResponseWriter, r *http.Request, group string) {
}
}
func
checkGroupPermissions
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
group
string
)
bool
{
desc
,
err
:=
g
etDescription
(
group
)
func
checkGroupPermissions
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
group
name
string
)
bool
{
desc
,
err
:=
g
roup
.
GetDescription
(
groupname
)
if
err
!=
nil
{
return
false
}
...
...
@@ -420,7 +425,7 @@ func checkGroupPermissions(w http.ResponseWriter, r *http.Request, group string)
return
false
}
p
,
err
:=
getPermission
(
desc
,
c
lientCredentials
{
user
,
pass
})
p
,
err
:=
desc
.
GetPermission
(
group
.
C
lientCredentials
{
user
,
pass
})
if
err
!=
nil
||
!
p
.
Record
{
return
false
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment