service: start room composite egress configured in a room preset (#4926)
* service: start room composite egress configured in a room preset StandardRoomAllocator.CreateRoom merged a room preset into a copy of the request and discarded it. RoomInternal has no field for room composite egress, and getOrCreateRoom started it from the caller's original request, so a room composite egress from a preset was never started. RoomAllocator.CreateRoom now also returns the request with the preset applied, and getOrCreateRoom starts the egress from it. * service: return ErrEgressNotConnected instead of calling a nil egress launcher getOrCreateRoom started a room composite egress without checking that an egress launcher exists. RoomService.CreateRoom rejects egress on the request when there is none, but not egress from a room preset or a token's roomConfig.
This commit is contained in:
@@ -81,7 +81,9 @@ type IngressStore interface {
|
||||
type RoomAllocator interface {
|
||||
AutoCreateEnabled(ctx context.Context) bool
|
||||
SelectRoomNode(ctx context.Context, roomName livekit.RoomName, nodeID livekit.NodeID) error
|
||||
CreateRoom(ctx context.Context, req *livekit.CreateRoomRequest, isExplicit bool) (*livekit.Room, *livekit.RoomInternal, bool, error)
|
||||
// CreateRoom also returns the request with its room preset applied,
|
||||
// or req unchanged when the implementation does not resolve presets
|
||||
CreateRoom(ctx context.Context, req *livekit.CreateRoomRequest, isExplicit bool) (*livekit.Room, *livekit.RoomInternal, *livekit.CreateRoomRequest, bool, error)
|
||||
ValidateCreateRoom(ctx context.Context, roomName livekit.RoomName) error
|
||||
}
|
||||
|
||||
|
||||
@@ -59,10 +59,10 @@ func (r *StandardRoomAllocator) AutoCreateEnabled(context.Context) bool {
|
||||
|
||||
// CreateRoom creates a new room from a request and allocates it to a node to handle
|
||||
// it'll also monitor its state, and cleans it up when appropriate
|
||||
func (r *StandardRoomAllocator) CreateRoom(ctx context.Context, req *livekit.CreateRoomRequest, isExplicit bool) (*livekit.Room, *livekit.RoomInternal, bool, error) {
|
||||
func (r *StandardRoomAllocator) CreateRoom(ctx context.Context, req *livekit.CreateRoomRequest, isExplicit bool) (*livekit.Room, *livekit.RoomInternal, *livekit.CreateRoomRequest, bool, error) {
|
||||
token, err := r.roomStore.LockRoom(ctx, livekit.RoomName(req.Name), 5*time.Second)
|
||||
if err != nil {
|
||||
return nil, nil, false, err
|
||||
return nil, nil, nil, false, err
|
||||
}
|
||||
defer func() {
|
||||
_ = r.roomStore.UnlockRoom(ctx, livekit.RoomName(req.Name), token)
|
||||
@@ -84,12 +84,12 @@ func (r *StandardRoomAllocator) CreateRoom(ctx context.Context, req *livekit.Cre
|
||||
internal = &livekit.RoomInternal{}
|
||||
applyDefaultRoomConfig(rm, internal, &r.config.Room)
|
||||
} else if err != nil {
|
||||
return nil, nil, false, err
|
||||
return nil, nil, nil, false, err
|
||||
}
|
||||
|
||||
req, err = r.applyNamedRoomConfiguration(req)
|
||||
if err != nil {
|
||||
return nil, nil, false, err
|
||||
return nil, nil, nil, false, err
|
||||
}
|
||||
|
||||
if req.EmptyTimeout > 0 {
|
||||
@@ -127,10 +127,10 @@ func (r *StandardRoomAllocator) CreateRoom(ctx context.Context, req *livekit.Cre
|
||||
}
|
||||
|
||||
if err = r.roomStore.StoreRoom(ctx, rm, internal); err != nil {
|
||||
return nil, nil, false, err
|
||||
return nil, nil, nil, false, err
|
||||
}
|
||||
|
||||
return rm, internal, created, nil
|
||||
return rm, internal, req, created, nil
|
||||
}
|
||||
|
||||
func (r *StandardRoomAllocator) SelectRoomNode(ctx context.Context, roomName livekit.RoomName, nodeID livekit.NodeID) error {
|
||||
|
||||
@@ -39,12 +39,44 @@ func TestCreateRoom(t *testing.T) {
|
||||
|
||||
ra, conf := newTestRoomAllocator(t, conf, node.Clone())
|
||||
|
||||
room, _, _, err := ra.CreateRoom(context.Background(), &livekit.CreateRoomRequest{Name: "myroom"}, true)
|
||||
room, _, _, _, err := ra.CreateRoom(context.Background(), &livekit.CreateRoomRequest{Name: "myroom"}, true)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, conf.Room.EmptyTimeout, room.EmptyTimeout)
|
||||
require.Equal(t, conf.Room.DepartureTimeout, room.DepartureTimeout)
|
||||
require.NotEmpty(t, room.EnabledCodecs)
|
||||
})
|
||||
|
||||
t.Run("return the request with the room preset applied", func(t *testing.T) {
|
||||
conf, err := config.NewConfig(`
|
||||
room:
|
||||
room_configurations:
|
||||
record:
|
||||
egress:
|
||||
room:
|
||||
layout: grid
|
||||
`, true, nil, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
node, err := routing.NewLocalNode(conf)
|
||||
require.NoError(t, err)
|
||||
|
||||
ra, conf := newTestRoomAllocator(t, conf, node.Clone())
|
||||
|
||||
// without a preset, the request itself is returned
|
||||
req := &livekit.CreateRoomRequest{Name: "myroom"}
|
||||
_, _, applied, _, err := ra.CreateRoom(context.Background(), req, true)
|
||||
require.NoError(t, err)
|
||||
require.Same(t, req, applied)
|
||||
|
||||
// with a preset, a copy with the preset applied is returned,
|
||||
// leaving both the request and the preset unchanged
|
||||
req = &livekit.CreateRoomRequest{Name: "myroom", RoomPreset: "record"}
|
||||
_, _, applied, _, err = ra.CreateRoom(context.Background(), req, true)
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, req.Egress)
|
||||
require.Equal(t, "grid", applied.GetEgress().GetRoom().GetLayout())
|
||||
require.NotSame(t, conf.Room.RoomConfigurations["record"].Egress.Room, applied.Egress.Room)
|
||||
})
|
||||
}
|
||||
|
||||
func SelectRoomNode(t *testing.T) {
|
||||
|
||||
@@ -656,7 +656,9 @@ func (r *RoomManager) getOrCreateRoom(ctx context.Context, createRoom *livekit.C
|
||||
}
|
||||
|
||||
// create new room, get details first
|
||||
ri, internal, created, err := r.roomAllocator.CreateRoom(ctx, createRoom, true)
|
||||
// createRoom is reassigned to the request with the room preset applied, so that a room
|
||||
// composite egress from the preset, which RoomInternal has no field for, is started
|
||||
ri, internal, createRoom, created, err := r.roomAllocator.CreateRoom(ctx, createRoom, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -733,6 +735,11 @@ func (r *RoomManager) getOrCreateRoom(ctx context.Context, createRoom *livekit.C
|
||||
prometheus.RoomStarted()
|
||||
|
||||
if created && createRoom.GetEgress().GetRoom() != nil {
|
||||
if r.egressLauncher == nil {
|
||||
newRoom.Release()
|
||||
return nil, ErrEgressNotConnected
|
||||
}
|
||||
|
||||
// ensure room name matches
|
||||
createRoom.Egress.Room.RoomName = createRoom.Name
|
||||
_, err = r.egressLauncher.StartEgress(ctx, &rpc.StartEgressRequest{
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
// Copyright 2026 LiveKit, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package service_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/livekit/mediatransportutil/pkg/rtcconfig"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/rpc"
|
||||
"github.com/livekit/protocol/utils"
|
||||
"github.com/livekit/psrpc"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/config"
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/pkg/routing/routingfakes"
|
||||
"github.com/livekit/livekit-server/pkg/rtc"
|
||||
"github.com/livekit/livekit-server/pkg/service"
|
||||
"github.com/livekit/livekit-server/pkg/telemetry/telemetryfakes"
|
||||
)
|
||||
|
||||
// fakeEgressLauncher records the egress start requests it receives.
|
||||
type fakeEgressLauncher struct {
|
||||
mu sync.Mutex
|
||||
started []*rpc.StartEgressRequest
|
||||
}
|
||||
|
||||
func (f *fakeEgressLauncher) StartEgress(_ context.Context, req *rpc.StartEgressRequest) (*livekit.EgressInfo, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
f.started = append(f.started, req)
|
||||
return &livekit.EgressInfo{}, nil
|
||||
}
|
||||
|
||||
func (f *fakeEgressLauncher) StopEgress(context.Context, *livekit.StopEgressRequest) (*livekit.EgressInfo, error) {
|
||||
return &livekit.EgressInfo{}, nil
|
||||
}
|
||||
|
||||
func (f *fakeEgressLauncher) roomCompositeRoomNames() []string {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
|
||||
var names []string
|
||||
for _, req := range f.started {
|
||||
if rc := req.GetRoomComposite(); rc != nil {
|
||||
names = append(names, rc.RoomName)
|
||||
}
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
func newTestRoomManager(t *testing.T, conf *config.Config, egressLauncher rtc.EgressLauncher) *service.RoomManager {
|
||||
// no fixed RTC ports, so that the test does not bind the defaults
|
||||
conf.RTC.TCPPort = 0
|
||||
conf.RTC.UDPPort = rtcconfig.PortRange{}
|
||||
|
||||
node, err := routing.NewLocalNode(conf)
|
||||
require.NoError(t, err)
|
||||
router := &routingfakes.FakeRouter{}
|
||||
router.GetNodeForRoomReturns(node.Clone(), nil)
|
||||
|
||||
store := service.NewLocalStore()
|
||||
ra, err := service.NewRoomAllocator(conf, router, store)
|
||||
require.NoError(t, err)
|
||||
|
||||
rm, err := service.NewLocalRoomManager(
|
||||
conf,
|
||||
store,
|
||||
node,
|
||||
router,
|
||||
ra,
|
||||
&telemetryfakes.FakeTelemetryService{},
|
||||
nil,
|
||||
store,
|
||||
egressLauncher,
|
||||
utils.NewDefaultTimedVersionGenerator(),
|
||||
nil,
|
||||
psrpc.NewLocalMessageBus(),
|
||||
nil,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(rm.Stop)
|
||||
return rm
|
||||
}
|
||||
|
||||
func TestRoomCompositeEgressOnCreateRoom(t *testing.T) {
|
||||
t.Run("from request", func(t *testing.T) {
|
||||
conf, err := config.NewConfig("", true, nil, nil)
|
||||
require.NoError(t, err)
|
||||
launcher := &fakeEgressLauncher{}
|
||||
rm := newTestRoomManager(t, conf, launcher)
|
||||
|
||||
_, err = rm.CreateRoom(context.Background(), &livekit.CreateRoomRequest{
|
||||
Name: "room-a",
|
||||
Egress: &livekit.RoomEgress{
|
||||
Room: &livekit.RoomCompositeEgressRequest{
|
||||
Layout: "grid",
|
||||
FileOutputs: []*livekit.EncodedFileOutput{{Filepath: "recordings/{room_name}.mp4"}},
|
||||
},
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []string{"room-a"}, launcher.roomCompositeRoomNames())
|
||||
})
|
||||
|
||||
t.Run("from room preset", func(t *testing.T) {
|
||||
conf, err := config.NewConfig(roomPresetConfig, true, nil, nil)
|
||||
require.NoError(t, err)
|
||||
launcher := &fakeEgressLauncher{}
|
||||
rm := newTestRoomManager(t, conf, launcher)
|
||||
|
||||
for _, name := range []string{"room-a", "room-b"} {
|
||||
_, err = rm.CreateRoom(context.Background(), &livekit.CreateRoomRequest{
|
||||
Name: name,
|
||||
RoomPreset: "record",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// each room records itself, and the preset is left as configured
|
||||
require.Equal(t, []string{"room-a", "room-b"}, launcher.roomCompositeRoomNames())
|
||||
require.Empty(t, conf.Room.RoomConfigurations["record"].GetEgress().GetRoom().GetRoomName())
|
||||
})
|
||||
|
||||
t.Run("from room preset when a session starts", func(t *testing.T) {
|
||||
conf, err := config.NewConfig(roomPresetConfig, true, nil, nil)
|
||||
require.NoError(t, err)
|
||||
launcher := &fakeEgressLauncher{}
|
||||
rm := newTestRoomManager(t, conf, launcher)
|
||||
|
||||
// the join and WHIP paths create the room through StartSession, with the
|
||||
// token's room preset; without an identity only the room is created
|
||||
err = rm.StartSession(context.Background(), routing.ParticipantInit{
|
||||
CreateRoom: &livekit.CreateRoomRequest{
|
||||
Name: "room-a",
|
||||
RoomPreset: "record",
|
||||
},
|
||||
}, nil, nil, false)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []string{"room-a"}, launcher.roomCompositeRoomNames())
|
||||
})
|
||||
|
||||
t.Run("from room preset without an egress launcher", func(t *testing.T) {
|
||||
conf, err := config.NewConfig(roomPresetConfig, true, nil, nil)
|
||||
require.NoError(t, err)
|
||||
rm := newTestRoomManager(t, conf, nil)
|
||||
|
||||
_, err = rm.CreateRoom(context.Background(), &livekit.CreateRoomRequest{
|
||||
Name: "room-a",
|
||||
RoomPreset: "record",
|
||||
})
|
||||
require.ErrorIs(t, err, service.ErrEgressNotConnected)
|
||||
})
|
||||
}
|
||||
|
||||
const roomPresetConfig = `
|
||||
room:
|
||||
room_configurations:
|
||||
record:
|
||||
egress:
|
||||
room:
|
||||
layout: grid
|
||||
file_outputs:
|
||||
- filepath: recordings/{room_name}.mp4
|
||||
`
|
||||
@@ -21,7 +21,7 @@ type FakeRoomAllocator struct {
|
||||
autoCreateEnabledReturnsOnCall map[int]struct {
|
||||
result1 bool
|
||||
}
|
||||
CreateRoomStub func(context.Context, *livekit.CreateRoomRequest, bool) (*livekit.Room, *livekit.RoomInternal, bool, error)
|
||||
CreateRoomStub func(context.Context, *livekit.CreateRoomRequest, bool) (*livekit.Room, *livekit.RoomInternal, *livekit.CreateRoomRequest, bool, error)
|
||||
createRoomMutex sync.RWMutex
|
||||
createRoomArgsForCall []struct {
|
||||
arg1 context.Context
|
||||
@@ -31,14 +31,16 @@ type FakeRoomAllocator struct {
|
||||
createRoomReturns struct {
|
||||
result1 *livekit.Room
|
||||
result2 *livekit.RoomInternal
|
||||
result3 bool
|
||||
result4 error
|
||||
result3 *livekit.CreateRoomRequest
|
||||
result4 bool
|
||||
result5 error
|
||||
}
|
||||
createRoomReturnsOnCall map[int]struct {
|
||||
result1 *livekit.Room
|
||||
result2 *livekit.RoomInternal
|
||||
result3 bool
|
||||
result4 error
|
||||
result3 *livekit.CreateRoomRequest
|
||||
result4 bool
|
||||
result5 error
|
||||
}
|
||||
SelectRoomNodeStub func(context.Context, livekit.RoomName, livekit.NodeID) error
|
||||
selectRoomNodeMutex sync.RWMutex
|
||||
@@ -130,7 +132,7 @@ func (fake *FakeRoomAllocator) AutoCreateEnabledReturnsOnCall(i int, result1 boo
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeRoomAllocator) CreateRoom(arg1 context.Context, arg2 *livekit.CreateRoomRequest, arg3 bool) (*livekit.Room, *livekit.RoomInternal, bool, error) {
|
||||
func (fake *FakeRoomAllocator) CreateRoom(arg1 context.Context, arg2 *livekit.CreateRoomRequest, arg3 bool) (*livekit.Room, *livekit.RoomInternal, *livekit.CreateRoomRequest, bool, error) {
|
||||
fake.createRoomMutex.Lock()
|
||||
ret, specificReturn := fake.createRoomReturnsOnCall[len(fake.createRoomArgsForCall)]
|
||||
fake.createRoomArgsForCall = append(fake.createRoomArgsForCall, struct {
|
||||
@@ -146,9 +148,9 @@ func (fake *FakeRoomAllocator) CreateRoom(arg1 context.Context, arg2 *livekit.Cr
|
||||
return stub(arg1, arg2, arg3)
|
||||
}
|
||||
if specificReturn {
|
||||
return ret.result1, ret.result2, ret.result3, ret.result4
|
||||
return ret.result1, ret.result2, ret.result3, ret.result4, ret.result5
|
||||
}
|
||||
return fakeReturns.result1, fakeReturns.result2, fakeReturns.result3, fakeReturns.result4
|
||||
return fakeReturns.result1, fakeReturns.result2, fakeReturns.result3, fakeReturns.result4, fakeReturns.result5
|
||||
}
|
||||
|
||||
func (fake *FakeRoomAllocator) CreateRoomCallCount() int {
|
||||
@@ -157,7 +159,7 @@ func (fake *FakeRoomAllocator) CreateRoomCallCount() int {
|
||||
return len(fake.createRoomArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeRoomAllocator) CreateRoomCalls(stub func(context.Context, *livekit.CreateRoomRequest, bool) (*livekit.Room, *livekit.RoomInternal, bool, error)) {
|
||||
func (fake *FakeRoomAllocator) CreateRoomCalls(stub func(context.Context, *livekit.CreateRoomRequest, bool) (*livekit.Room, *livekit.RoomInternal, *livekit.CreateRoomRequest, bool, error)) {
|
||||
fake.createRoomMutex.Lock()
|
||||
defer fake.createRoomMutex.Unlock()
|
||||
fake.CreateRoomStub = stub
|
||||
@@ -170,19 +172,20 @@ func (fake *FakeRoomAllocator) CreateRoomArgsForCall(i int) (context.Context, *l
|
||||
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3
|
||||
}
|
||||
|
||||
func (fake *FakeRoomAllocator) CreateRoomReturns(result1 *livekit.Room, result2 *livekit.RoomInternal, result3 bool, result4 error) {
|
||||
func (fake *FakeRoomAllocator) CreateRoomReturns(result1 *livekit.Room, result2 *livekit.RoomInternal, result3 *livekit.CreateRoomRequest, result4 bool, result5 error) {
|
||||
fake.createRoomMutex.Lock()
|
||||
defer fake.createRoomMutex.Unlock()
|
||||
fake.CreateRoomStub = nil
|
||||
fake.createRoomReturns = struct {
|
||||
result1 *livekit.Room
|
||||
result2 *livekit.RoomInternal
|
||||
result3 bool
|
||||
result4 error
|
||||
}{result1, result2, result3, result4}
|
||||
result3 *livekit.CreateRoomRequest
|
||||
result4 bool
|
||||
result5 error
|
||||
}{result1, result2, result3, result4, result5}
|
||||
}
|
||||
|
||||
func (fake *FakeRoomAllocator) CreateRoomReturnsOnCall(i int, result1 *livekit.Room, result2 *livekit.RoomInternal, result3 bool, result4 error) {
|
||||
func (fake *FakeRoomAllocator) CreateRoomReturnsOnCall(i int, result1 *livekit.Room, result2 *livekit.RoomInternal, result3 *livekit.CreateRoomRequest, result4 bool, result5 error) {
|
||||
fake.createRoomMutex.Lock()
|
||||
defer fake.createRoomMutex.Unlock()
|
||||
fake.CreateRoomStub = nil
|
||||
@@ -190,16 +193,18 @@ func (fake *FakeRoomAllocator) CreateRoomReturnsOnCall(i int, result1 *livekit.R
|
||||
fake.createRoomReturnsOnCall = make(map[int]struct {
|
||||
result1 *livekit.Room
|
||||
result2 *livekit.RoomInternal
|
||||
result3 bool
|
||||
result4 error
|
||||
result3 *livekit.CreateRoomRequest
|
||||
result4 bool
|
||||
result5 error
|
||||
})
|
||||
}
|
||||
fake.createRoomReturnsOnCall[i] = struct {
|
||||
result1 *livekit.Room
|
||||
result2 *livekit.RoomInternal
|
||||
result3 bool
|
||||
result4 error
|
||||
}{result1, result2, result3, result4}
|
||||
result3 *livekit.CreateRoomRequest
|
||||
result4 bool
|
||||
result5 error
|
||||
}{result1, result2, result3, result4, result5}
|
||||
}
|
||||
|
||||
func (fake *FakeRoomAllocator) SelectRoomNode(arg1 context.Context, arg2 livekit.RoomName, arg3 livekit.NodeID) error {
|
||||
|
||||
Reference in New Issue
Block a user