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 {
|
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
|
|
|
|
emmiter events.EventEmmiter
|
|
|
|
emitUpdate chan bool
|
|
|
|
emitStop chan bool
|
2021-02-06 02:03:53 +13:00
|
|
|
started bool
|
2021-02-05 09:39:48 +13:00
|
|
|
}
|
|
|
|
|
2021-02-06 01:58:02 +13:00
|
|
|
func streamNew(codec codec.RTPCodec, pipelineStr string) *StreamManagerCtx {
|
2021-02-05 09:39:48 +13:00
|
|
|
manager := &StreamManagerCtx{
|
2021-02-06 01:58:02 +13:00
|
|
|
logger: log.With().Str("module", "capture").Str("submodule", "stream").Logger(),
|
|
|
|
codec: codec,
|
|
|
|
pipelineStr: pipelineStr,
|
|
|
|
emmiter: events.New(),
|
|
|
|
emitUpdate: make(chan bool),
|
|
|
|
emitStop: make(chan bool),
|
2021-02-06 02:03:53 +13:00
|
|
|
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:
|
|
|
|
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-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()
|
|
|
|
manager.logger.Info().
|
|
|
|
Str("codec", codec.Name).
|
2021-02-06 01:58:02 +13:00
|
|
|
Str("src", manager.pipelineStr).
|
2021-02-05 09:39:48 +13:00
|
|
|
Msgf("creating pipeline")
|
|
|
|
|
2021-02-06 01:58:02 +13:00
|
|
|
manager.pipeline, err = gst.CreatePipeline(manager.pipelineStr)
|
2021-02-05 09:39:48 +13:00
|
|
|
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")
|
2021-02-05 09:39:48 +13:00
|
|
|
manager.pipeline = nil
|
|
|
|
}
|