mirror of
https://github.com/AlexxIT/go2rtc.git
synced 2025-10-07 09:11:28 +08:00
Rewrite WebRTC peer connection constructor
This commit is contained in:
@@ -40,7 +40,7 @@ func asyncClient(url string) (streamer.Producer, error) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// 2. Create PeerConnection
|
// 2. Create PeerConnection
|
||||||
pc, err := newPeerConnection(false)
|
pc, err := PeerConnection(true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Caller().Send()
|
log.Error().Err(err).Caller().Send()
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -125,7 +125,7 @@ func asyncClient(url string) (streamer.Producer, error) {
|
|||||||
// syncClient - support WebRTC-HTTP Egress Protocol (WHEP)
|
// syncClient - support WebRTC-HTTP Egress Protocol (WHEP)
|
||||||
func syncClient(url string) (streamer.Producer, error) {
|
func syncClient(url string) (streamer.Producer, error) {
|
||||||
// 2. Create PeerConnection
|
// 2. Create PeerConnection
|
||||||
pc, err := newPeerConnection(false)
|
pc, err := PeerConnection(true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Caller().Send()
|
log.Error().Err(err).Caller().Send()
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@@ -54,11 +54,12 @@ func Init() {
|
|||||||
SDPSemantics: pion.SDPSemanticsUnifiedPlanWithFallback,
|
SDPSemantics: pion.SDPSemanticsUnifiedPlanWithFallback,
|
||||||
}
|
}
|
||||||
|
|
||||||
newPeerConnection = func(isServer bool) (*pion.PeerConnection, error) {
|
PeerConnection = func(active bool) (*pion.PeerConnection, error) {
|
||||||
if isServer {
|
// active - client, passive - server
|
||||||
return serverAPI.NewPeerConnection(pionConf)
|
if active {
|
||||||
} else {
|
|
||||||
return clientAPI.NewPeerConnection(pionConf)
|
return clientAPI.NewPeerConnection(pionConf)
|
||||||
|
} else {
|
||||||
|
return serverAPI.NewPeerConnection(pionConf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,7 +82,7 @@ func Init() {
|
|||||||
var Port string
|
var Port string
|
||||||
var log zerolog.Logger
|
var log zerolog.Logger
|
||||||
|
|
||||||
var newPeerConnection func(isServer bool) (*pion.PeerConnection, error)
|
var PeerConnection func(active bool) (*pion.PeerConnection, error)
|
||||||
|
|
||||||
func asyncHandler(tr *api.Transport, msg *api.Message) error {
|
func asyncHandler(tr *api.Transport, msg *api.Message) error {
|
||||||
src := tr.Request.URL.Query().Get("src")
|
src := tr.Request.URL.Query().Get("src")
|
||||||
@@ -93,7 +94,7 @@ func asyncHandler(tr *api.Transport, msg *api.Message) error {
|
|||||||
log.Debug().Str("url", src).Msg("[webrtc] new consumer")
|
log.Debug().Str("url", src).Msg("[webrtc] new consumer")
|
||||||
|
|
||||||
// create new PeerConnection instance
|
// create new PeerConnection instance
|
||||||
pc, err := newPeerConnection(true)
|
pc, err := PeerConnection(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Caller().Send()
|
log.Error().Err(err).Caller().Send()
|
||||||
return err
|
return err
|
||||||
@@ -168,7 +169,7 @@ func asyncHandler(tr *api.Transport, msg *api.Message) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ExchangeSDP(stream *streams.Stream, offer string, userAgent string) (answer string, err error) {
|
func ExchangeSDP(stream *streams.Stream, offer string, userAgent string) (answer string, err error) {
|
||||||
pc, err := newPeerConnection(true)
|
pc, err := PeerConnection(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Caller().Send()
|
log.Error().Err(err).Caller().Send()
|
||||||
return
|
return
|
||||||
|
@@ -139,7 +139,7 @@ func inputWebRTC(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
log.Trace().Msgf("[webrtc] WHIP offer\n%s", offer)
|
log.Trace().Msgf("[webrtc] WHIP offer\n%s", offer)
|
||||||
|
|
||||||
pc, err := newPeerConnection(true)
|
pc, err := PeerConnection(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Caller().Send()
|
log.Error().Err(err).Caller().Send()
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
Reference in New Issue
Block a user