Skip to content

Commit

Permalink
Fix panic on write to WebRTC source #935
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Apr 30, 2024
1 parent bec7927 commit 0698f90
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions internal/webrtc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/url"
"strings"
"sync"
"time"

"github.com/AlexxIT/go2rtc/internal/api/ws"
Expand Down Expand Up @@ -82,6 +83,7 @@ func go2rtcClient(url string) (core.Producer, error) {

// waiter will wait PC error or WS error or nil (connection OK)
var connState core.Waiter
var connMu sync.Mutex

prod := webrtc.NewConn(pc)
prod.Desc = "WebRTC/WebSocket async"
Expand All @@ -91,7 +93,9 @@ func go2rtcClient(url string) (core.Producer, error) {
case *pion.ICECandidate:
s := msg.ToJSON().Candidate
log.Trace().Str("candidate", s).Msg("[webrtc] local ")
connMu.Lock()
_ = conn.WriteJSON(&ws.Message{Type: "webrtc/candidate", Value: s})
connMu.Unlock()

case pion.PeerConnectionState:
switch msg {
Expand All @@ -118,9 +122,9 @@ func go2rtcClient(url string) (core.Producer, error) {

// 4. Send offer
msg := &ws.Message{Type: "webrtc/offer", Value: offer}
if err = conn.WriteJSON(msg); err != nil {
return nil, err
}
connMu.Lock()
_ = conn.WriteJSON(msg)
connMu.Unlock()

// 5. Get answer
if err = conn.ReadJSON(msg); err != nil {
Expand Down

0 comments on commit 0698f90

Please sign in to comment.