From 3fd41696a6e4ef9a514f9e7be8d222fc5c7a9fbf Mon Sep 17 00:00:00 2001 From: benyamin Date: Tue, 19 Sep 2023 21:39:09 +0330 Subject: [PATCH] feat: add tcp network type (to generate tcp type ice candidates) --- app.go | 11 ++++++----- main.go | 4 ++-- models/config.go | 9 +++++---- repositories/room.go | 39 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 51 insertions(+), 12 deletions(-) diff --git a/app.go b/app.go index 664afde..af4e73a 100644 --- a/app.go +++ b/app.go @@ -23,7 +23,7 @@ type App struct { src string } -func (a *App) Init(srcListenAddr string, svcAddr string, logjamBaseUrl string, targetRoom string) { +func (a *App) Init(srcListenAddr string, svcAddr string, logjamBaseUrl string, targetRoom string, iceTCPMUXListenPort uint) { println("initializing ..") a.src = srcListenAddr var iceServers []webrtc.ICEServer @@ -37,10 +37,11 @@ func (a *App) Init(srcListenAddr string, svcAddr string, logjamBaseUrl string, t } } a.conf = &models.ConfigModel{ - LogjamBaseUrl: logjamBaseUrl + "/auxiliary-node", - TargetRoom: targetRoom, - ServiceAddress: svcAddr, - ICEServers: iceServers, + LogjamBaseUrl: logjamBaseUrl + "/auxiliary-node", + TargetRoom: targetRoom, + ServiceAddress: svcAddr, + ICEServers: iceServers, + ICETCPMUXListenPort: iceTCPMUXListenPort, } roomRepo := repositories.NewRoomRepository(a.conf) a.router = &routers.Router{} diff --git a/main.go b/main.go index 9172338..f96db8b 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,7 @@ func main() { src := flag.String("src", ":8080", "listenHost:listenPort") logjamBaseUrl := flag.String("logjam-base-url", "http://localhost:8090", "logjam base url( shouldn't end with / )") targetRoom := flag.String("targetRoom", "test", "target room") - + icetcpmuxListenPort := flag.Uint("ice-tcp-mux-listen-port", 4444, "listen port to use for tcp ice candidates") flag.Parse() if strings.HasSuffix(*logjamBaseUrl, "/") { @@ -20,6 +20,6 @@ func main() { panic("service address shouldn't end with /") } app := App{} - app.Init(*src, *svcAddr, *logjamBaseUrl, *targetRoom) + app.Init(*src, *svcAddr, *logjamBaseUrl, *targetRoom, *icetcpmuxListenPort) app.Run() } diff --git a/models/config.go b/models/config.go index fde1fde..ac16148 100644 --- a/models/config.go +++ b/models/config.go @@ -3,8 +3,9 @@ package models import "github.com/pion/webrtc/v3" type ConfigModel struct { - ServiceAddress string `json:"serviceAddress"` - LogjamBaseUrl string `json:"logjamBaseUrl"` - TargetRoom string `json:"targetRoom"` - ICEServers []webrtc.ICEServer `json:"iceServers"` + ServiceAddress string `json:"serviceAddress"` + LogjamBaseUrl string `json:"logjamBaseUrl"` + TargetRoom string `json:"targetRoom"` + ICETCPMUXListenPort uint `json:"ice_tcpmux_listenPort"` + ICEServers []webrtc.ICEServer `json:"iceServers"` } diff --git a/repositories/room.go b/repositories/room.go index 32ed6b6..425e7d5 100644 --- a/repositories/room.go +++ b/repositories/room.go @@ -5,8 +5,10 @@ import ( "encoding/json" "errors" "fmt" + "github.com/pion/interceptor" "github.com/pion/rtcp" "github.com/pion/webrtc/v3" + "net" "net/http" "sourcecode.social/greatape/goldgorilla/models" "sourcecode.social/greatape/goldgorilla/models/dto" @@ -39,13 +41,47 @@ type Room struct { } type RoomRepository struct { + api *webrtc.API Rooms map[string]*Room conf *models.ConfigModel *sync.Mutex } func NewRoomRepository(conf *models.ConfigModel) *RoomRepository { + settingEngine := webrtc.SettingEngine{} + + settingEngine.SetNetworkTypes([]webrtc.NetworkType{ + webrtc.NetworkTypeTCP6, + webrtc.NetworkTypeUDP6, + webrtc.NetworkTypeTCP4, + webrtc.NetworkTypeUDP4, + }) + tcpListener, err := net.ListenTCP("tcp", &net.TCPAddr{ + IP: net.IP{0, 0, 0, 0}, + Port: int(conf.ICETCPMUXListenPort), + }) + if err != nil { + panic(err) + } + fmt.Printf("Listening for ICE TCP at %s\n", tcpListener.Addr()) + + tcpMux := webrtc.NewICETCPMux(nil, tcpListener, 64) + settingEngine.SetICETCPMux(tcpMux) + + m := &webrtc.MediaEngine{} + if err := m.RegisterDefaultCodecs(); err != nil { + panic(err) + } + + i := &interceptor.Registry{} + if err := webrtc.RegisterDefaultInterceptors(m, i); err != nil { + panic(err) + } + + api := webrtc.NewAPI(webrtc.WithMediaEngine(m), webrtc.WithInterceptorRegistry(i), webrtc.WithSettingEngine(settingEngine)) + return &RoomRepository{ + api: api, Mutex: &sync.Mutex{}, Rooms: make(map[string]*Room), conf: conf, @@ -118,7 +154,7 @@ func (r *RoomRepository) CreatePeer(roomId string, id uint64, canPublish bool, i room := r.Rooms[roomId] r.Unlock() - peerConn, err := webrtc.NewPeerConnection(webrtc.Configuration{ + peerConn, err := r.api.NewPeerConnection(webrtc.Configuration{ ICEServers: r.conf.ICEServers, }) if err != nil { @@ -241,6 +277,7 @@ func (r *RoomRepository) onPeerConnectionStateChange(room *Room, peer *Peer, new func (r *RoomRepository) onPeerTrack(roomId string, id uint64, remote *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) { fmt.Println("got a track!", remote.ID(), remote.StreamID(), remote.Kind().String()) + println("pc", id, "streamid", remote.StreamID()) r.Lock() if !r.doesRoomExists(roomId) { r.Unlock()