2022-09-17 22:43:17 +12:00
|
|
|
package capture
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2023-01-29 10:08:36 +13:00
|
|
|
"sync"
|
2022-09-17 22:43:17 +12:00
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
|
|
|
"m1k1o/neko/internal/capture/gst"
|
|
|
|
"m1k1o/neko/internal/types"
|
|
|
|
"m1k1o/neko/internal/types/codec"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StreamSinkManagerCtx struct {
|
|
|
|
logger zerolog.Logger
|
|
|
|
mu sync.Mutex
|
|
|
|
|
2023-01-30 05:40:07 +13:00
|
|
|
codec codec.RTPCodec
|
|
|
|
pipeline *gst.Pipeline
|
|
|
|
pipelineMu sync.Mutex
|
|
|
|
pipelineFn func() (string, error)
|
2022-09-17 22:43:17 +12:00
|
|
|
|
|
|
|
listeners int
|
|
|
|
listenersMu sync.Mutex
|
|
|
|
}
|
|
|
|
|
2022-09-22 04:58:28 +12:00
|
|
|
func streamSinkNew(codec codec.RTPCodec, pipelineFn func() (string, error), video_id string) *StreamSinkManagerCtx {
|
2022-09-17 22:43:17 +12:00
|
|
|
logger := log.With().
|
|
|
|
Str("module", "capture").
|
|
|
|
Str("submodule", "stream-sink").
|
|
|
|
Str("video_id", video_id).Logger()
|
|
|
|
|
|
|
|
manager := &StreamSinkManagerCtx{
|
2023-01-30 05:40:07 +13:00
|
|
|
logger: logger,
|
|
|
|
codec: codec,
|
|
|
|
pipelineFn: pipelineFn,
|
2022-09-17 22:43:17 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
return manager
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *StreamSinkManagerCtx) shutdown() {
|
|
|
|
manager.logger.Info().Msgf("shutdown")
|
|
|
|
|
|
|
|
manager.destroyPipeline()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *StreamSinkManagerCtx) Codec() codec.RTPCodec {
|
|
|
|
return manager.codec
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *StreamSinkManagerCtx) start() error {
|
|
|
|
if manager.listeners == 0 {
|
|
|
|
err := manager.createPipeline()
|
|
|
|
if err != nil && !errors.Is(err, types.ErrCapturePipelineAlreadyExists) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.logger.Info().Msgf("first listener, starting")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *StreamSinkManagerCtx) stop() {
|
|
|
|
if manager.listeners == 0 {
|
|
|
|
manager.destroyPipeline()
|
|
|
|
manager.logger.Info().Msgf("last listener, stopping")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *StreamSinkManagerCtx) addListener() {
|
|
|
|
manager.listenersMu.Lock()
|
|
|
|
manager.listeners++
|
|
|
|
manager.listenersMu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *StreamSinkManagerCtx) removeListener() {
|
|
|
|
manager.listenersMu.Lock()
|
|
|
|
manager.listeners--
|
|
|
|
manager.listenersMu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *StreamSinkManagerCtx) AddListener() error {
|
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
|
|
|
// start if stopped
|
|
|
|
if err := manager.start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// add listener
|
|
|
|
manager.addListener()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *StreamSinkManagerCtx) RemoveListener() error {
|
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
|
|
|
// remove listener
|
|
|
|
manager.removeListener()
|
|
|
|
|
|
|
|
// stop if started
|
|
|
|
manager.stop()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *StreamSinkManagerCtx) ListenersCount() int {
|
|
|
|
manager.listenersMu.Lock()
|
|
|
|
defer manager.listenersMu.Unlock()
|
|
|
|
|
|
|
|
return manager.listeners
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *StreamSinkManagerCtx) Started() bool {
|
|
|
|
return manager.ListenersCount() > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *StreamSinkManagerCtx) createPipeline() error {
|
|
|
|
manager.pipelineMu.Lock()
|
|
|
|
defer manager.pipelineMu.Unlock()
|
|
|
|
|
|
|
|
if manager.pipeline != nil {
|
|
|
|
return types.ErrCapturePipelineAlreadyExists
|
|
|
|
}
|
|
|
|
|
2022-09-22 04:58:28 +12:00
|
|
|
pipelineStr, err := manager.pipelineFn()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-17 22:43:17 +12:00
|
|
|
|
|
|
|
manager.logger.Info().
|
|
|
|
Str("codec", manager.codec.Name).
|
2022-09-22 04:58:28 +12:00
|
|
|
Str("src", pipelineStr).
|
2022-09-17 22:43:17 +12:00
|
|
|
Msgf("creating pipeline")
|
|
|
|
|
2022-09-22 04:58:28 +12:00
|
|
|
manager.pipeline, err = gst.CreatePipeline(pipelineStr)
|
2022-09-17 22:43:17 +12:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-22 11:43:04 +13:00
|
|
|
appsinkSubfix := "audio"
|
2023-01-30 06:22:48 +13:00
|
|
|
if manager.codec.IsVideo() {
|
2023-01-22 11:43:04 +13:00
|
|
|
appsinkSubfix = "video"
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.pipeline.AttachAppsink("appsink" + appsinkSubfix)
|
2022-09-17 22:43:17 +12:00
|
|
|
manager.pipeline.Play()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *StreamSinkManagerCtx) destroyPipeline() {
|
|
|
|
manager.pipelineMu.Lock()
|
|
|
|
defer manager.pipelineMu.Unlock()
|
|
|
|
|
|
|
|
if manager.pipeline == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.pipeline.Destroy()
|
|
|
|
manager.logger.Info().Msgf("destroying pipeline")
|
|
|
|
manager.pipeline = nil
|
|
|
|
}
|
2023-01-22 11:43:04 +13:00
|
|
|
|
2023-01-29 10:08:36 +13:00
|
|
|
func (manager *StreamSinkManagerCtx) GetSampleChannel() chan types.Sample {
|
2023-01-25 13:11:42 +13:00
|
|
|
if manager.pipeline != nil {
|
|
|
|
return manager.pipeline.Sample
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-01-22 11:43:04 +13:00
|
|
|
}
|