neko/internal/capture/screencast.go

211 lines
3.9 KiB
Go
Raw Normal View History

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"
2022-03-20 23:43:00 +13:00
"gitlab.com/demodesk/neko/server/pkg/gst"
"gitlab.com/demodesk/neko/server/pkg/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
2022-01-23 05:58:05 +13:00
image types.Sample
2022-02-15 06:41:47 +13:00
imageMu sync.Mutex
2022-01-23 05:58:05 +13:00
tickerStop 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{
2022-01-23 05:58:05 +13:00
logger: logger,
pipelineStr: pipelineStr,
tickerStop: make(chan struct{}),
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-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 {
2022-01-23 05:58:05 +13:00
case <-manager.tickerStop:
2021-01-23 06:13:32 +13:00
return
2021-01-24 03:17:52 +13:00
case <-ticker.C:
2022-01-23 05:58:05 +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
2022-01-23 05:58:05 +13:00
close(manager.tickerStop)
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
2022-01-23 05:58:05 +13:00
err := manager.start()
if err != nil && !errors.Is(err, types.ErrCapturePipelineAlreadyExists) {
return nil, err
2021-01-24 03:17:52 +13:00
}
2022-02-15 06:41:47 +13:00
manager.imageMu.Lock()
defer manager.imageMu.Unlock()
2021-01-24 03:17:52 +13:00
if manager.image.Data == nil {
2022-01-23 05:58:05 +13:00
return nil, errors.New("image data 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-12-10 11:22:24 +13:00
return errors.New("screencast 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
2022-01-23 05:58:05 +13:00
// get first image
select {
2022-02-15 06:41:47 +13:00
case image, ok := <-manager.pipeline.Sample:
2022-01-23 05:58:05 +13:00
if !ok {
return errors.New("unable to get first image")
2022-02-15 06:41:47 +13:00
} else {
manager.imageMu.Lock()
manager.image = image
manager.imageMu.Unlock()
2022-01-23 05:58:05 +13:00
}
case <-time.After(1 * time.Second):
return errors.New("timeouted while waiting for first image")
}
manager.wg.Add(1)
2022-02-15 06:41:47 +13:00
pipeline := manager.pipeline
2022-01-23 05:58:05 +13:00
go func() {
manager.logger.Debug().Msg("started receiving images")
defer manager.wg.Done()
for {
2022-02-15 06:41:47 +13:00
image, ok := <-pipeline.Sample
2022-01-23 05:58:05 +13:00
if !ok {
manager.logger.Debug().Msg("stopped receiving images")
return
}
2022-02-15 06:41:47 +13:00
manager.imageMu.Lock()
2022-01-23 05:58:05 +13:00
manager.image = image
2022-02-15 06:41:47 +13:00
manager.imageMu.Unlock()
2022-01-23 05:58:05 +13:00
}
}()
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
}
2021-12-06 06:16:26 +13:00
manager.pipeline.Destroy()
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
}