Participant egress (#2070)
* participant egress * remove hasPublished when participant leaves room
This commit is contained in:
+33
-14
@@ -28,8 +28,6 @@ import (
|
||||
|
||||
"github.com/pion/sctp"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/sfu/connectionquality"
|
||||
sutils "github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/utils"
|
||||
@@ -38,8 +36,10 @@ import (
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/buffer"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/connectionquality"
|
||||
"github.com/livekit/livekit-server/pkg/telemetry"
|
||||
"github.com/livekit/livekit-server/pkg/telemetry/prometheus"
|
||||
sutils "github.com/livekit/livekit-server/pkg/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -81,6 +81,7 @@ type Room struct {
|
||||
participants map[livekit.ParticipantIdentity]types.LocalParticipant
|
||||
participantOpts map[livekit.ParticipantIdentity]*ParticipantOptions
|
||||
participantRequestSources map[livekit.ParticipantIdentity]routing.MessageSource
|
||||
hasPublished sync.Map // map of identity -> bool
|
||||
bufferFactory *buffer.FactoryOfBufferFactory
|
||||
|
||||
// batch update participant info for non-publishers
|
||||
@@ -509,6 +510,7 @@ func (r *Room) RemoveParticipant(identity livekit.ParticipantIdentity, pID livek
|
||||
for _, t := range p.GetPublishedTracks() {
|
||||
r.trackManager.RemoveTrack(t)
|
||||
}
|
||||
r.hasPublished.Delete(p.Identity())
|
||||
|
||||
p.OnTrackUpdated(nil)
|
||||
p.OnTrackPublished(nil)
|
||||
@@ -897,18 +899,35 @@ func (r *Room) onTrackPublished(participant types.LocalParticipant, track types.
|
||||
|
||||
r.trackManager.AddTrack(track, participant.Identity(), participant.ID())
|
||||
|
||||
// auto track egress
|
||||
if r.internal != nil && r.internal.TrackEgress != nil {
|
||||
if err := StartTrackEgress(
|
||||
context.Background(),
|
||||
r.egressLauncher,
|
||||
r.telemetry,
|
||||
r.internal.TrackEgress,
|
||||
track,
|
||||
r.Name(),
|
||||
r.ID(),
|
||||
); err != nil {
|
||||
r.Logger.Errorw("failed to launch track egress", err)
|
||||
// auto egress
|
||||
if r.internal != nil {
|
||||
if r.internal.ParticipantEgress != nil {
|
||||
if _, hasPublished := r.hasPublished.Swap(participant.Identity(), true); !hasPublished {
|
||||
if err := StartParticipantEgress(
|
||||
context.Background(),
|
||||
r.egressLauncher,
|
||||
r.telemetry,
|
||||
r.internal.ParticipantEgress,
|
||||
participant.Identity(),
|
||||
r.Name(),
|
||||
r.ID(),
|
||||
); err != nil {
|
||||
r.Logger.Errorw("failed to launch participant egress", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if r.internal.TrackEgress != nil {
|
||||
if err := StartTrackEgress(
|
||||
context.Background(),
|
||||
r.egressLauncher,
|
||||
r.telemetry,
|
||||
r.internal.TrackEgress,
|
||||
track,
|
||||
r.Name(),
|
||||
r.ID(),
|
||||
); err != nil {
|
||||
r.Logger.Errorw("failed to launch track egress", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+61
-1
@@ -32,6 +32,67 @@ type EgressLauncher interface {
|
||||
StartEgressWithClusterId(ctx context.Context, clusterId string, req *rpc.StartEgressRequest) (*livekit.EgressInfo, error)
|
||||
}
|
||||
|
||||
func StartParticipantEgress(
|
||||
ctx context.Context,
|
||||
launcher EgressLauncher,
|
||||
ts telemetry.TelemetryService,
|
||||
opts *livekit.AutoParticipantEgress,
|
||||
identity livekit.ParticipantIdentity,
|
||||
roomName livekit.RoomName,
|
||||
roomID livekit.RoomID,
|
||||
) error {
|
||||
if req, err := startParticipantEgress(ctx, launcher, opts, identity, roomName, roomID); err != nil {
|
||||
// send egress failed webhook
|
||||
ts.NotifyEvent(ctx, &livekit.WebhookEvent{
|
||||
Event: webhook.EventEgressEnded,
|
||||
EgressInfo: &livekit.EgressInfo{
|
||||
RoomId: string(roomID),
|
||||
RoomName: string(roomName),
|
||||
Status: livekit.EgressStatus_EGRESS_FAILED,
|
||||
Error: err.Error(),
|
||||
Request: &livekit.EgressInfo_Participant{Participant: req},
|
||||
},
|
||||
})
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func startParticipantEgress(
|
||||
ctx context.Context,
|
||||
launcher EgressLauncher,
|
||||
opts *livekit.AutoParticipantEgress,
|
||||
identity livekit.ParticipantIdentity,
|
||||
roomName livekit.RoomName,
|
||||
roomID livekit.RoomID,
|
||||
) (*livekit.ParticipantEgressRequest, error) {
|
||||
req := &livekit.ParticipantEgressRequest{
|
||||
RoomName: string(roomName),
|
||||
Identity: string(identity),
|
||||
FileOutputs: opts.FileOutputs,
|
||||
SegmentOutputs: opts.SegmentOutputs,
|
||||
}
|
||||
|
||||
switch o := opts.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}
|
||||
}
|
||||
|
||||
if launcher == nil {
|
||||
return req, errors.New("egress launcher not found")
|
||||
}
|
||||
|
||||
_, err := launcher.StartEgress(ctx, &rpc.StartEgressRequest{
|
||||
Request: &rpc.StartEgressRequest_Participant{
|
||||
Participant: req,
|
||||
},
|
||||
RoomId: string(roomID),
|
||||
})
|
||||
return req, err
|
||||
}
|
||||
|
||||
func StartTrackEgress(
|
||||
ctx context.Context,
|
||||
launcher EgressLauncher,
|
||||
@@ -66,7 +127,6 @@ func startTrackEgress(
|
||||
roomName livekit.RoomName,
|
||||
roomID livekit.RoomID,
|
||||
) (*livekit.TrackEgressRequest, error) {
|
||||
|
||||
output := &livekit.DirectFileOutput{
|
||||
Filepath: getFilePath(opts.Filepath),
|
||||
}
|
||||
|
||||
+50
-32
@@ -17,7 +17,6 @@ package service
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
@@ -25,6 +24,7 @@ import (
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/rtc"
|
||||
"github.com/livekit/livekit-server/pkg/telemetry"
|
||||
"github.com/livekit/protocol/egress"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/rpc"
|
||||
@@ -80,9 +80,10 @@ func NewEgressService(
|
||||
}
|
||||
|
||||
func (s *EgressService) StartRoomCompositeEgress(ctx context.Context, req *livekit.RoomCompositeEgressRequest) (*livekit.EgressInfo, error) {
|
||||
fields := []interface{}{"room", req.RoomName, "baseUrl", req.CustomBaseUrl}
|
||||
if t := reflect.TypeOf(req.Output); t != nil {
|
||||
fields = append(fields, "outputType", t.String())
|
||||
fields := []interface{}{
|
||||
"room", req.RoomName,
|
||||
"baseUrl", req.CustomBaseUrl,
|
||||
"outputType", egress.GetOutputType(req),
|
||||
}
|
||||
defer func() {
|
||||
AppendLogFields(ctx, fields...)
|
||||
@@ -99,12 +100,53 @@ func (s *EgressService) StartRoomCompositeEgress(ctx context.Context, req *livek
|
||||
return ei, err
|
||||
}
|
||||
|
||||
func (s *EgressService) StartWebEgress(ctx context.Context, req *livekit.WebEgressRequest) (*livekit.EgressInfo, error) {
|
||||
fields := []interface{}{
|
||||
"url", req.Url,
|
||||
"outputType", egress.GetOutputType(req),
|
||||
}
|
||||
defer func() {
|
||||
AppendLogFields(ctx, fields...)
|
||||
}()
|
||||
ei, err := s.startEgress(ctx, "", &rpc.StartEgressRequest{
|
||||
Request: &rpc.StartEgressRequest_Web{
|
||||
Web: req,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fields = append(fields, "egressID", ei.EgressId)
|
||||
return ei, err
|
||||
}
|
||||
|
||||
func (s *EgressService) StartParticipantEgress(ctx context.Context, req *livekit.ParticipantEgressRequest) (*livekit.EgressInfo, error) {
|
||||
fields := []interface{}{
|
||||
"room", req.RoomName,
|
||||
"identity", req.Identity,
|
||||
"outputType", egress.GetOutputType(req),
|
||||
}
|
||||
defer func() {
|
||||
AppendLogFields(ctx, fields...)
|
||||
}()
|
||||
ei, err := s.startEgress(ctx, livekit.RoomName(req.RoomName), &rpc.StartEgressRequest{
|
||||
Request: &rpc.StartEgressRequest_Participant{
|
||||
Participant: req,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fields = append(fields, "egressID", ei.EgressId)
|
||||
return ei, err
|
||||
}
|
||||
|
||||
func (s *EgressService) StartTrackCompositeEgress(ctx context.Context, req *livekit.TrackCompositeEgressRequest) (*livekit.EgressInfo, error) {
|
||||
fields := []interface{}{
|
||||
"room", req.RoomName, "audioTrackID", req.AudioTrackId, "videoTrackID", req.VideoTrackId,
|
||||
}
|
||||
if t := reflect.TypeOf(req.Output); t != nil {
|
||||
fields = append(fields, "outputType", t.String())
|
||||
"room", req.RoomName,
|
||||
"audioTrackID", req.AudioTrackId,
|
||||
"videoTrackID", req.VideoTrackId,
|
||||
"outputType", egress.GetOutputType(req),
|
||||
}
|
||||
defer func() {
|
||||
AppendLogFields(ctx, fields...)
|
||||
@@ -141,30 +183,6 @@ func (s *EgressService) StartTrackEgress(ctx context.Context, req *livekit.Track
|
||||
return ei, err
|
||||
}
|
||||
|
||||
func (s *EgressService) StartWebEgress(ctx context.Context, req *livekit.WebEgressRequest) (*livekit.EgressInfo, error) {
|
||||
fields := []interface{}{"url", req.Url}
|
||||
if t := reflect.TypeOf(req.Output); t != nil {
|
||||
fields = append(fields, "outputType", t.String())
|
||||
}
|
||||
defer func() {
|
||||
AppendLogFields(ctx, fields...)
|
||||
}()
|
||||
ei, err := s.startEgress(ctx, "", &rpc.StartEgressRequest{
|
||||
Request: &rpc.StartEgressRequest_Web{
|
||||
Web: req,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fields = append(fields, "egressID", ei.EgressId)
|
||||
return ei, err
|
||||
}
|
||||
|
||||
func (s *EgressService) StartParticipantEgress(ctx context.Context, req *livekit.ParticipantEgressRequest) (*livekit.EgressInfo, error) {
|
||||
return nil, errors.New("under development")
|
||||
}
|
||||
|
||||
func (s *EgressService) startEgress(ctx context.Context, roomName livekit.RoomName, req *rpc.StartEgressRequest) (*livekit.EgressInfo, error) {
|
||||
if err := EnsureRecordPermission(ctx); err != nil {
|
||||
return nil, twirpAuthError(err)
|
||||
|
||||
Reference in New Issue
Block a user