use dedicated PeerConnections for sending and receiving (#19)

* protobuf updates to support new protocol

* use dedicated publish & subscribe peerconnections

* update client to work with dedicated PCs

* fix Go client negotiation, reduced length of UUID
This commit is contained in:
David Zhao
2021-02-12 22:30:31 -08:00
committed by GitHub
parent fe497a3cc1
commit 090f8d0664
18 changed files with 780 additions and 1881 deletions
+1 -1
View File
@@ -90,7 +90,7 @@ func (t *DataTrack) OnClose(f func()) {
func (t *DataTrack) AddSubscriber(participant types.Participant) error {
label := PackDataTrackLabel(t.participantId, t.ID(), t.dataChannel.Label())
downChannel, err := participant.PeerConnection().CreateDataChannel(label, t.dataChannelOptions())
downChannel, err := participant.SubscriberPC().CreateDataChannel(label, t.dataChannelOptions())
if err != nil {
return err
}
+8 -5
View File
@@ -6,11 +6,10 @@ import (
)
const (
repairedRTP = "urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id"
frameMarking = "urn:ietf:params:rtp-hdrext:framemarking"
)
func createMediaEngine() (*webrtc.MediaEngine, error) {
func createPubMediaEngine() (*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},
@@ -21,7 +20,7 @@ func createMediaEngine() (*webrtc.MediaEngine, error) {
videoRTCPFeedback := []webrtc.RTCPFeedback{
{webrtc.TypeRTCPFBGoogREMB, ""},
//{webrtc.TypeRTCPFBCCM, "fir"},
{webrtc.TypeRTCPFBCCM, "fir"},
{webrtc.TypeRTCPFBNACK, ""},
{webrtc.TypeRTCPFBNACK, "pli"}}
for _, codec := range []webrtc.RTPCodecParameters{
@@ -66,8 +65,7 @@ func createMediaEngine() (*webrtc.MediaEngine, error) {
for _, extension := range []string{
sdp.SDESMidURI,
sdp.SDESRTPStreamIDURI,
repairedRTP,
//sdp.TransportCCURI,
sdp.TransportCCURI,
frameMarking,
} {
if err := me.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: extension}, webrtc.RTPCodecTypeVideo); err != nil {
@@ -86,3 +84,8 @@ func createMediaEngine() (*webrtc.MediaEngine, error) {
return me, nil
}
func createSubMediaEngine() (*webrtc.MediaEngine, error) {
me := &webrtc.MediaEngine{}
return me, nil
}
+13 -15
View File
@@ -125,6 +125,9 @@ func (t *MediaTrack) AddSubscriber(sub types.Participant) error {
}
codec := t.receiver.Codec()
if err := sub.SubscriberMediaEngine().RegisterCodec(codec, t.receiver.Kind()); err != nil {
return err
}
// using DownTrack from ion-sfu
downTrack, err := sfu.NewDownTrack(webrtc.RTPCodecCapability{
@@ -140,8 +143,8 @@ func (t *MediaTrack) AddSubscriber(sub types.Participant) error {
downTrack.SetBufferFactory(bufferFactory)
subTrack := NewSubscribedTrack(downTrack)
transceiver, err := sub.PeerConnection().AddTransceiverFromTrack(downTrack, webrtc.RTPTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionSendrecv,
transceiver, err := sub.SubscriberPC().AddTransceiverFromTrack(downTrack, webrtc.RTPTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionSendonly,
})
if err != nil {
return err
@@ -159,7 +162,7 @@ func (t *MediaTrack) AddSubscriber(sub types.Participant) error {
t.lock.Unlock()
// ignore if the subscribing sub is not connected
if sub.PeerConnection().ConnectionState() == webrtc.PeerConnectionStateClosed {
if sub.SubscriberPC().ConnectionState() == webrtc.PeerConnectionStateClosed {
return
}
@@ -173,7 +176,7 @@ func (t *MediaTrack) AddSubscriber(sub types.Participant) error {
"track", t.id,
"participantId", t.participantId,
"destParticipant", sub.Identity())
if err := sub.PeerConnection().RemoveTrack(sender); err != nil {
if err := sub.SubscriberPC().RemoveTrack(sender); err != nil {
if err == webrtc.ErrConnectionClosed {
// sub closing, can skip removing subscribedtracks
return
@@ -197,7 +200,7 @@ func (t *MediaTrack) AddSubscriber(sub types.Participant) error {
}
// adds a new RTP receiver to the track, returns true if this is a new track
func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.TrackRemote) {
func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.TrackRemote, twcc *twcc.Responder) {
//rid := track.RID()
buff, rtcpReader := bufferFactory.GetBufferPair(uint32(track.SSRC()))
buff.OnFeedback(func(fb []rtcp.Packet) {
@@ -208,16 +211,11 @@ func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.Tra
if t.Kind() == livekit.TrackType_AUDIO {
// TODO: audio level stuff
} else if t.Kind() == livekit.TrackType_VIDEO {
// TODO: handle twcc
//if t.twcc == nil {
// t.twcc = twcc.NewTransportWideCCResponder(uint32(track.SSRC()))
// t.twcc.OnFeedback(func(p rtcp.RawPacket) {
// t.rtcpCh <- []rtcp.Packet{&p}
// })
//}
buff.OnTransportWideCC(func(sn uint16, timeNS int64, marker bool) {
//t.twcc.Push(sn, timeNS, marker)
})
if twcc != nil {
buff.OnTransportWideCC(func(sn uint16, timeNS int64, marker bool) {
twcc.Push(sn, timeNS, marker)
})
}
}
rtcpReader.OnPacket(func(bytes []byte) {
+115 -177
View File
@@ -7,7 +7,7 @@ import (
"sync/atomic"
"time"
"github.com/bep/debounce"
"github.com/pion/ion-sfu/pkg/twcc"
"github.com/pion/rtcp"
"github.com/pion/webrtc/v3"
"github.com/pkg/errors"
@@ -18,32 +18,29 @@ import (
"github.com/livekit/livekit-server/pkg/rtc/types"
"github.com/livekit/livekit-server/pkg/utils"
"github.com/livekit/livekit-server/proto/livekit"
"github.com/livekit/livekit-server/version"
)
const (
placeholderDataChannel = "_private"
sdBatchSize = 20
negotiationFrequency = 100 * time.Millisecond
)
const (
negotiationStateNone = iota
negotiationStateClient
negotiationStateServer
)
type ParticipantImpl struct {
id string
peerConn types.PeerConnection
publisher *PCTransport
subscriber *PCTransport
responseSink routing.MessageSink
receiverConfig ReceiverConfig
isClosed utils.AtomicFlag
mediaEngine *webrtc.MediaEngine
identity string
// JSON encoded metadata to pass to clients
metadata string
state atomic.Value // livekit.ParticipantInfo_State
rtcpCh chan []rtcp.Packet
twcc *twcc.Responder
// tracks the current participant is subscribed to, map of otherParticipantId => []DownTrack
subscribedTracks map[string][]types.SubscribedTrack
// publishedTracks that participant is publishing
@@ -51,101 +48,70 @@ type ParticipantImpl struct {
// client intended to publish, yet to be reconciled
pendingTracks map[string]*livekit.TrackInfo
negotiationCond *sync.Cond
negotiationState int
debouncedNegotiate func(func())
lock sync.RWMutex
once sync.Once
// callbacks & handlers
onTrackPublished func(types.Participant, types.PublishedTrack)
onTrackUpdated func(types.Participant, types.PublishedTrack)
onICECandidate func(c *webrtc.ICECandidateInit)
onStateChange func(p types.Participant, oldState livekit.ParticipantInfo_State)
onClose func(types.Participant)
}
func NewPeerConnection(conf *WebRTCConfig) (*webrtc.PeerConnection, error) {
me, err := createMediaEngine()
func NewParticipant(identity string, conf *WebRTCConfig, rs routing.MessageSink, receiverConfig ReceiverConfig) (*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: receiverConfig,
rtcpCh: make(chan []rtcp.Packet, 50),
subscribedTracks: make(map[string][]types.SubscribedTrack),
lock: sync.RWMutex{},
publishedTracks: make(map[string]types.PublishedTrack, 0),
pendingTracks: make(map[string]*livekit.TrackInfo),
}
p.state.Store(livekit.ParticipantInfo_JOINING)
var err error
p.publisher, err = NewPCTransport(livekit.SignalTarget_PUBLISHER, conf)
if err != nil {
return nil, err
}
se := conf.SettingEngine
se.BufferFactory = bufferFactory.GetOrNew
api := webrtc.NewAPI(webrtc.WithMediaEngine(me), webrtc.WithSettingEngine(se))
pc, err := api.NewPeerConnection(conf.Configuration)
return pc, err
}
func NewParticipant(identity string, pc types.PeerConnection, rs routing.MessageSink, receiverConfig ReceiverConfig) (*ParticipantImpl, error) {
// TODO: check to ensure params are valid, id and identity can't be empty
participant := &ParticipantImpl{
id: utils.NewGuid(utils.ParticipantPrefix),
identity: identity,
peerConn: pc,
responseSink: rs,
receiverConfig: receiverConfig,
rtcpCh: make(chan []rtcp.Packet, 50),
subscribedTracks: make(map[string][]types.SubscribedTrack),
lock: sync.RWMutex{},
negotiationCond: sync.NewCond(&sync.Mutex{}),
publishedTracks: make(map[string]types.PublishedTrack, 0),
pendingTracks: make(map[string]*livekit.TrackInfo),
debouncedNegotiate: debounce.New(negotiationFrequency),
p.subscriber, err = NewPCTransport(livekit.SignalTarget_SUBSCRIBER, conf)
if err != nil {
return nil, err
}
participant.state.Store(livekit.ParticipantInfo_JOINING)
pc.OnTrack(participant.onMediaTrack)
pc.OnICECandidate(func(c *webrtc.ICECandidate) {
p.publisher.pc.OnICECandidate(func(c *webrtc.ICECandidate) {
if c == nil {
return
}
ci := c.ToJSON()
// write candidate
//logger.Debugw("sending ice candidates")
err := rs.WriteMessage(&livekit.SignalResponse{
Message: &livekit.SignalResponse_Trickle{
Trickle: ToProtoTrickle(ci),
},
})
if err != nil {
logger.Errorw("could not send trickle", "err", err,
"participant", identity)
}
if participant.onICECandidate != nil {
participant.onICECandidate(&ci)
}
p.sendIceCandidate(c, livekit.SignalTarget_PUBLISHER)
})
pc.OnICEConnectionStateChange(func(state webrtc.ICEConnectionState) {
//logger.Debugw("ICE connection state changed", "state", state.String())
if state == webrtc.ICEConnectionStateConnected {
participant.updateState(livekit.ParticipantInfo_ACTIVE)
} else if state == webrtc.ICEConnectionStateDisconnected {
go participant.Close()
}
})
pc.OnDataChannel(participant.onDataChannel)
// only set after answered
pc.OnNegotiationNeeded(func() {
logger.Debugw("negotiation needed", "participant", participant.Identity())
if !participant.IsReady() {
// ignore negotiation requests before connected
p.subscriber.pc.OnICECandidate(func(c *webrtc.ICECandidate) {
if c == nil {
return
}
participant.scheduleNegotiate()
p.sendIceCandidate(c, livekit.SignalTarget_SUBSCRIBER)
})
return participant, nil
p.publisher.pc.OnICEConnectionStateChange(func(state webrtc.ICEConnectionState) {
//logger.Debugw("ICE connection state changed", "state", state.String())
if state == webrtc.ICEConnectionStateConnected {
p.updateState(livekit.ParticipantInfo_ACTIVE)
} else if state == webrtc.ICEConnectionStateDisconnected {
go p.Close()
}
})
p.publisher.pc.OnTrack(p.onMediaTrack)
p.subscriber.pc.OnDataChannel(p.onDataChannel)
p.subscriber.OnNegotiationNeeded(p.negotiate)
return p, nil
}
func (p *ParticipantImpl) ID() string {
@@ -209,15 +175,15 @@ func (p *ParticipantImpl) SetResponseSink(sink routing.MessageSink) {
p.responseSink = sink
}
func (p *ParticipantImpl) SubscriberMediaEngine() *webrtc.MediaEngine {
return p.subscriber.me
}
// callbacks for clients
func (p *ParticipantImpl) OnTrackPublished(callback func(types.Participant, types.PublishedTrack)) {
p.onTrackPublished = callback
}
func (p *ParticipantImpl) OnICECandidate(callback func(c *webrtc.ICECandidateInit)) {
p.onICECandidate = callback
}
func (p *ParticipantImpl) OnStateChange(callback func(p types.Participant, oldState livekit.ParticipantInfo_State)) {
p.onStateChange = callback
}
@@ -230,42 +196,28 @@ func (p *ParticipantImpl) OnClose(callback func(types.Participant)) {
p.onClose = callback
}
// Answer an offer from remote participant, used when clients make the initial connection
func (p *ParticipantImpl) Answer(sdp webrtc.SessionDescription) (answer webrtc.SessionDescription, err error) {
if p.State() != livekit.ParticipantInfo_JOINING && p.negotiationState != negotiationStateClient {
// not in a valid state to continue
err = ErrUnexpectedNegotiation
return
}
logger.Debugw("answering client offer", "state", p.State().String(),
// HandleOffer an offer from remote participant, used when clients make the initial connection
func (p *ParticipantImpl) HandleOffer(sdp webrtc.SessionDescription) (answer webrtc.SessionDescription, err error) {
logger.Debugw("answering pub offer", "state", p.State().String(),
"participant", p.Identity(),
//"sdp", sdp.SDP,
)
if err = p.peerConn.SetRemoteDescription(sdp); err != nil {
if err = p.publisher.SetRemoteDescription(sdp); err != nil {
return
}
answer, err = p.peerConn.CreateAnswer(nil)
answer, err = p.publisher.pc.CreateAnswer(nil)
if err != nil {
err = errors.Wrap(err, "could not create answer")
return
}
if err = p.peerConn.SetLocalDescription(answer); err != nil {
if err = p.publisher.pc.SetLocalDescription(answer); err != nil {
err = errors.Wrap(err, "could not set local description")
return
}
// if this is a client initiated re-negotiation, we'll need to flip back our state
p.negotiationCond.L.Lock()
if p.negotiationState == negotiationStateClient {
p.negotiationState = negotiationStateNone
p.negotiationCond.Broadcast()
}
p.negotiationCond.L.Unlock()
logger.Debugw("sending answer to client",
"participant", p.Identity(),
//"sdp", sdp.SDP,
@@ -313,57 +265,32 @@ func (p *ParticipantImpl) AddTrack(clientId, name string, trackType livekit.Trac
}
}
// handles a client answer response, with subscriber PC, server initiates the offer
// and client answers
func (p *ParticipantImpl) HandleAnswer(sdp webrtc.SessionDescription) error {
if sdp.Type != webrtc.SDPTypeAnswer {
return ErrUnexpectedOffer
}
logger.Debugw("setting participant answer",
logger.Debugw("setting subPC answer",
"participant", p.Identity(),
//"sdp", sdp.SDP,
)
if err := p.peerConn.SetRemoteDescription(sdp); err != nil {
if err := p.subscriber.SetRemoteDescription(sdp); err != nil {
return errors.Wrap(err, "could not set remote description")
}
// negotiated, reset flag
p.negotiationCond.L.Lock()
p.negotiationState = negotiationStateNone
p.negotiationCond.Broadcast()
p.negotiationCond.L.Unlock()
return nil
}
// client requested negotiation, when it's able to, send a signal to let it
func (p *ParticipantImpl) HandleClientNegotiation() {
logger.Debugw("participant requested negotiation",
"participant", p.Identity())
// wait until client is able to request negotiation
p.negotiationCond.L.Lock()
for p.negotiationState != negotiationStateNone {
p.negotiationCond.Wait()
}
p.negotiationState = negotiationStateClient
p.negotiationCond.L.Unlock()
logger.Debugw("allowing participant to negotiate",
"participant", p.Identity())
err := p.responseSink.WriteMessage(&livekit.SignalResponse{
Message: &livekit.SignalResponse_Negotiate{
Negotiate: &livekit.NegotiationResponse{},
},
})
if err != nil {
logger.Errorw("could not write message", "error", err,
"participant", p.identity)
}
}
// AddICECandidate adds candidates for remote peer
func (p *ParticipantImpl) AddICECandidate(candidate webrtc.ICECandidateInit) error {
if err := p.peerConn.AddICECandidate(candidate); err != nil {
return err
func (p *ParticipantImpl) AddICECandidate(candidate webrtc.ICECandidateInit, target livekit.SignalTarget) error {
var err error
if target == livekit.SignalTarget_PUBLISHER {
err = p.publisher.AddICECandidate(candidate)
} else {
err = p.subscriber.AddICECandidate(candidate)
}
return nil
return err
}
func (p *ParticipantImpl) Start() {
@@ -389,25 +316,26 @@ func (p *ParticipantImpl) Close() error {
p.lock.Unlock()
p.updateState(livekit.ParticipantInfo_DISCONNECTED)
p.onICECandidate = nil
p.peerConn.OnDataChannel(nil)
p.peerConn.OnICECandidate(nil)
p.peerConn.OnNegotiationNeeded(nil)
p.peerConn.OnTrack(nil)
p.subscriber.pc.OnDataChannel(nil)
p.subscriber.pc.OnICECandidate(nil)
p.subscriber.pc.OnNegotiationNeeded(nil)
p.subscriber.pc.OnTrack(nil)
p.publisher.pc.OnICECandidate(nil)
p.responseSink.Close()
if p.onClose != nil {
p.onClose(p)
}
p.peerConn.Close()
p.publisher.Close()
p.subscriber.Close()
close(p.rtcpCh)
return nil
}
// Subscribes otherPeer to all of the publishedTracks
// Subscribes op to all publishedTracks
func (p *ParticipantImpl) AddSubscriber(op types.Participant) error {
p.lock.RLock()
tracks := funk.Values(p.publishedTracks).([]types.PublishedTrack)
p.lock.RUnlock()
defer p.lock.RUnlock()
if len(tracks) == 0 {
return nil
@@ -444,6 +372,7 @@ func (p *ParticipantImpl) SendJoinResponse(roomInfo *livekit.Room, otherParticip
Room: roomInfo,
Participant: p.ToProto(),
OtherParticipants: ToProtoParticipants(otherParticipants),
ServerVersion: version.Version,
},
},
})
@@ -483,8 +412,8 @@ func (p *ParticipantImpl) SetTrackMuted(trackId string, muted bool) {
}
}
func (p *ParticipantImpl) PeerConnection() types.PeerConnection {
return p.peerConn
func (p *ParticipantImpl) SubscriberPC() *webrtc.PeerConnection {
return p.subscriber.pc
}
// add a track to the participant's subscribed list
@@ -511,8 +440,22 @@ func (p *ParticipantImpl) RemoveSubscribedTrack(pubId string, subTrack types.Sub
p.subscribedTracks[pubId] = tracks
}
func (p *ParticipantImpl) scheduleNegotiate() {
p.debouncedNegotiate(p.negotiate)
func (p *ParticipantImpl) sendIceCandidate(c *webrtc.ICECandidate, target livekit.SignalTarget) {
ci := c.ToJSON()
// write candidate
//logger.Debugw("sending ice candidates")
trickle := ToProtoTrickle(ci)
trickle.Target = target
err := p.responseSink.WriteMessage(&livekit.SignalResponse{
Message: &livekit.SignalResponse_Trickle{
Trickle: trickle,
},
})
if err != nil {
logger.Errorw("could not send trickle", "err", err,
"participant", p.identity)
}
}
// initiates server-driven negotiation by creating an offer
@@ -522,27 +465,15 @@ func (p *ParticipantImpl) negotiate() {
return
}
p.negotiationCond.L.Lock()
for p.negotiationState != negotiationStateNone {
p.negotiationCond.Wait()
}
p.negotiationState = negotiationStateServer
p.negotiationCond.L.Unlock()
logger.Debugw("starting server negotiation", "participant", p.Identity())
offer, err := p.peerConn.CreateOffer(nil)
offer, err := p.subscriber.pc.CreateOffer(nil)
if err != nil {
logger.Errorw("could not create offer", "err", err)
return
}
if p.peerConn.SignalingState() != webrtc.SignalingStateStable {
// try this again
p.scheduleNegotiate()
return
}
err = p.peerConn.SetLocalDescription(offer)
err = p.subscriber.pc.SetLocalDescription(offer)
if err != nil {
logger.Errorw("could not set local description", "err", err)
return
@@ -588,13 +519,9 @@ func (p *ParticipantImpl) onMediaTrack(track *webrtc.TrackRemote, rtpReceiver *w
return
}
// create ReceiverImpl
//receiver := NewReceiver(p.rtcpCh, rtpReceiver, track, p.receiverConfig)
// use existing mediatrack to handle simulcast
p.lock.RLock()
p.lock.Lock()
ptrack := p.publishedTracks[ti.Sid]
p.lock.RUnlock()
var mt *MediaTrack
var newTrack bool
@@ -607,7 +534,14 @@ func (p *ParticipantImpl) onMediaTrack(track *webrtc.TrackRemote, rtpReceiver *w
newTrack = true
}
mt.AddReceiver(rtpReceiver, track)
if p.twcc == nil {
p.twcc = twcc.NewTransportWideCCResponder(uint32(track.SSRC()))
p.twcc.OnFeedback(func(pkt rtcp.RawPacket) {
p.publisher.pc.WriteRTCP([]rtcp.Packet{&pkt})
})
}
mt.AddReceiver(rtpReceiver, track, p.twcc)
p.lock.Unlock()
if newTrack {
p.handleTrackPublished(mt)
@@ -638,7 +572,7 @@ func (p *ParticipantImpl) popPendingTrack(clientId string, kind livekit.TrackTyp
ti := p.pendingTracks[clientId]
// then find the first one that matches type. with MediaStreamTrack, it's possible for the client id to
// change after being added to PeerConnection
// change after being added to SubscriberPC
if ti == nil {
for cid, info := range p.pendingTracks {
if info.Type == kind {
@@ -690,6 +624,10 @@ func (p *ParticipantImpl) downTracksRTCPWorker() {
for {
time.Sleep(5 * time.Second)
if p.subscriber.pc.ConnectionState() != webrtc.PeerConnectionStateConnected {
continue
}
var pkts []rtcp.Packet
var sd []rtcp.SourceDescriptionChunk
p.lock.RLock()
@@ -717,7 +655,7 @@ func (p *ParticipantImpl) downTracksRTCPWorker() {
batch = sd[:size]
sd = sd[size:]
pkts = append(pkts, &rtcp.SourceDescription{Chunks: batch})
if err := p.peerConn.WriteRTCP(pkts); err != nil {
if err := p.subscriber.pc.WriteRTCP(pkts); err != nil {
if err == io.EOF || err == io.ErrClosedPipe {
return
}
@@ -740,7 +678,7 @@ func (p *ParticipantImpl) rtcpSendWorker() {
//for _, pkt := range pkts {
// logger.Debugw("writing RTCP", "packet", pkt)
//}
if err := p.peerConn.WriteRTCP(pkts); err != nil {
if err := p.publisher.pc.WriteRTCP(pkts); err != nil {
logger.Errorw("could not write RTCP to participant",
"participant", p.Identity(),
"err", err)
+1 -1
View File
@@ -121,7 +121,7 @@ func TestDisconnectTiming(t *testing.T) {
func newParticipantForTest(identity string) *ParticipantImpl {
p, _ := NewParticipant(
identity,
&typesfakes.FakePeerConnection{},
&WebRTCConfig{},
&routingfakes.FakeMessageSink{},
ReceiverConfig{})
return p
+141
View File
@@ -0,0 +1,141 @@
package rtc
import (
"sync"
"sync/atomic"
"time"
"github.com/bep/debounce"
"github.com/pion/webrtc/v3"
"github.com/livekit/livekit-server/proto/livekit"
)
const (
negotiationFrequency = 150 * time.Millisecond
)
const (
negotiationStateNone = iota
// waiting for client answer
negotiationStateClient
// need to negotiate again
negotiationStateServer
)
// PCTransport is a wrapper around PeerConnection, with some helper methods
type PCTransport struct {
pc *webrtc.PeerConnection
me *webrtc.MediaEngine
lock sync.Mutex
pendingCandidates []webrtc.ICECandidateInit
debouncedNegotiate func(func())
onNegotiation func()
negotiationState atomic.Value
}
func newPeerConnection(target livekit.SignalTarget, conf *WebRTCConfig) (*webrtc.PeerConnection, *webrtc.MediaEngine, error) {
var me *webrtc.MediaEngine
var err error
if target == livekit.SignalTarget_PUBLISHER {
me, err = createPubMediaEngine()
} else {
me, err = createSubMediaEngine()
}
if err != nil {
return nil, nil, err
}
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)
return pc, me, err
}
func NewPCTransport(target livekit.SignalTarget, conf *WebRTCConfig) (*PCTransport, error) {
pc, me, err := newPeerConnection(target, conf)
if err != nil {
return nil, err
}
t := &PCTransport{
pc: pc,
me: me,
debouncedNegotiate: debounce.New(negotiationFrequency),
}
t.negotiationState.Store(negotiationStateNone)
t.pc.OnNegotiationNeeded(t.negotiate)
return t, nil
}
func (t *PCTransport) AddICECandidate(candidate webrtc.ICECandidateInit) error {
if t.pc.RemoteDescription() == nil {
t.lock.Lock()
t.pendingCandidates = append(t.pendingCandidates, candidate)
t.lock.Unlock()
return nil
}
return t.pc.AddICECandidate(candidate)
}
func (t *PCTransport) PeerConnection() *webrtc.PeerConnection {
return t.pc
}
func (t *PCTransport) Close() {
t.pc.Close()
}
func (t *PCTransport) SetRemoteDescription(sd webrtc.SessionDescription) error {
if err := t.pc.SetRemoteDescription(sd); err != nil {
return err
}
t.lock.Lock()
for _, c := range t.pendingCandidates {
if err := t.pc.AddICECandidate(c); err != nil {
return err
}
}
t.pendingCandidates = nil
t.lock.Unlock()
// negotiated, reset flag
state := t.negotiationState.Load().(int)
t.negotiationState.Store(negotiationStateNone)
if state == negotiationStateServer && t.onNegotiation != nil {
// need to negotiate again
t.negotiate()
}
return nil
}
func (t *PCTransport) OnNegotiationNeeded(f func()) {
t.onNegotiation = f
}
func (t *PCTransport) negotiate() {
t.debouncedNegotiate(func() {
state := t.negotiationState.Load().(int)
// when there's an ongoing negotiation, let it finish and not disrupt its state
if state != negotiationStateNone {
t.negotiationState.Store(negotiationStateServer)
return
}
if t.onNegotiation != nil {
t.onNegotiation()
// indicate waiting for client
t.negotiationState.Store(negotiationStateClient)
}
})
}
+4 -30
View File
@@ -20,30 +20,6 @@ type WebsocketClient interface {
WriteControl(messageType int, data []byte, deadline time.Time) error
}
//counterfeiter:generate . PeerConnection
type PeerConnection interface {
OnICECandidate(f func(*webrtc.ICECandidate))
OnICEConnectionStateChange(func(webrtc.ICEConnectionState))
//OnConnectionStateChange(f func(webrtc.PeerConnectionState))
OnTrack(f func(*webrtc.TrackRemote, *webrtc.RTPReceiver))
OnDataChannel(func(d *webrtc.DataChannel))
OnNegotiationNeeded(f func())
Close() error
SetRemoteDescription(desc webrtc.SessionDescription) error
SetLocalDescription(desc webrtc.SessionDescription) error
CreateOffer(options *webrtc.OfferOptions) (webrtc.SessionDescription, error)
CreateAnswer(options *webrtc.AnswerOptions) (webrtc.SessionDescription, error)
AddICECandidate(candidate webrtc.ICECandidateInit) error
WriteRTCP(pkts []rtcp.Packet) error
// used by datatrack
CreateDataChannel(label string, options *webrtc.DataChannelInit) (*webrtc.DataChannel, error)
// used by mediatrack
AddTransceiverFromTrack(track webrtc.TrackLocal, init ...webrtc.RtpTransceiverInit) (*webrtc.RTPTransceiver, error)
ConnectionState() webrtc.PeerConnectionState
SignalingState() webrtc.SignalingState
RemoveTrack(sender *webrtc.RTPSender) error
}
//counterfeiter:generate . Participant
type Participant interface {
ID() string
@@ -55,12 +31,12 @@ type Participant interface {
SetMetadata(metadata map[string]interface{}) error
GetResponseSink() routing.MessageSink
SetResponseSink(sink routing.MessageSink)
SubscriberMediaEngine() *webrtc.MediaEngine
AddTrack(clientId, name string, trackType livekit.TrackType)
Answer(sdp webrtc.SessionDescription) (answer webrtc.SessionDescription, err error)
HandleOffer(sdp webrtc.SessionDescription) (answer webrtc.SessionDescription, err error)
HandleAnswer(sdp webrtc.SessionDescription) error
HandleClientNegotiation()
AddICECandidate(candidate webrtc.ICECandidateInit) error
AddICECandidate(candidate webrtc.ICECandidateInit, target livekit.SignalTarget) error
AddSubscriber(op Participant) error
RemoveSubscriber(peerId string)
SendJoinResponse(info *livekit.Room, otherParticipants []Participant) error
@@ -71,8 +47,6 @@ type Participant interface {
Close() error
// callbacks
// OnICECandidate - ice candidate discovered for local peer
OnICECandidate(func(c *webrtc.ICECandidateInit))
OnStateChange(func(p Participant, oldState livekit.ParticipantInfo_State))
// OnTrackPublished - remote added a remoteTrack
OnTrackPublished(func(Participant, PublishedTrack))
@@ -83,7 +57,7 @@ type Participant interface {
// package methods
AddSubscribedTrack(participantId string, st SubscribedTrack)
RemoveSubscribedTrack(participantId string, st SubscribedTrack)
PeerConnection() PeerConnection
SubscriberPC() *webrtc.PeerConnection
}
// PublishedTrack is the main interface representing a track published to the room
+210 -212
View File
@@ -12,10 +12,11 @@ import (
)
type FakeParticipant struct {
AddICECandidateStub func(webrtc.ICECandidateInit) error
AddICECandidateStub func(webrtc.ICECandidateInit, livekit.SignalTarget) error
addICECandidateMutex sync.RWMutex
addICECandidateArgsForCall []struct {
arg1 webrtc.ICECandidateInit
arg2 livekit.SignalTarget
}
addICECandidateReturns struct {
result1 error
@@ -47,19 +48,6 @@ type FakeParticipant struct {
arg2 string
arg3 livekit.TrackType
}
AnswerStub func(webrtc.SessionDescription) (webrtc.SessionDescription, error)
answerMutex sync.RWMutex
answerArgsForCall []struct {
arg1 webrtc.SessionDescription
}
answerReturns struct {
result1 webrtc.SessionDescription
result2 error
}
answerReturnsOnCall map[int]struct {
result1 webrtc.SessionDescription
result2 error
}
CloseStub func() error
closeMutex sync.RWMutex
closeArgsForCall []struct {
@@ -91,9 +79,18 @@ type FakeParticipant struct {
handleAnswerReturnsOnCall map[int]struct {
result1 error
}
HandleClientNegotiationStub func()
handleClientNegotiationMutex sync.RWMutex
handleClientNegotiationArgsForCall []struct {
HandleOfferStub func(webrtc.SessionDescription) (webrtc.SessionDescription, error)
handleOfferMutex sync.RWMutex
handleOfferArgsForCall []struct {
arg1 webrtc.SessionDescription
}
handleOfferReturns struct {
result1 webrtc.SessionDescription
result2 error
}
handleOfferReturnsOnCall map[int]struct {
result1 webrtc.SessionDescription
result2 error
}
IDStub func() string
iDMutex sync.RWMutex
@@ -130,11 +127,6 @@ type FakeParticipant struct {
onCloseArgsForCall []struct {
arg1 func(types.Participant)
}
OnICECandidateStub func(func(c *webrtc.ICECandidateInit))
onICECandidateMutex sync.RWMutex
onICECandidateArgsForCall []struct {
arg1 func(c *webrtc.ICECandidateInit)
}
OnStateChangeStub func(func(p types.Participant, oldState livekit.ParticipantInfo_State))
onStateChangeMutex sync.RWMutex
onStateChangeArgsForCall []struct {
@@ -150,16 +142,6 @@ type FakeParticipant struct {
onTrackUpdatedArgsForCall []struct {
arg1 func(types.Participant, types.PublishedTrack)
}
PeerConnectionStub func() types.PeerConnection
peerConnectionMutex sync.RWMutex
peerConnectionArgsForCall []struct {
}
peerConnectionReturns struct {
result1 types.PeerConnection
}
peerConnectionReturnsOnCall map[int]struct {
result1 types.PeerConnection
}
RTCPChanStub func() chan []rtcp.Packet
rTCPChanMutex sync.RWMutex
rTCPChanArgsForCall []struct {
@@ -240,6 +222,26 @@ type FakeParticipant struct {
stateReturnsOnCall map[int]struct {
result1 livekit.ParticipantInfo_State
}
SubscriberMediaEngineStub func() *webrtc.MediaEngine
subscriberMediaEngineMutex sync.RWMutex
subscriberMediaEngineArgsForCall []struct {
}
subscriberMediaEngineReturns struct {
result1 *webrtc.MediaEngine
}
subscriberMediaEngineReturnsOnCall map[int]struct {
result1 *webrtc.MediaEngine
}
SubscriberPCStub func() *webrtc.PeerConnection
subscriberPCMutex sync.RWMutex
subscriberPCArgsForCall []struct {
}
subscriberPCReturns struct {
result1 *webrtc.PeerConnection
}
subscriberPCReturnsOnCall map[int]struct {
result1 *webrtc.PeerConnection
}
ToProtoStub func() *livekit.ParticipantInfo
toProtoMutex sync.RWMutex
toProtoArgsForCall []struct {
@@ -254,18 +256,19 @@ type FakeParticipant struct {
invocationsMutex sync.RWMutex
}
func (fake *FakeParticipant) AddICECandidate(arg1 webrtc.ICECandidateInit) error {
func (fake *FakeParticipant) AddICECandidate(arg1 webrtc.ICECandidateInit, arg2 livekit.SignalTarget) error {
fake.addICECandidateMutex.Lock()
ret, specificReturn := fake.addICECandidateReturnsOnCall[len(fake.addICECandidateArgsForCall)]
fake.addICECandidateArgsForCall = append(fake.addICECandidateArgsForCall, struct {
arg1 webrtc.ICECandidateInit
}{arg1})
arg2 livekit.SignalTarget
}{arg1, arg2})
stub := fake.AddICECandidateStub
fakeReturns := fake.addICECandidateReturns
fake.recordInvocation("AddICECandidate", []interface{}{arg1})
fake.recordInvocation("AddICECandidate", []interface{}{arg1, arg2})
fake.addICECandidateMutex.Unlock()
if stub != nil {
return stub(arg1)
return stub(arg1, arg2)
}
if specificReturn {
return ret.result1
@@ -279,17 +282,17 @@ func (fake *FakeParticipant) AddICECandidateCallCount() int {
return len(fake.addICECandidateArgsForCall)
}
func (fake *FakeParticipant) AddICECandidateCalls(stub func(webrtc.ICECandidateInit) error) {
func (fake *FakeParticipant) AddICECandidateCalls(stub func(webrtc.ICECandidateInit, livekit.SignalTarget) error) {
fake.addICECandidateMutex.Lock()
defer fake.addICECandidateMutex.Unlock()
fake.AddICECandidateStub = stub
}
func (fake *FakeParticipant) AddICECandidateArgsForCall(i int) webrtc.ICECandidateInit {
func (fake *FakeParticipant) AddICECandidateArgsForCall(i int) (webrtc.ICECandidateInit, livekit.SignalTarget) {
fake.addICECandidateMutex.RLock()
defer fake.addICECandidateMutex.RUnlock()
argsForCall := fake.addICECandidateArgsForCall[i]
return argsForCall.arg1
return argsForCall.arg1, argsForCall.arg2
}
func (fake *FakeParticipant) AddICECandidateReturns(result1 error) {
@@ -443,70 +446,6 @@ func (fake *FakeParticipant) AddTrackArgsForCall(i int) (string, string, livekit
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3
}
func (fake *FakeParticipant) Answer(arg1 webrtc.SessionDescription) (webrtc.SessionDescription, error) {
fake.answerMutex.Lock()
ret, specificReturn := fake.answerReturnsOnCall[len(fake.answerArgsForCall)]
fake.answerArgsForCall = append(fake.answerArgsForCall, struct {
arg1 webrtc.SessionDescription
}{arg1})
stub := fake.AnswerStub
fakeReturns := fake.answerReturns
fake.recordInvocation("Answer", []interface{}{arg1})
fake.answerMutex.Unlock()
if stub != nil {
return stub(arg1)
}
if specificReturn {
return ret.result1, ret.result2
}
return fakeReturns.result1, fakeReturns.result2
}
func (fake *FakeParticipant) AnswerCallCount() int {
fake.answerMutex.RLock()
defer fake.answerMutex.RUnlock()
return len(fake.answerArgsForCall)
}
func (fake *FakeParticipant) AnswerCalls(stub func(webrtc.SessionDescription) (webrtc.SessionDescription, error)) {
fake.answerMutex.Lock()
defer fake.answerMutex.Unlock()
fake.AnswerStub = stub
}
func (fake *FakeParticipant) AnswerArgsForCall(i int) webrtc.SessionDescription {
fake.answerMutex.RLock()
defer fake.answerMutex.RUnlock()
argsForCall := fake.answerArgsForCall[i]
return argsForCall.arg1
}
func (fake *FakeParticipant) AnswerReturns(result1 webrtc.SessionDescription, result2 error) {
fake.answerMutex.Lock()
defer fake.answerMutex.Unlock()
fake.AnswerStub = nil
fake.answerReturns = struct {
result1 webrtc.SessionDescription
result2 error
}{result1, result2}
}
func (fake *FakeParticipant) AnswerReturnsOnCall(i int, result1 webrtc.SessionDescription, result2 error) {
fake.answerMutex.Lock()
defer fake.answerMutex.Unlock()
fake.AnswerStub = nil
if fake.answerReturnsOnCall == nil {
fake.answerReturnsOnCall = make(map[int]struct {
result1 webrtc.SessionDescription
result2 error
})
}
fake.answerReturnsOnCall[i] = struct {
result1 webrtc.SessionDescription
result2 error
}{result1, result2}
}
func (fake *FakeParticipant) Close() error {
fake.closeMutex.Lock()
ret, specificReturn := fake.closeReturnsOnCall[len(fake.closeArgsForCall)]
@@ -674,28 +613,68 @@ 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()
func (fake *FakeParticipant) HandleOffer(arg1 webrtc.SessionDescription) (webrtc.SessionDescription, error) {
fake.handleOfferMutex.Lock()
ret, specificReturn := fake.handleOfferReturnsOnCall[len(fake.handleOfferArgsForCall)]
fake.handleOfferArgsForCall = append(fake.handleOfferArgsForCall, struct {
arg1 webrtc.SessionDescription
}{arg1})
stub := fake.HandleOfferStub
fakeReturns := fake.handleOfferReturns
fake.recordInvocation("HandleOffer", []interface{}{arg1})
fake.handleOfferMutex.Unlock()
if stub != nil {
fake.HandleClientNegotiationStub()
return stub(arg1)
}
if specificReturn {
return ret.result1, ret.result2
}
return fakeReturns.result1, fakeReturns.result2
}
func (fake *FakeParticipant) HandleClientNegotiationCallCount() int {
fake.handleClientNegotiationMutex.RLock()
defer fake.handleClientNegotiationMutex.RUnlock()
return len(fake.handleClientNegotiationArgsForCall)
func (fake *FakeParticipant) HandleOfferCallCount() int {
fake.handleOfferMutex.RLock()
defer fake.handleOfferMutex.RUnlock()
return len(fake.handleOfferArgsForCall)
}
func (fake *FakeParticipant) HandleClientNegotiationCalls(stub func()) {
fake.handleClientNegotiationMutex.Lock()
defer fake.handleClientNegotiationMutex.Unlock()
fake.HandleClientNegotiationStub = stub
func (fake *FakeParticipant) HandleOfferCalls(stub func(webrtc.SessionDescription) (webrtc.SessionDescription, error)) {
fake.handleOfferMutex.Lock()
defer fake.handleOfferMutex.Unlock()
fake.HandleOfferStub = stub
}
func (fake *FakeParticipant) HandleOfferArgsForCall(i int) webrtc.SessionDescription {
fake.handleOfferMutex.RLock()
defer fake.handleOfferMutex.RUnlock()
argsForCall := fake.handleOfferArgsForCall[i]
return argsForCall.arg1
}
func (fake *FakeParticipant) HandleOfferReturns(result1 webrtc.SessionDescription, result2 error) {
fake.handleOfferMutex.Lock()
defer fake.handleOfferMutex.Unlock()
fake.HandleOfferStub = nil
fake.handleOfferReturns = struct {
result1 webrtc.SessionDescription
result2 error
}{result1, result2}
}
func (fake *FakeParticipant) HandleOfferReturnsOnCall(i int, result1 webrtc.SessionDescription, result2 error) {
fake.handleOfferMutex.Lock()
defer fake.handleOfferMutex.Unlock()
fake.HandleOfferStub = nil
if fake.handleOfferReturnsOnCall == nil {
fake.handleOfferReturnsOnCall = make(map[int]struct {
result1 webrtc.SessionDescription
result2 error
})
}
fake.handleOfferReturnsOnCall[i] = struct {
result1 webrtc.SessionDescription
result2 error
}{result1, result2}
}
func (fake *FakeParticipant) ID() string {
@@ -889,38 +868,6 @@ func (fake *FakeParticipant) OnCloseArgsForCall(i int) func(types.Participant) {
return argsForCall.arg1
}
func (fake *FakeParticipant) OnICECandidate(arg1 func(c *webrtc.ICECandidateInit)) {
fake.onICECandidateMutex.Lock()
fake.onICECandidateArgsForCall = append(fake.onICECandidateArgsForCall, struct {
arg1 func(c *webrtc.ICECandidateInit)
}{arg1})
stub := fake.OnICECandidateStub
fake.recordInvocation("OnICECandidate", []interface{}{arg1})
fake.onICECandidateMutex.Unlock()
if stub != nil {
fake.OnICECandidateStub(arg1)
}
}
func (fake *FakeParticipant) OnICECandidateCallCount() int {
fake.onICECandidateMutex.RLock()
defer fake.onICECandidateMutex.RUnlock()
return len(fake.onICECandidateArgsForCall)
}
func (fake *FakeParticipant) OnICECandidateCalls(stub func(func(c *webrtc.ICECandidateInit))) {
fake.onICECandidateMutex.Lock()
defer fake.onICECandidateMutex.Unlock()
fake.OnICECandidateStub = stub
}
func (fake *FakeParticipant) OnICECandidateArgsForCall(i int) func(c *webrtc.ICECandidateInit) {
fake.onICECandidateMutex.RLock()
defer fake.onICECandidateMutex.RUnlock()
argsForCall := fake.onICECandidateArgsForCall[i]
return argsForCall.arg1
}
func (fake *FakeParticipant) OnStateChange(arg1 func(p types.Participant, oldState livekit.ParticipantInfo_State)) {
fake.onStateChangeMutex.Lock()
fake.onStateChangeArgsForCall = append(fake.onStateChangeArgsForCall, struct {
@@ -1017,59 +964,6 @@ func (fake *FakeParticipant) OnTrackUpdatedArgsForCall(i int) func(types.Partici
return argsForCall.arg1
}
func (fake *FakeParticipant) PeerConnection() types.PeerConnection {
fake.peerConnectionMutex.Lock()
ret, specificReturn := fake.peerConnectionReturnsOnCall[len(fake.peerConnectionArgsForCall)]
fake.peerConnectionArgsForCall = append(fake.peerConnectionArgsForCall, struct {
}{})
stub := fake.PeerConnectionStub
fakeReturns := fake.peerConnectionReturns
fake.recordInvocation("PeerConnection", []interface{}{})
fake.peerConnectionMutex.Unlock()
if stub != nil {
return stub()
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeParticipant) PeerConnectionCallCount() int {
fake.peerConnectionMutex.RLock()
defer fake.peerConnectionMutex.RUnlock()
return len(fake.peerConnectionArgsForCall)
}
func (fake *FakeParticipant) PeerConnectionCalls(stub func() types.PeerConnection) {
fake.peerConnectionMutex.Lock()
defer fake.peerConnectionMutex.Unlock()
fake.PeerConnectionStub = stub
}
func (fake *FakeParticipant) PeerConnectionReturns(result1 types.PeerConnection) {
fake.peerConnectionMutex.Lock()
defer fake.peerConnectionMutex.Unlock()
fake.PeerConnectionStub = nil
fake.peerConnectionReturns = struct {
result1 types.PeerConnection
}{result1}
}
func (fake *FakeParticipant) PeerConnectionReturnsOnCall(i int, result1 types.PeerConnection) {
fake.peerConnectionMutex.Lock()
defer fake.peerConnectionMutex.Unlock()
fake.PeerConnectionStub = nil
if fake.peerConnectionReturnsOnCall == nil {
fake.peerConnectionReturnsOnCall = make(map[int]struct {
result1 types.PeerConnection
})
}
fake.peerConnectionReturnsOnCall[i] = struct {
result1 types.PeerConnection
}{result1}
}
func (fake *FakeParticipant) RTCPChan() chan []rtcp.Packet {
fake.rTCPChanMutex.Lock()
ret, specificReturn := fake.rTCPChanReturnsOnCall[len(fake.rTCPChanArgsForCall)]
@@ -1524,6 +1418,112 @@ func (fake *FakeParticipant) StateReturnsOnCall(i int, result1 livekit.Participa
}{result1}
}
func (fake *FakeParticipant) SubscriberMediaEngine() *webrtc.MediaEngine {
fake.subscriberMediaEngineMutex.Lock()
ret, specificReturn := fake.subscriberMediaEngineReturnsOnCall[len(fake.subscriberMediaEngineArgsForCall)]
fake.subscriberMediaEngineArgsForCall = append(fake.subscriberMediaEngineArgsForCall, struct {
}{})
stub := fake.SubscriberMediaEngineStub
fakeReturns := fake.subscriberMediaEngineReturns
fake.recordInvocation("SubscriberMediaEngine", []interface{}{})
fake.subscriberMediaEngineMutex.Unlock()
if stub != nil {
return stub()
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeParticipant) SubscriberMediaEngineCallCount() int {
fake.subscriberMediaEngineMutex.RLock()
defer fake.subscriberMediaEngineMutex.RUnlock()
return len(fake.subscriberMediaEngineArgsForCall)
}
func (fake *FakeParticipant) SubscriberMediaEngineCalls(stub func() *webrtc.MediaEngine) {
fake.subscriberMediaEngineMutex.Lock()
defer fake.subscriberMediaEngineMutex.Unlock()
fake.SubscriberMediaEngineStub = stub
}
func (fake *FakeParticipant) SubscriberMediaEngineReturns(result1 *webrtc.MediaEngine) {
fake.subscriberMediaEngineMutex.Lock()
defer fake.subscriberMediaEngineMutex.Unlock()
fake.SubscriberMediaEngineStub = nil
fake.subscriberMediaEngineReturns = struct {
result1 *webrtc.MediaEngine
}{result1}
}
func (fake *FakeParticipant) SubscriberMediaEngineReturnsOnCall(i int, result1 *webrtc.MediaEngine) {
fake.subscriberMediaEngineMutex.Lock()
defer fake.subscriberMediaEngineMutex.Unlock()
fake.SubscriberMediaEngineStub = nil
if fake.subscriberMediaEngineReturnsOnCall == nil {
fake.subscriberMediaEngineReturnsOnCall = make(map[int]struct {
result1 *webrtc.MediaEngine
})
}
fake.subscriberMediaEngineReturnsOnCall[i] = struct {
result1 *webrtc.MediaEngine
}{result1}
}
func (fake *FakeParticipant) SubscriberPC() *webrtc.PeerConnection {
fake.subscriberPCMutex.Lock()
ret, specificReturn := fake.subscriberPCReturnsOnCall[len(fake.subscriberPCArgsForCall)]
fake.subscriberPCArgsForCall = append(fake.subscriberPCArgsForCall, struct {
}{})
stub := fake.SubscriberPCStub
fakeReturns := fake.subscriberPCReturns
fake.recordInvocation("SubscriberPC", []interface{}{})
fake.subscriberPCMutex.Unlock()
if stub != nil {
return stub()
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeParticipant) SubscriberPCCallCount() int {
fake.subscriberPCMutex.RLock()
defer fake.subscriberPCMutex.RUnlock()
return len(fake.subscriberPCArgsForCall)
}
func (fake *FakeParticipant) SubscriberPCCalls(stub func() *webrtc.PeerConnection) {
fake.subscriberPCMutex.Lock()
defer fake.subscriberPCMutex.Unlock()
fake.SubscriberPCStub = stub
}
func (fake *FakeParticipant) SubscriberPCReturns(result1 *webrtc.PeerConnection) {
fake.subscriberPCMutex.Lock()
defer fake.subscriberPCMutex.Unlock()
fake.SubscriberPCStub = nil
fake.subscriberPCReturns = struct {
result1 *webrtc.PeerConnection
}{result1}
}
func (fake *FakeParticipant) SubscriberPCReturnsOnCall(i int, result1 *webrtc.PeerConnection) {
fake.subscriberPCMutex.Lock()
defer fake.subscriberPCMutex.Unlock()
fake.SubscriberPCStub = nil
if fake.subscriberPCReturnsOnCall == nil {
fake.subscriberPCReturnsOnCall = make(map[int]struct {
result1 *webrtc.PeerConnection
})
}
fake.subscriberPCReturnsOnCall[i] = struct {
result1 *webrtc.PeerConnection
}{result1}
}
func (fake *FakeParticipant) ToProto() *livekit.ParticipantInfo {
fake.toProtoMutex.Lock()
ret, specificReturn := fake.toProtoReturnsOnCall[len(fake.toProtoArgsForCall)]
@@ -1588,16 +1588,14 @@ func (fake *FakeParticipant) Invocations() map[string][][]interface{} {
defer fake.addSubscriberMutex.RUnlock()
fake.addTrackMutex.RLock()
defer fake.addTrackMutex.RUnlock()
fake.answerMutex.RLock()
defer fake.answerMutex.RUnlock()
fake.closeMutex.RLock()
defer fake.closeMutex.RUnlock()
fake.getResponseSinkMutex.RLock()
defer fake.getResponseSinkMutex.RUnlock()
fake.handleAnswerMutex.RLock()
defer fake.handleAnswerMutex.RUnlock()
fake.handleClientNegotiationMutex.RLock()
defer fake.handleClientNegotiationMutex.RUnlock()
fake.handleOfferMutex.RLock()
defer fake.handleOfferMutex.RUnlock()
fake.iDMutex.RLock()
defer fake.iDMutex.RUnlock()
fake.identityMutex.RLock()
@@ -1606,16 +1604,12 @@ func (fake *FakeParticipant) Invocations() map[string][][]interface{} {
defer fake.isReadyMutex.RUnlock()
fake.onCloseMutex.RLock()
defer fake.onCloseMutex.RUnlock()
fake.onICECandidateMutex.RLock()
defer fake.onICECandidateMutex.RUnlock()
fake.onStateChangeMutex.RLock()
defer fake.onStateChangeMutex.RUnlock()
fake.onTrackPublishedMutex.RLock()
defer fake.onTrackPublishedMutex.RUnlock()
fake.onTrackUpdatedMutex.RLock()
defer fake.onTrackUpdatedMutex.RUnlock()
fake.peerConnectionMutex.RLock()
defer fake.peerConnectionMutex.RUnlock()
fake.rTCPChanMutex.RLock()
defer fake.rTCPChanMutex.RUnlock()
fake.removeSubscribedTrackMutex.RLock()
@@ -1636,6 +1630,10 @@ func (fake *FakeParticipant) Invocations() map[string][][]interface{} {
defer fake.startMutex.RUnlock()
fake.stateMutex.RLock()
defer fake.stateMutex.RUnlock()
fake.subscriberMediaEngineMutex.RLock()
defer fake.subscriberMediaEngineMutex.RUnlock()
fake.subscriberPCMutex.RLock()
defer fake.subscriberPCMutex.RUnlock()
fake.toProtoMutex.RLock()
defer fake.toProtoMutex.RUnlock()
copiedInvocations := map[string][][]interface{}{}
File diff suppressed because it is too large Load Diff
+3 -11
View File
@@ -177,13 +177,7 @@ func (r *RoomManager) StartSession(roomName, identity, metadata string, reconnec
"num_participants", len(room.GetParticipants()),
)
pc, err := rtc.NewPeerConnection(r.config)
if err != nil {
logger.Errorw("could not create peerConnection", "error", err)
return
}
participant, err = rtc.NewParticipant(identity, pc, responseSink, r.config.Receiver)
participant, err = rtc.NewParticipant(identity, r.config, responseSink, r.config.Receiver)
if err != nil {
logger.Errorw("could not create participant", "error", err)
return
@@ -266,7 +260,7 @@ func (r *RoomManager) rtcSessionWorker(room *rtc.Room, participant types.Partici
switch msg := req.Message.(type) {
case *livekit.SignalRequest_Offer:
_, err := participant.Answer(rtc.FromProtoSessionDescription(msg.Offer))
_, err := participant.HandleOffer(rtc.FromProtoSessionDescription(msg.Offer))
if err != nil {
logger.Errorw("could not handle offer", "err", err, "participant", participant.Identity())
return
@@ -288,8 +282,6 @@ func (r *RoomManager) rtcSessionWorker(room *rtc.Room, participant types.Partici
// jsonError(http.StatusInternalServerError, "could not handle negotiate", err.Error()))
return
}
case *livekit.SignalRequest_Negotiate:
participant.HandleClientNegotiation()
case *livekit.SignalRequest_Trickle:
if participant.State() == livekit.ParticipantInfo_JOINING {
logger.Errorw("cannot trickle before offer", "participant", participant.Identity())
@@ -299,7 +291,7 @@ func (r *RoomManager) rtcSessionWorker(room *rtc.Room, participant types.Partici
candidateInit := rtc.FromProtoTrickle(msg.Trickle)
//logger.Debugw("adding peer candidate", "participant", participant.ID())
if err := participant.AddICECandidate(candidateInit); err != nil {
if err := participant.AddICECandidate(candidateInit, msg.Trickle.Target); err != nil {
logger.Errorw("could not handle trickle", "participant", participant.Identity(), "err", err)
//conn.WriteJSON(
// jsonError(http.StatusInternalServerError, "could not handle trickle", err.Error()))
+1 -1
View File
@@ -13,5 +13,5 @@ const (
)
func NewGuid(prefix string) string {
return prefix + shortuuid.New()
return prefix + shortuuid.New()[:12]
}