decouple websocket and RTC nodes, prep for distributed
This commit is contained in:
+4
-2
@@ -19,7 +19,9 @@ type ReceiverConfig struct {
|
||||
maxBufferTime int
|
||||
}
|
||||
|
||||
func NewWebRTCConfig(conf *config.RTCConfig, externalIP string) (*WebRTCConfig, error) {
|
||||
type ExternalIP string
|
||||
|
||||
func NewWebRTCConfig(conf *config.RTCConfig, externalIP ExternalIP) (*WebRTCConfig, error) {
|
||||
c := webrtc.Configuration{
|
||||
SDPSemantics: webrtc.SDPSemanticsUnifiedPlan,
|
||||
}
|
||||
@@ -41,7 +43,7 @@ func NewWebRTCConfig(conf *config.RTCConfig, externalIP string) (*WebRTCConfig,
|
||||
},
|
||||
}
|
||||
if conf.UseExternalIP {
|
||||
s.SetNAT1To1IPs([]string{externalIP}, webrtc.ICECandidateTypeHost)
|
||||
s.SetNAT1To1IPs([]string{string(externalIP)}, webrtc.ICECandidateTypeHost)
|
||||
}
|
||||
|
||||
return &WebRTCConfig{
|
||||
|
||||
@@ -141,7 +141,7 @@ func (t *DataTrack) forwardWorker() {
|
||||
for _, sub := range t.subscribers {
|
||||
err := sub.SendMessage(msg)
|
||||
if err != nil {
|
||||
logger.GetLogger().Errorw("could not send data message",
|
||||
logger.Errorw("could not send data message",
|
||||
"err", err,
|
||||
"source", t.participantId,
|
||||
"dest", sub.participantId)
|
||||
|
||||
@@ -1,99 +0,0 @@
|
||||
package rtc
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/config"
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
)
|
||||
|
||||
// A RoomManager maintains active rooms that are hosted on the current node
|
||||
type RoomManager struct {
|
||||
rtcConf config.RTCConfig
|
||||
externalIP string
|
||||
|
||||
config WebRTCConfig
|
||||
|
||||
rooms map[string]*Room
|
||||
roomsByName map[string]*Room
|
||||
roomLock sync.RWMutex
|
||||
}
|
||||
|
||||
func NewRoomManager(rtcConf config.RTCConfig, externalIP string) (m *RoomManager, err error) {
|
||||
m = &RoomManager{
|
||||
rtcConf: rtcConf,
|
||||
externalIP: externalIP,
|
||||
rooms: make(map[string]*Room),
|
||||
roomsByName: make(map[string]*Room),
|
||||
roomLock: sync.RWMutex{},
|
||||
}
|
||||
|
||||
wc, err := NewWebRTCConfig(&rtcConf, externalIP)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
m.config = *wc
|
||||
return
|
||||
}
|
||||
|
||||
func (m *RoomManager) GetRoom(idOrName string) *Room {
|
||||
m.roomLock.RLock()
|
||||
defer m.roomLock.RUnlock()
|
||||
rm := m.rooms[idOrName]
|
||||
if rm == nil {
|
||||
rm = m.roomsByName[idOrName]
|
||||
}
|
||||
return rm
|
||||
}
|
||||
|
||||
func (m *RoomManager) GetRoomWithConstraint(idOrName string, onlyName string) (*Room, error) {
|
||||
if idOrName == "" {
|
||||
idOrName = onlyName
|
||||
}
|
||||
if idOrName == "" {
|
||||
return nil, ErrRoomIdMissing
|
||||
}
|
||||
|
||||
rm := m.GetRoom(idOrName)
|
||||
if rm == nil {
|
||||
return nil, ErrRoomNotFound
|
||||
}
|
||||
|
||||
if onlyName != "" && rm.Name != onlyName {
|
||||
return nil, ErrPermissionDenied
|
||||
}
|
||||
return rm, nil
|
||||
}
|
||||
|
||||
func (m *RoomManager) CreateRoom(req *livekit.CreateRoomRequest) (*Room, error) {
|
||||
if req.Name == "" {
|
||||
return nil, ErrInvalidRoomName
|
||||
}
|
||||
r := NewRoomForRequest(req, &m.config)
|
||||
m.roomLock.Lock()
|
||||
defer m.roomLock.Unlock()
|
||||
|
||||
if m.roomsByName[req.Name] != nil {
|
||||
return nil, ErrInvalidRoomName
|
||||
}
|
||||
|
||||
m.rooms[r.Sid] = r
|
||||
m.roomsByName[r.Name] = r
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (m *RoomManager) DeleteRoom(idOrName string) error {
|
||||
rm := m.GetRoom(idOrName)
|
||||
if rm == nil {
|
||||
return nil
|
||||
}
|
||||
m.roomLock.Lock()
|
||||
defer m.roomLock.Unlock()
|
||||
delete(m.rooms, rm.Sid)
|
||||
delete(m.roomsByName, rm.Name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *RoomManager) Config() *WebRTCConfig {
|
||||
return &m.config
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
package rtc_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/config"
|
||||
"github.com/livekit/livekit-server/pkg/rtc"
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
)
|
||||
|
||||
func TestRoomManager_CreateRoom(t *testing.T) {
|
||||
man := newRoomManager(t)
|
||||
|
||||
t.Run("creation and duplicates", func(t *testing.T) {
|
||||
r := &livekit.CreateRoomRequest{Name: "basic"}
|
||||
rm, err := man.CreateRoom(r)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, rm)
|
||||
|
||||
rm, err = man.CreateRoom(r)
|
||||
assert.Equal(t, rtc.ErrInvalidRoomName, err)
|
||||
assert.Nil(t, rm)
|
||||
})
|
||||
|
||||
t.Run("name is required", func(t *testing.T) {
|
||||
_, err := man.CreateRoom(&livekit.CreateRoomRequest{})
|
||||
assert.Equal(t, rtc.ErrInvalidRoomName, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRoomManager_GetRoomByName(t *testing.T) {
|
||||
man := newRoomManager(t)
|
||||
_, err := man.CreateRoom(&livekit.CreateRoomRequest{
|
||||
Name: "hello",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
rm := man.GetRoom("hello")
|
||||
assert.Equal(t, "hello", rm.Name)
|
||||
}
|
||||
|
||||
func TestRoomManager_GetRoomWithConstraint(t *testing.T) {
|
||||
man := newRoomManager(t)
|
||||
rm, _ := man.CreateRoom(&livekit.CreateRoomRequest{
|
||||
Name: "hello",
|
||||
})
|
||||
|
||||
t.Run("no constraint, get by id", func(t *testing.T) {
|
||||
r, err := man.GetRoomWithConstraint(rm.Sid, "")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, r, rm)
|
||||
})
|
||||
|
||||
t.Run("no constraint, get by name", func(t *testing.T) {
|
||||
r, err := man.GetRoomWithConstraint(rm.Name, "")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, r, rm)
|
||||
})
|
||||
|
||||
t.Run("no constraint, room doesn't exist", func(t *testing.T) {
|
||||
_, err := man.GetRoomWithConstraint("wtf", "")
|
||||
assert.Equal(t, rtc.ErrRoomNotFound, err)
|
||||
})
|
||||
|
||||
t.Run("constraint, no name", func(t *testing.T) {
|
||||
r, err := man.GetRoomWithConstraint("", rm.Name)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, r, rm)
|
||||
})
|
||||
|
||||
t.Run("constraint does not match name", func(t *testing.T) {
|
||||
_, err := man.GetRoomWithConstraint(rm.Name, "anotherroom")
|
||||
assert.Equal(t, rtc.ErrPermissionDenied, err)
|
||||
})
|
||||
}
|
||||
|
||||
func newRoomManager(t *testing.T) *rtc.RoomManager {
|
||||
man, err := rtc.NewRoomManager(config.RTCConfig{}, "1.2.3.4")
|
||||
assert.NoError(t, err)
|
||||
return man
|
||||
}
|
||||
@@ -152,13 +152,13 @@ func (t *MediaTrack) AddSubscriber(participant types.Participant) error {
|
||||
// however, if the dest participant has disconnected, then we can skip
|
||||
sender := transceiver.Sender()
|
||||
if sender != nil {
|
||||
logger.GetLogger().Debugw("removing peerconnection track",
|
||||
logger.Debugw("removing peerconnection track",
|
||||
"track", t.id,
|
||||
"srcParticipant", t.participantId,
|
||||
"destParticipant", participant.ID())
|
||||
if err := participant.PeerConnection().RemoveTrack(sender); err != nil {
|
||||
if _, ok := err.(*rtcerr.InvalidStateError); !ok {
|
||||
logger.GetLogger().Warnw("could not remove remoteTrack from forwarder",
|
||||
logger.Warnw("could not remove remoteTrack from forwarder",
|
||||
"participant", participant.ID(),
|
||||
"err", err)
|
||||
}
|
||||
@@ -188,7 +188,7 @@ func (t *MediaTrack) RemoveSubscriber(participantId string) {
|
||||
}
|
||||
|
||||
func (t *MediaTrack) RemoveAllSubscribers() {
|
||||
logger.GetLogger().Debugw("removing all subscribers", "track", t.id)
|
||||
logger.Debugw("removing all subscribers", "track", t.id)
|
||||
t.lock.RLock()
|
||||
defer t.lock.RUnlock()
|
||||
for _, dt := range t.downtracks {
|
||||
@@ -244,7 +244,7 @@ func (t *MediaTrack) forwardRTPWorker() {
|
||||
}()
|
||||
|
||||
for pkt := range t.receiver.RTPChan() {
|
||||
//logger.GetLogger().Debugw("read packet from remoteTrack",
|
||||
//logger.Debugw("read packet from remoteTrack",
|
||||
// "participant", t.participantId,
|
||||
// "track", t.ID())
|
||||
// when track is muted, it's "disabled" on the client side, and will still be sending black frames
|
||||
@@ -255,7 +255,7 @@ func (t *MediaTrack) forwardRTPWorker() {
|
||||
|
||||
t.lock.RLock()
|
||||
for dstId, dt := range t.downtracks {
|
||||
//logger.GetLogger().Debugw("read packet from remoteTrack",
|
||||
//logger.Debugw("read packet from remoteTrack",
|
||||
// "srcParticipant", t.participantId,
|
||||
// "destParticipant", dstId,
|
||||
// "track", t.ID())
|
||||
@@ -271,7 +271,7 @@ func (t *MediaTrack) forwardRTPWorker() {
|
||||
if delta < maxPLIFrequency {
|
||||
continue
|
||||
}
|
||||
logger.GetLogger().Infow("keyframe required, sending PLI",
|
||||
logger.Infow("keyframe required, sending PLI",
|
||||
"srcParticipant", t.participantId)
|
||||
rtcpPkts := []rtcp.Packet{
|
||||
&rtcp.PictureLossIndication{SenderSSRC: uint32(t.ssrc), MediaSSRC: pkt.SSRC},
|
||||
@@ -279,7 +279,7 @@ func (t *MediaTrack) forwardRTPWorker() {
|
||||
t.rtcpCh <- rtcpPkts
|
||||
t.lastPLI = time.Now()
|
||||
} else if err != nil {
|
||||
logger.GetLogger().Warnw("could not forward packet to participant",
|
||||
logger.Warnw("could not forward packet to participant",
|
||||
"src", t.participantId,
|
||||
"dest", dstId,
|
||||
"remoteTrack", t.id,
|
||||
@@ -294,7 +294,7 @@ func (t *MediaTrack) handleRTCP(dt *sfu.DownTrack, rtcpBuf []byte) {
|
||||
defer Recover()
|
||||
pkts, err := rtcp.Unmarshal(rtcpBuf)
|
||||
if err != nil {
|
||||
logger.GetLogger().Warnw("could not decode RTCP packet", "err", err)
|
||||
logger.Warnw("could not decode RTCP packet", "err", err)
|
||||
}
|
||||
|
||||
var fwdPkts []rtcp.Packet
|
||||
@@ -322,7 +322,7 @@ func (t *MediaTrack) handleRTCP(dt *sfu.DownTrack, rtcpBuf []byte) {
|
||||
//log.Tracef("Slow link for sender %s, fraction packet lost %.2f", f.track.peerID, float64(p.Reports[0].FractionLost)/256)
|
||||
}
|
||||
case *rtcp.TransportLayerNack:
|
||||
logger.GetLogger().Debugw("forwarder got nack",
|
||||
logger.Debugw("forwarder got nack",
|
||||
"packet", p)
|
||||
var nackedPackets []uint16
|
||||
for _, pair := range p.Nacks {
|
||||
|
||||
+32
-30
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/logger"
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
"github.com/livekit/livekit-server/pkg/sfu"
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
@@ -33,7 +34,7 @@ const (
|
||||
type ParticipantImpl struct {
|
||||
id string
|
||||
peerConn types.PeerConnection
|
||||
sigConn types.SignalConnection
|
||||
responseSink routing.MessageSink
|
||||
receiverConfig ReceiverConfig
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
@@ -69,19 +70,21 @@ func NewPeerConnection(conf *WebRTCConfig) (*webrtc.PeerConnection, error) {
|
||||
se.BufferFactory = bufferFactory.GetOrNew
|
||||
|
||||
api := webrtc.NewAPI(webrtc.WithMediaEngine(me), webrtc.WithSettingEngine(se))
|
||||
return api.NewPeerConnection(conf.Configuration)
|
||||
pc, err := api.NewPeerConnection(conf.Configuration)
|
||||
return pc, err
|
||||
}
|
||||
|
||||
func NewParticipant(pc types.PeerConnection, sc types.SignalConnection, name string, receiverConfig ReceiverConfig) (*ParticipantImpl, error) {
|
||||
func NewParticipant(participantId, name string, pc types.PeerConnection, rs routing.MessageSink, receiverConfig ReceiverConfig) (*ParticipantImpl, error) {
|
||||
// TODO: check to ensure params are valid, id and name can't be empty
|
||||
me := &webrtc.MediaEngine{}
|
||||
me.RegisterDefaultCodecs()
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
participant := &ParticipantImpl{
|
||||
id: utils.NewGuid(utils.ParticipantPrefix),
|
||||
id: participantId,
|
||||
name: name,
|
||||
peerConn: pc,
|
||||
sigConn: sc,
|
||||
responseSink: rs,
|
||||
receiverConfig: receiverConfig,
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
@@ -96,7 +99,6 @@ func NewParticipant(pc types.PeerConnection, sc types.SignalConnection, name str
|
||||
debouncedNegotiate: debounce.New(negotiationFrequency),
|
||||
}
|
||||
|
||||
log := logger.GetLogger()
|
||||
pc.OnTrack(participant.onMediaTrack)
|
||||
|
||||
pc.OnICECandidate(func(c *webrtc.ICECandidate) {
|
||||
@@ -107,14 +109,14 @@ func NewParticipant(pc types.PeerConnection, sc types.SignalConnection, name str
|
||||
ci := c.ToJSON()
|
||||
|
||||
// write candidate
|
||||
logger.GetLogger().Debugw("sending ice candidates")
|
||||
err := sc.WriteResponse(&livekit.SignalResponse{
|
||||
logger.Debugw("sending ice candidates")
|
||||
err := rs.WriteMessage(&livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_Trickle{
|
||||
Trickle: ToProtoTrickle(ci),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
log.Errorw("could not send trickle", "err", err)
|
||||
logger.Errorw("could not send trickle", "err", err)
|
||||
}
|
||||
|
||||
if participant.onICECandidate != nil {
|
||||
@@ -123,7 +125,7 @@ func NewParticipant(pc types.PeerConnection, sc types.SignalConnection, name str
|
||||
})
|
||||
|
||||
pc.OnICEConnectionStateChange(func(state webrtc.ICEConnectionState) {
|
||||
logger.GetLogger().Debugw("ICE connection state changed", "state", state.String())
|
||||
logger.Debugw("ICE connection state changed", "state", state.String())
|
||||
if state == webrtc.ICEConnectionStateConnected {
|
||||
participant.updateState(livekit.ParticipantInfo_ACTIVE)
|
||||
}
|
||||
@@ -228,7 +230,7 @@ func (p *ParticipantImpl) Answer(sdp webrtc.SessionDescription) (answer webrtc.S
|
||||
}
|
||||
p.negotiationCond.L.Unlock()
|
||||
|
||||
err = p.sigConn.WriteResponse(&livekit.SignalResponse{
|
||||
err = p.responseSink.WriteMessage(&livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_Answer{
|
||||
Answer: ToProtoSessionDescription(answer),
|
||||
},
|
||||
@@ -252,7 +254,7 @@ func (p *ParticipantImpl) AddTrack(clientId, name string, trackType livekit.Trac
|
||||
}
|
||||
p.pendingTracks[clientId] = ti
|
||||
|
||||
p.sigConn.WriteResponse(&livekit.SignalResponse{
|
||||
p.responseSink.WriteMessage(&livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_TrackPublished{
|
||||
TrackPublished: &livekit.TrackPublishedResponse{
|
||||
Cid: clientId,
|
||||
@@ -266,7 +268,7 @@ func (p *ParticipantImpl) HandleAnswer(sdp webrtc.SessionDescription) error {
|
||||
if sdp.Type != webrtc.SDPTypeAnswer {
|
||||
return ErrUnexpectedOffer
|
||||
}
|
||||
logger.GetLogger().Debugw("setting remote answer")
|
||||
logger.Debugw("setting remote answer")
|
||||
if err := p.peerConn.SetRemoteDescription(sdp); err != nil {
|
||||
return errors.Wrap(err, "could not set remote description")
|
||||
}
|
||||
@@ -288,7 +290,7 @@ func (p *ParticipantImpl) HandleClientNegotiation() {
|
||||
}
|
||||
p.negotiationState = negotiationStateClient
|
||||
p.negotiationCond.L.Unlock()
|
||||
p.sigConn.WriteResponse(&livekit.SignalResponse{
|
||||
p.responseSink.WriteMessage(&livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_Negotiate{
|
||||
Negotiate: &livekit.NegotiationResponse{},
|
||||
},
|
||||
@@ -329,7 +331,7 @@ func (p *ParticipantImpl) AddSubscriber(op types.Participant) error {
|
||||
defer p.lock.RUnlock()
|
||||
|
||||
for _, track := range p.publishedTracks {
|
||||
logger.GetLogger().Debugw("subscribing to remoteTrack",
|
||||
logger.Debugw("subscribing to remoteTrack",
|
||||
"srcParticipant", p.ID(),
|
||||
"dstParticipant", op.ID(),
|
||||
"remoteTrack", track.ID())
|
||||
@@ -350,9 +352,9 @@ func (p *ParticipantImpl) RemoveSubscriber(participantId string) {
|
||||
}
|
||||
|
||||
// signal connection methods
|
||||
func (p *ParticipantImpl) SendJoinResponse(roomInfo *livekit.RoomInfo, otherParticipants []types.Participant) error {
|
||||
func (p *ParticipantImpl) SendJoinResponse(roomInfo *livekit.Room, otherParticipants []types.Participant) error {
|
||||
// send Join response
|
||||
return p.sigConn.WriteResponse(&livekit.SignalResponse{
|
||||
return p.responseSink.WriteMessage(&livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_Join{
|
||||
Join: &livekit.JoinResponse{
|
||||
Room: roomInfo,
|
||||
@@ -364,7 +366,7 @@ func (p *ParticipantImpl) SendJoinResponse(roomInfo *livekit.RoomInfo, otherPart
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) SendParticipantUpdate(participants []*livekit.ParticipantInfo) error {
|
||||
return p.sigConn.WriteResponse(&livekit.SignalResponse{
|
||||
return p.responseSink.WriteMessage(&livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_Update{
|
||||
Update: &livekit.ParticipantUpdate{
|
||||
Participants: participants,
|
||||
@@ -429,10 +431,10 @@ func (p *ParticipantImpl) negotiate() {
|
||||
}
|
||||
p.negotiationCond.L.Unlock()
|
||||
|
||||
logger.GetLogger().Debugw("starting negotiation", "participant", p.ID())
|
||||
logger.Debugw("starting negotiation", "participant", p.ID())
|
||||
offer, err := p.peerConn.CreateOffer(nil)
|
||||
if err != nil {
|
||||
logger.GetLogger().Errorw("could not create offer", "err", err)
|
||||
logger.Errorw("could not create offer", "err", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -444,18 +446,18 @@ func (p *ParticipantImpl) negotiate() {
|
||||
|
||||
err = p.peerConn.SetLocalDescription(offer)
|
||||
if err != nil {
|
||||
logger.GetLogger().Errorw("could not set local description", "err", err)
|
||||
logger.Errorw("could not set local description", "err", err)
|
||||
return
|
||||
}
|
||||
|
||||
logger.GetLogger().Debugw("sending available offer to participant")
|
||||
err = p.sigConn.WriteResponse(&livekit.SignalResponse{
|
||||
logger.Debugw("sending available offer to participant")
|
||||
err = p.responseSink.WriteMessage(&livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_Offer{
|
||||
Offer: ToProtoSessionDescription(offer),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
logger.GetLogger().Errorw("could not send offer to peer",
|
||||
logger.Errorw("could not send offer to peer",
|
||||
"err", err)
|
||||
}
|
||||
}
|
||||
@@ -477,7 +479,7 @@ func (p *ParticipantImpl) updateState(state livekit.ParticipantInfo_State) {
|
||||
|
||||
// when a new remoteTrack is created, creates a Track and adds it to room
|
||||
func (p *ParticipantImpl) onMediaTrack(track *webrtc.TrackRemote, rtpReceiver *webrtc.RTPReceiver) {
|
||||
logger.GetLogger().Debugw("mediaTrack added", "participantId", p.ID(), "remoteTrack", track.ID())
|
||||
logger.Debugw("mediaTrack added", "participantId", p.ID(), "remoteTrack", track.ID())
|
||||
|
||||
ti := p.popPendingTrack(track.ID())
|
||||
if ti == nil {
|
||||
@@ -496,7 +498,7 @@ func (p *ParticipantImpl) onDataChannel(dc *webrtc.DataChannel) {
|
||||
if dc.Label() == placeholderDataChannel {
|
||||
return
|
||||
}
|
||||
logger.GetLogger().Debugw("dataChannel added", "participantId", p.ID(), "label", dc.Label())
|
||||
logger.Debugw("dataChannel added", "participantId", p.ID(), "label", dc.Label())
|
||||
|
||||
// data channels have numeric ids, so we use its label to identify
|
||||
ti := p.popPendingTrack(dc.Label())
|
||||
@@ -515,7 +517,7 @@ func (p *ParticipantImpl) popPendingTrack(clientId string) *livekit.TrackInfo {
|
||||
defer p.lock.Unlock()
|
||||
ti := p.pendingTracks[clientId]
|
||||
if ti == nil {
|
||||
logger.GetLogger().Errorw("track info not published prior to track", "clientId", clientId)
|
||||
logger.Errorw("track info not published prior to track", "clientId", clientId)
|
||||
} else {
|
||||
delete(p.pendingTracks, clientId)
|
||||
}
|
||||
@@ -584,7 +586,7 @@ func (p *ParticipantImpl) downTracksRTCPWorker() {
|
||||
if err == io.EOF || err == io.ErrClosedPipe {
|
||||
return
|
||||
}
|
||||
logger.GetLogger().Errorw("could not send downtrack reports",
|
||||
logger.Errorw("could not send downtrack reports",
|
||||
"participant", p.id,
|
||||
"err", err)
|
||||
}
|
||||
@@ -598,10 +600,10 @@ func (p *ParticipantImpl) rtcpSendWorker() {
|
||||
// read from rtcpChan
|
||||
for pkts := range p.rtcpCh {
|
||||
//for _, pkt := range pkts {
|
||||
// logger.GetLogger().Debugw("writing RTCP", "packet", pkt)
|
||||
// logger.Debugw("writing RTCP", "packet", pkt)
|
||||
//}
|
||||
if err := p.peerConn.WriteRTCP(pkts); err != nil {
|
||||
logger.GetLogger().Errorw("could not write RTCP to participant",
|
||||
logger.Errorw("could not write RTCP to participant",
|
||||
"participant", p.id,
|
||||
"err", err)
|
||||
}
|
||||
|
||||
@@ -5,8 +5,10 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/routing/routingfakes"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types/typesfakes"
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
)
|
||||
|
||||
@@ -67,6 +69,11 @@ func TestTrackPublishEvents(t *testing.T) {
|
||||
}
|
||||
|
||||
func newParticipantForTest(name string) *ParticipantImpl {
|
||||
p, _ := NewParticipant(&typesfakes.FakePeerConnection{}, &typesfakes.FakeSignalConnection{}, name, ReceiverConfig{})
|
||||
p, _ := NewParticipant(
|
||||
utils.NewGuid(utils.ParticipantPrefix),
|
||||
name,
|
||||
&typesfakes.FakePeerConnection{},
|
||||
&routingfakes.FakeMessageSink{},
|
||||
ReceiverConfig{})
|
||||
return p
|
||||
}
|
||||
|
||||
+1
-1
@@ -52,7 +52,7 @@ func NewReceiver(rtcpCh chan []rtcp.Packet, rtpReceiver *webrtc.RTPReceiver, tra
|
||||
r.rtcpReader.OnPacket(func(bytes []byte) {
|
||||
pkts, err := rtcp.Unmarshal(bytes)
|
||||
if err != nil {
|
||||
logger.GetLogger().Warnw("could not unmarshal RTCP packet")
|
||||
logger.Warnw("could not unmarshal RTCP packet")
|
||||
return
|
||||
}
|
||||
for _, pkt := range pkts {
|
||||
|
||||
+10
-32
@@ -2,13 +2,11 @@ package rtc
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/thoas/go-funk"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/logger"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
)
|
||||
|
||||
@@ -20,16 +18,10 @@ type Room struct {
|
||||
participants map[string]types.Participant
|
||||
}
|
||||
|
||||
func NewRoomForRequest(req *livekit.CreateRoomRequest, config *WebRTCConfig) *Room {
|
||||
func NewRoom(room *livekit.Room, config WebRTCConfig) *Room {
|
||||
return &Room{
|
||||
Room: livekit.Room{
|
||||
Sid: utils.NewGuid(utils.RoomPrefix),
|
||||
Name: req.Name,
|
||||
EmptyTimeout: req.EmptyTimeout,
|
||||
MaxParticipants: req.MaxParticipants,
|
||||
CreationTime: time.Now().Unix(),
|
||||
},
|
||||
config: *config,
|
||||
Room: *room,
|
||||
config: config,
|
||||
lock: sync.RWMutex{},
|
||||
participants: make(map[string]types.Participant),
|
||||
}
|
||||
@@ -47,28 +39,14 @@ func (r *Room) GetParticipants() []types.Participant {
|
||||
return funk.Values(r.participants).([]types.Participant)
|
||||
}
|
||||
|
||||
func (r *Room) ToRoomInfo(node *livekit.Node) *livekit.RoomInfo {
|
||||
ri := &livekit.RoomInfo{
|
||||
Sid: r.Sid,
|
||||
Name: r.Name,
|
||||
CreationTime: r.CreationTime,
|
||||
}
|
||||
if node != nil {
|
||||
ri.NodeIp = node.Ip
|
||||
}
|
||||
return ri
|
||||
}
|
||||
|
||||
func (r *Room) Join(participant types.Participant) error {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
log := logger.GetLogger()
|
||||
|
||||
// it's important to set this before connection, we don't want to miss out on any publishedTracks
|
||||
participant.OnTrackPublished(r.onTrackAdded)
|
||||
participant.OnStateChange(func(p types.Participant, oldState livekit.ParticipantInfo_State) {
|
||||
log.Debugw("participant state changed", "state", p.State(), "participant", p.ID(),
|
||||
logger.Debugw("participant state changed", "state", p.State(), "participant", p.ID(),
|
||||
"oldState", oldState)
|
||||
r.broadcastParticipantState(p)
|
||||
|
||||
@@ -81,7 +59,7 @@ func (r *Room) Join(participant types.Participant) error {
|
||||
}
|
||||
if err := op.AddSubscriber(p); err != nil {
|
||||
// TODO: log error? or disconnect?
|
||||
logger.GetLogger().Errorw("could not subscribe to participant",
|
||||
logger.Errorw("could not subscribe to participant",
|
||||
"dstParticipant", p.ID(),
|
||||
"srcParticipant", op.ID())
|
||||
}
|
||||
@@ -92,7 +70,7 @@ func (r *Room) Join(participant types.Participant) error {
|
||||
})
|
||||
participant.OnTrackUpdated(r.onTrackUpdated)
|
||||
|
||||
log.Infow("new participant joined",
|
||||
logger.Infow("new participant joined",
|
||||
"id", participant.ID(),
|
||||
"name", participant.Name(),
|
||||
"roomId", r.Sid)
|
||||
@@ -107,7 +85,7 @@ func (r *Room) Join(participant types.Participant) error {
|
||||
}
|
||||
}
|
||||
|
||||
return participant.SendJoinResponse(r.ToRoomInfo(nil), otherParticipants)
|
||||
return participant.SendJoinResponse(&r.Room, otherParticipants)
|
||||
}
|
||||
|
||||
func (r *Room) RemoveParticipant(id string) {
|
||||
@@ -145,12 +123,12 @@ func (r *Room) onTrackAdded(participant types.Participant, track types.Published
|
||||
// not fully joined. don't subscribe yet
|
||||
continue
|
||||
}
|
||||
logger.GetLogger().Debugw("subscribing to new track",
|
||||
logger.Debugw("subscribing to new track",
|
||||
"srcParticipant", participant.ID(),
|
||||
"remoteTrack", track.ID(),
|
||||
"dstParticipant", existingParticipant.ID())
|
||||
if err := track.AddSubscriber(existingParticipant); err != nil {
|
||||
logger.GetLogger().Errorw("could not subscribe to remoteTrack",
|
||||
logger.Errorw("could not subscribe to remoteTrack",
|
||||
"srcParticipant", participant.ID(),
|
||||
"remoteTrack", track.ID(),
|
||||
"dstParticipant", existingParticipant.ID())
|
||||
@@ -176,7 +154,7 @@ func (r *Room) broadcastParticipantState(p types.Participant) {
|
||||
|
||||
err := op.SendParticipantUpdate(updates)
|
||||
if err != nil {
|
||||
logger.GetLogger().Errorw("could not send update to participant",
|
||||
logger.Errorw("could not send update to participant",
|
||||
"participant", p.ID(),
|
||||
"err", err)
|
||||
}
|
||||
|
||||
+4
-27
@@ -15,32 +15,6 @@ const (
|
||||
numParticipants = 3
|
||||
)
|
||||
|
||||
func TestNewRoomForRequest(t *testing.T) {
|
||||
req := &livekit.CreateRoomRequest{
|
||||
Name: "myroom",
|
||||
EmptyTimeout: 120,
|
||||
MaxParticipants: 50,
|
||||
}
|
||||
|
||||
rm := rtc.NewRoomForRequest(req, &rtc.WebRTCConfig{})
|
||||
assert.NotEmpty(t, rm.Sid)
|
||||
assert.Equal(t, req.Name, rm.Name)
|
||||
assert.Equal(t, req.EmptyTimeout, rm.EmptyTimeout)
|
||||
assert.Equal(t, req.MaxParticipants, rm.MaxParticipants)
|
||||
}
|
||||
|
||||
func TestToRoomInfo(t *testing.T) {
|
||||
rm := rtc.NewRoomForRequest(&livekit.CreateRoomRequest{
|
||||
Name: "myroom",
|
||||
EmptyTimeout: 120,
|
||||
MaxParticipants: 50,
|
||||
}, &rtc.WebRTCConfig{})
|
||||
info := rm.ToRoomInfo(&livekit.Node{Ip: "0.0.0.0"})
|
||||
assert.Equal(t, rm.Sid, info.Sid)
|
||||
assert.Equal(t, rm.Name, info.Name)
|
||||
assert.Equal(t, "0.0.0.0", info.NodeIp)
|
||||
}
|
||||
|
||||
func TestRoomJoin(t *testing.T) {
|
||||
t.Run("joining returns existing participant data", func(t *testing.T) {
|
||||
rm := newRoomWithParticipants(t, numParticipants)
|
||||
@@ -123,7 +97,10 @@ func TestNewTrack(t *testing.T) {
|
||||
}
|
||||
|
||||
func newRoomWithParticipants(t *testing.T, num int) *rtc.Room {
|
||||
rm := rtc.NewRoomForRequest(&livekit.CreateRoomRequest{}, &rtc.WebRTCConfig{})
|
||||
rm := rtc.NewRoom(
|
||||
&livekit.Room{Name: "name"},
|
||||
rtc.WebRTCConfig{},
|
||||
)
|
||||
for i := 0; i < num; i++ {
|
||||
participant := newMockParticipant("")
|
||||
err := rm.Join(participant)
|
||||
|
||||
@@ -20,12 +20,6 @@ type WebsocketClient interface {
|
||||
WriteControl(messageType int, data []byte, deadline time.Time) error
|
||||
}
|
||||
|
||||
//counterfeiter:generate . SignalConnection
|
||||
type SignalConnection interface {
|
||||
ReadRequest() (*livekit.SignalRequest, error)
|
||||
WriteResponse(*livekit.SignalResponse) error
|
||||
}
|
||||
|
||||
//counterfeiter:generate . PeerConnection
|
||||
type PeerConnection interface {
|
||||
OnICECandidate(f func(*webrtc.ICECandidate))
|
||||
@@ -61,10 +55,11 @@ type Participant interface {
|
||||
AddTrack(clientId, name string, trackType livekit.TrackType)
|
||||
Answer(sdp webrtc.SessionDescription) (answer webrtc.SessionDescription, err error)
|
||||
HandleAnswer(sdp webrtc.SessionDescription) error
|
||||
HandleClientNegotiation()
|
||||
AddICECandidate(candidate webrtc.ICECandidateInit) error
|
||||
AddSubscriber(op Participant) error
|
||||
RemoveSubscriber(peerId string)
|
||||
SendJoinResponse(info *livekit.RoomInfo, otherParticipants []Participant) error
|
||||
SendJoinResponse(info *livekit.Room, otherParticipants []Participant) error
|
||||
SendParticipantUpdate(participants []*livekit.ParticipantInfo) error
|
||||
SetTrackMuted(trackId string, muted bool)
|
||||
|
||||
|
||||
@@ -81,6 +81,10 @@ type FakeParticipant struct {
|
||||
handleAnswerReturnsOnCall map[int]struct {
|
||||
result1 error
|
||||
}
|
||||
HandleClientNegotiationStub func()
|
||||
handleClientNegotiationMutex sync.RWMutex
|
||||
handleClientNegotiationArgsForCall []struct {
|
||||
}
|
||||
IDStub func() string
|
||||
iDMutex sync.RWMutex
|
||||
iDArgsForCall []struct {
|
||||
@@ -167,10 +171,10 @@ type FakeParticipant struct {
|
||||
removeSubscriberArgsForCall []struct {
|
||||
arg1 string
|
||||
}
|
||||
SendJoinResponseStub func(*livekit.RoomInfo, []types.Participant) error
|
||||
SendJoinResponseStub func(*livekit.Room, []types.Participant) error
|
||||
sendJoinResponseMutex sync.RWMutex
|
||||
sendJoinResponseArgsForCall []struct {
|
||||
arg1 *livekit.RoomInfo
|
||||
arg1 *livekit.Room
|
||||
arg2 []types.Participant
|
||||
}
|
||||
sendJoinResponseReturns struct {
|
||||
@@ -591,6 +595,30 @@ func (fake *FakeParticipant) HandleAnswerReturnsOnCall(i int, result1 error) {
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) HandleClientNegotiation() {
|
||||
fake.handleClientNegotiationMutex.Lock()
|
||||
fake.handleClientNegotiationArgsForCall = append(fake.handleClientNegotiationArgsForCall, struct {
|
||||
}{})
|
||||
stub := fake.HandleClientNegotiationStub
|
||||
fake.recordInvocation("HandleClientNegotiation", []interface{}{})
|
||||
fake.handleClientNegotiationMutex.Unlock()
|
||||
if stub != nil {
|
||||
fake.HandleClientNegotiationStub()
|
||||
}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) HandleClientNegotiationCallCount() int {
|
||||
fake.handleClientNegotiationMutex.RLock()
|
||||
defer fake.handleClientNegotiationMutex.RUnlock()
|
||||
return len(fake.handleClientNegotiationArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) HandleClientNegotiationCalls(stub func()) {
|
||||
fake.handleClientNegotiationMutex.Lock()
|
||||
defer fake.handleClientNegotiationMutex.Unlock()
|
||||
fake.HandleClientNegotiationStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) ID() string {
|
||||
fake.iDMutex.Lock()
|
||||
ret, specificReturn := fake.iDReturnsOnCall[len(fake.iDArgsForCall)]
|
||||
@@ -1081,7 +1109,7 @@ func (fake *FakeParticipant) RemoveSubscriberArgsForCall(i int) string {
|
||||
return argsForCall.arg1
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) SendJoinResponse(arg1 *livekit.RoomInfo, arg2 []types.Participant) error {
|
||||
func (fake *FakeParticipant) SendJoinResponse(arg1 *livekit.Room, arg2 []types.Participant) error {
|
||||
var arg2Copy []types.Participant
|
||||
if arg2 != nil {
|
||||
arg2Copy = make([]types.Participant, len(arg2))
|
||||
@@ -1090,7 +1118,7 @@ func (fake *FakeParticipant) SendJoinResponse(arg1 *livekit.RoomInfo, arg2 []typ
|
||||
fake.sendJoinResponseMutex.Lock()
|
||||
ret, specificReturn := fake.sendJoinResponseReturnsOnCall[len(fake.sendJoinResponseArgsForCall)]
|
||||
fake.sendJoinResponseArgsForCall = append(fake.sendJoinResponseArgsForCall, struct {
|
||||
arg1 *livekit.RoomInfo
|
||||
arg1 *livekit.Room
|
||||
arg2 []types.Participant
|
||||
}{arg1, arg2Copy})
|
||||
stub := fake.SendJoinResponseStub
|
||||
@@ -1112,13 +1140,13 @@ func (fake *FakeParticipant) SendJoinResponseCallCount() int {
|
||||
return len(fake.sendJoinResponseArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) SendJoinResponseCalls(stub func(*livekit.RoomInfo, []types.Participant) error) {
|
||||
func (fake *FakeParticipant) SendJoinResponseCalls(stub func(*livekit.Room, []types.Participant) error) {
|
||||
fake.sendJoinResponseMutex.Lock()
|
||||
defer fake.sendJoinResponseMutex.Unlock()
|
||||
fake.SendJoinResponseStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) SendJoinResponseArgsForCall(i int) (*livekit.RoomInfo, []types.Participant) {
|
||||
func (fake *FakeParticipant) SendJoinResponseArgsForCall(i int) (*livekit.Room, []types.Participant) {
|
||||
fake.sendJoinResponseMutex.RLock()
|
||||
defer fake.sendJoinResponseMutex.RUnlock()
|
||||
argsForCall := fake.sendJoinResponseArgsForCall[i]
|
||||
@@ -1394,6 +1422,8 @@ func (fake *FakeParticipant) Invocations() map[string][][]interface{} {
|
||||
defer fake.closeMutex.RUnlock()
|
||||
fake.handleAnswerMutex.RLock()
|
||||
defer fake.handleAnswerMutex.RUnlock()
|
||||
fake.handleClientNegotiationMutex.RLock()
|
||||
defer fake.handleClientNegotiationMutex.RUnlock()
|
||||
fake.iDMutex.RLock()
|
||||
defer fake.iDMutex.RUnlock()
|
||||
fake.isReadyMutex.RLock()
|
||||
|
||||
@@ -1,182 +0,0 @@
|
||||
// Code generated by counterfeiter. DO NOT EDIT.
|
||||
package typesfakes
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
)
|
||||
|
||||
type FakeSignalConnection struct {
|
||||
ReadRequestStub func() (*livekit.SignalRequest, error)
|
||||
readRequestMutex sync.RWMutex
|
||||
readRequestArgsForCall []struct {
|
||||
}
|
||||
readRequestReturns struct {
|
||||
result1 *livekit.SignalRequest
|
||||
result2 error
|
||||
}
|
||||
readRequestReturnsOnCall map[int]struct {
|
||||
result1 *livekit.SignalRequest
|
||||
result2 error
|
||||
}
|
||||
WriteResponseStub func(*livekit.SignalResponse) error
|
||||
writeResponseMutex sync.RWMutex
|
||||
writeResponseArgsForCall []struct {
|
||||
arg1 *livekit.SignalResponse
|
||||
}
|
||||
writeResponseReturns struct {
|
||||
result1 error
|
||||
}
|
||||
writeResponseReturnsOnCall map[int]struct {
|
||||
result1 error
|
||||
}
|
||||
invocations map[string][][]interface{}
|
||||
invocationsMutex sync.RWMutex
|
||||
}
|
||||
|
||||
func (fake *FakeSignalConnection) ReadRequest() (*livekit.SignalRequest, error) {
|
||||
fake.readRequestMutex.Lock()
|
||||
ret, specificReturn := fake.readRequestReturnsOnCall[len(fake.readRequestArgsForCall)]
|
||||
fake.readRequestArgsForCall = append(fake.readRequestArgsForCall, struct {
|
||||
}{})
|
||||
stub := fake.ReadRequestStub
|
||||
fakeReturns := fake.readRequestReturns
|
||||
fake.recordInvocation("ReadRequest", []interface{}{})
|
||||
fake.readRequestMutex.Unlock()
|
||||
if stub != nil {
|
||||
return stub()
|
||||
}
|
||||
if specificReturn {
|
||||
return ret.result1, ret.result2
|
||||
}
|
||||
return fakeReturns.result1, fakeReturns.result2
|
||||
}
|
||||
|
||||
func (fake *FakeSignalConnection) ReadRequestCallCount() int {
|
||||
fake.readRequestMutex.RLock()
|
||||
defer fake.readRequestMutex.RUnlock()
|
||||
return len(fake.readRequestArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeSignalConnection) ReadRequestCalls(stub func() (*livekit.SignalRequest, error)) {
|
||||
fake.readRequestMutex.Lock()
|
||||
defer fake.readRequestMutex.Unlock()
|
||||
fake.ReadRequestStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeSignalConnection) ReadRequestReturns(result1 *livekit.SignalRequest, result2 error) {
|
||||
fake.readRequestMutex.Lock()
|
||||
defer fake.readRequestMutex.Unlock()
|
||||
fake.ReadRequestStub = nil
|
||||
fake.readRequestReturns = struct {
|
||||
result1 *livekit.SignalRequest
|
||||
result2 error
|
||||
}{result1, result2}
|
||||
}
|
||||
|
||||
func (fake *FakeSignalConnection) ReadRequestReturnsOnCall(i int, result1 *livekit.SignalRequest, result2 error) {
|
||||
fake.readRequestMutex.Lock()
|
||||
defer fake.readRequestMutex.Unlock()
|
||||
fake.ReadRequestStub = nil
|
||||
if fake.readRequestReturnsOnCall == nil {
|
||||
fake.readRequestReturnsOnCall = make(map[int]struct {
|
||||
result1 *livekit.SignalRequest
|
||||
result2 error
|
||||
})
|
||||
}
|
||||
fake.readRequestReturnsOnCall[i] = struct {
|
||||
result1 *livekit.SignalRequest
|
||||
result2 error
|
||||
}{result1, result2}
|
||||
}
|
||||
|
||||
func (fake *FakeSignalConnection) WriteResponse(arg1 *livekit.SignalResponse) error {
|
||||
fake.writeResponseMutex.Lock()
|
||||
ret, specificReturn := fake.writeResponseReturnsOnCall[len(fake.writeResponseArgsForCall)]
|
||||
fake.writeResponseArgsForCall = append(fake.writeResponseArgsForCall, struct {
|
||||
arg1 *livekit.SignalResponse
|
||||
}{arg1})
|
||||
stub := fake.WriteResponseStub
|
||||
fakeReturns := fake.writeResponseReturns
|
||||
fake.recordInvocation("WriteResponse", []interface{}{arg1})
|
||||
fake.writeResponseMutex.Unlock()
|
||||
if stub != nil {
|
||||
return stub(arg1)
|
||||
}
|
||||
if specificReturn {
|
||||
return ret.result1
|
||||
}
|
||||
return fakeReturns.result1
|
||||
}
|
||||
|
||||
func (fake *FakeSignalConnection) WriteResponseCallCount() int {
|
||||
fake.writeResponseMutex.RLock()
|
||||
defer fake.writeResponseMutex.RUnlock()
|
||||
return len(fake.writeResponseArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeSignalConnection) WriteResponseCalls(stub func(*livekit.SignalResponse) error) {
|
||||
fake.writeResponseMutex.Lock()
|
||||
defer fake.writeResponseMutex.Unlock()
|
||||
fake.WriteResponseStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeSignalConnection) WriteResponseArgsForCall(i int) *livekit.SignalResponse {
|
||||
fake.writeResponseMutex.RLock()
|
||||
defer fake.writeResponseMutex.RUnlock()
|
||||
argsForCall := fake.writeResponseArgsForCall[i]
|
||||
return argsForCall.arg1
|
||||
}
|
||||
|
||||
func (fake *FakeSignalConnection) WriteResponseReturns(result1 error) {
|
||||
fake.writeResponseMutex.Lock()
|
||||
defer fake.writeResponseMutex.Unlock()
|
||||
fake.WriteResponseStub = nil
|
||||
fake.writeResponseReturns = struct {
|
||||
result1 error
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeSignalConnection) WriteResponseReturnsOnCall(i int, result1 error) {
|
||||
fake.writeResponseMutex.Lock()
|
||||
defer fake.writeResponseMutex.Unlock()
|
||||
fake.WriteResponseStub = nil
|
||||
if fake.writeResponseReturnsOnCall == nil {
|
||||
fake.writeResponseReturnsOnCall = make(map[int]struct {
|
||||
result1 error
|
||||
})
|
||||
}
|
||||
fake.writeResponseReturnsOnCall[i] = struct {
|
||||
result1 error
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeSignalConnection) Invocations() map[string][][]interface{} {
|
||||
fake.invocationsMutex.RLock()
|
||||
defer fake.invocationsMutex.RUnlock()
|
||||
fake.readRequestMutex.RLock()
|
||||
defer fake.readRequestMutex.RUnlock()
|
||||
fake.writeResponseMutex.RLock()
|
||||
defer fake.writeResponseMutex.RUnlock()
|
||||
copiedInvocations := map[string][][]interface{}{}
|
||||
for key, value := range fake.invocations {
|
||||
copiedInvocations[key] = value
|
||||
}
|
||||
return copiedInvocations
|
||||
}
|
||||
|
||||
func (fake *FakeSignalConnection) recordInvocation(key string, args []interface{}) {
|
||||
fake.invocationsMutex.Lock()
|
||||
defer fake.invocationsMutex.Unlock()
|
||||
if fake.invocations == nil {
|
||||
fake.invocations = map[string][][]interface{}{}
|
||||
}
|
||||
if fake.invocations[key] == nil {
|
||||
fake.invocations[key] = [][]interface{}{}
|
||||
}
|
||||
fake.invocations[key] = append(fake.invocations[key], args)
|
||||
}
|
||||
|
||||
var _ types.SignalConnection = new(FakeSignalConnection)
|
||||
+14
-1
@@ -5,8 +5,11 @@ 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"
|
||||
@@ -16,6 +19,15 @@ 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 {
|
||||
@@ -118,6 +130,7 @@ func RecoverSilent() {
|
||||
|
||||
func Recover() {
|
||||
if r := recover(); r != nil {
|
||||
logger.GetLogger().Errorw("recovered panic", "err", r)
|
||||
log := logger.Desugar().WithOptions(zap.AddCallerSkip(1))
|
||||
log.Error("recovered panic", zap.Any("error", r))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,92 +0,0 @@
|
||||
package rtc
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/logger"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
)
|
||||
|
||||
const (
|
||||
pingFrequency = 10 * time.Second
|
||||
pingTimeout = 2 * time.Second
|
||||
)
|
||||
|
||||
type WSSignalConnection struct {
|
||||
conn types.WebsocketClient
|
||||
mu sync.Mutex
|
||||
useJSON bool
|
||||
}
|
||||
|
||||
func NewWSSignalConnection(conn types.WebsocketClient) *WSSignalConnection {
|
||||
wsc := &WSSignalConnection{
|
||||
conn: conn,
|
||||
mu: sync.Mutex{},
|
||||
useJSON: true,
|
||||
}
|
||||
go wsc.pingWorker()
|
||||
return wsc
|
||||
}
|
||||
|
||||
func (c *WSSignalConnection) ReadRequest() (*livekit.SignalRequest, error) {
|
||||
for {
|
||||
// handle special messages and pass on the rest
|
||||
messageType, payload, err := c.conn.ReadMessage()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
msg := &livekit.SignalRequest{}
|
||||
switch messageType {
|
||||
case websocket.BinaryMessage:
|
||||
// protobuf encoded
|
||||
err := proto.Unmarshal(payload, msg)
|
||||
return msg, err
|
||||
case websocket.TextMessage:
|
||||
// json encoded, also write back JSON
|
||||
c.useJSON = true
|
||||
err := protojson.Unmarshal(payload, msg)
|
||||
return msg, err
|
||||
default:
|
||||
logger.GetLogger().Debugw("unsupported message", "message", messageType)
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *WSSignalConnection) WriteResponse(msg *livekit.SignalResponse) error {
|
||||
var msgType int
|
||||
var payload []byte
|
||||
var err error
|
||||
|
||||
if c.useJSON {
|
||||
msgType = websocket.TextMessage
|
||||
payload, err = protojson.Marshal(msg)
|
||||
} else {
|
||||
msgType = websocket.BinaryMessage
|
||||
payload, err = proto.Marshal(msg)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
return c.conn.WriteMessage(msgType, payload)
|
||||
}
|
||||
|
||||
func (c *WSSignalConnection) pingWorker() {
|
||||
for {
|
||||
<-time.After(pingFrequency)
|
||||
err := c.conn.WriteControl(websocket.PingMessage, []byte(""), time.Now().Add(pingTimeout))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user