Skip to content

Commit

Permalink
Add the option to pass ICE servers with an async WebRTC offer #1408
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Oct 24, 2024
1 parent 9d709f0 commit 16e4831
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 30 deletions.
28 changes: 14 additions & 14 deletions internal/api/ws/ws.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ws

import (
"encoding/json"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -36,22 +37,16 @@ var log zerolog.Logger
type Message struct {
Type string `json:"type"`
Value any `json:"value,omitempty"`
Raw []byte `json:"-"`
}

func (m *Message) String() string {
if s, ok := m.Value.(string); ok {
return s
}
return ""
func (m *Message) String() (value string) {
_ = json.Unmarshal(m.Raw, &value)
return
}

func (m *Message) GetString(key string) string {
if v, ok := m.Value.(map[string]any); ok {
if s, ok := v[key].(string); ok {
return s
}
}
return ""
func (m *Message) Unmarshal(v any) error {
return json.Unmarshal(m.Raw, v)
}

type WSHandler func(tr *Transport, msg *Message) error
Expand Down Expand Up @@ -118,15 +113,20 @@ func apiWS(w http.ResponseWriter, r *http.Request) {
})

for {
msg := new(Message)
if err = ws.ReadJSON(msg); err != nil {
var raw struct {
Type string `json:"type"`
Value json.RawMessage `json:"value"`
}
if err = ws.ReadJSON(&raw); err != nil {
if !websocket.IsCloseError(err, websocket.CloseNoStatusReceived) {
log.Trace().Err(err).Caller().Send()
}
_ = ws.Close()
break
}

msg := &Message{Type: raw.Type, Raw: raw.Value}

log.Trace().Str("type", msg.Type).Msg("[api] ws msg")

if handler := wsHandlers[msg.Type]; handler != nil {
Expand Down
48 changes: 32 additions & 16 deletions internal/webrtc/webrtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ func Init() {
AddCandidate(network, candidate)
}

var err error

// create pionAPI with custom codecs list and custom network settings
serverAPI, err := webrtc.NewServerAPI(network, address, &filters)
serverAPI, err = webrtc.NewServerAPI(network, address, &filters)
if err != nil {
log.Error().Err(err).Caller().Send()
return
}

// use same API for WebRTC server and client if no address
clientAPI := serverAPI
clientAPI = serverAPI

if address != "" {
log.Info().Str("addr", cfg.Mod.Listen).Msg("[webrtc] listen")
Expand Down Expand Up @@ -81,11 +83,13 @@ func Init() {
streams.HandleFunc("webrtc", streamsHandler)
}

var serverAPI, clientAPI *pion.API

var log zerolog.Logger

var PeerConnection func(active bool) (*pion.PeerConnection, error)

func asyncHandler(tr *ws.Transport, msg *ws.Message) error {
func asyncHandler(tr *ws.Transport, msg *ws.Message) (err error) {
var stream *streams.Stream
var mode core.Mode

Expand All @@ -104,8 +108,30 @@ func asyncHandler(tr *ws.Transport, msg *ws.Message) error {
return errors.New(api.StreamNotFound)
}

var offer struct {
Type string `json:"type"`
SDP string `json:"sdp"`
ICEServers []pion.ICEServer `json:"ice_servers"`
}

// V2 - json/object exchange, V1 - raw SDP exchange
apiV2 := msg.Type == "webrtc"

if apiV2 {
if err = msg.Unmarshal(&offer); err != nil {
return err
}
} else {
offer.SDP = msg.String()
}

// create new PeerConnection instance
pc, err := PeerConnection(false)
var pc *pion.PeerConnection
if offer.ICEServers == nil {
pc, err = PeerConnection(false)
} else {
pc, err = serverAPI.NewPeerConnection(pion.Configuration{ICEServers: offer.ICEServers})
}
if err != nil {
log.Error().Err(err).Caller().Send()
return err
Expand Down Expand Up @@ -145,20 +171,10 @@ func asyncHandler(tr *ws.Transport, msg *ws.Message) error {
}
})

// V2 - json/object exchange, V1 - raw SDP exchange
apiV2 := msg.Type == "webrtc"
log.Trace().Msgf("[webrtc] offer:\n%s", offer.SDP)

// 1. SetOffer, so we can get remote client codecs
var offer string
if apiV2 {
offer = msg.GetString("sdp")
} else {
offer = msg.String()
}

log.Trace().Msgf("[webrtc] offer:\n%s", offer)

if err = conn.SetOffer(offer); err != nil {
if err = conn.SetOffer(offer.SDP); err != nil {
log.Warn().Err(err).Caller().Send()
return err
}
Expand Down

0 comments on commit 16e4831

Please sign in to comment.