From 89b747ae4121402e062c79da216626284308ae38 Mon Sep 17 00:00:00 2001 From: "jan.rosecky" Date: Thu, 24 Apr 2025 11:55:59 +0200 Subject: [PATCH 1/4] hacked ice server cfg --- engine.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/engine.go b/engine.go index bc15e76d..1edd8726 100644 --- a/engine.go +++ b/engine.go @@ -16,7 +16,11 @@ package lksdk import ( "context" + "crypto/hmac" + "crypto/sha1" + "encoding/base64" "errors" + "fmt" "sync" "time" @@ -260,11 +264,60 @@ func (e *RTCEngine) setRTT(rtt uint32) { } } +type Credential struct { + Username string + Password string +} + +// newCredential creates a new Credential object +func NewCredential(secretkey string) *Credential { + if len(secretkey) == 0 { + // Default for test case + secretkey = "north" + } + usercombo := generateUser() + + return &Credential{ + Username: usercombo, + Password: generatePassword(secretkey, usercombo), + } +} + +// usercombo -> "timestamp:userid" +// turn user -> usercombo +// generaUser according to what is expected by coturn +// the user is available only for 60 second +func generateUser() string { + now := time.Now() + timestamp := now.Unix() + timestamp += 60 + return fmt.Sprintf("%d:%s", timestamp, "unblu") +} + +// generate a password +func generatePassword(secretkey string, usercombo string) string { + mac := hmac.New(sha1.New, []byte(secretkey)) + mac.Write([]byte(usercombo)) + return base64.StdEncoding.EncodeToString(mac.Sum(nil)) +} + func (e *RTCEngine) configure( iceServers []*livekit.ICEServer, clientConfig *livekit.ClientConfiguration, subscriberPrimary *bool) error { + var credential = NewCredential("ggg") + iceServers = []*livekit.ICEServer{ + { + Urls: []string{ + "stun:abc:3478", + }, + Username: credential.Username, + Credential: credential.Password, + }, + } + logger.Infow("using ICE servers", "servers", iceServers) + configuration := e.makeRTCConfiguration(iceServers, clientConfig) e.pclock.Lock() defer e.pclock.Unlock() From 5f5920322cd4378cdfb2ae688c86b1319b1eb9ae Mon Sep 17 00:00:00 2001 From: "jan.rosecky" Date: Fri, 25 Apr 2025 10:48:59 +0200 Subject: [PATCH 2/4] Allowing passing of custom ICE servers and force relay flag to WebRTC engine config --- engine.go | 66 +++++++++++++------------------------------------------ room.go | 14 ++++++++++++ 2 files changed, 29 insertions(+), 51 deletions(-) diff --git a/engine.go b/engine.go index 1edd8726..3a7b914c 100644 --- a/engine.go +++ b/engine.go @@ -16,11 +16,7 @@ package lksdk import ( "context" - "crypto/hmac" - "crypto/sha1" - "encoding/base64" "errors" - "fmt" "sync" "time" @@ -170,7 +166,18 @@ func (e *RTCEngine) JoinContext(ctx context.Context, url string, token string, p e.token.Store(token) e.connParams = params - err = e.configure(res.IceServers, res.ClientConfiguration, proto.Bool(res.SubscriberPrimary)) + usedIceServers := params.ICEServers + if usedIceServers == nil { + usedIceServers = res.IceServers + } + clientConfig := res.ClientConfiguration + if clientConfig == nil { + clientConfig = &livekit.ClientConfiguration{} + } + if params.ForceRelay { + clientConfig.ForceRelay = livekit.ClientConfigSetting_ENABLED + } + err = e.configure(usedIceServers, clientConfig, proto.Bool(res.SubscriberPrimary)) if err != nil { return nil, err } @@ -264,58 +271,15 @@ func (e *RTCEngine) setRTT(rtt uint32) { } } -type Credential struct { - Username string - Password string -} - -// newCredential creates a new Credential object -func NewCredential(secretkey string) *Credential { - if len(secretkey) == 0 { - // Default for test case - secretkey = "north" - } - usercombo := generateUser() - - return &Credential{ - Username: usercombo, - Password: generatePassword(secretkey, usercombo), - } -} - -// usercombo -> "timestamp:userid" -// turn user -> usercombo -// generaUser according to what is expected by coturn -// the user is available only for 60 second -func generateUser() string { - now := time.Now() - timestamp := now.Unix() - timestamp += 60 - return fmt.Sprintf("%d:%s", timestamp, "unblu") -} - -// generate a password -func generatePassword(secretkey string, usercombo string) string { - mac := hmac.New(sha1.New, []byte(secretkey)) - mac.Write([]byte(usercombo)) - return base64.StdEncoding.EncodeToString(mac.Sum(nil)) -} - func (e *RTCEngine) configure( iceServers []*livekit.ICEServer, clientConfig *livekit.ClientConfiguration, subscriberPrimary *bool) error { - var credential = NewCredential("ggg") - iceServers = []*livekit.ICEServer{ - { - Urls: []string{ - "stun:abc:3478", - }, - Username: credential.Username, - Credential: credential.Password, - }, + if clientConfig == nil { + clientConfig = &livekit.ClientConfiguration{} } + clientConfig.ForceRelay = livekit.ClientConfigSetting_ENABLED logger.Infow("using ICE servers", "servers", iceServers) configuration := e.makeRTCConfiguration(iceServers, clientConfig) diff --git a/room.go b/room.go index 6d08e5d1..8e3c7aba 100644 --- a/room.go +++ b/room.go @@ -103,6 +103,8 @@ type SignalClientConnectParams struct { Interceptors []interceptor.Factory ICETransportPolicy webrtc.ICETransportPolicy + ICEServers []*livekit.ICEServer + ForceRelay bool } type ConnectOption func(*SignalClientConnectParams) @@ -147,6 +149,18 @@ func WithDisableRegionDiscovery() ConnectOption { } } +func WithICEServers(iceServers []*livekit.ICEServer) ConnectOption { + return func(p *SignalClientConnectParams) { + p.ICEServers = iceServers + } +} + +func WithForceRelay(forceRelay bool) ConnectOption { + return func(p *SignalClientConnectParams) { + p.ForceRelay = forceRelay + } +} + type PLIWriter func(webrtc.SSRC) type Room struct { From ac719b08dfb1bc482f6fffd922fac674919676ba Mon Sep 17 00:00:00 2001 From: "jan.rosecky" Date: Fri, 25 Apr 2025 11:02:34 +0200 Subject: [PATCH 3/4] Removed logging and fixed ice server len check --- engine.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/engine.go b/engine.go index 3a7b914c..a2d8564b 100644 --- a/engine.go +++ b/engine.go @@ -167,7 +167,7 @@ func (e *RTCEngine) JoinContext(ctx context.Context, url string, token string, p e.connParams = params usedIceServers := params.ICEServers - if usedIceServers == nil { + if len(usedIceServers) == 0 { usedIceServers = res.IceServers } clientConfig := res.ClientConfiguration @@ -280,7 +280,6 @@ func (e *RTCEngine) configure( clientConfig = &livekit.ClientConfiguration{} } clientConfig.ForceRelay = livekit.ClientConfigSetting_ENABLED - logger.Infow("using ICE servers", "servers", iceServers) configuration := e.makeRTCConfiguration(iceServers, clientConfig) e.pclock.Lock() From 981f77cb24986efde62d5edec9eb48da57e4f2ea Mon Sep 17 00:00:00 2001 From: "jan.rosecky" Date: Fri, 25 Apr 2025 11:11:16 +0200 Subject: [PATCH 4/4] Fixed force relay --- engine.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/engine.go b/engine.go index a2d8564b..b44b906e 100644 --- a/engine.go +++ b/engine.go @@ -171,10 +171,10 @@ func (e *RTCEngine) JoinContext(ctx context.Context, url string, token string, p usedIceServers = res.IceServers } clientConfig := res.ClientConfiguration - if clientConfig == nil { - clientConfig = &livekit.ClientConfiguration{} - } if params.ForceRelay { + if clientConfig == nil { + clientConfig = &livekit.ClientConfiguration{} + } clientConfig.ForceRelay = livekit.ClientConfigSetting_ENABLED } err = e.configure(usedIceServers, clientConfig, proto.Bool(res.SubscriberPrimary)) @@ -276,11 +276,6 @@ func (e *RTCEngine) configure( clientConfig *livekit.ClientConfiguration, subscriberPrimary *bool) error { - if clientConfig == nil { - clientConfig = &livekit.ClientConfiguration{} - } - clientConfig.ForceRelay = livekit.ClientConfigSetting_ENABLED - configuration := e.makeRTCConfiguration(iceServers, clientConfig) e.pclock.Lock() defer e.pclock.Unlock()