From 5a5b5127758ee52b6483e8b6d6fbc997375fefaa Mon Sep 17 00:00:00 2001 From: Neeraj Jakhar Date: Wed, 14 Aug 2024 11:21:44 +0530 Subject: [PATCH] Support for websocket without turn server --- pkg/configuration.go | 6 + pkg/main.go | 4 +- pkg/stream.go | 55 ++++--- samples/websocket/stream-no-turn.php | 218 +++++++++++++++++++++++++++ 4 files changed, 265 insertions(+), 18 deletions(-) create mode 100644 samples/websocket/stream-no-turn.php diff --git a/pkg/configuration.go b/pkg/configuration.go index 288f3fa..39eafad 100644 --- a/pkg/configuration.go +++ b/pkg/configuration.go @@ -19,6 +19,11 @@ package main import "github.com/pion/webrtc/v3" +const ( + TurnInternal = "internal" + TurnPublicIp = "ip" +) + type OpenRelay struct { AppName string `yaml:"app_name"` ApiKey string `yaml:"api_key"` @@ -37,6 +42,7 @@ type UserCredentials struct { } type TurnConfiguration struct { + TurnType string `yaml:"type" validate:"oneof=ip internal" default:"ip"` PublicIp string `yaml:"public_ip" validate:"required"` UdpPort int `yaml:"port" validate:"required,number,gte=1,lte=65535" default:"8080"` Users []UserCredentials `yaml:"users" validate:"required"` diff --git a/pkg/main.go b/pkg/main.go index f7615d1..14d7246 100644 --- a/pkg/main.go +++ b/pkg/main.go @@ -146,7 +146,9 @@ func setupCommon(config *Configuration) *os.File { log.Fatalln("At least one user needs to be provided for server") } - go setupTurnServer(config) + if config.TurnConfiguration.TurnType == TurnInternal { + go setupTurnServer(config) + } } return f diff --git a/pkg/stream.go b/pkg/stream.go index 98aebc6..a4219bb 100644 --- a/pkg/stream.go +++ b/pkg/stream.go @@ -27,6 +27,7 @@ import ( "github.com/go-gst/go-gst/gst" "github.com/go-gst/go-gst/gst/app" + "github.com/pion/interceptor" "github.com/pion/webrtc/v3" "github.com/pion/webrtc/v3/pkg/media" ) @@ -38,20 +39,42 @@ func StartStreaming(conf *Configuration, videoSrc, audioSrc, sdpFile string, wai startExecuting(conf, videoSrc, audioSrc, sdpFile, wait) } -func getWebrtcConfiguration(conf *Configuration) webrtc.Configuration { +func getWebrtcPeerConfiguration(conf *Configuration) (*webrtc.PeerConnection, error) { config := webrtc.Configuration{} if conf.UseInternalTurn { - config.ICEServers = make([]webrtc.ICEServer, 2*len(conf.TurnConfiguration.Users)) - - for i, user := range conf.TurnConfiguration.Users { - config.ICEServers[2*i].URLs = make([]string, 1) - config.ICEServers[2*i].URLs[0] = fmt.Sprintf("stun:%s:%d", "127.0.0.1", conf.TurnConfiguration.UdpPort) - config.ICEServers[2*i].Username = user.User - config.ICEServers[2*i].Credential = user.Password - config.ICEServers[2*i+1].URLs = make([]string, 1) - config.ICEServers[2*i+1].URLs[0] = fmt.Sprintf("turn:%s:%d", "127.0.0.1", conf.TurnConfiguration.UdpPort) - config.ICEServers[2*i+1].Username = user.User - config.ICEServers[2*i+1].Credential = user.Password + if conf.TurnConfiguration.TurnType == TurnInternal { + config.ICEServers = make([]webrtc.ICEServer, 2*len(conf.TurnConfiguration.Users)) + + for i, user := range conf.TurnConfiguration.Users { + config.ICEServers[2*i].URLs = make([]string, 1) + config.ICEServers[2*i].URLs[0] = fmt.Sprintf("stun:%s:%d", "127.0.0.1", conf.TurnConfiguration.UdpPort) + config.ICEServers[2*i].Username = user.User + config.ICEServers[2*i].Credential = user.Password + config.ICEServers[2*i+1].URLs = make([]string, 1) + config.ICEServers[2*i+1].URLs[0] = fmt.Sprintf("turn:%s:%d", "127.0.0.1", conf.TurnConfiguration.UdpPort) + config.ICEServers[2*i+1].Username = user.User + config.ICEServers[2*i+1].Credential = user.Password + } + } else { + m := &webrtc.MediaEngine{} + if err := m.RegisterDefaultCodecs(); err != nil { + return nil, err + } + + i := &interceptor.Registry{} + if err := webrtc.RegisterDefaultInterceptors(m, i); err != nil { + return nil, err + } + + s := webrtc.SettingEngine{} + s.SetNAT1To1IPs([]string{conf.TurnConfiguration.PublicIp}, webrtc.ICECandidateTypeSrflx) + + config.ICEServers = make([]webrtc.ICEServer, 1) + config.ICEServers[0].URLs = make([]string, 1) + config.ICEServers[0].URLs[0] = "stun:stun.l.google.com:19302" + + api := webrtc.NewAPI(webrtc.WithMediaEngine(m), webrtc.WithInterceptorRegistry(i), webrtc.WithSettingEngine(s)) + return api.NewPeerConnection(config) } } else if conf.OpenRelayConfig != nil { fmt.Println("Found Open Relay Config") @@ -90,7 +113,8 @@ func getWebrtcConfiguration(conf *Configuration) webrtc.Configuration { config.ICEServers[0].URLs[0] = "stun:stun.l.google.com:19302" } - return config + fmt.Printf("Webrtc config: %v\n", config) + return webrtc.NewPeerConnection(config) } func printAnswer(answer webrtc.SessionDescription) { @@ -101,10 +125,7 @@ func printAnswer(answer webrtc.SessionDescription) { func startExecuting(conf *Configuration, videoSrc, audioSrc, sdpFile string, wait bool) { gst.Init(nil) - config := getWebrtcConfiguration(conf) - fmt.Printf("Webrtc config: %v\n", config) - - peerConnection, err := webrtc.NewPeerConnection(config) + peerConnection, err := getWebrtcPeerConfiguration(conf) if err != nil { log.Fatalln(err) } diff --git a/samples/websocket/stream-no-turn.php b/samples/websocket/stream-no-turn.php new file mode 100644 index 0000000..8a4e0c2 --- /dev/null +++ b/samples/websocket/stream-no-turn.php @@ -0,0 +1,218 @@ +'; +const HTTP_PORT = ''; +const WS_USERNAME = ''; +const WS_PASSWORD = ''; + +?> + + + + + + + Video Stream + + + + + +
+ +
+ + \ No newline at end of file