subscribe new participant after JOINED, to ensure negotiate requests aren't missed

This commit is contained in:
David Zhao
2020-12-13 23:08:20 -08:00
parent af0c9dfdf0
commit ac83bf4c78
7 changed files with 253 additions and 73 deletions
+15 -11
View File
@@ -94,10 +94,10 @@ func (f *SimpleForwarder) Start() {
}
func (f *SimpleForwarder) Close() {
//if f.ctx.Err() != nil {
// return
//}
//f.cancel()
if f.ctx.Err() != nil {
return
}
f.cancel()
if f.onClose != nil {
f.onClose(f)
}
@@ -105,10 +105,10 @@ func (f *SimpleForwarder) Close() {
// Writes an RTP packet to peer
func (f *SimpleForwarder) WriteRTP(pkt *rtp.Packet) error {
//if f.ctx.Err() != nil {
// // skip canceled context errors
// return nil
//}
if f.ctx.Err() != nil {
// skip canceled context errors
return nil
}
err := f.track.WriteRTP(pkt)
@@ -144,13 +144,14 @@ func (f *SimpleForwarder) rtcpWorker() {
return
}
//if f.ctx.Err() != nil {
// return
//}
if f.ctx.Err() != nil {
return
}
if err != nil {
logger.GetLogger().Errorw("could not write read RTCP",
"err", err)
continue
}
var fwdPkts []rtcp.Packet
@@ -196,6 +197,9 @@ func (f *SimpleForwarder) rtcpWorker() {
}
}
if f.ctx.Err() != nil {
return
}
if len(fwdPkts) > 0 {
f.sourceRtcpCh <- fwdPkts
}
+39 -16
View File
@@ -147,11 +147,7 @@ func (p *Participant) ToProto() *livekit.ParticipantInfo {
// Answer an offer from remote participant
func (p *Participant) Answer(sdp webrtc.SessionDescription) (answer webrtc.SessionDescription, err error) {
if p.state == livekit.ParticipantInfo_JOINING {
p.updateState(livekit.ParticipantInfo_JOINED)
}
if err = p.SetRemoteDescription(sdp); err != nil {
if err = p.peerConn.SetRemoteDescription(sdp); err != nil {
return
}
@@ -166,16 +162,6 @@ func (p *Participant) Answer(sdp webrtc.SessionDescription) (answer webrtc.Sessi
return
}
// send client the answer
err = p.sigConn.WriteResponse(&livekit.SignalResponse{
Message: &livekit.SignalResponse_Answer{
Answer: ToProtoSessionDescription(answer),
},
})
if err != nil {
return
}
// only set after answered
p.peerConn.OnNegotiationNeeded(func() {
logger.GetLogger().Debugw("negotiation needed", "participantId", p.ID())
@@ -206,11 +192,48 @@ func (p *Participant) Answer(sdp webrtc.SessionDescription) (answer webrtc.Sessi
p.OnOffer(offer)
}
})
err = p.sigConn.WriteResponse(&livekit.SignalResponse{
Message: &livekit.SignalResponse_Answer{
Answer: ToProtoSessionDescription(answer),
},
})
if p.state == livekit.ParticipantInfo_JOINING {
p.updateState(livekit.ParticipantInfo_JOINED)
}
return
}
// SetRemoteDescription when receiving an answer from remote
// HandleNegotiate when receiving session description from client
func (p *Participant) HandleNegotiate(sd webrtc.SessionDescription) error {
if err := p.peerConn.SetRemoteDescription(sd); err != nil {
return errors.Wrap(err, "could not set remote description")
}
if sd.Type == webrtc.SDPTypeOffer {
answer, err := p.peerConn.CreateAnswer(nil)
if err != nil {
return errors.Wrap(err, "could not create answer")
}
if err = p.peerConn.SetLocalDescription(answer); err != nil {
return errors.Wrap(err, "could not set local description")
}
// send a negotiate response back
return p.sigConn.WriteResponse(&livekit.SignalResponse{
Message: &livekit.SignalResponse_Negotiate{
Negotiate: ToProtoSessionDescription(answer),
},
})
}
return nil
}
func (p *Participant) SetRemoteDescription(sdp webrtc.SessionDescription) error {
logger.GetLogger().Debugw("setting remote description", "type", sdp.Type)
if err := p.peerConn.SetRemoteDescription(sdp); err != nil {
return errors.Wrap(err, "could not set remote description")
}
+16 -14
View File
@@ -67,6 +67,22 @@ func (r *Room) Join(participant *Participant) error {
participant.OnStateChange = func(p *Participant, oldState livekit.ParticipantInfo_State) {
log.Debugw("participant state changed", "state", p.state, "participant", p.id)
r.broadcastParticipantState(p)
if oldState == livekit.ParticipantInfo_JOINING && p.state == livekit.ParticipantInfo_JOINED {
// subscribe participant to existing tracks
for _, p := range r.participants {
if p.id == participant.id {
// don't send to itself
continue
}
if err := p.AddSubscriber(participant); err != nil {
// TODO: log error? or disconnect?
logger.GetLogger().Errorw("could not subscribe to participant",
"dstParticipant", participant.ID(),
"srcParticipant", p.ID())
}
}
}
}
log.Infow("new participant joined",
@@ -74,20 +90,6 @@ func (r *Room) Join(participant *Participant) error {
"name", participant.Name(),
"roomId", r.Sid)
// subscribe participant to existing tracks
for _, p := range r.participants {
if p.id == participant.id {
// don't send to itself
continue
}
if err := p.AddSubscriber(participant); err != nil {
// TODO: log error? or disconnect?
logger.GetLogger().Errorw("could not subscribe to participant",
"dstParticipant", participant.ID(),
"srcParticipant", p.ID())
}
}
r.participants[participant.ID()] = participant
// gather other participants and send join response
+1
View File
@@ -141,6 +141,7 @@ func (t *Track) RemoveSubscriber(participantId string) {
}
func (t *Track) RemoveAllSubscribers() {
logger.GetLogger().Debugw("removing all subscribers", "track", t.id)
t.lock.RLock()
defer t.lock.RUnlock()
for _, f := range t.forwarders {
+2 -30
View File
@@ -6,7 +6,6 @@ import (
"net/http"
"github.com/gorilla/websocket"
"github.com/pion/webrtc/v3"
"github.com/pkg/errors"
"github.com/livekit/livekit-server/pkg/config"
@@ -122,7 +121,8 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
//conn.WriteJSON(jsonError(http.StatusNotAcceptable, "cannot negotiate before peer offer"))
return
}
err = s.handleNegotiate(signalConn, participant, msg.Negotiate)
sd := rtc.FromProtoSessionDescription(msg.Negotiate)
err = participant.HandleNegotiate(sd)
if err != nil {
log.Errorw("could not handle negotiate", "participant", participant.ID(), "err", err)
//conn.WriteJSON(
@@ -160,34 +160,6 @@ func (s *RTCService) handleOffer(participant *rtc.Participant, offer *livekit.Se
return nil
}
func (s *RTCService) handleNegotiate(sc rtc.SignalConnection, participant *rtc.Participant, neg *livekit.SessionDescription) error {
logger.GetLogger().Debugw("handling incoming negotiate", "participant", participant.ID())
if neg.Type == webrtc.SDPTypeOffer.String() {
offer := rtc.FromProtoSessionDescription(neg)
answer, err := participant.Answer(offer)
if err != nil {
return err
}
err = sc.WriteResponse(&livekit.SignalResponse{
Message: &livekit.SignalResponse_Negotiate{
Negotiate: rtc.ToProtoSessionDescription(answer),
},
})
if err != nil {
return err
}
} else if neg.Type == webrtc.SDPTypeAnswer.String() {
answer := rtc.FromProtoSessionDescription(neg)
err := participant.SetRemoteDescription(answer)
if err != nil {
return err
}
}
return nil
}
func (s *RTCService) handleTrickle(peer *rtc.Participant, trickle *livekit.Trickle) error {
candidateInit := rtc.FromProtoTrickle(trickle)
logger.GetLogger().Debugw("adding peer candidate", "participantId", peer.ID())