Skip to content

Commit

Permalink
WebRTCTransportConfig Breakout, only call session.onClosed once (#283)
Browse files Browse the repository at this point in the history
* Split out function to create a WebRTCTransportConfig so external projects can replicate sfu.SFU{} functionality easily

* Use interface transportProvider for sfu.Peer{}

* Only call sesson.OnClosed once
  • Loading branch information
billylindeman authored Nov 11, 2020
1 parent 03fab2f commit b0d44f5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
16 changes: 11 additions & 5 deletions pkg/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ var (
ErrOfferIgnored = errors.New("offered ignored")
)

// TransportProvider provides the peerConnection to the sfu.Peer{}
// This allows the sfu.SFU{} implementation to be customized / wrapped by another package
type TransportProvider interface {
NewWebRTCTransport(sid string, me MediaEngine) (*WebRTCTransport, error)
}

// Peer represents a single peer signal session
type Peer struct {
sync.Mutex
sfu *SFU
pc *WebRTCTransport
provider TransportProvider
pc *WebRTCTransport

OnIceCandidate func(*webrtc.ICECandidateInit)
OnOffer func(*webrtc.SessionDescription)
Expand All @@ -32,9 +38,9 @@ type Peer struct {
}

// NewPeer creates a new Peer for signaling with the given SFU
func NewPeer(sfu *SFU) Peer {
func NewPeer(provider TransportProvider) Peer {
return Peer{
sfu: sfu,
provider: provider,
}
}

Expand All @@ -53,7 +59,7 @@ func (p *Peer) Join(sid string, sdp webrtc.SessionDescription) (*webrtc.SessionD
return nil, fmt.Errorf("error parsing sdp: %v", err)
}

pc, err := p.sfu.NewWebRTCTransport(sid, me)
pc, err := p.provider.NewWebRTCTransport(sid, me)
if err != nil {
return nil, fmt.Errorf("error creating transport: %v", err)
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ type Session struct {
mu sync.RWMutex
transports map[string]Transport
onCloseHandler func()
closed bool
}

// NewSession creates a new session
func NewSession(id string) *Session {
return &Session{
id: id,
transports: make(map[string]Transport),
closed: false,
}
}

Expand All @@ -39,8 +41,9 @@ func (s *Session) RemoveTransport(tid string) {
delete(s.transports, tid)

// Close session if no transports
if len(s.transports) == 0 && s.onCloseHandler != nil {
if len(s.transports) == 0 && s.onCloseHandler != nil && !s.closed {
s.onCloseHandler()
s.closed = true
}
}

Expand Down
20 changes: 14 additions & 6 deletions pkg/sfu.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,8 @@ type SFU struct {
sessions map[string]*Session
}

// NewSFU creates a new sfu instance
func NewSFU(c Config) *SFU {
// Init random seed
rand.Seed(time.Now().UnixNano())
// Init ballast
ballast := make([]byte, c.SFU.Ballast*1024*1024)
// NewWebRTCTransportConfig parses our settings and returns a usable WebRTCTransportConfig for creating PeerConnections
func NewWebRTCTransportConfig(c Config) WebRTCTransportConfig {
se := webrtc.SettingEngine{}

// Configure required extensions
Expand Down Expand Up @@ -138,6 +134,18 @@ func NewSFU(c Config) *SFU {
w.setting.SetNAT1To1IPs(c.WebRTC.Candidates.NAT1To1IPs, webrtc.ICECandidateTypeHost)
}

return w
}

// NewSFU creates a new sfu instance
func NewSFU(c Config) *SFU {
// Init random seed
rand.Seed(time.Now().UnixNano())
// Init ballast
ballast := make([]byte, c.SFU.Ballast*1024*1024)

w := NewWebRTCTransportConfig(c)

s := &SFU{
webrtc: w,
sessions: make(map[string]*Session),
Expand Down

0 comments on commit b0d44f5

Please sign in to comment.