Skip to content

Commit

Permalink
fix(transport): sub on connected (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev authored Oct 26, 2020
1 parent d55850c commit cdd5c17
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
3 changes: 2 additions & 1 deletion pkg/simplesender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
30 changes: 17 additions & 13 deletions pkg/webrtctransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit cdd5c17

Please sign in to comment.