neko/internal/capture/streamsink.go

258 lines
5.7 KiB
Go
Raw Normal View History

package capture
import (
2021-09-27 11:50:49 +13:00
"errors"
"reflect"
2021-02-15 02:40:17 +13:00
"sync"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"gitlab.com/demodesk/neko/server/internal/capture/gst"
"gitlab.com/demodesk/neko/server/internal/types"
"gitlab.com/demodesk/neko/server/internal/types/codec"
)
2021-12-02 08:30:18 +13:00
var moveSinkListenerMu = sync.Mutex{}
2021-12-02 08:30:18 +13:00
type StreamSinkManagerCtx struct {
2021-09-27 11:50:49 +13:00
logger zerolog.Logger
mu sync.Mutex
wg sync.WaitGroup
codec codec.RTPCodec
2021-02-15 02:40:17 +13:00
pipeline *gst.Pipeline
2021-09-27 11:50:49 +13:00
pipelineMu sync.Mutex
pipelineStr func() string
2021-10-02 00:46:10 +13:00
listeners map[uintptr]*func(sample types.Sample)
listenersMu sync.Mutex
}
2021-12-02 08:30:18 +13:00
func streamSinkNew(codec codec.RTPCodec, pipelineStr func() string, video_id string) *StreamSinkManagerCtx {
2021-08-29 06:15:54 +12:00
logger := log.With().
Str("module", "capture").
2021-12-02 08:30:18 +13:00
Str("submodule", "stream-sink").
2021-08-29 06:15:54 +12:00
Str("video_id", video_id).Logger()
2021-12-02 08:30:18 +13:00
manager := &StreamSinkManagerCtx{
2022-01-23 05:58:05 +13:00
logger: logger,
codec: codec,
pipelineStr: pipelineStr,
listeners: map[uintptr]*func(sample types.Sample){},
}
return manager
}
2021-12-02 08:30:18 +13:00
func (manager *StreamSinkManagerCtx) shutdown() {
2021-09-02 10:00:56 +12:00
manager.logger.Info().Msgf("shutdown")
2021-09-27 11:50:49 +13:00
manager.listenersMu.Lock()
for key := range manager.listeners {
delete(manager.listeners, key)
}
2021-09-27 11:50:49 +13:00
manager.listenersMu.Unlock()
manager.destroyPipeline()
2021-09-02 10:00:56 +12:00
manager.wg.Wait()
}
2021-12-02 08:30:18 +13:00
func (manager *StreamSinkManagerCtx) Codec() codec.RTPCodec {
return manager.codec
}
2021-12-02 08:30:18 +13:00
func (manager *StreamSinkManagerCtx) start() error {
2021-10-02 00:46:10 +13:00
if len(manager.listeners) == 0 {
2021-09-27 11:50:49 +13:00
err := manager.createPipeline()
if err != nil && !errors.Is(err, types.ErrCapturePipelineAlreadyExists) {
2021-10-02 00:46:10 +13:00
return err
2021-09-27 11:50:49 +13:00
}
2021-09-27 11:50:49 +13:00
manager.logger.Info().Msgf("first listener, starting")
}
2021-10-02 00:46:10 +13:00
return nil
}
2021-12-02 08:30:18 +13:00
func (manager *StreamSinkManagerCtx) stop() {
2021-10-02 00:46:10 +13:00
if len(manager.listeners) == 0 {
manager.destroyPipeline()
manager.logger.Info().Msgf("last listener, stopping")
}
}
2021-09-27 11:50:49 +13:00
2021-12-02 08:30:18 +13:00
func (manager *StreamSinkManagerCtx) addListener(listener *func(sample types.Sample)) {
2021-10-02 00:46:10 +13:00
ptr := reflect.ValueOf(listener).Pointer()
2021-09-27 11:50:49 +13:00
2021-10-02 00:46:10 +13:00
manager.listenersMu.Lock()
manager.listeners[ptr] = listener
manager.listenersMu.Unlock()
2021-10-02 00:46:10 +13:00
manager.logger.Debug().Interface("ptr", ptr).Msgf("adding listener")
}
2021-12-02 08:30:18 +13:00
func (manager *StreamSinkManagerCtx) removeListener(listener *func(sample types.Sample)) {
2021-09-27 11:50:49 +13:00
ptr := reflect.ValueOf(listener).Pointer()
2021-09-27 11:50:49 +13:00
manager.listenersMu.Lock()
delete(manager.listeners, ptr)
manager.listenersMu.Unlock()
2021-09-27 11:50:49 +13:00
manager.logger.Debug().Interface("ptr", ptr).Msgf("removing listener")
2021-10-02 00:46:10 +13:00
}
2021-02-06 00:49:02 +13:00
2021-12-02 08:30:18 +13:00
func (manager *StreamSinkManagerCtx) AddListener(listener *func(sample types.Sample)) error {
2021-09-27 12:17:25 +13:00
manager.mu.Lock()
2021-10-02 00:46:10 +13:00
defer manager.mu.Unlock()
2021-09-27 12:17:25 +13:00
2021-10-02 00:46:10 +13:00
if listener == nil {
return errors.New("listener cannot be nil")
}
2021-09-29 12:03:39 +13:00
2021-10-02 00:46:10 +13:00
// start if stopped
if err := manager.start(); err != nil {
return err
}
2021-02-06 00:41:02 +13:00
2021-10-02 00:46:10 +13:00
// add listener
manager.addListener(listener)
2021-09-29 11:21:53 +13:00
2021-10-02 00:46:10 +13:00
return nil
}
2021-12-02 08:30:18 +13:00
func (manager *StreamSinkManagerCtx) RemoveListener(listener *func(sample types.Sample)) error {
2021-10-02 00:46:10 +13:00
manager.mu.Lock()
defer manager.mu.Unlock()
if listener == nil {
return errors.New("listener cannot be nil")
}
// remove listener
manager.removeListener(listener)
// stop if started
manager.stop()
return nil
}
// moving listeners between streams ensures, that target pipeline is running
// before listener is added, and stops source pipeline if there are 0 listeners
2021-12-02 08:30:18 +13:00
func (manager *StreamSinkManagerCtx) MoveListenerTo(listener *func(sample types.Sample), stream types.StreamSinkManager) error {
if listener == nil {
return errors.New("listener cannot be nil")
}
2021-10-02 00:46:10 +13:00
2021-12-02 08:30:18 +13:00
targetStream, ok := stream.(*StreamSinkManagerCtx)
2021-10-02 00:46:10 +13:00
if !ok {
return errors.New("target stream manager does not support moving listeners")
2021-10-02 00:46:10 +13:00
}
// we need to acquire both mutextes, from source stream and from target stream
// in order to do that safely (without possibility of deadlock) we need third
// global mutex, that ensures atomic locking
// lock global mutex
2021-12-02 08:30:18 +13:00
moveSinkListenerMu.Lock()
// lock source stream
manager.mu.Lock()
defer manager.mu.Unlock()
// lock target stream
targetStream.mu.Lock()
defer targetStream.mu.Unlock()
// unlock global mutex
2021-12-02 08:30:18 +13:00
moveSinkListenerMu.Unlock()
2021-09-29 12:03:39 +13:00
2021-10-02 00:46:10 +13:00
// start if stopped
if err := targetStream.start(); err != nil {
return err
}
// swap listeners
manager.removeListener(listener)
targetStream.addListener(listener)
// stop if started
manager.stop()
return nil
}
2021-12-02 08:30:18 +13:00
func (manager *StreamSinkManagerCtx) ListenersCount() int {
2021-09-27 11:50:49 +13:00
manager.listenersMu.Lock()
defer manager.listenersMu.Unlock()
2021-02-06 00:49:02 +13:00
2021-09-27 11:50:49 +13:00
return len(manager.listeners)
}
2021-12-02 08:30:18 +13:00
func (manager *StreamSinkManagerCtx) Started() bool {
2021-10-02 00:46:10 +13:00
return manager.ListenersCount() > 0
}
2021-12-02 08:30:18 +13:00
func (manager *StreamSinkManagerCtx) createPipeline() error {
2021-09-27 11:50:49 +13:00
manager.pipelineMu.Lock()
defer manager.pipelineMu.Unlock()
2021-02-06 00:49:02 +13:00
if manager.pipeline != nil {
2021-08-30 03:09:13 +12:00
return types.ErrCapturePipelineAlreadyExists
2021-02-06 00:49:02 +13:00
}
var err error
2021-02-10 08:36:22 +13:00
pipelineStr := manager.pipelineStr()
manager.logger.Info().
2021-12-02 08:30:18 +13:00
Str("codec", manager.codec.Name).
2021-02-10 08:36:22 +13:00
Str("src", pipelineStr).
Msgf("creating pipeline")
2021-02-10 08:36:22 +13:00
manager.pipeline, err = gst.CreatePipeline(pipelineStr)
if err != nil {
return err
}
2021-11-29 10:19:06 +13:00
manager.pipeline.AttachAppsink("appsink")
manager.pipeline.Play()
2022-01-23 05:58:05 +13:00
manager.wg.Add(1)
2022-02-15 06:41:47 +13:00
pipeline := manager.pipeline
2022-01-23 05:58:05 +13:00
go func() {
manager.logger.Debug().Msg("started emitting samples")
defer manager.wg.Done()
for {
2022-02-15 06:41:47 +13:00
sample, ok := <-pipeline.Sample
2022-01-23 05:58:05 +13:00
if !ok {
manager.logger.Debug().Msg("stopped emitting samples")
return
}
manager.listenersMu.Lock()
for _, emit := range manager.listeners {
(*emit)(sample)
}
manager.listenersMu.Unlock()
}
}()
return nil
}
2021-12-02 08:30:18 +13:00
func (manager *StreamSinkManagerCtx) destroyPipeline() {
2021-09-27 11:50:49 +13:00
manager.pipelineMu.Lock()
defer manager.pipelineMu.Unlock()
if manager.pipeline == nil {
return
}
2021-12-06 06:16:26 +13:00
manager.pipeline.Destroy()
2021-02-06 01:58:02 +13:00
manager.logger.Info().Msgf("destroying pipeline")
manager.pipeline = nil
}