neko/internal/capture/screencast.go

163 lines
3.2 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/config"
"demodesk/neko/internal/types"
"demodesk/neko/internal/capture/gst"
)
type ScreencastManagerCtx struct {
logger zerolog.Logger
2021-01-24 03:17:52 +13:00
mu sync.Mutex
2021-01-23 06:13:32 +13:00
config *config.Capture
pipeline *gst.Pipeline
enabled bool
2021-01-24 03:17:52 +13:00
started bool
2021-01-23 06:13:32 +13:00
shutdown chan bool
refresh chan bool
2021-01-24 04:29:47 +13:00
expired int32
2021-01-23 06:13:32 +13:00
sample chan types.Sample
2021-01-24 03:17:52 +13:00
image types.Sample
2021-01-23 06:13:32 +13:00
}
func screencastNew(config *config.Capture) *ScreencastManagerCtx {
manager := &ScreencastManagerCtx{
logger: log.With().Str("module", "capture").Str("submodule", "screencast").Logger(),
config: config,
enabled: config.Screencast,
2021-01-24 03:17:52 +13:00
started: false,
2021-01-23 06:13:32 +13:00
shutdown: make(chan bool),
refresh: make(chan bool),
2021-01-24 03:17:52 +13:00
}
if !manager.enabled {
return manager
2021-01-23 06:13:32 +13:00
}
go func() {
2021-01-24 04:29:47 +13:00
ticker := time.NewTicker(5 * time.Second)
2021-01-23 06:13:32 +13:00
manager.logger.Debug().Msg("subroutine started")
for {
select {
case <-manager.shutdown:
manager.logger.Debug().Msg("shutting down")
2021-01-24 03:17:52 +13:00
ticker.Stop()
2021-01-23 06:13:32 +13:00
return
case <-manager.refresh:
manager.logger.Debug().Msg("subroutine updated")
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-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 {
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() {
manager.started = false
manager.destroyPipeline()
2021-01-23 06:13:32 +13:00
}
func (manager *ScreencastManagerCtx) createPipeline() error {
2021-01-24 03:17:52 +13:00
manager.mu.Lock()
defer manager.mu.Unlock()
2021-01-23 06:13:32 +13:00
var err error
manager.logger.Info().
Str("video_display", manager.config.Display).
Str("screencast_pipeline", manager.config.ScreencastPipeline).
Msgf("creating pipeline")
2021-02-03 06:28:32 +13:00
2021-01-23 06:13:32 +13:00
manager.pipeline, err = gst.CreateJPEGPipeline(
manager.config.Display,
manager.config.ScreencastPipeline,
2021-01-23 06:44:27 +13:00
manager.config.ScreencastRate,
manager.config.ScreencastQuality,
2021-01-23 06:13:32 +13:00
)
if err != nil {
return err
}
manager.pipeline.Start()
manager.logger.Info().Msgf("starting pipeline")
manager.sample = manager.pipeline.Sample
manager.refresh <-true
return nil
}
func (manager *ScreencastManagerCtx) destroyPipeline() {
2021-01-24 03:17:52 +13:00
manager.mu.Lock()
defer manager.mu.Unlock()
2021-01-23 06:13:32 +13:00
if manager.pipeline == nil {
return
}
manager.pipeline.Stop()
manager.logger.Info().Msgf("stopping pipeline")
manager.pipeline = nil
}