neko/internal/capture/stream.go

177 lines
3.8 KiB
Go
Raw Normal View History

package capture
import (
2021-02-06 00:49:02 +13:00
"fmt"
"reflect"
2021-02-15 02:40:17 +13:00
"sync"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
2021-02-15 02:40:17 +13:00
"demodesk/neko/internal/capture/gst"
"demodesk/neko/internal/types"
"demodesk/neko/internal/types/codec"
)
type StreamManagerCtx struct {
2021-02-15 02:40:17 +13:00
logger zerolog.Logger
mu sync.Mutex
codec codec.RTPCodec
pipelineStr func() string
pipeline *gst.Pipeline
sample chan types.Sample
listeners map[uintptr]*func(sample types.Sample)
emitMu sync.Mutex
emitUpdate chan bool
emitStop chan bool
started bool
}
2021-08-29 06:15:54 +12:00
func streamNew(codec codec.RTPCodec, pipelineStr func() string, video_id string) *StreamManagerCtx {
logger := log.With().
Str("module", "capture").
Str("submodule", "stream").
Str("video_id", video_id).Logger()
manager := &StreamManagerCtx{
2021-08-29 06:15:54 +12:00
logger: logger,
2021-02-15 02:40:17 +13:00
codec: codec,
pipelineStr: pipelineStr,
listeners: map[uintptr]*func(sample types.Sample){},
emitUpdate: make(chan bool),
emitStop: make(chan bool),
started: false,
}
go func() {
manager.logger.Debug().Msg("started emitting samples")
for {
select {
case <-manager.emitStop:
manager.logger.Debug().Msg("stopped emitting samples")
return
case <-manager.emitUpdate:
manager.logger.Debug().Msg("update emitting samples")
case sample := <-manager.sample:
manager.emitMu.Lock()
for _, emit := range manager.listeners {
2021-02-07 05:05:25 +13:00
(*emit)(sample)
}
manager.emitMu.Unlock()
}
}
}()
return manager
}
2021-02-06 00:18:46 +13:00
func (manager *StreamManagerCtx) shutdown() {
manager.logger.Info().Msgf("shutting down")
manager.emitMu.Lock()
for key := range manager.listeners {
delete(manager.listeners, key)
}
manager.emitMu.Unlock()
manager.destroyPipeline()
manager.emitStop <- true
}
func (manager *StreamManagerCtx) Codec() codec.RTPCodec {
return manager.codec
}
2021-02-07 05:05:25 +13:00
func (manager *StreamManagerCtx) AddListener(listener *func(sample types.Sample)) {
manager.emitMu.Lock()
defer manager.emitMu.Unlock()
2021-02-07 05:05:25 +13:00
if listener != nil {
ptr := reflect.ValueOf(listener).Pointer()
manager.listeners[ptr] = listener
2021-08-29 06:15:54 +12:00
manager.logger.Debug().Interface("ptr", ptr).Msgf("adding listener")
2021-02-07 05:05:25 +13:00
}
}
2021-02-07 05:05:25 +13:00
func (manager *StreamManagerCtx) RemoveListener(listener *func(sample types.Sample)) {
manager.emitMu.Lock()
defer manager.emitMu.Unlock()
2021-02-07 05:05:25 +13:00
if listener != nil {
ptr := reflect.ValueOf(listener).Pointer()
delete(manager.listeners, ptr)
2021-08-29 06:15:54 +12:00
manager.logger.Debug().Interface("ptr", ptr).Msgf("removing listener")
2021-02-07 05:05:25 +13:00
}
}
func (manager *StreamManagerCtx) ListenersCount() int {
manager.emitMu.Lock()
defer manager.emitMu.Unlock()
return len(manager.listeners)
}
2021-02-06 00:41:02 +13:00
func (manager *StreamManagerCtx) Start() error {
2021-02-06 00:49:02 +13:00
manager.mu.Lock()
defer manager.mu.Unlock()
2021-02-06 00:41:02 +13:00
err := manager.createPipeline()
if err != nil {
return err
}
2021-08-29 06:15:54 +12:00
manager.logger.Info().Msgf("start")
2021-02-06 02:03:53 +13:00
manager.started = true
2021-02-06 00:41:02 +13:00
return nil
}
func (manager *StreamManagerCtx) Stop() {
2021-02-06 00:49:02 +13:00
manager.mu.Lock()
defer manager.mu.Unlock()
2021-08-29 06:15:54 +12:00
manager.logger.Info().Msgf("stop")
2021-02-06 02:03:53 +13:00
manager.started = false
manager.destroyPipeline()
}
2021-02-06 02:03:53 +13:00
func (manager *StreamManagerCtx) Started() bool {
return manager.started
}
func (manager *StreamManagerCtx) createPipeline() error {
2021-02-06 00:49:02 +13:00
if manager.pipeline != nil {
2021-02-06 01:58:02 +13:00
return fmt.Errorf("pipeline already exists")
2021-02-06 00:49:02 +13:00
}
var err error
codec := manager.Codec()
2021-02-10 08:36:22 +13:00
pipelineStr := manager.pipelineStr()
manager.logger.Info().
Str("codec", 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
}
manager.pipeline.Start()
manager.sample = manager.pipeline.Sample
2021-02-15 02:40:17 +13:00
manager.emitUpdate <- true
return nil
}
func (manager *StreamManagerCtx) destroyPipeline() {
if manager.pipeline == nil {
return
}
manager.pipeline.Stop()
2021-02-06 01:58:02 +13:00
manager.logger.Info().Msgf("destroying pipeline")
manager.pipeline = nil
}