feat: add tcp network type (to generate tcp type ice candidates)

pull/1/head
Benyamin Azarkhazin 2023-09-19 21:39:09 +03:30
parent d7591684d7
commit 3fd41696a6
Signed by: benyamin
GPG Key ID: 3AE44F5623C70269
4 changed files with 51 additions and 12 deletions

3
app.go
View File

@ -23,7 +23,7 @@ type App struct {
src string 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 ..") println("initializing ..")
a.src = srcListenAddr a.src = srcListenAddr
var iceServers []webrtc.ICEServer var iceServers []webrtc.ICEServer
@ -41,6 +41,7 @@ func (a *App) Init(srcListenAddr string, svcAddr string, logjamBaseUrl string, t
TargetRoom: targetRoom, TargetRoom: targetRoom,
ServiceAddress: svcAddr, ServiceAddress: svcAddr,
ICEServers: iceServers, ICEServers: iceServers,
ICETCPMUXListenPort: iceTCPMUXListenPort,
} }
roomRepo := repositories.NewRoomRepository(a.conf) roomRepo := repositories.NewRoomRepository(a.conf)
a.router = &routers.Router{} a.router = &routers.Router{}

View File

@ -10,7 +10,7 @@ func main() {
src := flag.String("src", ":8080", "listenHost:listenPort") src := flag.String("src", ":8080", "listenHost:listenPort")
logjamBaseUrl := flag.String("logjam-base-url", "http://localhost:8090", "logjam base url( shouldn't end with / )") logjamBaseUrl := flag.String("logjam-base-url", "http://localhost:8090", "logjam base url( shouldn't end with / )")
targetRoom := flag.String("targetRoom", "test", "target room") 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() flag.Parse()
if strings.HasSuffix(*logjamBaseUrl, "/") { if strings.HasSuffix(*logjamBaseUrl, "/") {
@ -20,6 +20,6 @@ func main() {
panic("service address shouldn't end with /") panic("service address shouldn't end with /")
} }
app := App{} app := App{}
app.Init(*src, *svcAddr, *logjamBaseUrl, *targetRoom) app.Init(*src, *svcAddr, *logjamBaseUrl, *targetRoom, *icetcpmuxListenPort)
app.Run() app.Run()
} }

View File

@ -6,5 +6,6 @@ type ConfigModel struct {
ServiceAddress string `json:"serviceAddress"` ServiceAddress string `json:"serviceAddress"`
LogjamBaseUrl string `json:"logjamBaseUrl"` LogjamBaseUrl string `json:"logjamBaseUrl"`
TargetRoom string `json:"targetRoom"` TargetRoom string `json:"targetRoom"`
ICETCPMUXListenPort uint `json:"ice_tcpmux_listenPort"`
ICEServers []webrtc.ICEServer `json:"iceServers"` ICEServers []webrtc.ICEServer `json:"iceServers"`
} }

View File

@ -5,8 +5,10 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/pion/interceptor"
"github.com/pion/rtcp" "github.com/pion/rtcp"
"github.com/pion/webrtc/v3" "github.com/pion/webrtc/v3"
"net"
"net/http" "net/http"
"sourcecode.social/greatape/goldgorilla/models" "sourcecode.social/greatape/goldgorilla/models"
"sourcecode.social/greatape/goldgorilla/models/dto" "sourcecode.social/greatape/goldgorilla/models/dto"
@ -39,13 +41,47 @@ type Room struct {
} }
type RoomRepository struct { type RoomRepository struct {
api *webrtc.API
Rooms map[string]*Room Rooms map[string]*Room
conf *models.ConfigModel conf *models.ConfigModel
*sync.Mutex *sync.Mutex
} }
func NewRoomRepository(conf *models.ConfigModel) *RoomRepository { 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{ return &RoomRepository{
api: api,
Mutex: &sync.Mutex{}, Mutex: &sync.Mutex{},
Rooms: make(map[string]*Room), Rooms: make(map[string]*Room),
conf: conf, conf: conf,
@ -118,7 +154,7 @@ func (r *RoomRepository) CreatePeer(roomId string, id uint64, canPublish bool, i
room := r.Rooms[roomId] room := r.Rooms[roomId]
r.Unlock() r.Unlock()
peerConn, err := webrtc.NewPeerConnection(webrtc.Configuration{ peerConn, err := r.api.NewPeerConnection(webrtc.Configuration{
ICEServers: r.conf.ICEServers, ICEServers: r.conf.ICEServers,
}) })
if err != nil { 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) { 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()) fmt.Println("got a track!", remote.ID(), remote.StreamID(), remote.Kind().String())
println("pc", id, "streamid", remote.StreamID())
r.Lock() r.Lock()
if !r.doesRoomExists(roomId) { if !r.doesRoomExists(roomId) {
r.Unlock() r.Unlock()