neko/internal/capture/stream.go

154 lines
3.2 KiB
Go
Raw Normal View History

package capture
import (
2021-02-06 00:49:02 +13:00
"fmt"
"sync"
"reflect"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"demodesk/neko/internal/types"
"demodesk/neko/internal/types/codec"
"demodesk/neko/internal/capture/gst"
)
type StreamManagerCtx struct {
2021-02-06 01:58:02 +13:00
logger zerolog.Logger
mu sync.Mutex
codec codec.RTPCodec
pipelineStr string
pipeline *gst.Pipeline
sample chan types.Sample
2021-02-07 05:05:25 +13:00
listeners map[uintptr]*func(sample types.Sample)
emitMu sync.Mutex
2021-02-06 01:58:02 +13:00
emitUpdate chan bool
emitStop chan bool
2021-02-06 02:03:53 +13:00
started bool
}
2021-02-06 01:58:02 +13:00
func streamNew(codec codec.RTPCodec, pipelineStr string) *StreamManagerCtx {
manager := &StreamManagerCtx{
2021-02-06 01:58:02 +13:00
logger: log.With().Str("module", "capture").Str("submodule", "stream").Logger(),
codec: codec,
pipelineStr: pipelineStr,
2021-02-07 05:05:25 +13:00
listeners: map[uintptr]*func(sample types.Sample){},
2021-02-06 01:58:02 +13:00
emitUpdate: make(chan bool),
emitStop: make(chan bool),
2021-02-06 02:03:53 +13:00
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.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-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-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-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-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()
manager.logger.Info().
Str("codec", codec.Name).
2021-02-06 01:58:02 +13:00
Str("src", manager.pipelineStr).
Msgf("creating pipeline")
2021-02-06 01:58:02 +13:00
manager.pipeline, err = gst.CreatePipeline(manager.pipelineStr)
if err != nil {
return err
}
manager.pipeline.Start()
manager.sample = manager.pipeline.Sample
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
}