2021-02-05 09:39:48 +13:00
|
|
|
package capture
|
|
|
|
|
|
|
|
import (
|
2021-09-27 11:50:49 +13:00
|
|
|
"errors"
|
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-09-27 11:50:49 +13:00
|
|
|
logger zerolog.Logger
|
|
|
|
mu sync.Mutex
|
|
|
|
wg sync.WaitGroup
|
|
|
|
codec codec.RTPCodec
|
|
|
|
|
2021-02-15 02:40:17 +13:00
|
|
|
pipeline *gst.Pipeline
|
2021-09-27 11:50:49 +13:00
|
|
|
pipelineMu sync.Mutex
|
|
|
|
pipelineStr func() string
|
|
|
|
|
|
|
|
sample chan types.Sample
|
|
|
|
sampleStop chan interface{}
|
|
|
|
sampleUpdate chan interface{}
|
|
|
|
|
|
|
|
listeners map[uintptr]*func(sample types.Sample)
|
|
|
|
listenersMu sync.Mutex
|
2021-09-29 11:21:53 +13:00
|
|
|
listenersCount int
|
2021-02-05 09:39:48 +13:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2021-02-05 09:39:48 +13:00
|
|
|
manager := &StreamManagerCtx{
|
2021-09-27 11:50:49 +13:00
|
|
|
logger: logger,
|
|
|
|
codec: codec,
|
|
|
|
pipelineStr: pipelineStr,
|
|
|
|
sampleStop: make(chan interface{}),
|
|
|
|
sampleUpdate: make(chan interface{}),
|
|
|
|
listeners: map[uintptr]*func(sample types.Sample){},
|
2021-02-05 09:39:48 +13:00
|
|
|
}
|
|
|
|
|
2021-09-02 10:00:56 +12:00
|
|
|
manager.wg.Add(1)
|
|
|
|
|
2021-02-05 09:39:48 +13:00
|
|
|
go func() {
|
|
|
|
manager.logger.Debug().Msg("started emitting samples")
|
2021-09-02 10:00:56 +12:00
|
|
|
defer manager.wg.Done()
|
2021-02-05 09:39:48 +13:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2021-09-27 11:50:49 +13:00
|
|
|
case <-manager.sampleStop:
|
2021-02-05 09:39:48 +13:00
|
|
|
manager.logger.Debug().Msg("stopped emitting samples")
|
|
|
|
return
|
2021-09-27 11:50:49 +13:00
|
|
|
case <-manager.sampleUpdate:
|
2021-02-05 09:39:48 +13:00
|
|
|
manager.logger.Debug().Msg("update emitting samples")
|
|
|
|
case sample := <-manager.sample:
|
2021-09-27 11:50:49 +13:00
|
|
|
manager.listenersMu.Lock()
|
2021-02-07 00:52:02 +13:00
|
|
|
for _, emit := range manager.listeners {
|
2021-02-07 05:05:25 +13:00
|
|
|
(*emit)(sample)
|
2021-02-07 00:52:02 +13:00
|
|
|
}
|
2021-09-27 11:50:49 +13:00
|
|
|
manager.listenersMu.Unlock()
|
2021-02-05 09:39:48 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return manager
|
|
|
|
}
|
|
|
|
|
2021-02-06 00:18:46 +13:00
|
|
|
func (manager *StreamManagerCtx) shutdown() {
|
2021-09-02 10:00:56 +12:00
|
|
|
manager.logger.Info().Msgf("shutdown")
|
2021-02-05 09:39:48 +13:00
|
|
|
|
2021-09-27 11:50:49 +13:00
|
|
|
manager.listenersMu.Lock()
|
2021-03-01 06:56:04 +13:00
|
|
|
for key := range manager.listeners {
|
|
|
|
delete(manager.listeners, key)
|
|
|
|
}
|
2021-09-27 11:50:49 +13:00
|
|
|
manager.listenersMu.Unlock()
|
2021-03-01 06:56:04 +13:00
|
|
|
|
2021-02-05 09:39:48 +13:00
|
|
|
manager.destroyPipeline()
|
2021-09-02 10:00:56 +12:00
|
|
|
|
2021-09-27 11:50:49 +13:00
|
|
|
close(manager.sampleStop)
|
2021-09-02 10:00:56 +12:00
|
|
|
manager.wg.Wait()
|
2021-02-05 09:39:48 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *StreamManagerCtx) Codec() codec.RTPCodec {
|
|
|
|
return manager.codec
|
|
|
|
}
|
|
|
|
|
2021-09-27 11:50:49 +13:00
|
|
|
func (manager *StreamManagerCtx) NewListener(listener *func(sample types.Sample)) (addListener func(), err error) {
|
|
|
|
if listener == nil {
|
|
|
|
return addListener, errors.New("listener cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-09-27 12:17:25 +13:00
|
|
|
manager.listenersCount++
|
|
|
|
if manager.listenersCount == 1 {
|
2021-09-27 11:50:49 +13:00
|
|
|
err := manager.createPipeline()
|
|
|
|
if err != nil && !errors.Is(err, types.ErrCapturePipelineAlreadyExists) {
|
|
|
|
return addListener, err
|
|
|
|
}
|
2021-02-07 00:52:02 +13:00
|
|
|
|
2021-09-27 11:50:49 +13:00
|
|
|
manager.logger.Info().Msgf("first listener, starting")
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() {
|
2021-02-07 05:05:25 +13:00
|
|
|
ptr := reflect.ValueOf(listener).Pointer()
|
2021-09-27 11:50:49 +13:00
|
|
|
|
|
|
|
manager.listenersMu.Lock()
|
2021-02-07 05:05:25 +13:00
|
|
|
manager.listeners[ptr] = listener
|
2021-09-27 11:50:49 +13:00
|
|
|
manager.listenersMu.Unlock()
|
|
|
|
|
2021-08-29 06:15:54 +12:00
|
|
|
manager.logger.Debug().Interface("ptr", ptr).Msgf("adding listener")
|
2021-09-27 11:50:49 +13:00
|
|
|
}, nil
|
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-09-27 11:50:49 +13:00
|
|
|
if listener == nil {
|
|
|
|
return
|
2021-02-07 05:05:25 +13:00
|
|
|
}
|
2021-02-05 09:39:48 +13:00
|
|
|
|
2021-09-27 11:50:49 +13:00
|
|
|
ptr := reflect.ValueOf(listener).Pointer()
|
2021-02-07 06:16:24 +13:00
|
|
|
|
2021-09-27 11:50:49 +13:00
|
|
|
manager.listenersMu.Lock()
|
|
|
|
delete(manager.listeners, ptr)
|
|
|
|
manager.listenersMu.Unlock()
|
2021-02-07 06:16:24 +13:00
|
|
|
|
2021-09-27 11:50:49 +13:00
|
|
|
manager.logger.Debug().Interface("ptr", ptr).Msgf("removing listener")
|
2021-02-06 00:49:02 +13:00
|
|
|
|
2021-09-27 12:17:25 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
manager.listenersCount--
|
|
|
|
manager.mu.Unlock()
|
|
|
|
|
2021-09-27 11:50:49 +13:00
|
|
|
go func() {
|
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
2021-02-06 00:41:02 +13:00
|
|
|
|
2021-09-29 11:21:53 +13:00
|
|
|
if manager.listenersCount <= 0 {
|
2021-09-27 11:50:49 +13:00
|
|
|
manager.destroyPipeline()
|
|
|
|
manager.logger.Info().Msgf("last listener, stopping")
|
|
|
|
}
|
2021-09-29 11:21:53 +13:00
|
|
|
|
|
|
|
if manager.listenersCount < 0 {
|
|
|
|
manager.listenersCount = 0
|
|
|
|
manager.logger.Error().Int("listeners-count", manager.listenersCount).Msgf("listener counter is < 0, something is wrong")
|
|
|
|
}
|
2021-09-27 11:50:49 +13:00
|
|
|
}()
|
2021-02-05 09:39:48 +13:00
|
|
|
}
|
|
|
|
|
2021-09-27 11:50:49 +13:00
|
|
|
func (manager *StreamManagerCtx) ListenersCount() int {
|
|
|
|
manager.listenersMu.Lock()
|
|
|
|
defer manager.listenersMu.Unlock()
|
2021-02-06 00:49:02 +13:00
|
|
|
|
2021-09-27 11:50:49 +13:00
|
|
|
return len(manager.listeners)
|
2021-02-05 09:39:48 +13:00
|
|
|
}
|
|
|
|
|
2021-02-06 02:03:53 +13:00
|
|
|
func (manager *StreamManagerCtx) Started() bool {
|
2021-09-27 11:50:49 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
|
|
|
return manager.listenersCount > 0
|
2021-02-05 09:39:48 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *StreamManagerCtx) createPipeline() error {
|
2021-09-27 11:50:49 +13:00
|
|
|
manager.pipelineMu.Lock()
|
|
|
|
defer manager.pipelineMu.Unlock()
|
|
|
|
|
2021-02-06 00:49:02 +13:00
|
|
|
if manager.pipeline != nil {
|
2021-08-30 03:09:13 +12:00
|
|
|
return types.ErrCapturePipelineAlreadyExists
|
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-09-27 11:50:49 +13:00
|
|
|
manager.sampleUpdate <- struct{}{}
|
2021-02-05 09:39:48 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *StreamManagerCtx) destroyPipeline() {
|
2021-09-27 11:50:49 +13:00
|
|
|
manager.pipelineMu.Lock()
|
|
|
|
defer manager.pipelineMu.Unlock()
|
|
|
|
|
2021-02-05 09:39:48 +13:00
|
|
|
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
|
|
|
|
}
|