Support join request as proto + base64 encoded query param (#3836)

* Support join request as proto + base64 encoded query param

* joinPublish

* staticcheck

* deps

* tests

* gzip

* test

* deps

* clean up
This commit is contained in:
Raja Subramanian
2025-08-07 11:13:27 +05:30
committed by GitHub
parent 7dea101286
commit 5ca1626439
12 changed files with 198 additions and 86 deletions
+2 -1
View File
@@ -22,6 +22,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/livekit/livekit-server/pkg/testutils"
testclient "github.com/livekit/livekit-server/test/client"
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/livekit"
)
@@ -69,7 +70,7 @@ func TestAgents(t *testing.T) {
}, RegisterTimeout)
c1 := createRTCClient("c1", defaultServerPort, nil)
c2 := createRTCClient("c2", defaultServerPort, nil)
c2 := createRTCClient("c2", defaultServerPort, &testclient.Options{UseJoinRequestQueryParam: true})
waitUntilConnected(t, c1, c2)
// publish 2 tracks
+57 -20
View File
@@ -15,6 +15,8 @@
package client
import (
"bytes"
"compress/gzip"
"context"
"encoding/base64"
"encoding/json"
@@ -24,6 +26,7 @@ import (
"net/http"
"net/url"
"path/filepath"
"runtime"
"sync"
"time"
@@ -122,10 +125,11 @@ type Options struct {
TokenCustomizer func(token *auth.AccessToken, grants *auth.VideoGrant)
SignalRequestInterceptor SignalRequestInterceptor
SignalResponseInterceptor SignalResponseInterceptor
UseJoinRequestQueryParam bool
}
func NewWebSocketConn(host, token string, opts *Options) (*websocket.Conn, error) {
u, err := url.Parse(host + fmt.Sprintf("/rtc?protocol=%d", types.CurrentProtocol))
u, err := url.Parse(host + "/rtc")
if err != nil {
return nil, err
}
@@ -133,32 +137,65 @@ func NewWebSocketConn(host, token string, opts *Options) (*websocket.Conn, error
SetAuthorizationToken(requestHeader, token)
connectUrl := u.String()
sdk := "go"
if opts != nil {
connectUrl = fmt.Sprintf("%s&auto_subscribe=%t", connectUrl, opts.AutoSubscribe)
if opts.Publish != "" {
connectUrl += encodeQueryParam("publish", opts.Publish)
}
if len(opts.Attributes) != 0 {
data, err := json.Marshal(opts.Attributes)
if err != nil {
return nil, err
}
connectUrl += encodeQueryParam("attributes", base64.URLEncoding.EncodeToString(data))
if opts != nil && opts.UseJoinRequestQueryParam {
clientInfo := &livekit.ClientInfo{
Os: runtime.GOOS,
Sdk: livekit.ClientInfo_GO,
Protocol: types.CurrentProtocol,
}
if opts.ClientInfo != nil {
if opts.ClientInfo.DeviceModel != "" {
connectUrl += encodeQueryParam("device_model", opts.ClientInfo.DeviceModel)
clientInfo = opts.ClientInfo
}
connectionSettings := &livekit.ConnectionSettings{
AutoSubscribe: opts.AutoSubscribe,
}
joinRequest := &livekit.JoinRequest{
ClientInfo: clientInfo,
ConnectionSettings: connectionSettings,
ParticipantAttributes: opts.Attributes,
}
if marshalled, err := proto.Marshal(joinRequest); err == nil {
var buf bytes.Buffer
writer := gzip.NewWriter(&buf)
writer.Write(marshalled)
writer.Close()
connectUrl += fmt.Sprintf("?join_request=%s", base64.URLEncoding.EncodeToString(buf.Bytes()))
}
} else {
connectUrl += fmt.Sprintf("?protocol=%d", types.CurrentProtocol)
sdk := "go"
if opts != nil {
connectUrl += fmt.Sprintf("&auto_subscribe=%t", opts.AutoSubscribe)
if opts.Publish != "" {
connectUrl += encodeQueryParam("publish", opts.Publish)
}
if opts.ClientInfo.Os != "" {
connectUrl += encodeQueryParam("os", opts.ClientInfo.Os)
if len(opts.Attributes) != 0 {
data, err := json.Marshal(opts.Attributes)
if err != nil {
return nil, err
}
connectUrl += encodeQueryParam("attributes", base64.URLEncoding.EncodeToString(data))
}
if opts.ClientInfo.Sdk != livekit.ClientInfo_UNKNOWN {
sdk = opts.ClientInfo.Sdk.String()
if opts.ClientInfo != nil {
if opts.ClientInfo.DeviceModel != "" {
connectUrl += encodeQueryParam("device_model", opts.ClientInfo.DeviceModel)
}
if opts.ClientInfo.Os != "" {
connectUrl += encodeQueryParam("os", opts.ClientInfo.Os)
}
if opts.ClientInfo.Sdk != livekit.ClientInfo_UNKNOWN {
sdk = opts.ClientInfo.Sdk.String()
}
}
}
connectUrl += encodeQueryParam("sdk", sdk)
}
connectUrl += encodeQueryParam("sdk", sdk)
conn, _, err := websocket.DefaultDialer.Dial(connectUrl, requestHeader)
return conn, err
}
+4 -1
View File
@@ -564,6 +564,7 @@ func TestSingleNodeAttributes(t *testing.T) {
"b": "1",
})
},
UseJoinRequestQueryParam: true,
})
grant := &auth.VideoGrant{RoomJoin: true, Room: testRoom}
grant.SetCanSubscribe(false)
@@ -663,7 +664,9 @@ func TestSubscribeToCodecUnsupported(t *testing.T) {
_, finish := setupSingleNodeTest("TestSubscribeToCodecUnsupported")
defer finish()
c1 := createRTCClient("c1", defaultServerPort, nil)
c1 := createRTCClient("c1", defaultServerPort, &testclient.Options{
UseJoinRequestQueryParam: true,
})
// create a client that doesn't support H264
c2 := createRTCClient("c2", defaultServerPort, &testclient.Options{
AutoSubscribe: true,