Allow participants to create new connection for publishing new tracks (#224)
* Allow participants to create new connection for publishing new tracks new param 'publish=<name>' enables publishing new connection with publish only grants. Also, identity is appended with '#<name>' * Add integration test to duplicate publish only connection Signed-off-by: shishir gowda <shishir@livekit.io> * Reuse token in test and set subscribeGrant to false on publish param Signed-off-by: shishir gowda <shishir@livekit.io> * create auth token with grants Signed-off-by: shishir gowda <shishir@livekit.io> * Signal ice on negotiate in test client Signed-off-by: shishir gowda <shishir@livekit.io> * Update pkg/service/rtcservice.go Co-authored-by: David Zhao <david@davidzhao.com> Co-authored-by: David Zhao <david@davidzhao.com>
This commit is contained in:
@@ -72,11 +72,23 @@ func (s *RTCService) validate(r *http.Request) (string, routing.ParticipantInit,
|
|||||||
roomName := r.FormValue("room")
|
roomName := r.FormValue("room")
|
||||||
reconnectParam := r.FormValue("reconnect")
|
reconnectParam := r.FormValue("reconnect")
|
||||||
autoSubParam := r.FormValue("auto_subscribe")
|
autoSubParam := r.FormValue("auto_subscribe")
|
||||||
|
publishParam := r.FormValue("publish")
|
||||||
|
|
||||||
if onlyName != "" {
|
if onlyName != "" {
|
||||||
roomName = onlyName
|
roomName = onlyName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this is new connection for existing participant - with publish only permissions
|
||||||
|
if publishParam != "" {
|
||||||
|
// Make sure grant has CanPublish set,
|
||||||
|
if claims.Video.CanPublish != nil && *claims.Video.CanPublish == false {
|
||||||
|
return "", routing.ParticipantInit{}, http.StatusUnauthorized, rtc.ErrPermissionDenied
|
||||||
|
}
|
||||||
|
// Make sure by default subscribe is off
|
||||||
|
claims.Video.SetCanSubscribe(false)
|
||||||
|
claims.Identity += "#" + publishParam
|
||||||
|
}
|
||||||
|
|
||||||
if router, ok := s.router.(routing.Router); ok {
|
if router, ok := s.router.(routing.Router); ok {
|
||||||
if foundNode, err := router.GetNodeForRoom(r.Context(), roomName); err == nil {
|
if foundNode, err := router.GetNodeForRoom(r.Context(), roomName); err == nil {
|
||||||
if selector.LimitsReached(s.limits, foundNode.Stats) {
|
if selector.LimitsReached(s.limits, foundNode.Stats) {
|
||||||
|
|||||||
+19
-6
@@ -48,11 +48,12 @@ type RTCClient struct {
|
|||||||
localParticipant *livekit.ParticipantInfo
|
localParticipant *livekit.ParticipantInfo
|
||||||
remoteParticipants map[string]*livekit.ParticipantInfo
|
remoteParticipants map[string]*livekit.ParticipantInfo
|
||||||
|
|
||||||
reliableDC *webrtc.DataChannel
|
reliableDC *webrtc.DataChannel
|
||||||
reliableDCSub *webrtc.DataChannel
|
reliableDCSub *webrtc.DataChannel
|
||||||
lossyDC *webrtc.DataChannel
|
lossyDC *webrtc.DataChannel
|
||||||
lossyDCSub *webrtc.DataChannel
|
lossyDCSub *webrtc.DataChannel
|
||||||
publisherConnected utils.AtomicFlag
|
publisherConnected utils.AtomicFlag
|
||||||
|
publisherNegotiated utils.AtomicFlag
|
||||||
|
|
||||||
// tracks waiting to be acked, cid => trackInfo
|
// tracks waiting to be acked, cid => trackInfo
|
||||||
pendingPublishedTracks map[string]*livekit.TrackInfo
|
pendingPublishedTracks map[string]*livekit.TrackInfo
|
||||||
@@ -84,6 +85,7 @@ var (
|
|||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
AutoSubscribe bool
|
AutoSubscribe bool
|
||||||
|
Publish string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWebSocketConn(host, token string, opts *Options) (*websocket.Conn, error) {
|
func NewWebSocketConn(host, token string, opts *Options) (*websocket.Conn, error) {
|
||||||
@@ -96,7 +98,8 @@ func NewWebSocketConn(host, token string, opts *Options) (*websocket.Conn, error
|
|||||||
|
|
||||||
connectUrl := u.String()
|
connectUrl := u.String()
|
||||||
if opts != nil {
|
if opts != nil {
|
||||||
connectUrl = fmt.Sprintf("%s&auto_subscribe=%t", connectUrl, opts.AutoSubscribe)
|
connectUrl = fmt.Sprintf("%s&auto_subscribe=%t&publish=%s",
|
||||||
|
connectUrl, opts.AutoSubscribe, opts.Publish)
|
||||||
}
|
}
|
||||||
conn, _, err := websocket.DefaultDialer.Dial(connectUrl, requestHeader)
|
conn, _, err := websocket.DefaultDialer.Dial(connectUrl, requestHeader)
|
||||||
return conn, err
|
return conn, err
|
||||||
@@ -224,8 +227,13 @@ func NewRTCClient(conn *websocket.Conn) (*RTCClient, error) {
|
|||||||
c.publisher.PeerConnection().OnICEConnectionStateChange(func(state webrtc.ICEConnectionState) {
|
c.publisher.PeerConnection().OnICEConnectionStateChange(func(state webrtc.ICEConnectionState) {
|
||||||
logger.Infow("publisher ICE state changed", "state", state.String(),
|
logger.Infow("publisher ICE state changed", "state", state.String(),
|
||||||
"participant", c.localParticipant.Identity)
|
"participant", c.localParticipant.Identity)
|
||||||
|
|
||||||
if state == webrtc.ICEConnectionStateConnected {
|
if state == webrtc.ICEConnectionStateConnected {
|
||||||
c.publisherConnected.TrySet(true)
|
c.publisherConnected.TrySet(true)
|
||||||
|
// check if publisher triggered negotiate (!subscriberPrimary)
|
||||||
|
if c.publisherNegotiated.Get() {
|
||||||
|
c.iceConnected.TrySet(true)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
c.publisherConnected.TrySet(false)
|
c.publisherConnected.TrySet(false)
|
||||||
}
|
}
|
||||||
@@ -265,6 +273,11 @@ func (c *RTCClient) Run() error {
|
|||||||
c.remoteParticipants[p.Sid] = p
|
c.remoteParticipants[p.Sid] = p
|
||||||
}
|
}
|
||||||
c.lock.Unlock()
|
c.lock.Unlock()
|
||||||
|
// if publish only, negotiate
|
||||||
|
if !msg.Join.SubscriberPrimary {
|
||||||
|
c.publisherNegotiated.TrySet(true)
|
||||||
|
c.publisher.Negotiate()
|
||||||
|
}
|
||||||
|
|
||||||
logger.Infow("join accepted, awaiting offer", "participant", msg.Join.Participant.Identity)
|
logger.Infow("join accepted, awaiting offer", "participant", msg.Join.Participant.Identity)
|
||||||
case *livekit.SignalResponse_Answer:
|
case *livekit.SignalResponse_Answer:
|
||||||
|
|||||||
@@ -203,6 +203,22 @@ func createRTCClient(name string, port int, opts *testclient.Options) *testclien
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// creates a client and runs against server
|
||||||
|
func createRTCClientWithToken(token string, port int, opts *testclient.Options) *testclient.RTCClient {
|
||||||
|
ws, err := testclient.NewWebSocketConn(fmt.Sprintf("ws://localhost:%d", port), token, opts)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := testclient.NewRTCClient(ws)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
go c.Run()
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
func redisClient() *redis.Client {
|
func redisClient() *redis.Client {
|
||||||
return redis.NewClient(&redis.Options{
|
return redis.NewClient(&redis.Options{
|
||||||
Addr: "localhost:6379",
|
Addr: "localhost:6379",
|
||||||
@@ -220,6 +236,17 @@ func joinToken(room, name string) string {
|
|||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func joinTokenWithGrant(name string, grant *auth.VideoGrant) string {
|
||||||
|
at := auth.NewAccessToken(testApiKey, testApiSecret).
|
||||||
|
AddGrant(grant).
|
||||||
|
SetIdentity(name)
|
||||||
|
t, err := at.ToJWT()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
func createRoomToken() string {
|
func createRoomToken() string {
|
||||||
at := auth.NewAccessToken(testApiKey, testApiSecret).
|
at := auth.NewAccessToken(testApiKey, testApiSecret).
|
||||||
AddGrant(&auth.VideoGrant{RoomCreate: true}).
|
AddGrant(&auth.VideoGrant{RoomCreate: true}).
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/livekit/protocol/auth"
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/pkg/rtc"
|
"github.com/livekit/livekit-server/pkg/rtc"
|
||||||
"github.com/pion/webrtc/v3"
|
"github.com/pion/webrtc/v3"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@@ -33,6 +35,82 @@ func TestClientCouldConnect(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestClientConnectDuplicate(t *testing.T) {
|
||||||
|
if testing.Short() {
|
||||||
|
t.SkipNow()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, finish := setupSingleNodeTest("TestClientCouldConnect", testRoom)
|
||||||
|
defer finish()
|
||||||
|
|
||||||
|
grant := &auth.VideoGrant{RoomJoin: true, Room: testRoom}
|
||||||
|
grant.SetCanPublish(true)
|
||||||
|
grant.SetCanSubscribe(true)
|
||||||
|
token := joinTokenWithGrant("c1", grant)
|
||||||
|
|
||||||
|
c1 := createRTCClientWithToken(token, defaultServerPort, nil)
|
||||||
|
|
||||||
|
// publish 2 tracks
|
||||||
|
t1, err := c1.AddStaticTrack("audio/opus", "audio", "webcam")
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer t1.Stop()
|
||||||
|
t2, err := c1.AddStaticTrack("video/vp8", "video", "webcam")
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer t2.Stop()
|
||||||
|
|
||||||
|
c2 := createRTCClient("c2", defaultServerPort, nil)
|
||||||
|
waitUntilConnected(t, c1, c2)
|
||||||
|
|
||||||
|
opts := &testclient.Options{
|
||||||
|
Publish: "duplicate_connection",
|
||||||
|
}
|
||||||
|
success := testutils.WithTimeout(t, "c2 should receive two tracks", func() bool {
|
||||||
|
if len(c2.SubscribedTracks()) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// should have received three tracks
|
||||||
|
if len(c2.SubscribedTracks()[c1.ID()]) != 2 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
//participant ID can be appended with '#..' . but should contain orig id as prefix
|
||||||
|
tr1 := c2.SubscribedTracks()[c1.ID()][0]
|
||||||
|
participantId1, _ := rtc.UnpackStreamID(tr1.StreamID())
|
||||||
|
require.Equal(t, c1.ID(), participantId1)
|
||||||
|
tr2 := c2.SubscribedTracks()[c1.ID()][1]
|
||||||
|
participantId2, _ := rtc.UnpackStreamID(tr2.StreamID())
|
||||||
|
require.Equal(t, c1.ID(), participantId2)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
if !success {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
c1Dup := createRTCClientWithToken(token, defaultServerPort, opts)
|
||||||
|
|
||||||
|
waitUntilConnected(t, c1Dup)
|
||||||
|
|
||||||
|
t3, err := c1Dup.AddStaticTrack("video/vp8", "video", "webcam")
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer t3.Stop()
|
||||||
|
|
||||||
|
success = testutils.WithTimeout(t, "c2 should receive third tracks", func() bool {
|
||||||
|
if len(c2.SubscribedTracks()[c1Dup.ID()]) != 1 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
tr3 := c2.SubscribedTracks()[c1Dup.ID()][0]
|
||||||
|
participantId3, _ := rtc.UnpackStreamID(tr3.StreamID())
|
||||||
|
require.Contains(t, c1Dup.ID(), participantId3)
|
||||||
|
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
if !success {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSinglePublisher(t *testing.T) {
|
func TestSinglePublisher(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.SkipNow()
|
t.SkipNow()
|
||||||
|
|||||||
Reference in New Issue
Block a user