From bcdccb1ff3e75314477a7a3c5ddf5d6b9129bc9f Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 7 Apr 2020 12:17:21 +1000 Subject: [PATCH] Refactoring in webtorrent.Transport --- webtorrent/transport.go | 49 ++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/webtorrent/transport.go b/webtorrent/transport.go index e613b26e..0bb5701d 100644 --- a/webtorrent/transport.go +++ b/webtorrent/transport.go @@ -10,24 +10,34 @@ import ( "github.com/pion/webrtc/v2" ) +var ( + api = func() *webrtc.API { + // Enable the detach API (since it's non-standard but more idiomatic) + // (This should be done once globally) + s := webrtc.SettingEngine{} + s.DetachDataChannels() + return webrtc.NewAPI(webrtc.WithSettingEngine(s)) + }() + config = webrtc.Configuration{ICEServers: []webrtc.ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}}} + newPeerConnectionMu sync.Mutex +) + +func newPeerConnection() (*webrtc.PeerConnection, error) { + newPeerConnectionMu.Lock() + defer newPeerConnectionMu.Unlock() + return api.NewPeerConnection(config) +} + type Transport struct { pc *webrtc.PeerConnection dc *webrtc.DataChannel - lock *sync.Mutex + lock sync.Mutex } -// NewTransport creates a transport and returns a WebRTC offer -// to be announced +// NewTransport creates a transport and returns a WebRTC offer to be announced func NewTransport() (*Transport, webrtc.SessionDescription, error) { - // Enable the detach API (since it's non-standard but more idiomatic) - // (This should be done once globally) - s := webrtc.SettingEngine{} - s.DetachDataChannels() - api := webrtc.NewAPI(webrtc.WithSettingEngine(s)) - - config := webrtc.Configuration{ICEServers: []webrtc.ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}}} - peerConnection, err := api.NewPeerConnection(config) + peerConnection, err := newPeerConnection() if err != nil { return nil, webrtc.SessionDescription{}, fmt.Errorf("failed to peer connection: %v\n", err) } @@ -51,21 +61,14 @@ func NewTransport() (*Transport, webrtc.SessionDescription, error) { return nil, webrtc.SessionDescription{}, fmt.Errorf("failed to set local description: %v\n", err) } - t := &Transport{pc: peerConnection, dc: dataChannel, lock: &sync.Mutex{}} + t := &Transport{pc: peerConnection, dc: dataChannel} return t, offer, nil } -// NewTransportFromOffer creates a transport from a WebRTC offer and -// and returns a WebRTC answer to be announced +// NewTransportFromOffer creates a transport from a WebRTC offer and and returns a WebRTC answer to +// be announced. func NewTransportFromOffer(offer webrtc.SessionDescription, onOpen func(datachannel.ReadWriteCloser)) (*Transport, webrtc.SessionDescription, error) { - // Enable the detach API (since it's non-standard but more idiomatic) - // (This should be done once globally) - s := webrtc.SettingEngine{} - s.DetachDataChannels() - api := webrtc.NewAPI(webrtc.WithSettingEngine(s)) - - config := webrtc.Configuration{ICEServers: []webrtc.ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}}} - peerConnection, err := api.NewPeerConnection(config) + peerConnection, err := newPeerConnection() if err != nil { return nil, webrtc.SessionDescription{}, fmt.Errorf("failed to peer connection: %v", err) } @@ -73,7 +76,7 @@ func NewTransportFromOffer(offer webrtc.SessionDescription, onOpen func(datachan fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String()) }) - t := &Transport{pc: peerConnection, lock: &sync.Mutex{}} + t := &Transport{pc: peerConnection} peerConnection.OnDataChannel(func(d *webrtc.DataChannel) { fmt.Printf("New DataChannel %s %d\n", d.Label(), d.ID()) t.lock.Lock() -- 2.48.1