2021-02-05 09:39:48 +13:00
|
|
|
package capture
|
|
|
|
|
|
|
|
import (
|
2021-02-06 00:49:02 +13:00
|
|
|
"fmt"
|
2021-02-07 00:52:02 +13:00
|
|
|
"reflect"
|
2021-02-15 02:40:17 +13:00
|
|
|
"sync"
|
2021-02-05 09:39:48 +13:00
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2021-02-15 02:40:17 +13:00
|
|
|
"demodesk/neko/internal/capture/gst"
|
2021-02-05 09:39:48 +13:00
|
|
|
"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-02-05 09:39:48 +13:00
|
|
|
}
|
|
|
|
|
2021-02-10 08:36:22 +13:00
|
|
|
func streamNew(codec codec.RTPCodec, pipelineStr func() string) *StreamManagerCtx {
|
2021-02-05 09:39:48 +13:00
|
|
|
manager := &StreamManagerCtx{
|
2021-02-15 02:40:17 +13:00
|
|
|
logger: log.With().Str("module", "capture").Str("submodule", "stream").Logger(),
|
|
|
|
codec: codec,
|
|
|
|
pipelineStr: pipelineStr,
|
|
|
|
listeners: map[uintptr]*func(sample types.Sample){},
|
|
|
|
emitUpdate: make(chan bool),
|
|
|
|
emitStop: make(chan bool),
|
|
|
|
started: false,
|
2021-02-05 09:39:48 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2021-02-07 00:52:02 +13:00
|
|
|
manager.emitMu.Lock()
|
|
|
|
for _, emit := range manager.listeners {
|
2021-02-07 05:05:25 +13:00
|
|
|
(*emit)(sample)
|
2021-02-07 00:52:02 +13:00
|
|
|
}
|
|
|
|
manager.emitMu.Unlock()
|
2021-02-05 09:39:48 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return manager
|
|
|
|
}
|
|
|
|
|
2021-02-06 00:18:46 +13:00
|
|
|
func (manager *StreamManagerCtx) shutdown() {
|
2021-02-05 09:39:48 +13:00
|
|
|
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)) {
|
2021-02-07 00:52:02 +13:00
|
|
|
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 00:52:02 +13:00
|
|
|
}
|
|
|
|
|
2021-02-07 05:05:25 +13:00
|
|
|
func (manager *StreamManagerCtx) RemoveListener(listener *func(sample types.Sample)) {
|
2021-02-07 00:52:02 +13:00
|
|
|
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-05 09:39:48 +13:00
|
|
|
}
|
|
|
|
|
2021-02-07 06:16:24 +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-02-06 02:03:53 +13:00
|
|
|
manager.started = true
|
2021-02-06 00:41:02 +13:00
|
|
|
return nil
|
2021-02-05 09:39:48 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2021-02-05 09:39:48 +13:00
|
|
|
manager.destroyPipeline()
|
|
|
|
}
|
|
|
|
|
2021-02-06 02:03:53 +13:00
|
|
|
func (manager *StreamManagerCtx) Started() bool {
|
|
|
|
return manager.started
|
2021-02-05 09:39:48 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-02-05 09:39:48 +13:00
|
|
|
var err error
|
|
|
|
|
|
|
|
codec := manager.Codec()
|
2021-02-10 08:36:22 +13:00
|
|
|
pipelineStr := manager.pipelineStr()
|
2021-02-05 09:39:48 +13:00
|
|
|
manager.logger.Info().
|
|
|
|
Str("codec", codec.Name).
|
2021-02-10 08:36:22 +13:00
|
|
|
Str("src", pipelineStr).
|
2021-02-05 09:39:48 +13:00
|
|
|
Msgf("creating pipeline")
|
|
|
|
|
2021-02-10 08:36:22 +13:00
|
|
|
manager.pipeline, err = gst.CreatePipeline(pipelineStr)
|
2021-02-05 09:39:48 +13:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.pipeline.Start()
|
|
|
|
|
|
|
|
manager.sample = manager.pipeline.Sample
|
2021-02-15 02:40:17 +13:00
|
|
|
manager.emitUpdate <- true
|
2021-02-05 09:39:48 +13:00
|
|
|
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")
|
2021-02-05 09:39:48 +13:00
|
|
|
manager.pipeline = nil
|
|
|
|
}
|