2021-02-05 09:39:48 +13:00
|
|
|
package capture
|
|
|
|
|
|
|
|
import (
|
2021-02-06 00:49:02 +13:00
|
|
|
"fmt"
|
2021-02-05 09:39:48 +13:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/kataras/go-events"
|
|
|
|
"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 {
|
|
|
|
logger zerolog.Logger
|
|
|
|
mu sync.Mutex
|
|
|
|
codec codec.RTPCodec
|
|
|
|
pipelineDevice string
|
|
|
|
pipelineSrc string
|
|
|
|
pipeline *gst.Pipeline
|
|
|
|
sample chan types.Sample
|
|
|
|
emmiter events.EventEmmiter
|
|
|
|
emitUpdate chan bool
|
|
|
|
emitStop chan bool
|
|
|
|
enabled bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func streamNew(codec codec.RTPCodec, pipelineDevice string, pipelineSrc string) *StreamManagerCtx {
|
|
|
|
manager := &StreamManagerCtx{
|
|
|
|
logger: log.With().Str("module", "capture").Str("submodule", "stream").Logger(),
|
|
|
|
mu: sync.Mutex{},
|
|
|
|
codec: codec,
|
|
|
|
pipelineDevice: pipelineDevice,
|
|
|
|
pipelineSrc: pipelineSrc,
|
|
|
|
emmiter: events.New(),
|
|
|
|
emitUpdate: make(chan bool),
|
|
|
|
emitStop: make(chan bool),
|
|
|
|
enabled: 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.emmiter.Emit("sample", sample)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *StreamManagerCtx) OnSample(listener func(sample types.Sample)) {
|
|
|
|
manager.emmiter.On("sample", func(payload ...interface{}) {
|
|
|
|
listener(payload[0].(types.Sample))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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-05 09:39:48 +13:00
|
|
|
manager.enabled = 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-05 09:39:48 +13:00
|
|
|
manager.enabled = false
|
|
|
|
manager.destroyPipeline()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *StreamManagerCtx) Enabled() bool {
|
|
|
|
return manager.enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *StreamManagerCtx) createPipeline() error {
|
2021-02-06 00:49:02 +13:00
|
|
|
if manager.pipeline != nil {
|
|
|
|
return fmt.Errorf("pipeline already running")
|
|
|
|
}
|
|
|
|
|
2021-02-05 09:39:48 +13:00
|
|
|
var err error
|
|
|
|
|
|
|
|
codec := manager.Codec()
|
|
|
|
manager.logger.Info().
|
|
|
|
Str("codec", codec.Name).
|
|
|
|
Str("device", manager.pipelineDevice).
|
|
|
|
Str("src", manager.pipelineSrc).
|
|
|
|
Msgf("creating pipeline")
|
|
|
|
|
|
|
|
manager.pipeline, err = gst.CreateAppPipeline(codec, manager.pipelineDevice, manager.pipelineSrc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.logger.Info().
|
|
|
|
Str("src", manager.pipeline.Src).
|
|
|
|
Msgf("starting pipeline")
|
|
|
|
|
|
|
|
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()
|
|
|
|
manager.logger.Info().Msgf("stopping pipeline")
|
|
|
|
manager.pipeline = nil
|
|
|
|
}
|