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
|
|
|
)
|
|
|
|
|
2021-10-01 07:01:26 +13:00
|
|
|
// timeout between intervals, when screencast pipeline is checked
|
|
|
|
const screencastTimeout = 5 * time.Second
|
|
|
|
|
2021-01-23 06:13:32 +13:00
|
|
|
type ScreencastManagerCtx struct {
|
2021-10-01 07:01:26 +13:00
|
|
|
logger zerolog.Logger
|
|
|
|
mu sync.Mutex
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
2021-02-15 02:40:17 +13:00
|
|
|
pipeline *gst.Pipeline
|
2021-10-01 07:01:26 +13:00
|
|
|
pipelineStr string
|
|
|
|
pipelineMu sync.Mutex
|
2021-01-23 06:13:32 +13:00
|
|
|
|
2021-10-01 07:01:26 +13:00
|
|
|
image types.Sample
|
|
|
|
sample chan types.Sample
|
2021-10-05 01:40:20 +13:00
|
|
|
sampleStop chan struct{}
|
|
|
|
sampleUpdate chan struct{}
|
2021-10-01 07:01:26 +13:00
|
|
|
|
|
|
|
enabled bool
|
|
|
|
started bool
|
|
|
|
expired int32
|
|
|
|
}
|
2021-01-24 03:17:52 +13:00
|
|
|
|
2021-02-06 01:58:02 +13:00
|
|
|
func screencastNew(enabled bool, pipelineStr string) *ScreencastManagerCtx {
|
2021-10-01 07:01:26 +13:00
|
|
|
logger := log.With().
|
|
|
|
Str("module", "capture").
|
|
|
|
Str("submodule", "screencast").
|
|
|
|
Logger()
|
|
|
|
|
2021-02-06 01:58:02 +13:00
|
|
|
manager := &ScreencastManagerCtx{
|
2021-10-01 07:01:26 +13:00
|
|
|
logger: logger,
|
|
|
|
pipelineStr: pipelineStr,
|
2021-10-05 01:40:20 +13:00
|
|
|
sampleStop: make(chan struct{}),
|
|
|
|
sampleUpdate: make(chan struct{}),
|
2021-10-01 07:01:26 +13:00
|
|
|
enabled: enabled,
|
|
|
|
started: false,
|
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-10-01 07:01:26 +13:00
|
|
|
case <-manager.sampleStop:
|
2021-02-06 00:18:46 +13:00
|
|
|
manager.logger.Debug().Msg("stopped emitting samples")
|
2021-01-23 06:13:32 +13:00
|
|
|
return
|
2021-10-01 07:01:26 +13:00
|
|
|
case <-manager.sampleUpdate:
|
2021-02-06 00:18:46 +13:00
|
|
|
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-10-01 07:01:26 +13:00
|
|
|
close(manager.sampleStop)
|
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 {
|
2021-10-01 07:01:26 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-01-24 03:17:52 +13:00
|
|
|
return manager.enabled
|
2021-01-23 06:13:32 +13:00
|
|
|
}
|
|
|
|
|
2021-01-24 03:17:52 +13:00
|
|
|
func (manager *ScreencastManagerCtx) Started() bool {
|
2021-10-01 07:01:26 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-01-24 03:17:52 +13:00
|
|
|
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-10-01 07:01:26 +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-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-11-29 10:19:06 +13:00
|
|
|
manager.pipeline.AttachAppsink("appsink")
|
|
|
|
manager.pipeline.Play()
|
2021-10-01 07:01:26 +13:00
|
|
|
|
2021-01-23 06:13:32 +13:00
|
|
|
manager.sample = manager.pipeline.Sample
|
2021-10-01 07:01:26 +13:00
|
|
|
manager.sampleUpdate <- struct{}{}
|
2021-01-23 06:13:32 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *ScreencastManagerCtx) destroyPipeline() {
|
2021-10-01 07:01:26 +13:00
|
|
|
manager.pipelineMu.Lock()
|
|
|
|
defer manager.pipelineMu.Unlock()
|
|
|
|
|
2021-01-23 06:13:32 +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-01-23 06:13:32 +13:00
|
|
|
manager.pipeline = nil
|
|
|
|
}
|