RoomRecorder (#70)
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package recording
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/livekit/protocol/utils"
|
||||
"github.com/pkg/errors"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
livekit "github.com/livekit/livekit-server/proto"
|
||||
)
|
||||
|
||||
type RoomRecorder struct {
|
||||
rc *redis.Client
|
||||
}
|
||||
|
||||
const (
|
||||
ReservationChannel = "RESERVE_RECORDER"
|
||||
recorderTimeout = time.Second * 3
|
||||
// used by livekit-recorder
|
||||
ReservationTimeout = time.Second * 2
|
||||
)
|
||||
|
||||
func NewRoomRecorder(rc *redis.Client) *RoomRecorder {
|
||||
if rc == nil {
|
||||
return nil
|
||||
}
|
||||
return &RoomRecorder{rc: rc}
|
||||
}
|
||||
|
||||
func (s *RoomRecorder) ReserveRecorder(ctx context.Context, req *livekit.RecordRoomRequest) (string, error) {
|
||||
id := utils.NewGuid(utils.RecordingPrefix)
|
||||
reservation := &livekit.RecordingReservation{
|
||||
Id: id,
|
||||
SubmittedAt: time.Now().UnixNano(),
|
||||
Input: req.Input,
|
||||
Output: req.Output,
|
||||
}
|
||||
b, err := proto.Marshal(reservation)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
sub := s.rc.Subscribe(ctx, ResponseChannel(id))
|
||||
defer sub.Close()
|
||||
|
||||
err = s.rc.Publish(ctx, ReservationChannel, string(b)).Err()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
select {
|
||||
case <-sub.Channel():
|
||||
return id, nil
|
||||
case <-time.After(recorderTimeout):
|
||||
return "", errors.New("no recorders available")
|
||||
}
|
||||
}
|
||||
|
||||
func (s *RoomRecorder) StartRecording(ctx context.Context, recordingID string) error {
|
||||
return s.rc.Publish(ctx, StartRecordingChannel(recordingID), nil).Err()
|
||||
}
|
||||
|
||||
func (s *RoomRecorder) EndRecording(ctx context.Context, recordingID string) error {
|
||||
return s.rc.Publish(ctx, EndRecordingChannel(recordingID), nil).Err()
|
||||
}
|
||||
|
||||
func ResponseChannel(id string) string {
|
||||
return "RESPONSE_" + id
|
||||
}
|
||||
|
||||
func StartRecordingChannel(id string) string {
|
||||
return "START_RECORDING_" + id
|
||||
}
|
||||
|
||||
func EndRecordingChannel(id string) string {
|
||||
return "END_RECORDING_" + id
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/thoas/go-funk"
|
||||
"github.com/twitchtv/twirp"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/recording"
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
livekit "github.com/livekit/livekit-server/proto"
|
||||
)
|
||||
@@ -14,11 +15,13 @@ import (
|
||||
// A rooms service that supports a single node
|
||||
type RoomService struct {
|
||||
roomManager *RoomManager
|
||||
recorder *recording.RoomRecorder
|
||||
}
|
||||
|
||||
func NewRoomService(roomManager *RoomManager) (svc *RoomService, err error) {
|
||||
func NewRoomService(roomManager *RoomManager, rs *recording.RoomRecorder) (svc *RoomService, err error) {
|
||||
svc = &RoomService{
|
||||
roomManager: roomManager,
|
||||
recorder: rs,
|
||||
}
|
||||
|
||||
return
|
||||
@@ -29,10 +32,31 @@ func (s *RoomService) CreateRoom(ctx context.Context, req *livekit.CreateRoomReq
|
||||
return nil, twirpAuthError(err)
|
||||
}
|
||||
|
||||
var recordingID string
|
||||
if req.Recording != nil {
|
||||
if s.recorder == nil {
|
||||
return nil, errors.New("recording not configured (redis required)")
|
||||
}
|
||||
|
||||
recordingID, err = s.recorder.ReserveRecorder(ctx, req.Recording)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "could not reserve recorder")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
rm, err = s.roomManager.CreateRoom(req)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "could not create room")
|
||||
}
|
||||
|
||||
if recordingID != "" {
|
||||
err = s.recorder.StartRecording(ctx, recordingID)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "could not start recording")
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -196,6 +220,36 @@ func (s *RoomService) UpdateSubscriptions(ctx context.Context, req *livekit.Upda
|
||||
return &livekit.UpdateSubscriptionsResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *RoomService) RecordRoom(ctx context.Context, req *livekit.RecordRoomRequest) (*livekit.RecordingResponse, error) {
|
||||
if s.recorder == nil {
|
||||
return nil, errors.New("recording not configured (redis required)")
|
||||
}
|
||||
|
||||
id, err := s.recorder.ReserveRecorder(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = s.recorder.StartRecording(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &livekit.RecordingResponse{RecordingId: id}, nil
|
||||
}
|
||||
|
||||
func (s *RoomService) EndRoomRecording(ctx context.Context, req *livekit.EndRecordingRequest) (*livekit.RecordingResponse, error) {
|
||||
if s.recorder == nil {
|
||||
return nil, errors.New("recording not configured (redis required)")
|
||||
}
|
||||
|
||||
err := s.recorder.EndRecording(ctx, req.RecordingId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &livekit.RecordingResponse{RecordingId: req.RecordingId}, nil
|
||||
}
|
||||
|
||||
func (s *RoomService) createRTCSink(ctx context.Context, room, identity string) (routing.MessageSink, error) {
|
||||
if err := EnsureAdminPermission(ctx, room); err != nil {
|
||||
return nil, twirpAuthError(err)
|
||||
|
||||
@@ -1,16 +1,25 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/google/wire"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/config"
|
||||
"github.com/livekit/livekit-server/pkg/logger"
|
||||
"github.com/livekit/livekit-server/pkg/recording"
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
livekit "github.com/livekit/livekit-server/proto"
|
||||
)
|
||||
|
||||
var ServiceSet = wire.NewSet(
|
||||
createRedisClient,
|
||||
createRouter,
|
||||
createStore,
|
||||
recording.NewRoomRecorder,
|
||||
NewRoomService,
|
||||
NewRTCService,
|
||||
NewLivekitServer,
|
||||
@@ -20,6 +29,43 @@ var ServiceSet = wire.NewSet(
|
||||
wire.Bind(new(livekit.RoomService), new(*RoomService)),
|
||||
)
|
||||
|
||||
func createRedisClient(conf *config.Config) (*redis.Client, error) {
|
||||
if !conf.HasRedis() {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
logger.Infow("using multi-node routing via redis", "addr", conf.Redis.Address)
|
||||
rc := redis.NewClient(&redis.Options{
|
||||
Addr: conf.Redis.Address,
|
||||
Username: conf.Redis.Username,
|
||||
Password: conf.Redis.Password,
|
||||
DB: conf.Redis.DB,
|
||||
})
|
||||
if err := rc.Ping(context.Background()).Err(); err != nil {
|
||||
err = errors.Wrap(err, "unable to connect to redis")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rc, nil
|
||||
}
|
||||
|
||||
func createRouter(rc *redis.Client, node routing.LocalNode) routing.Router {
|
||||
if rc != nil {
|
||||
return routing.NewRedisRouter(node, rc)
|
||||
}
|
||||
|
||||
// local routing and store
|
||||
logger.Infow("using single-node routing")
|
||||
return routing.NewLocalRouter(node)
|
||||
}
|
||||
|
||||
func createStore(rc *redis.Client) RoomStore {
|
||||
if rc != nil {
|
||||
return NewRedisRoomStore(rc)
|
||||
}
|
||||
return NewLocalRoomStore()
|
||||
}
|
||||
|
||||
func handleError(w http.ResponseWriter, status int, msg string) {
|
||||
// GetLogger already with extra depth 1
|
||||
logger.GetLogger().V(1).Info("error handling request", "error", msg, "status", status)
|
||||
|
||||
+3
-4
@@ -5,14 +5,13 @@ package service
|
||||
import (
|
||||
"github.com/google/wire"
|
||||
|
||||
"github.com/livekit/protocol/auth"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/config"
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/protocol/auth"
|
||||
)
|
||||
|
||||
func InitializeServer(conf *config.Config, keyProvider auth.KeyProvider,
|
||||
roomStore RoomStore, router routing.Router, currentNode routing.LocalNode,
|
||||
selector routing.NodeSelector) (*LivekitServer, error) {
|
||||
func InitializeServer(conf *config.Config, keyProvider auth.KeyProvider, currentNode routing.LocalNode, selector routing.NodeSelector) (*LivekitServer, error) {
|
||||
wire.Build(
|
||||
ServiceSet,
|
||||
)
|
||||
|
||||
+10
-2
@@ -7,18 +7,26 @@ package service
|
||||
|
||||
import (
|
||||
"github.com/livekit/livekit-server/pkg/config"
|
||||
"github.com/livekit/livekit-server/pkg/recording"
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/protocol/auth"
|
||||
)
|
||||
|
||||
// Injectors from wire.go:
|
||||
|
||||
func InitializeServer(conf *config.Config, keyProvider auth.KeyProvider, roomStore RoomStore, router routing.Router, currentNode routing.LocalNode, selector routing.NodeSelector) (*LivekitServer, error) {
|
||||
func InitializeServer(conf *config.Config, keyProvider auth.KeyProvider, currentNode routing.LocalNode, selector routing.NodeSelector) (*LivekitServer, error) {
|
||||
client, err := createRedisClient(conf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
roomStore := createStore(client)
|
||||
router := createRouter(client, currentNode)
|
||||
roomManager, err := NewRoomManager(roomStore, router, currentNode, selector, conf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
roomService, err := NewRoomService(roomManager)
|
||||
roomRecorder := recording.NewRoomRecorder(client)
|
||||
roomService, err := NewRoomService(roomManager, roomRecorder)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user