Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/auto-egress-v2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@livekit/protocol": patch
---

v2 auto-egress config on `RoomEgress` (`room_egress`, `participant_egress`, `track_egress`) and `RoomInternal`, where the participant and track configs carry encoding, outputs, storage and webhooks (plus `prefer_screen_share` and `capture_data` for participants), and `egress.AutoRoomRequest`/`AutoParticipantRequest`/`AutoTrackRequest`/`ValidateRoomEgress` to build egress requests from v1 or v2 config
200 changes: 200 additions & 0 deletions egress/auto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
// Copyright 2025 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 egress

import (
"fmt"
"strings"

"google.golang.org/protobuf/proto"

"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/rpc"
"github.com/livekit/psrpc"
)

// AutoRoomRequest builds a room's auto room egress request from its v1 or v2 config, or nil if it has none
func AutoRoomRequest(cfg *livekit.RoomEgress, roomName string) *rpc.StartEgressRequest {
Comment thread
milos-lk marked this conversation as resolved.
Resolved
if v2 := cfg.GetRoomEgress(); v2 != nil {
req := proto.Clone(v2).(*livekit.StartEgressRequest)
req.RoomName = roomName
return &rpc.StartEgressRequest{Request: &rpc.StartEgressRequest_Egress{Egress: req}}
}
if v1 := cfg.GetRoom(); v1 != nil {
req := proto.Clone(v1).(*livekit.RoomCompositeEgressRequest)
req.RoomName = roomName
return &rpc.StartEgressRequest{Request: &rpc.StartEgressRequest_RoomComposite{RoomComposite: req}}
}
return nil
}

// AutoParticipantRequest builds a participant's auto-egress request from the room's v1 or v2 config, or nil if it has none
func AutoParticipantRequest(internal *livekit.RoomInternal, roomName, identity string) *rpc.StartEgressRequest {
if v2 := internal.GetParticipantEgressConfig(); v2 != nil {
c := proto.Clone(v2).(*livekit.ParticipantEgressConfig)
req := &livekit.StartEgressRequest{
RoomName: roomName,
Source: participantSource(identity, c.PreferScreenShare, c.CaptureData),
Outputs: c.Outputs,
Storage: c.Storage,
Webhooks: c.Webhooks,
}
switch e := c.Encoding.(type) {
case *livekit.ParticipantEgressConfig_Preset:
req.Encoding = &livekit.StartEgressRequest_Preset{Preset: e.Preset}
case *livekit.ParticipantEgressConfig_Advanced:
req.Encoding = &livekit.StartEgressRequest_Advanced{Advanced: e.Advanced}
}
return &rpc.StartEgressRequest{Request: &rpc.StartEgressRequest_Egress{Egress: req}}
}

if v1 := internal.GetParticipantEgress(); v1 != nil {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does AutoParticipantRequest mean to change file naming for v1 participant configs?

Once v1 AutoParticipantEgress becomes a Media source with ParticipantVideo, egress only sets the identity and {publisher_identity} for RequestTypeParticipant (SDKSource.joinRoom). If I'm reading it right:

  • rec/{publisher_identity}-{time}.mp4 would no longer be replaced, so the path keeps the literal {publisher_identity}
  • an empty filepath would name the file <room_name>-<time> instead of <identity>-<time>. Would two participants who start publishing in the same second overwrite each other?

I reproduced the first one with an egress integration case: a Media ParticipantVideo request with {publisher_identity} in the filename fails verifyFile.

Should egress set the identity for Media ParticipantVideo before callers switch, or should v1 participant config stay on StartEgressRequest_Participant?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, v1 looks good now. One follow-up for the v2 configs, since they still go through Media:

  • participant_egress: {publisher_identity} isn't replaced for Media ParticipantVideo, and an empty filepath names the file <room_name>-<time>
  • track_egress: with encoding unset (default transcode), {track_id} is only replaced for passthrough, so the file is also <room_name>-<time>. v1 avoids this via trackFilepath

So with default settings, participants or tracks that start in the same second would write to the same path. Should egress set the identity / track ID templates for these Media requests, or should the builders put the identity / track ID into the v2 output paths?

🤖 Generated with Claude Code

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Egress should set them

c := proto.Clone(v1).(*livekit.AutoParticipantEgress)
req := &livekit.ParticipantEgressRequest{
RoomName: roomName,
Identity: identity,
FileOutputs: c.FileOutputs,
SegmentOutputs: c.SegmentOutputs,
}
switch o := c.Options.(type) {
case *livekit.AutoParticipantEgress_Preset:
req.Options = &livekit.ParticipantEgressRequest_Preset{Preset: o.Preset}
case *livekit.AutoParticipantEgress_Advanced:
req.Options = &livekit.ParticipantEgressRequest_Advanced{Advanced: o.Advanced}
}
return &rpc.StartEgressRequest{Request: &rpc.StartEgressRequest_Participant{Participant: req}}
}

return nil
}

func participantSource(identity string, preferScreenShare, captureData bool) *livekit.StartEgressRequest_Media {
media := &livekit.MediaSource{
Video: &livekit.MediaSource_ParticipantVideo{ParticipantVideo: &livekit.ParticipantVideo{
Identity: identity,
PreferScreenShare: preferScreenShare,
}},
Audio: &livekit.AudioConfig{Routes: []*livekit.AudioRoute{
{Match: &livekit.AudioRoute_ParticipantIdentity{ParticipantIdentity: identity}},
}},
}
if captureData {
media.Data = &livekit.DataConfig{Selectors: []*livekit.DataSelector{
{Match: &livekit.DataSelector_ParticipantIdentity{ParticipantIdentity: identity}},
}}
}
return &livekit.StartEgressRequest_Media{Media: media}
}

// AutoTrackRequest builds a published track's auto-egress request from the room's v1 or v2 config, or nil if it has none
func AutoTrackRequest(internal *livekit.RoomInternal, roomName, trackID string, trackType livekit.TrackType) *rpc.StartEgressRequest {
if v2 := internal.GetTrackEgressConfig(); v2 != nil {
c := proto.Clone(v2).(*livekit.TrackEgressConfig)
media := &livekit.MediaSource{}
if trackType == livekit.TrackType_AUDIO {
media.Audio = &livekit.AudioConfig{Routes: []*livekit.AudioRoute{
{Match: &livekit.AudioRoute_TrackId{TrackId: trackID}},
}}
} else {
media.Video = &livekit.MediaSource_VideoTrackId{VideoTrackId: trackID}
}
req := &livekit.StartEgressRequest{
RoomName: roomName,
Source: &livekit.StartEgressRequest_Media{Media: media},
Outputs: c.Outputs,
Storage: c.Storage,
Webhooks: c.Webhooks,
}
switch e := c.Encoding.(type) {
case *livekit.TrackEgressConfig_Preset:
req.Encoding = &livekit.StartEgressRequest_Preset{Preset: e.Preset}
case *livekit.TrackEgressConfig_Advanced:
req.Encoding = &livekit.StartEgressRequest_Advanced{Advanced: e.Advanced}
}
return &rpc.StartEgressRequest{Request: &rpc.StartEgressRequest_Egress{Egress: req}}
}

if v1 := internal.GetTrackEgress(); v1 != nil {
Comment thread
milos-lk marked this conversation as resolved.
Resolved
c := proto.Clone(v1).(*livekit.AutoTrackEgress)
output := &livekit.DirectFileOutput{
Filepath: trackFilepath(c.Filepath),
DisableManifest: c.DisableManifest,
}
switch o := c.Output.(type) {
case *livekit.AutoTrackEgress_S3:
output.Output = &livekit.DirectFileOutput_S3{S3: o.S3}
case *livekit.AutoTrackEgress_Gcp:
output.Output = &livekit.DirectFileOutput_Gcp{Gcp: o.Gcp}
case *livekit.AutoTrackEgress_Azure:
output.Output = &livekit.DirectFileOutput_Azure{Azure: o.Azure}
case *livekit.AutoTrackEgress_AliOSS:
output.Output = &livekit.DirectFileOutput_AliOSS{AliOSS: o.AliOSS}
}
req := &livekit.TrackEgressRequest{
RoomName: roomName,
TrackId: trackID,
Output: &livekit.TrackEgressRequest_File{File: output},
}
return &rpc.StartEgressRequest{Request: &rpc.StartEgressRequest_Track{Track: req}}
}

return nil
}

func trackFilepath(filepath string) string {
if filepath == "" || strings.HasSuffix(filepath, "/") || strings.Contains(filepath, "{track_id}") {
return filepath
}

idx := strings.Index(filepath, ".")
if idx == -1 {
return fmt.Sprintf("%s-{track_id}", filepath)
} else {
return fmt.Sprintf("%s-%s%s", filepath[:idx], "{track_id}", filepath[idx:])
}
}

// ValidateRoomEgress rejects v1 and v2 config for the same kind, and v2 config that egress would reject
func ValidateRoomEgress(cfg *livekit.RoomEgress) error {
Comment thread
milos-lk marked this conversation as resolved.
Resolved
switch {
case cfg.GetRoom() != nil && cfg.GetRoomEgress() != nil:
return psrpc.NewErrorf(psrpc.InvalidArgument, "room and room_egress are mutually exclusive")
case cfg.GetParticipant() != nil && cfg.GetParticipantEgress() != nil:
return psrpc.NewErrorf(psrpc.InvalidArgument, "participant and participant_egress are mutually exclusive")
case cfg.GetTracks() != nil && cfg.GetTrackEgress() != nil:
return psrpc.NewErrorf(psrpc.InvalidArgument, "tracks and track_egress are mutually exclusive")
}

if re := cfg.GetRoomEgress(); re != nil {
if re.Source == nil {
return psrpc.NewErrorf(psrpc.InvalidArgument, "room_egress requires a source")
}
if len(re.Outputs) == 0 {
return psrpc.NewErrorf(psrpc.InvalidArgument, "room_egress requires an output")
}
}
if pe := cfg.GetParticipantEgress(); pe != nil {
if len(pe.Outputs) == 0 {
return psrpc.NewErrorf(psrpc.InvalidArgument, "participant_egress requires an output")
}
if pe.CaptureData && (len(pe.Outputs) != 1 || pe.Outputs[0].GetFile().GetFileType() != livekit.EncodedFileType_MCAP) {
return psrpc.NewErrorf(psrpc.InvalidArgument, "participant_egress capture_data requires a single MCAP file output")
}
}
if te := cfg.GetTrackEgress(); te != nil && len(te.Outputs) == 0 {
return psrpc.NewErrorf(psrpc.InvalidArgument, "track_egress requires an output")
}
return nil
}
Loading
Loading