neko/internal/capture/screencast.go

162 lines
3.3 KiB
Go
Raw Normal View History

2021-01-23 06:13:32 +13:00
package capture
import (
2021-01-24 03:17:52 +13:00
"fmt"
"time"
"sync"
2021-01-24 04:29:47 +13:00
"sync/atomic"
2021-01-23 06:13:32 +13:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"demodesk/neko/internal/types"
"demodesk/neko/internal/capture/gst"
)
type ScreencastManagerCtx struct {
2021-02-06 01:58:02 +13:00
logger zerolog.Logger
mu sync.Mutex
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-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{
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
}
go func() {
2021-02-06 01:58:02 +13:00
ticker := time.NewTicker(screencastTimeout)
2021-02-06 00:18:46 +13:00
manager.logger.Debug().Msg("started emitting samples")
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-24 03:17:52 +13:00
ticker.Stop()
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() {
manager.logger.Info().Msgf("shutting down")
manager.destroyPipeline()
manager.emitStop <- true
}
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):
return nil, fmt.Errorf("timeouted")
}
}
if manager.image.Data == nil {
return nil, fmt.Errorf("image sample not found")
}
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 {
return fmt.Errorf("screenshot pipeline not enabled")
}
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-02-06 01:58:02 +13:00
return fmt.Errorf("pipeline already exists")
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-06 00:18:46 +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
}