Recording webhooks (#125)

* dump

* update protocol

* const

* regenerate wire

* message bus nil check

* nil message bus if no redis

* start and stop rec service
This commit is contained in:
David Colburn
2021-09-21 23:46:25 -05:00
committed by GitHub
parent 3ed3afad2c
commit 347bf0345a
6 changed files with 108 additions and 20 deletions
+1 -1
View File
@@ -14,7 +14,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.9.1
github.com/livekit/protocol v0.9.2
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
@@ -246,8 +246,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.9 h1:Ih+tE5CXqdL1o6iXfZn+uBoddBXWceaOKVIun6aVuZM=
github.com/livekit/ion-sfu v1.20.9/go.mod h1:g8hwobZI5fvX1RXvayf4ZXkgP7spV5YGE4yTSsumpB4=
github.com/livekit/protocol v0.9.1 h1:Ebt17AbmDux5Exc6xmk2uixuQoYxqRMhXNQGeaJ7Xrk=
github.com/livekit/protocol v0.9.1/go.mod h1:MEKn847Iu/2U8ClZyUmEm2oHn8k9fnSHy81Wv8kkSDo=
github.com/livekit/protocol v0.9.2 h1:ZX5m7DjmRMHtYH5oM85QRMuJlr4feTfD5UL22h9HmtM=
github.com/livekit/protocol v0.9.2/go.mod h1:MEKn847Iu/2U8ClZyUmEm2oHn8k9fnSHy81Wv8kkSDo=
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=
+83 -3
View File
@@ -3,19 +3,40 @@ package service
import (
"context"
"errors"
"fmt"
"time"
"github.com/livekit/protocol/logger"
livekit "github.com/livekit/protocol/proto"
"github.com/livekit/protocol/utils"
"github.com/livekit/protocol/webhook"
"google.golang.org/protobuf/proto"
)
const lockExpiration = time.Second * 5
type RecordingService struct {
mb utils.MessageBus
mb utils.MessageBus
notifier *webhook.Notifier
shutdown chan struct{}
}
func NewRecordingService(mb utils.MessageBus) *RecordingService {
return &RecordingService{mb: mb}
func NewRecordingService(mb utils.MessageBus, notifier *webhook.Notifier) *RecordingService {
return &RecordingService{
mb: mb,
notifier: notifier,
shutdown: make(chan struct{}, 1),
}
}
func (s *RecordingService) Start() {
if s.mb != nil {
go s.resultsWorker()
}
}
func (s *RecordingService) Stop() {
s.shutdown <- struct{}{}
}
func (s *RecordingService) StartRecording(ctx context.Context, req *livekit.StartRecordingRequest) (*livekit.RecordingResponse, error) {
@@ -87,3 +108,62 @@ func (s *RecordingService) EndRecording(ctx context.Context, req *livekit.EndRec
return &livekit.RecordingResponse{RecordingId: req.RecordingId}, nil
}
func (s *RecordingService) resultsWorker() {
sub, err := s.mb.Subscribe(context.Background(), utils.RecordingResultChannel)
if err != nil {
logger.Errorw("failed to subscribe to results channel", err)
return
}
resChan := sub.Channel()
for {
select {
case msg := <-resChan:
b := sub.Payload(msg)
res := &livekit.RecordingResult{}
if err = proto.Unmarshal(b, res); err != nil {
logger.Errorw("failed to read results", err)
continue
}
s.notify(res)
case <-s.shutdown:
sub.Close()
return
}
}
}
func (s *RecordingService) notify(res *livekit.RecordingResult) {
acquired, err := s.mb.Lock(context.Background(), res.Id, lockExpiration)
if err != nil {
logger.Errorw("failed to lock", err)
return
}
if !acquired {
return
}
// log results
if res.Error != "" {
logger.Errorw("recording failed", errors.New(res.Error), "id", res.Id)
} else {
logger.Infow("recording complete",
"id", res.Id,
"duration", fmt.Sprint(time.Duration(res.Duration*1e6)),
"location", res.Location,
)
}
// webhook
if s.notifier != nil {
event := webhook.EventRecordingFinished
if err := s.notifier.Notify(&livekit.WebhookEvent{
Event: event,
RecordingResult: res,
}); err != nil {
logger.Warnw("could not notify webhook", err, "event", event)
}
}
}
+11 -8
View File
@@ -25,8 +25,7 @@ import (
type LivekitServer struct {
config *config.Config
roomServer livekit.TwirpServer
recServer livekit.TwirpServer
recService *RecordingService
rtcService *RTCService
httpServer *http.Server
promServer *http.Server
@@ -41,7 +40,7 @@ type LivekitServer struct {
func NewLivekitServer(conf *config.Config,
roomService livekit.RoomService,
recService livekit.RecordingService,
recService *RecordingService,
rtcService *RTCService,
keyProvider auth.KeyProvider,
router routing.Router,
@@ -51,8 +50,7 @@ func NewLivekitServer(conf *config.Config,
) (s *LivekitServer, err error) {
s = &LivekitServer{
config: conf,
roomServer: livekit.NewRoomServiceServer(roomService),
recServer: livekit.NewRecordingServiceServer(recService),
recService: recService,
rtcService: rtcService,
router: router,
roomManager: roomManager,
@@ -70,9 +68,12 @@ func NewLivekitServer(conf *config.Config,
middlewares = append(middlewares, NewAPIKeyAuthMiddleware(keyProvider))
}
roomServer := livekit.NewRoomServiceServer(roomService)
recServer := livekit.NewRecordingServiceServer(recService)
mux := http.NewServeMux()
mux.Handle(s.roomServer.PathPrefix(), s.roomServer)
mux.Handle(s.recServer.PathPrefix(), s.recServer)
mux.Handle(roomServer.PathPrefix(), roomServer)
mux.Handle(recServer.PathPrefix(), recServer)
mux.Handle("/rtc", rtcService)
mux.HandleFunc("/rtc/validate", rtcService.Validate)
mux.HandleFunc("/", s.healthCheck)
@@ -116,6 +117,7 @@ func (s *LivekitServer) Start() error {
if s.running.Get() {
return errors.New("already running")
}
s.doneChan = make(chan struct{})
if err := s.router.RegisterNode(); err != nil {
return err
@@ -130,7 +132,7 @@ func (s *LivekitServer) Start() error {
return err
}
s.doneChan = make(chan struct{})
s.recService.Start()
// ensure we could listen
ln, err := net.Listen("tcp", s.httpServer.Addr)
@@ -194,6 +196,7 @@ func (s *LivekitServer) Start() error {
}
s.roomManager.Stop()
s.recService.Stop()
close(s.closedChan)
return nil
+8 -2
View File
@@ -22,7 +22,7 @@ import (
var ServiceSet = wire.NewSet(
createRedisClient,
utils.NewRedisMessageBus,
createMessageBus,
createRouter,
createStore,
CreateKeyProvider,
@@ -37,7 +37,6 @@ var ServiceSet = wire.NewSet(
NewTurnServer,
config.GetAudioConfig,
wire.Bind(new(RoomManager), new(*LocalRoomManager)),
wire.Bind(new(livekit.RecordingService), new(*RecordingService)),
wire.Bind(new(livekit.RoomService), new(*RoomService)),
)
@@ -110,6 +109,13 @@ func createRedisClient(conf *config.Config) (*redis.Client, error) {
return rc, nil
}
func createMessageBus(rc *redis.Client) utils.MessageBus {
if rc == nil {
return nil
}
return utils.NewRedisMessageBus(rc)
}
func createRouter(rc *redis.Client, node routing.LocalNode) routing.Router {
if rc != nil {
return routing.NewRedisRouter(node, rc)
+3 -4
View File
@@ -8,7 +8,6 @@ package service
import (
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/routing"
"github.com/livekit/protocol/utils"
)
// Injectors from wire.go:
@@ -26,9 +25,7 @@ func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*Live
if err != nil {
return nil, err
}
messageBus := utils.NewRedisMessageBus(client)
recordingService := NewRecordingService(messageBus)
rtcService := NewRTCService(conf, roomAllocator, router, currentNode)
messageBus := createMessageBus(client)
keyProvider, err := CreateKeyProvider(conf)
if err != nil {
return nil, err
@@ -37,6 +34,8 @@ func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*Live
if err != nil {
return nil, err
}
recordingService := NewRecordingService(messageBus, notifier)
rtcService := NewRTCService(conf, roomAllocator, router, currentNode)
localRoomManager, err := NewLocalRoomManager(roomStore, router, currentNode, nodeSelector, notifier, conf)
if err != nil {
return nil, err