2021-01-23 06:13:32 +13:00
|
|
|
package capture
|
|
|
|
|
|
|
|
import (
|
2021-08-30 03:09:13 +12:00
|
|
|
"errors"
|
2021-01-24 03:17:52 +13:00
|
|
|
"sync"
|
2021-01-24 04:29:47 +13:00
|
|
|
"sync/atomic"
|
2021-02-15 02:40:17 +13:00
|
|
|
"time"
|
2021-01-23 06:13:32 +13:00
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
|
|
|
"demodesk/neko/internal/capture/gst"
|
2021-02-15 02:40:17 +13:00
|
|
|
"demodesk/neko/internal/types"
|
2021-01-23 06:13:32 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
type ScreencastManagerCtx struct {
|
2021-02-15 02:40:17 +13:00
|
|
|
logger zerolog.Logger
|
|
|
|
mu sync.Mutex
|
2021-09-02 10:00:56 +12:00
|
|
|
wg sync.WaitGroup
|
2021-02-15 02:40:17 +13:00
|
|
|
pipelineStr string
|
|
|
|
pipeline *gst.Pipeline
|
|
|
|
enabled bool
|
|
|
|
started bool
|
|
|
|
emitStop chan bool
|
|
|
|
emitUpdate chan bool
|
|
|
|
expired int32
|
|
|
|
sample chan types.Sample
|
|
|
|
image types.Sample
|
2021-01-23 06:13:32 +13:00
|
|
|
}
|
|
|
|
|
2021-09-03 07:37:24 +12:00
|
|
|
// timeout between intervals, when screencast pipeline is checked
|
2021-02-06 01:58:02 +13:00
|
|
|
const screencastTimeout = 5 * time.Second
|
2021-01-24 03:17:52 +13:00
|
|
|
|
2021-02-06 01:58:02 +13:00
|
|
|
func screencastNew(enabled bool, pipelineStr string) *ScreencastManagerCtx {
|
|
|
|
manager := &ScreencastManagerCtx{
|
2021-02-15 02:40:17 +13:00
|
|
|
logger: log.With().Str("module", "capture").Str("submodule", "screencast").Logger(),
|
|
|
|
pipelineStr: pipelineStr,
|
|
|
|
enabled: enabled,
|
|
|
|
started: false,
|
|
|
|
emitStop: make(chan bool),
|
|
|
|
emitUpdate: make(chan bool),
|
2021-01-23 06:13:32 +13:00
|
|
|
}
|
|
|
|
|
2021-09-02 10:00:56 +12:00
|
|
|
manager.wg.Add(1)
|
|
|
|
|
2021-01-23 06:13:32 +13:00
|
|
|
go func() {
|
2021-02-06 00:18:46 +13:00
|
|
|
manager.logger.Debug().Msg("started emitting samples")
|
2021-09-02 10:00:56 +12:00
|
|
|
defer manager.wg.Done()
|
2021-01-23 06:13:32 +13:00
|
|
|
|
2021-08-20 07:19:02 +12:00
|
|
|
ticker := time.NewTicker(screencastTimeout)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2021-01-23 06:13:32 +13:00
|
|
|
for {
|
|
|
|
select {
|
2021-02-06 00:18:46 +13:00
|
|
|
case <-manager.emitStop:
|
|
|
|
manager.logger.Debug().Msg("stopped emitting samples")
|
2021-01-23 06:13:32 +13:00
|
|
|
return
|
2021-02-06 00:18:46 +13:00
|
|
|
case <-manager.emitUpdate:
|
|
|
|
manager.logger.Debug().Msg("update emitting samples")
|
2021-01-23 06:13:32 +13:00
|
|
|
case sample := <-manager.sample:
|
2021-01-24 03:17:52 +13:00
|
|
|
manager.image = sample
|
|
|
|
case <-ticker.C:
|
2021-01-24 04:29:47 +13:00
|
|
|
if manager.started && !atomic.CompareAndSwapInt32(&manager.expired, 0, 1) {
|
2021-01-24 03:17:52 +13:00
|
|
|
manager.stop()
|
|
|
|
}
|
2021-01-23 06:13:32 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return manager
|
|
|
|
}
|
|
|
|
|
2021-02-06 00:18:46 +13:00
|
|
|
func (manager *ScreencastManagerCtx) shutdown() {
|
2021-09-02 10:00:56 +12:00
|
|
|
manager.logger.Info().Msgf("shutdown")
|
2021-02-06 00:18:46 +13:00
|
|
|
|
|
|
|
manager.destroyPipeline()
|
2021-09-02 10:00:56 +12:00
|
|
|
|
2021-02-06 00:18:46 +13:00
|
|
|
manager.emitStop <- true
|
2021-09-02 10:00:56 +12:00
|
|
|
manager.wg.Wait()
|
2021-02-06 00:18:46 +13:00
|
|
|
}
|
|
|
|
|
2021-01-24 03:17:52 +13:00
|
|
|
func (manager *ScreencastManagerCtx) Enabled() bool {
|
|
|
|
return manager.enabled
|
2021-01-23 06:13:32 +13:00
|
|
|
}
|
|
|
|
|
2021-01-24 03:17:52 +13:00
|
|
|
func (manager *ScreencastManagerCtx) Started() bool {
|
|
|
|
return manager.started
|
2021-01-23 06:13:32 +13:00
|
|
|
}
|
|
|
|
|
2021-01-24 03:17:52 +13:00
|
|
|
func (manager *ScreencastManagerCtx) Image() ([]byte, error) {
|
2021-01-24 04:29:47 +13:00
|
|
|
atomic.StoreInt32(&manager.expired, 0)
|
2021-01-24 03:17:52 +13:00
|
|
|
|
|
|
|
if !manager.started {
|
|
|
|
err := manager.start()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case sample := <-manager.sample:
|
|
|
|
return sample.Data, nil
|
|
|
|
case <-time.After(1 * time.Second):
|
2021-08-30 03:09:13 +12:00
|
|
|
return nil, errors.New("timeouted")
|
2021-01-24 03:17:52 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if manager.image.Data == nil {
|
2021-08-30 03:09:13 +12:00
|
|
|
return nil, errors.New("image sample not found")
|
2021-01-24 03:17:52 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
return manager.image.Data, nil
|
2021-01-23 06:13:32 +13:00
|
|
|
}
|
|
|
|
|
2021-01-24 03:17:52 +13:00
|
|
|
func (manager *ScreencastManagerCtx) start() error {
|
2021-02-06 00:49:02 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-01-24 03:17:52 +13:00
|
|
|
if !manager.enabled {
|
2021-08-30 03:09:13 +12:00
|
|
|
return errors.New("screenshot pipeline not enabled")
|
2021-01-24 03:17:52 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
err := manager.createPipeline()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.started = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *ScreencastManagerCtx) stop() {
|
2021-02-06 00:49:02 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-01-24 03:17:52 +13:00
|
|
|
manager.started = false
|
|
|
|
manager.destroyPipeline()
|
2021-01-23 06:13:32 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *ScreencastManagerCtx) createPipeline() error {
|
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-01-24 03:17:52 +13:00
|
|
|
|
2021-01-23 06:13:32 +13:00
|
|
|
var err error
|
|
|
|
|
|
|
|
manager.logger.Info().
|
2021-02-06 01:58:02 +13:00
|
|
|
Str("str", manager.pipelineStr).
|
2021-01-23 06:13:32 +13:00
|
|
|
Msgf("creating pipeline")
|
2021-02-03 06:28:32 +13:00
|
|
|
|
2021-02-06 01:58:02 +13:00
|
|
|
manager.pipeline, err = gst.CreatePipeline(manager.pipelineStr)
|
2021-01-23 06:13:32 +13:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-06 00:41:02 +13:00
|
|
|
manager.pipeline.Start()
|
2021-01-23 06:13:32 +13:00
|
|
|
manager.sample = manager.pipeline.Sample
|
2021-02-15 02:40:17 +13:00
|
|
|
manager.emitUpdate <- true
|
2021-01-23 06:13:32 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *ScreencastManagerCtx) destroyPipeline() {
|
|
|
|
if manager.pipeline == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.pipeline.Stop()
|
2021-02-06 01:58:02 +13:00
|
|
|
manager.logger.Info().Msgf("destroying pipeline")
|
2021-01-23 06:13:32 +13:00
|
|
|
manager.pipeline = nil
|
|
|
|
}
|