diff --git a/pkg/simplesender.go b/pkg/simplesender.go index 40da14747..1537afcf4 100644 --- a/pkg/simplesender.go +++ b/pkg/simplesender.go @@ -113,6 +113,7 @@ func (s *SimpleSender) WriteRTP(pkt *rtp.Packet) { s.tsOffset = pkt.Timestamp - s.lastTS + 1 s.reSync.set(false) } + // Backup payload bSN := pkt.SequenceNumber bTS := pkt.Timestamp @@ -123,6 +124,7 @@ func (s *SimpleSender) WriteRTP(pkt *rtp.Packet) { pkt.PayloadType = s.payload pkt.Timestamp = s.lastTS pkt.SequenceNumber = s.lastSN + err := s.track.WriteRTP(pkt) // Restore packet pkt.PayloadType = bPt @@ -189,7 +191,6 @@ func (s *SimpleSender) receiveRTCP() { for { pkts, err := s.sender.ReadRTCP() if err == io.ErrClosedPipe || err == io.EOF { - log.Debugf("Sender %s closed due to: %v", s.id, err) // Remove sender from receiver if recv := s.router.receivers[0]; recv != nil { recv.DeleteSender(s.id) diff --git a/pkg/webrtctransport.go b/pkg/webrtctransport.go index e9fcec8b2..1ed2d5e69 100644 --- a/pkg/webrtctransport.go +++ b/pkg/webrtctransport.go @@ -35,6 +35,8 @@ type WebRTCTransport struct { senders map[string][]Sender router Router onTrackHandler func(*webrtc.Track, *webrtc.RTPReceiver) + + subOnce sync.Once } // NewWebRTCTransport creates a new WebRTCTransport @@ -60,19 +62,7 @@ func NewWebRTCTransport(ctx context.Context, session *Session, me MediaEngine, c mids: make(map[string]Sender), senders: make(map[string][]Sender), } - // Subscribe to existing transports - defer func() { - for _, t := range session.Transports() { - if t.ID() == p.id { - continue - } - err := t.GetRouter().AddSender(p, nil) - if err != nil { - log.Errorf("Subscribing to router err: %v", err) - continue - } - } - }() + // Add transport to the session session.AddTransport(p) @@ -101,6 +91,20 @@ func NewWebRTCTransport(ctx context.Context, session *Session, me MediaEngine, c return default: switch connectionState { + case webrtc.ICEConnectionStateConnected: + p.subOnce.Do(func() { + // Subscribe to existing transports + for _, t := range session.Transports() { + if t.ID() == p.id { + continue + } + err := t.GetRouter().AddSender(p, nil) + if err != nil { + log.Errorf("Subscribing to router err: %v", err) + continue + } + } + }) case webrtc.ICEConnectionStateDisconnected: log.Debugf("webrtc ice disconnected for peer: %s", p.id) case webrtc.ICEConnectionStateFailed: