updated to ion-sfu v1.9.1, removed package bufferFactory

This commit is contained in:
David Zhao
2021-03-01 14:49:46 -08:00
parent e8d2701937
commit 03d0a8c047
14 changed files with 69 additions and 102 deletions
+4
View File
@@ -26,6 +26,9 @@ type RTCConfig struct {
StunServers []string `yaml:"stun_servers"`
UseExternalIP bool `yaml:"use_external_ip"`
// Number of packets to buffer for NACK
PacketBufferSize int `yaml:"packet_buffer_size"`
// Max bitrate for REMB
MaxBitrate uint64 `yaml:"max_bitrate"`
MaxBufferTime int `yaml:"max_buffer_time"`
@@ -51,6 +54,7 @@ func NewConfig(confString string) (*Config, error) {
conf := &Config{
Port: 7880,
RTC: RTCConfig{
UseExternalIP: true,
ICEPortRangeStart: 9000,
ICEPortRangeEnd: 9200,
StunServers: []string{
+18 -9
View File
@@ -3,6 +3,7 @@ package rtc
import (
"fmt"
"github.com/pion/ion-sfu/pkg/buffer"
"github.com/pion/webrtc/v3"
"github.com/livekit/livekit-server/pkg/config"
@@ -12,16 +13,16 @@ type WebRTCConfig struct {
Configuration webrtc.Configuration
SettingEngine webrtc.SettingEngine
Receiver ReceiverConfig
BufferFactory *buffer.Factory
}
type ReceiverConfig struct {
maxBitrate uint64
maxBufferTime int
packetBufferSize int
maxBitrate uint64
maxBufferTime int
}
type ExternalIP string
func NewWebRTCConfig(conf *config.RTCConfig, externalIP ExternalIP) (*WebRTCConfig, error) {
func NewWebRTCConfig(conf *config.RTCConfig, externalIP string) (*WebRTCConfig, error) {
c := webrtc.Configuration{
SDPSemantics: webrtc.SDPSemanticsUnifiedPlan,
}
@@ -42,16 +43,24 @@ func NewWebRTCConfig(conf *config.RTCConfig, externalIP ExternalIP) (*WebRTCConf
URLs: iceUrls,
},
}
if conf.UseExternalIP {
s.SetNAT1To1IPs([]string{string(externalIP)}, webrtc.ICECandidateTypeHost)
if conf.UseExternalIP && externalIP != "" {
s.SetNAT1To1IPs([]string{externalIP}, webrtc.ICECandidateTypeHost)
}
if conf.PacketBufferSize == 0 {
conf.PacketBufferSize = 500
}
bufferFactory := buffer.NewBufferFactory(conf.PacketBufferSize)
s.BufferFactory = bufferFactory.GetOrNew
return &WebRTCConfig{
Configuration: c,
SettingEngine: s,
BufferFactory: bufferFactory,
Receiver: ReceiverConfig{
maxBitrate: conf.MaxBitrate,
maxBufferTime: conf.MaxBufferTime,
packetBufferSize: conf.PacketBufferSize,
maxBitrate: conf.MaxBitrate,
maxBufferTime: conf.MaxBufferTime,
},
}, nil
}
-21
View File
@@ -1,21 +0,0 @@
package rtc
import (
"sync"
"github.com/pion/ion-sfu/pkg/buffer"
)
const (
rtpPacketMaxSize = 1460
)
// package level factories for buffers
var (
bufferFactory = buffer.NewBufferFactory()
packetFactory = &sync.Pool{
New: func() interface{} {
return make([]byte, rtpPacketMaxSize)
},
}
)
+13 -13
View File
@@ -33,14 +33,15 @@ type MediaTrack struct {
participantId string
muted utils.AtomicFlag
ssrc webrtc.SSRC
name string
streamID string
kind livekit.TrackType
codec webrtc.RTPCodecParameters
receiverConf ReceiverConfig
audioConf config.AudioConfig
onClose func()
ssrc webrtc.SSRC
name string
streamID string
kind livekit.TrackType
codec webrtc.RTPCodecParameters
bufferFactory *buffer.Factory
receiverConf ReceiverConfig
audioConf config.AudioConfig
onClose func()
// channel to send RTCP packets to the source
rtcpCh chan []rtcp.Packet
@@ -54,7 +55,7 @@ type MediaTrack struct {
lastPLI time.Time
}
func NewMediaTrack(trackId string, pId string, rtcpCh chan []rtcp.Packet, track *webrtc.TrackRemote, rc ReceiverConfig, ac config.AudioConfig) *MediaTrack {
func NewMediaTrack(trackId string, pId string, rtcpCh chan []rtcp.Packet, track *webrtc.TrackRemote, bufferFactory *buffer.Factory, rc ReceiverConfig, ac config.AudioConfig) *MediaTrack {
t := &MediaTrack{
id: trackId,
participantId: pId,
@@ -62,6 +63,7 @@ func NewMediaTrack(trackId string, pId string, rtcpCh chan []rtcp.Packet, track
streamID: track.StreamID(),
kind: ToProtoTrackKind(track.Kind()),
codec: track.Codec(),
bufferFactory: bufferFactory,
receiverConf: rc,
audioConf: ac,
rtcpCh: rtcpCh,
@@ -136,11 +138,10 @@ func (t *MediaTrack) AddSubscriber(sub types.Participant) error {
Channels: codec.Channels,
SDPFmtpLine: codec.SDPFmtpLine,
RTCPFeedback: feedbackTypes,
}, t.receiver, sub.ID())
}, t.receiver, t.bufferFactory, sub.ID(), t.receiverConf.packetBufferSize)
if err != nil {
return err
}
downTrack.SetBufferFactory(bufferFactory)
subTrack := NewSubscribedTrack(downTrack)
transceiver, err := sub.SubscriberPC().AddTransceiverFromTrack(downTrack, webrtc.RTPTransceiverInit{
@@ -205,7 +206,7 @@ func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.Tra
defer t.lock.Unlock()
//rid := track.RID()
buff, rtcpReader := bufferFactory.GetBufferPair(uint32(track.SSRC()))
buff, rtcpReader := t.bufferFactory.GetBufferPair(uint32(track.SSRC()))
buff.OnFeedback(func(fb []rtcp.Packet) {
// feedback for the source RTCP
t.rtcpCh <- fb
@@ -258,7 +259,6 @@ func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.Tra
t.receiver.AddUpTrack(track, buff)
buff.Bind(receiver.GetParameters(), buffer.Options{
BufferTime: t.receiverConf.maxBufferTime,
MaxBitRate: t.receiverConf.maxBitrate,
})
}
+12 -11
View File
@@ -29,19 +29,20 @@ const (
)
type ParticipantImpl struct {
id string
publisher *PCTransport
subscriber *PCTransport
responseSink routing.MessageSink
receiverConfig ReceiverConfig
audioConfig config.AudioConfig
isClosed utils.AtomicFlag
identity string
id string
publisher *PCTransport
subscriber *PCTransport
responseSink routing.MessageSink
audioConfig config.AudioConfig
isClosed utils.AtomicFlag
conf *WebRTCConfig
identity string
// JSON encoded metadata to pass to clients
metadata string
state atomic.Value // livekit.ParticipantInfo_State
rtcpCh chan []rtcp.Packet
// hold reference for MediaTrack
twcc *twcc.Responder
// tracks the current participant is subscribed to, map of otherParticipantId => []DownTrack
@@ -61,15 +62,15 @@ type ParticipantImpl struct {
onClose func(types.Participant)
}
func NewParticipant(identity string, conf *WebRTCConfig, rs routing.MessageSink, rc ReceiverConfig, ac config.AudioConfig) (*ParticipantImpl, error) {
func NewParticipant(identity string, conf *WebRTCConfig, rs routing.MessageSink, ac config.AudioConfig) (*ParticipantImpl, error) {
// TODO: check to ensure params are valid, id and identity can't be empty
p := &ParticipantImpl{
id: utils.NewGuid(utils.ParticipantPrefix),
identity: identity,
responseSink: rs,
receiverConfig: rc,
audioConfig: ac,
conf: conf,
rtcpCh: make(chan []rtcp.Packet, 50),
subscribedTracks: make(map[string][]types.SubscribedTrack),
lock: sync.RWMutex{},
@@ -593,7 +594,7 @@ func (p *ParticipantImpl) onMediaTrack(track *webrtc.TrackRemote, rtpReceiver *w
if trk, ok := ptrack.(*MediaTrack); ok {
mt = trk
} else {
mt = NewMediaTrack(ti.Sid, p.id, p.rtcpCh, track, p.receiverConfig, p.audioConfig)
mt = NewMediaTrack(ti.Sid, p.id, p.rtcpCh, track, p.conf.BufferFactory, p.conf.Receiver, p.audioConfig)
mt.name = ti.Name
newTrack = true
}
+3 -2
View File
@@ -120,11 +120,12 @@ func TestDisconnectTiming(t *testing.T) {
}
func newParticipantForTest(identity string) *ParticipantImpl {
conf, _ := config.NewConfig("")
rtcConf, _ := NewWebRTCConfig(&conf.RTC, "")
p, _ := NewParticipant(
identity,
&WebRTCConfig{},
rtcConf,
&routingfakes.FakeMessageSink{},
ReceiverConfig{},
config.AudioConfig{})
return p
}
-1
View File
@@ -50,7 +50,6 @@ func newPeerConnection(target livekit.SignalTarget, conf *WebRTCConfig) (*webrtc
}
se := conf.SettingEngine
se.DisableMediaEngineCopy(true)
se.BufferFactory = bufferFactory.GetOrNew
api := webrtc.NewAPI(webrtc.WithMediaEngine(me), webrtc.WithSettingEngine(se))
pc, err := api.NewPeerConnection(conf.Configuration)
-11
View File
@@ -5,11 +5,9 @@ import (
"io"
"strings"
"github.com/google/wire"
"github.com/pion/webrtc/v3"
"go.uber.org/zap"
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/logger"
"github.com/livekit/livekit-server/pkg/rtc/types"
"github.com/livekit/livekit-server/proto/livekit"
@@ -19,15 +17,6 @@ const (
trackIdSeparator = "|"
)
var RTCSet = wire.NewSet(
NewWebRTCConfig,
RTCConfigFromConfig,
)
func RTCConfigFromConfig(conf *config.Config) *config.RTCConfig {
return &conf.RTC
}
func UnpackTrackId(packed string) (peerId string, trackId string) {
parts := strings.Split(packed, trackIdSeparator)
if len(parts) > 1 {
+10 -5
View File
@@ -33,17 +33,22 @@ type RoomManager struct {
rooms map[string]*rtc.Room
}
func NewRoomManager(rp RoomStore, router routing.Router, currentNode routing.LocalNode, selector routing.NodeSelector, config *rtc.WebRTCConfig, ac config.AudioConfig) *RoomManager {
func NewRoomManager(rp RoomStore, router routing.Router, currentNode routing.LocalNode, selector routing.NodeSelector, config *config.Config) (*RoomManager, error) {
rtcConf, err := rtc.NewWebRTCConfig(&config.RTC, currentNode.Ip)
if err != nil {
return nil, err
}
return &RoomManager{
lock: sync.RWMutex{},
roomStore: rp,
config: config,
audioConfig: ac,
config: rtcConf,
audioConfig: config.Audio,
router: router,
selector: selector,
currentNode: currentNode,
rooms: make(map[string]*rtc.Room),
}
}, nil
}
// CreateRoom creates a new room from a request and allocates it to a node to handle
@@ -203,7 +208,7 @@ func (r *RoomManager) StartSession(roomName, identity, metadata string, reconnec
"num_participants", len(room.GetParticipants()),
)
participant, err = rtc.NewParticipant(identity, r.config, responseSink, r.config.Receiver, r.audioConfig)
participant, err = rtc.NewParticipant(identity, r.config, responseSink, r.audioConfig)
if err != nil {
logger.Errorw("could not create participant", "error", err)
return
-8
View File
@@ -8,8 +8,6 @@ import (
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/logger"
"github.com/livekit/livekit-server/pkg/routing"
"github.com/livekit/livekit-server/pkg/rtc"
"github.com/livekit/livekit-server/proto/livekit"
)
@@ -20,14 +18,8 @@ var ServiceSet = wire.NewSet(
NewRoomManager,
config.GetAudioConfig,
wire.Bind(new(livekit.RoomService), new(*RoomService)),
externalIpFromNode,
)
// helper to construct RTCConfig
func externalIpFromNode(currentNode routing.LocalNode) rtc.ExternalIP {
return rtc.ExternalIP(currentNode.Ip)
}
func handleError(w http.ResponseWriter, status int, msg string) {
l := logger.Desugar().WithOptions(zap.AddCallerSkip(1))
l.Debug("error handling request", zap.String("error", msg), zap.Int("status", status))
-2
View File
@@ -8,7 +8,6 @@ import (
"github.com/livekit/livekit-server/pkg/auth"
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/routing"
"github.com/livekit/livekit-server/pkg/rtc"
)
func InitializeServer(conf *config.Config, keyProvider auth.KeyProvider,
@@ -16,7 +15,6 @@ func InitializeServer(conf *config.Config, keyProvider auth.KeyProvider,
selector routing.NodeSelector) (*LivekitServer, error) {
wire.Build(
ServiceSet,
rtc.RTCSet,
)
return &LivekitServer{}, nil
}
+1 -6
View File
@@ -9,20 +9,15 @@ import (
"github.com/livekit/livekit-server/pkg/auth"
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/routing"
"github.com/livekit/livekit-server/pkg/rtc"
)
// Injectors from wire.go:
func InitializeServer(conf *config.Config, keyProvider auth.KeyProvider, roomStore RoomStore, router routing.Router, currentNode routing.LocalNode, selector routing.NodeSelector) (*LivekitServer, error) {
rtcConfig := rtc.RTCConfigFromConfig(conf)
externalIP := externalIpFromNode(currentNode)
webRTCConfig, err := rtc.NewWebRTCConfig(rtcConfig, externalIP)
roomManager, err := NewRoomManager(roomStore, router, currentNode, selector, conf)
if err != nil {
return nil, err
}
audioConfig := config.GetAudioConfig(conf)
roomManager := NewRoomManager(roomStore, router, currentNode, selector, webRTCConfig, audioConfig)
roomService, err := NewRoomService(roomManager)
if err != nil {
return nil, err