Feature: codec restrictions.
In order to ensure clients are publishing with compatible codecs, the server can enforce codec restrictions.
This commit is contained in:
@@ -40,7 +40,6 @@ func printPorts(c *cli.Context) error {
|
||||
}
|
||||
|
||||
if conf.TURN.Enabled {
|
||||
udpPorts = append(udpPorts, fmt.Sprintf("%d-%d", conf.TURN.PortRangeStart, conf.TURN.PortRangeEnd))
|
||||
tcpPorts = append(tcpPorts, strconv.Itoa(conf.TURN.TLSPort))
|
||||
}
|
||||
|
||||
|
||||
+18
-3
@@ -52,6 +52,21 @@ keys:
|
||||
key1: secret1
|
||||
key2: secret2
|
||||
|
||||
# Default room config
|
||||
# Each room created will inherit these settings. If rooms are created explicitly with CreateRoom, they will take
|
||||
# precedence over defaults
|
||||
#room:
|
||||
# # number of seconds to leave a room open when it's empty
|
||||
# empty_timeout: 300
|
||||
# # limit number of participants that can be in a room, 0 for no limit
|
||||
# max_participants: 0
|
||||
# # only accept specific codecs for clients publishing to this room
|
||||
# # this is useful to standardize codecs across clients
|
||||
# # other supported codecs are video/h264, video/vp9
|
||||
# enabled_codecs:
|
||||
# - mime: audio/opus
|
||||
# - mime: video/vp8
|
||||
|
||||
# customize audio level sensitivity
|
||||
#audio:
|
||||
# # minimum level to be considered active, 0-127, where 0 is loudest
|
||||
@@ -59,12 +74,12 @@ keys:
|
||||
# active_level: 40
|
||||
# # percentile to measure, a participant is considered active if it has exceeded the
|
||||
# # ActiveLevel more than MinPercentile% of the time
|
||||
# # defaults to 15
|
||||
# min_percentile: 15
|
||||
# # defaults to 40
|
||||
# min_percentile: 40
|
||||
# # frequency in ms to notify changes to clients, defaults to 500
|
||||
# update_interval: 500
|
||||
# # to prevent speaker updates from too jumpy, smooth out values over N samples
|
||||
# smooth_samples: 5
|
||||
# smooth_samples: 8
|
||||
|
||||
# turn server
|
||||
turn:
|
||||
|
||||
@@ -10,7 +10,7 @@ require (
|
||||
github.com/google/wire v0.5.0
|
||||
github.com/gorilla/websocket v1.4.2
|
||||
github.com/jxskiss/base62 v0.0.0-20191017122030-4f11678b909b
|
||||
github.com/livekit/protocol v0.5.4
|
||||
github.com/livekit/protocol v0.5.5
|
||||
github.com/magefile/mage v1.11.0
|
||||
github.com/maxbrunsfeld/counterfeiter/v6 v6.3.0
|
||||
github.com/mitchellh/go-homedir v1.1.0
|
||||
|
||||
@@ -236,6 +236,8 @@ github.com/livekit/pion-webrtc/v3 v3.0.30 h1:2RomVfztLegWIePXcyPFJDiF5pbT64aIX4R
|
||||
github.com/livekit/pion-webrtc/v3 v3.0.30/go.mod h1:XFQeLYBf++bWWA0sJqh6zF1ouWluosxwTOMOoTZGaD0=
|
||||
github.com/livekit/protocol v0.5.4 h1:OFOOahFXGOusgd8X1Ucnl8H/sxa3DzwSrnmKNIUOu/0=
|
||||
github.com/livekit/protocol v0.5.4/go.mod h1:wo3CGfYB7XMF8GoVJAfTARrYSP/ombi+sbLl6AYdKP0=
|
||||
github.com/livekit/protocol v0.5.5 h1:ZsfmTpaApf2o75qpJLP2f/FsCOwxW2sDJE3HKDa2OXE=
|
||||
github.com/livekit/protocol v0.5.5/go.mod h1:wo3CGfYB7XMF8GoVJAfTARrYSP/ombi+sbLl6AYdKP0=
|
||||
github.com/lucsky/cuid v1.0.2 h1:z4XlExeoderxoPj2/dxKOyPxe9RCOu7yNq9/XWxIUMQ=
|
||||
github.com/lucsky/cuid v1.0.2/go.mod h1:QaaJqckboimOmhRSJXSx/+IT+VTfxfPGSo/6mfgUfmE=
|
||||
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
|
||||
|
||||
+33
-14
@@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/mitchellh/go-homedir"
|
||||
"github.com/pion/webrtc/v3"
|
||||
"github.com/urfave/cli/v2"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
@@ -15,6 +16,7 @@ type Config struct {
|
||||
RTC RTCConfig `yaml:"rtc"`
|
||||
Redis RedisConfig `yaml:"redis"`
|
||||
Audio AudioConfig `yaml:"audio"`
|
||||
Room RoomConfig `yaml:"room"`
|
||||
TURN TURNConfig `yaml:"turn"`
|
||||
KeyFile string `yaml:"key_file"`
|
||||
Keys map[string]string `yaml:"keys"`
|
||||
@@ -69,14 +71,23 @@ type RedisConfig struct {
|
||||
DB int `yaml:"db"`
|
||||
}
|
||||
|
||||
type RoomConfig struct {
|
||||
EnabledCodecs []CodecSpec `yaml:"enabled_codecs"`
|
||||
MaxParticipants uint32 `yaml:"max_participants"`
|
||||
EmptyTimeout uint32 `yaml:"empty_timeout"`
|
||||
}
|
||||
|
||||
type CodecSpec struct {
|
||||
Mime string `yaml:"mime"`
|
||||
FmtpLine string `yaml:"fmtp_line"`
|
||||
}
|
||||
|
||||
type TURNConfig struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
Domain string `yaml:"domain"`
|
||||
CertFile string `yaml:"cert_file"`
|
||||
KeyFile string `yaml:"key_file"`
|
||||
TLSPort int `yaml:"tls_port"`
|
||||
PortRangeStart uint16 `yaml:"port_range_start"`
|
||||
PortRangeEnd uint16 `yaml:"port_range_end"`
|
||||
Enabled bool `yaml:"enabled"`
|
||||
Domain string `yaml:"domain"`
|
||||
CertFile string `yaml:"cert_file"`
|
||||
KeyFile string `yaml:"key_file"`
|
||||
TLSPort int `yaml:"tls_port"`
|
||||
}
|
||||
|
||||
func NewConfig(confString string) (*Config, error) {
|
||||
@@ -103,21 +114,29 @@ func NewConfig(confString string) (*Config, error) {
|
||||
},
|
||||
Audio: AudioConfig{
|
||||
ActiveLevel: 40,
|
||||
MinPercentile: 15,
|
||||
MinPercentile: 40,
|
||||
UpdateInterval: 500,
|
||||
SmoothSamples: 5,
|
||||
SmoothSamples: 8,
|
||||
},
|
||||
Redis: RedisConfig{},
|
||||
Room: RoomConfig{
|
||||
// by default only enable opus and VP8
|
||||
EnabledCodecs: []CodecSpec{
|
||||
{Mime: webrtc.MimeTypeOpus},
|
||||
{Mime: webrtc.MimeTypeVP8},
|
||||
//{Mime: webrtc.MimeTypeH264},
|
||||
//{Mime: webrtc.MimeTypeVP9},
|
||||
},
|
||||
EmptyTimeout: 5 * 60,
|
||||
},
|
||||
TURN: TURNConfig{
|
||||
Enabled: false,
|
||||
TLSPort: 3478,
|
||||
PortRangeStart: 12000,
|
||||
PortRangeEnd: 14000,
|
||||
Enabled: false,
|
||||
TLSPort: 3478,
|
||||
},
|
||||
Keys: map[string]string{},
|
||||
}
|
||||
if confString != "" {
|
||||
yaml.Unmarshal([]byte(confString), conf)
|
||||
_ = yaml.Unmarshal([]byte(confString), conf)
|
||||
}
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
+28
-8
@@ -1,6 +1,9 @@
|
||||
package rtc
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
livekit "github.com/livekit/livekit-server/proto"
|
||||
"github.com/pion/sdp/v3"
|
||||
"github.com/pion/webrtc/v3"
|
||||
)
|
||||
@@ -9,13 +12,16 @@ const (
|
||||
frameMarking = "urn:ietf:params:rtp-hdrext:framemarking"
|
||||
)
|
||||
|
||||
func createPubMediaEngine() (*webrtc.MediaEngine, error) {
|
||||
func createPubMediaEngine(codecs []*livekit.Codec) (*webrtc.MediaEngine, error) {
|
||||
me := &webrtc.MediaEngine{}
|
||||
if err := me.RegisterCodec(webrtc.RTPCodecParameters{
|
||||
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeOpus, ClockRate: 48000, Channels: 2, SDPFmtpLine: "minptime=10;useinbandfec=1", RTCPFeedback: nil},
|
||||
PayloadType: 111,
|
||||
}, webrtc.RTPCodecTypeAudio); err != nil {
|
||||
return nil, err
|
||||
opusCodec := webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeOpus, ClockRate: 48000, Channels: 2, SDPFmtpLine: "minptime=10;useinbandfec=1", RTCPFeedback: nil}
|
||||
if isCodecEnabled(codecs, opusCodec) {
|
||||
if err := me.RegisterCodec(webrtc.RTPCodecParameters{
|
||||
RTPCodecCapability: opusCodec,
|
||||
PayloadType: 111,
|
||||
}, webrtc.RTPCodecTypeAudio); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
videoRTCPFeedback := []webrtc.RTCPFeedback{
|
||||
@@ -57,8 +63,10 @@ func createPubMediaEngine() (*webrtc.MediaEngine, error) {
|
||||
PayloadType: 123,
|
||||
},
|
||||
} {
|
||||
if err := me.RegisterCodec(codec, webrtc.RTPCodecTypeVideo); err != nil {
|
||||
return nil, err
|
||||
if isCodecEnabled(codecs, codec.RTPCodecCapability) {
|
||||
if err := me.RegisterCodec(codec, webrtc.RTPCodecTypeVideo); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,3 +97,15 @@ func createSubMediaEngine() (*webrtc.MediaEngine, error) {
|
||||
me := &webrtc.MediaEngine{}
|
||||
return me, nil
|
||||
}
|
||||
|
||||
func isCodecEnabled(codecs []*livekit.Codec, cap webrtc.RTPCodecCapability) bool {
|
||||
for _, codec := range codecs {
|
||||
if !strings.EqualFold(codec.Mime, cap.MimeType) {
|
||||
continue
|
||||
}
|
||||
if codec.FmtpLine == "" || strings.EqualFold(codec.FmtpLine, cap.SDPFmtpLine) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package rtc
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
livekit "github.com/livekit/livekit-server/proto"
|
||||
"github.com/pion/webrtc/v3"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestIsCodecEnabled(t *testing.T) {
|
||||
t.Run("empty fmtp requirement should match all", func(t *testing.T) {
|
||||
enabledCodecs := []*livekit.Codec{{Mime: "video/h264"}}
|
||||
require.True(t, isCodecEnabled(enabledCodecs, webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeH264, SDPFmtpLine: "special"}))
|
||||
require.True(t, isCodecEnabled(enabledCodecs, webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeH264}))
|
||||
require.False(t, isCodecEnabled(enabledCodecs, webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8}))
|
||||
})
|
||||
|
||||
t.Run("when fmtp is provided, require match", func(t *testing.T) {
|
||||
enabledCodecs := []*livekit.Codec{{Mime: "video/h264", FmtpLine: "special"}}
|
||||
require.True(t, isCodecEnabled(enabledCodecs, webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeH264, SDPFmtpLine: "special"}))
|
||||
require.False(t, isCodecEnabled(enabledCodecs, webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeH264}))
|
||||
require.False(t, isCodecEnabled(enabledCodecs, webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8}))
|
||||
})
|
||||
}
|
||||
@@ -38,6 +38,7 @@ type ParticipantParams struct {
|
||||
ProtocolVersion types.ProtocolVersion
|
||||
Stats *RoomStatsReporter
|
||||
ThrottleConfig config.PLIThrottleConfig
|
||||
EnabledCodecs []*livekit.Codec
|
||||
}
|
||||
|
||||
type ParticipantImpl struct {
|
||||
@@ -102,9 +103,10 @@ func NewParticipant(params ParticipantParams) (*ParticipantImpl, error) {
|
||||
|
||||
var err error
|
||||
p.publisher, err = NewPCTransport(TransportParams{
|
||||
Target: livekit.SignalTarget_PUBLISHER,
|
||||
Config: params.Config,
|
||||
Stats: p.params.Stats,
|
||||
Target: livekit.SignalTarget_PUBLISHER,
|
||||
Config: params.Config,
|
||||
Stats: p.params.Stats,
|
||||
EnabledCodecs: p.params.EnabledCodecs,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -39,16 +39,17 @@ type PCTransport struct {
|
||||
}
|
||||
|
||||
type TransportParams struct {
|
||||
Target livekit.SignalTarget
|
||||
Config *WebRTCConfig
|
||||
Stats *RoomStatsReporter
|
||||
Target livekit.SignalTarget
|
||||
Config *WebRTCConfig
|
||||
Stats *RoomStatsReporter
|
||||
EnabledCodecs []*livekit.Codec
|
||||
}
|
||||
|
||||
func newPeerConnection(params TransportParams) (*webrtc.PeerConnection, *webrtc.MediaEngine, error) {
|
||||
var me *webrtc.MediaEngine
|
||||
var err error
|
||||
if params.Target == livekit.SignalTarget_PUBLISHER {
|
||||
me, err = createPubMediaEngine()
|
||||
me, err = createPubMediaEngine(params.EnabledCodecs)
|
||||
} else {
|
||||
me, err = createSubMediaEngine()
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ func NewRoomManager(rp RoomStore, router routing.Router, currentNode routing.Loc
|
||||
}
|
||||
|
||||
// CreateRoom creates a new room from a request and allocates it to a node to handle
|
||||
// it'll also monitor its state, and cleans it up when appropriate
|
||||
// it'll also monitor fits state, and cleans it up when appropriate
|
||||
func (r *RoomManager) CreateRoom(req *livekit.CreateRoomRequest) (*livekit.Room, error) {
|
||||
// find existing room and update it
|
||||
rm, err := r.roomStore.GetRoom(req.Name)
|
||||
@@ -63,6 +63,7 @@ func (r *RoomManager) CreateRoom(req *livekit.CreateRoomRequest) (*livekit.Room,
|
||||
CreationTime: time.Now().Unix(),
|
||||
TurnPassword: utils.RandomSecret(),
|
||||
}
|
||||
applyDefaultRoomConfig(rm, &r.config.Room)
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -268,6 +269,7 @@ func (r *RoomManager) StartSession(roomName string, pi routing.ParticipantInit,
|
||||
ProtocolVersion: pv,
|
||||
Stats: room.GetStatsReporter(),
|
||||
ThrottleConfig: r.config.RTC.PLIThrottle,
|
||||
EnabledCodecs: room.Room.EnabledCodecs,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Errorw("could not create participant", err)
|
||||
@@ -488,3 +490,14 @@ func (r *RoomManager) iceServersForRoom(ri *livekit.Room) []*livekit.ICEServer {
|
||||
}
|
||||
return iceServers
|
||||
}
|
||||
|
||||
func applyDefaultRoomConfig(room *livekit.Room, conf *config.RoomConfig) {
|
||||
room.EmptyTimeout = conf.EmptyTimeout
|
||||
room.MaxParticipants = conf.MaxParticipants
|
||||
for _, codec := range conf.EnabledCodecs {
|
||||
room.EnabledCodecs = append(room.EnabledCodecs, &livekit.Codec{
|
||||
Mime: codec.Mime,
|
||||
FmtpLine: codec.FmtpLine,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package service_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/config"
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/pkg/routing/routingfakes"
|
||||
"github.com/livekit/livekit-server/pkg/service"
|
||||
"github.com/livekit/livekit-server/pkg/service/servicefakes"
|
||||
livekit "github.com/livekit/livekit-server/proto"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCreateRoom(t *testing.T) {
|
||||
manager, conf := newTestRoomManager(t)
|
||||
|
||||
t.Run("ensure default room settings are applied", func(t *testing.T) {
|
||||
room, err := manager.CreateRoom(&livekit.CreateRoomRequest{Name: "myroom"})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, conf.Room.EmptyTimeout, room.EmptyTimeout)
|
||||
require.Len(t, room.EnabledCodecs, 4)
|
||||
})
|
||||
}
|
||||
|
||||
func newTestRoomManager(t *testing.T) (*service.RoomManager, *config.Config) {
|
||||
store := &servicefakes.FakeRoomStore{}
|
||||
store.GetRoomReturns(nil, service.ErrRoomNotFound)
|
||||
router := &routingfakes.FakeRouter{}
|
||||
conf, err := config.NewConfig("")
|
||||
require.NoError(t, err)
|
||||
selector := &routing.RandomSelector{}
|
||||
node, err := routing.NewLocalNode(conf)
|
||||
require.NoError(t, err)
|
||||
|
||||
router.GetNodeForRoomReturns(node, nil)
|
||||
|
||||
rm, err := service.NewRoomManager(store, router, node, selector, conf)
|
||||
require.NoError(t, err)
|
||||
|
||||
return rm, conf
|
||||
}
|
||||
+6
-6
@@ -2,7 +2,6 @@ package service
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
@@ -15,7 +14,9 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
allocateRetries = 1000
|
||||
allocateRetries = 50
|
||||
turnMinPort = 1024
|
||||
turnMaxPort = 30000
|
||||
livekitRealm = "livekit"
|
||||
)
|
||||
|
||||
@@ -53,8 +54,8 @@ func NewTurnServer(conf *config.Config, roomStore RoomStore, node routing.LocalN
|
||||
RelayAddressGenerator: &turn.RelayAddressGeneratorPortRange{
|
||||
RelayAddress: net.ParseIP(node.Ip),
|
||||
Address: "0.0.0.0",
|
||||
MinPort: turnConf.PortRangeStart,
|
||||
MaxPort: turnConf.PortRangeEnd,
|
||||
MinPort: turnMinPort,
|
||||
MaxPort: turnMaxPort,
|
||||
MaxRetries: allocateRetries,
|
||||
},
|
||||
},
|
||||
@@ -62,8 +63,7 @@ func NewTurnServer(conf *config.Config, roomStore RoomStore, node routing.LocalN
|
||||
}
|
||||
|
||||
logger.Infow("Starting TURN server",
|
||||
"TCP port", turnConf.TLSPort,
|
||||
"portRange", fmt.Sprintf("%d-%d", turnConf.PortRangeStart, turnConf.PortRangeEnd))
|
||||
"TLS port", turnConf.TLSPort)
|
||||
return turn.NewServer(serverConfig)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user