Make WebRTCPeer.preparePeerConnection block.

Formerly, preparePeerConnection set up a callback that sent into a
channel, and exchangeSDP waited until it could receive from the channel.
We can move the channel entirely into preparePeerConnection (having it
not return until the callback has been called) and that way remove some
shared state.
This commit is contained in:
David Fifield 2020-04-23 20:13:16 -06:00
parent 5787d5b8b0
commit 81d14ad33a

View file

@ -24,7 +24,6 @@ type WebRTCPeer struct {
transport *webrtc.DataChannel transport *webrtc.DataChannel
broker *BrokerChannel broker *BrokerChannel
offerChannel chan *webrtc.SessionDescription
recvPipe *io.PipeReader recvPipe *io.PipeReader
writePipe *io.PipeWriter writePipe *io.PipeWriter
lastReceive time.Time lastReceive time.Time
@ -51,7 +50,6 @@ func NewWebRTCPeer(config *webrtc.Configuration,
} }
connection.config = config connection.config = config
connection.broker = broker connection.broker = broker
connection.offerChannel = make(chan *webrtc.SessionDescription, 1)
// Override with something that's not NullLogger to have real logging. // Override with something that's not NullLogger to have real logging.
connection.BytesLogger = &BytesNullLogger{} connection.BytesLogger = &BytesNullLogger{}
@ -153,11 +151,12 @@ func (c *WebRTCPeer) preparePeerConnection() error {
return err return err
} }
// Prepare PeerConnection callbacks. // Prepare PeerConnection callbacks.
offerChannel := make(chan struct{})
// Allow candidates to accumulate until ICEGatheringStateComplete. // Allow candidates to accumulate until ICEGatheringStateComplete.
pc.OnICECandidate(func(candidate *webrtc.ICECandidate) { pc.OnICECandidate(func(candidate *webrtc.ICECandidate) {
if candidate == nil { if candidate == nil {
log.Printf("WebRTC: Done gathering candidates") log.Printf("WebRTC: Done gathering candidates")
c.offerChannel <- pc.LocalDescription() close(offerChannel)
} else { } else {
log.Printf("WebRTC: Got ICE candidate: %s", candidate.String()) log.Printf("WebRTC: Got ICE candidate: %s", candidate.String())
} }
@ -180,6 +179,7 @@ func (c *WebRTCPeer) preparePeerConnection() error {
} }
log.Println("WebRTC: Set local description") log.Println("WebRTC: Set local description")
<-offerChannel // Wait for ICE candidate gathering to complete.
log.Println("WebRTC: PeerConnection created.") log.Println("WebRTC: PeerConnection created.")
return nil return nil
} }
@ -254,10 +254,9 @@ func (c *WebRTCPeer) establishDataChannel() error {
return nil return nil
} }
// exchangeSDP blocks until an SDP offer is available, sends it to the Broker, // exchangeSDP sends the local SDP offer to the Broker and awaits the SDP
// then awaits the SDP answer. // answer.
func (c *WebRTCPeer) exchangeSDP() error { func (c *WebRTCPeer) exchangeSDP() error {
<-c.offerChannel
// Keep trying the same offer until a valid answer arrives. // Keep trying the same offer until a valid answer arrives.
var answer *webrtc.SessionDescription var answer *webrtc.SessionDescription
for { for {
@ -282,9 +281,6 @@ func (c *WebRTCPeer) exchangeSDP() error {
// Close all channels and transports // Close all channels and transports
func (c *WebRTCPeer) cleanup() { func (c *WebRTCPeer) cleanup() {
if nil != c.offerChannel {
close(c.offerChannel)
}
// Close this side of the SOCKS pipe. // Close this side of the SOCKS pipe.
if nil != c.writePipe { if nil != c.writePipe {
c.writePipe.Close() c.writePipe.Close()