Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/asticode/go-astits v1.13.0
github.com/expr-lang/expr v1.16.9
github.com/go-viper/mapstructure/v2 v2.2.1
github.com/gorilla/websocket v1.5.1
github.com/mattn/go-isatty v0.0.20
github.com/miekg/dns v1.1.59
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/expr-lang/expr v1.16.5 h1:m2hvtguFeVaVNTHj8L7BoAyt7O0PAIBaSVbjdHgRXMs
github.com/expr-lang/expr v1.16.5/go.mod h1:uCkhfG+x7fcZ5A5sXHKuQ07jGZRl6J0FCAaf2k4PtVQ=
github.com/expr-lang/expr v1.16.9 h1:WUAzmR0JNI9JCiF0/ewwHB1gmcGw5wW7nWt8gc6PpCI=
github.com/expr-lang/expr v1.16.9/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4=
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
Expand Down
76 changes: 50 additions & 26 deletions internal/webrtc/webrtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/AlexxIT/go2rtc/internal/streams"
"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/AlexxIT/go2rtc/pkg/webrtc"
"github.com/go-viper/mapstructure/v2"
pion "github.com/pion/webrtc/v3"
"github.com/rs/zerolog"
)
Expand Down Expand Up @@ -60,12 +61,20 @@ func Init() {
SDPSemantics: pion.SDPSemanticsUnifiedPlanWithFallback,
}

PeerConnection = func(active bool) (*pion.PeerConnection, error) {
PeerConnectionWithIceServers = func(active bool, servers []pion.ICEServer) (*pion.PeerConnection, error) {
conf := pionConf
if len(servers) > 0 {
conf = pion.Configuration{
ICEServers: append(cfg.Mod.IceServers, servers...),
SDPSemantics: pion.SDPSemanticsUnifiedPlanWithFallback,
}
}

// active - client, passive - server
if active {
return clientAPI.NewPeerConnection(pionConf)
return clientAPI.NewPeerConnection(conf)
} else {
return serverAPI.NewPeerConnection(pionConf)
return serverAPI.NewPeerConnection(conf)
}
}

Expand All @@ -83,7 +92,11 @@ func Init() {

var log zerolog.Logger

var PeerConnection func(active bool) (*pion.PeerConnection, error)
var PeerConnectionWithIceServers func(active bool, servers []pion.ICEServer) (*pion.PeerConnection, error)

func PeerConnection(active bool) (*pion.PeerConnection, error) {
return PeerConnectionWithIceServers(active, nil)
}

func asyncHandler(tr *ws.Transport, msg *ws.Message) error {
var stream *streams.Stream
Expand All @@ -104,18 +117,42 @@ func asyncHandler(tr *ws.Transport, msg *ws.Message) error {
return errors.New(api.StreamNotFound)
}

// create new PeerConnection instance
pc, err := PeerConnection(false)
if err != nil {
log.Error().Err(err).Caller().Send()
return err
}

var sendAnswer core.Waiter

// protect from blocking on errors
defer sendAnswer.Done(nil)

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

// 1. SetOffer, so we can get remote client codecs
var offer string
var pc *pion.PeerConnection
var pc_err error
if apiV2 {
var result struct {
Sdp string `mapstructure:"sdp"`
ICEServers []pion.ICEServer `mapstructure:"iceServers,omitempty"`
}

err := mapstructure.Decode(msg.Value, &result)
if err != nil {
panic(err)
}
offer = result.Sdp
pc, pc_err = PeerConnectionWithIceServers(false, result.ICEServers)
} else {
offer = msg.String()
pc, pc_err = PeerConnection(false)
}

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

if pc_err != nil {
log.Error().Err(pc_err).Caller().Send()
return pc_err
}

conn := webrtc.NewConn(pc)
conn.Mode = mode
conn.Protocol = "ws"
Expand Down Expand Up @@ -145,28 +182,15 @@ func asyncHandler(tr *ws.Transport, msg *ws.Message) error {
}
})

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

// 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); err != nil {
log.Warn().Err(err).Caller().Send()
return err
}

switch mode {
case core.ModePassiveConsumer:
// 2. AddConsumer, so we get new tracks
if err = stream.AddConsumer(conn); err != nil {
if err := stream.AddConsumer(conn); err != nil {
log.Debug().Err(err).Msg("[webrtc] add consumer")
_ = conn.Close()
return err
Expand Down