RoomRecorder (#70)

This commit is contained in:
David Colburn
2021-07-27 00:39:48 -05:00
committed by GitHub
parent f8782fae3f
commit b00d5a02cb
10 changed files with 203 additions and 68 deletions
+1 -35
View File
@@ -1,7 +1,6 @@
package main
import (
"context"
"fmt"
"io/ioutil"
"math/rand"
@@ -12,7 +11,6 @@ import (
"syscall"
"time"
"github.com/go-redis/redis/v8"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
@@ -197,14 +195,7 @@ func startServer(c *cli.Context) error {
return err
}
// local routing and store
router, roomStore, err := createRouterAndStore(conf, currentNode)
if err != nil {
return err
}
server, err := service.InitializeServer(conf, keyProvider,
roomStore, router, currentNode, &routing.RandomSelector{})
server, err := service.InitializeServer(conf, keyProvider, currentNode, &routing.RandomSelector{})
if err != nil {
return err
}
@@ -221,31 +212,6 @@ func startServer(c *cli.Context) error {
return server.Start()
}
func createRouterAndStore(config *config.Config, node routing.LocalNode) (router routing.Router, store service.RoomStore, err error) {
if config.HasRedis() {
logger.Infow("using multi-node routing via redis", "addr", config.Redis.Address)
rc := redis.NewClient(&redis.Options{
Addr: config.Redis.Address,
Username: config.Redis.Username,
Password: config.Redis.Password,
DB: config.Redis.DB,
})
if err = rc.Ping(context.Background()).Err(); err != nil {
err = errors.Wrap(err, "unable to connect to redis")
return
}
router = routing.NewRedisRouter(node, rc)
store = service.NewRedisRoomStore(rc)
} else {
// local routing and store
logger.Infow("using single-node routing")
router = routing.NewLocalRouter(node)
store = service.NewLocalRoomStore()
}
return
}
func createKeyProvider(conf *config.Config) (auth.KeyProvider, error) {
// prefer keyfile if set
if conf.KeyFile != "" {
+1 -1
View File
@@ -11,7 +11,7 @@ require (
github.com/google/wire v0.5.0
github.com/gorilla/websocket v1.4.2
github.com/jxskiss/base62 v0.0.0-20191017122030-4f11678b909b
github.com/livekit/protocol v0.5.9
github.com/livekit/protocol v0.6.0
github.com/magefile/mage v1.11.0
github.com/maxbrunsfeld/counterfeiter/v6 v6.3.0
github.com/mitchellh/go-homedir v1.1.0
+2 -2
View File
@@ -237,8 +237,8 @@ github.com/lithammer/shortuuid/v3 v3.0.6 h1:pr15YQyvhiSX/qPxncFtqk+v4xLEpOZObbsY
github.com/lithammer/shortuuid/v3 v3.0.6/go.mod h1:vMk8ke37EmiewwolSO1NLW8vP4ZaKlRuDIi8tWWmAts=
github.com/livekit/ion-sfu v1.20.5 h1:2PltjaVWd4y3gUcBssHwa/V3jIp92z5vC0+6CzB7rz8=
github.com/livekit/ion-sfu v1.20.5/go.mod h1:dEdOG4KSqIftr5HxxqciNKBIdu0v3OD0ZYL7A3J09KA=
github.com/livekit/protocol v0.5.9 h1:NIA/nX74dv199oVmfGCbBK5qpuHTDu6h0K7DAPiM++w=
github.com/livekit/protocol v0.5.9/go.mod h1:wo3CGfYB7XMF8GoVJAfTARrYSP/ombi+sbLl6AYdKP0=
github.com/livekit/protocol v0.6.0 h1:1tTwGMuA2WLwIaOXZHiuBZTjZMYY/t3TMK1zjQBvn7o=
github.com/livekit/protocol v0.6.0/go.mod h1:wo3CGfYB7XMF8GoVJAfTARrYSP/ombi+sbLl6AYdKP0=
github.com/lucsky/cuid v1.0.2 h1:z4XlExeoderxoPj2/dxKOyPxe9RCOu7yNq9/XWxIUMQ=
github.com/lucsky/cuid v1.0.2/go.mod h1:QaaJqckboimOmhRSJXSx/+IT+VTfxfPGSo/6mfgUfmE=
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
+80
View File
@@ -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
}
+55 -1
View File
@@ -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)
+46
View File
@@ -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
View File
@@ -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
View File
@@ -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
}
+3 -18
View File
@@ -145,11 +145,7 @@ func createSingleNodeServer() *service.LivekitServer {
}
currentNode.Id = utils.NewGuid(nodeId1)
// local routing and store
router := routing.NewLocalRouter(currentNode)
roomStore := service.NewLocalRoomStore()
_ = roomStore.DeleteRoom(testRoom)
s, err := service.InitializeServer(conf, &StaticKeyProvider{}, roomStore, router, currentNode, &routing.RandomSelector{})
s, err := service.InitializeServer(conf, &StaticKeyProvider{}, currentNode, &routing.RandomSelector{})
if err != nil {
panic(fmt.Sprintf("could not create server: %v", err))
}
@@ -176,19 +172,8 @@ func createMultiNodeServer(nodeId string, port uint32) *service.LivekitServer {
}
currentNode.Id = nodeId
// local routing and store
rc := redisClient()
if err = rc.Ping(context.Background()).Err(); err != nil {
panic(err)
}
if err = rc.Del(context.Background(), routing.NodesKey).Err(); err != nil {
panic(err)
}
router := routing.NewRedisRouter(currentNode, rc)
roomStore := service.NewRedisRoomStore(rc)
_ = roomStore.DeleteRoom(testRoom)
s, err := service.InitializeServer(conf, &StaticKeyProvider{}, roomStore, router, currentNode, &routing.RandomSelector{})
// redis routing and store
s, err := service.InitializeServer(conf, &StaticKeyProvider{}, currentNode, &routing.RandomSelector{})
if err != nil {
panic(fmt.Sprintf("could not create server: %v", err))
}
+2 -5
View File
@@ -28,10 +28,7 @@ func testTurnServer(t *testing.T) {
currentNode.Id = utils.NewGuid(nodeId1)
// local routing and store
router := routing.NewLocalRouter(currentNode)
roomStore := service.NewLocalRoomStore()
roomStore.DeleteRoom(testRoom)
s, err := service.InitializeServer(conf, &StaticKeyProvider{}, roomStore, router, currentNode, &routing.RandomSelector{})
s, err := service.InitializeServer(conf, &StaticKeyProvider{}, currentNode, &routing.RandomSelector{})
require.NoError(t, err)
go s.Start()
waitForServerToStart(s)
@@ -45,7 +42,7 @@ func testTurnServer(t *testing.T) {
Name: "testroom",
TurnPassword: utils.RandomSecret(),
}
require.NoError(t, roomStore.CreateRoom(rm))
// require.NoError(t, roomStore.CreateRoom(rm))
turnConf := &turn.ClientConfig{
STUNServerAddr: fmt.Sprintf("localhost:%d", conf.TURN.TLSPort),