2020-11-02 04:09:48 +13:00
|
|
|
package capture
|
|
|
|
|
|
|
|
import (
|
2020-11-15 11:14:48 +13:00
|
|
|
"sync"
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
|
|
|
"demodesk/neko/internal/types"
|
|
|
|
"demodesk/neko/internal/config"
|
2021-02-06 01:58:02 +13:00
|
|
|
"demodesk/neko/internal/capture/gst"
|
2020-11-02 04:09:48 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
type CaptureManagerCtx struct {
|
2021-02-05 09:39:48 +13:00
|
|
|
logger zerolog.Logger
|
|
|
|
mu sync.Mutex
|
|
|
|
desktop types.DesktopManager
|
|
|
|
streaming bool
|
|
|
|
broadcast *BroacastManagerCtx
|
|
|
|
screencast *ScreencastManagerCtx
|
|
|
|
audio *StreamManagerCtx
|
|
|
|
video *StreamManagerCtx
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(desktop types.DesktopManager, config *config.Capture) *CaptureManagerCtx {
|
2021-02-06 01:58:02 +13:00
|
|
|
logger := log.With().Str("module", "capture").Logger()
|
|
|
|
|
|
|
|
broadcastPipeline := gst.GetRTMPPipeline(
|
|
|
|
config.Device,
|
|
|
|
config.Display,
|
|
|
|
config.BroadcastPipeline,
|
|
|
|
)
|
|
|
|
|
|
|
|
screencastPipeline := gst.GetJPEGPipeline(
|
|
|
|
config.Display,
|
|
|
|
config.ScreencastPipeline,
|
|
|
|
config.ScreencastRate,
|
|
|
|
config.ScreencastQuality,
|
|
|
|
)
|
|
|
|
|
|
|
|
audioPipeline, err := gst.GetAppPipeline(
|
|
|
|
config.AudioCodec,
|
|
|
|
config.Device,
|
|
|
|
config.AudioParams,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Panic().Err(err).Msg("unable to get pipeline")
|
|
|
|
}
|
|
|
|
|
|
|
|
videoPipeline, err := gst.GetAppPipeline(
|
|
|
|
config.VideoCodec,
|
|
|
|
config.Display,
|
|
|
|
config.VideoParams,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Panic().Err(err).Msg("unable to get pipeline")
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
return &CaptureManagerCtx{
|
2021-02-06 01:58:02 +13:00
|
|
|
logger: logger,
|
2021-02-05 09:39:48 +13:00
|
|
|
desktop: desktop,
|
|
|
|
streaming: false,
|
2021-02-06 01:58:02 +13:00
|
|
|
broadcast: broadcastNew(broadcastPipeline),
|
|
|
|
screencast: screencastNew(config.Screencast, screencastPipeline),
|
|
|
|
audio: streamNew(config.AudioCodec, audioPipeline),
|
|
|
|
video: streamNew(config.VideoCodec, videoPipeline),
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *CaptureManagerCtx) Start() {
|
2021-01-23 02:09:47 +13:00
|
|
|
if manager.broadcast.Enabled() {
|
|
|
|
if err := manager.broadcast.createPipeline(); err != nil {
|
2020-11-19 11:32:43 +13:00
|
|
|
manager.logger.Panic().Err(err).Msg("unable to create broadcast pipeline")
|
|
|
|
}
|
2020-11-19 09:34:39 +13:00
|
|
|
}
|
2020-11-02 04:09:48 +13:00
|
|
|
|
2020-11-08 05:22:25 +13:00
|
|
|
manager.desktop.OnBeforeScreenSizeChange(func() {
|
2021-02-05 09:39:48 +13:00
|
|
|
if manager.video.Enabled() {
|
|
|
|
manager.video.destroyPipeline()
|
2020-11-15 11:14:48 +13:00
|
|
|
}
|
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
if manager.broadcast.Enabled() {
|
|
|
|
manager.broadcast.destroyPipeline()
|
2020-11-19 09:34:39 +13:00
|
|
|
}
|
2021-01-23 06:13:32 +13:00
|
|
|
|
2021-01-24 03:17:52 +13:00
|
|
|
if manager.screencast.Started() {
|
2021-01-23 06:13:32 +13:00
|
|
|
manager.screencast.destroyPipeline()
|
|
|
|
}
|
2020-11-08 05:22:25 +13:00
|
|
|
})
|
2020-11-02 04:09:48 +13:00
|
|
|
|
2020-11-08 05:22:25 +13:00
|
|
|
manager.desktop.OnAfterScreenSizeChange(func() {
|
2021-02-05 09:39:48 +13:00
|
|
|
if manager.video.Enabled() {
|
|
|
|
if err := manager.video.createPipeline(); err != nil {
|
|
|
|
manager.logger.Panic().Err(err).Msg("unable to recreate video pipeline")
|
|
|
|
}
|
2020-11-15 11:14:48 +13:00
|
|
|
}
|
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
if manager.broadcast.Enabled() {
|
|
|
|
if err := manager.broadcast.createPipeline(); err != nil {
|
2021-01-23 06:13:32 +13:00
|
|
|
manager.logger.Panic().Err(err).Msg("unable to recreate broadcast pipeline")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 03:17:52 +13:00
|
|
|
if manager.screencast.Started() {
|
2021-01-23 06:13:32 +13:00
|
|
|
if err := manager.screencast.createPipeline(); err != nil {
|
|
|
|
manager.logger.Panic().Err(err).Msg("unable to recreate screencast pipeline")
|
2020-11-19 11:32:43 +13:00
|
|
|
}
|
2020-11-19 09:34:39 +13:00
|
|
|
}
|
2020-11-04 12:27:47 +13:00
|
|
|
})
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *CaptureManagerCtx) Shutdown() error {
|
|
|
|
manager.logger.Info().Msgf("capture shutting down")
|
2021-02-03 06:28:32 +13:00
|
|
|
|
2021-02-06 00:18:46 +13:00
|
|
|
manager.broadcast.shutdown()
|
|
|
|
manager.screencast.shutdown()
|
|
|
|
|
|
|
|
manager.audio.shutdown()
|
|
|
|
manager.video.shutdown()
|
2020-11-02 04:09:48 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
func (manager *CaptureManagerCtx) Broadcast() types.BroadcastManager {
|
|
|
|
return manager.broadcast
|
|
|
|
}
|
|
|
|
|
2021-01-23 06:13:32 +13:00
|
|
|
func (manager *CaptureManagerCtx) Screencast() types.ScreencastManager {
|
|
|
|
return manager.screencast
|
|
|
|
}
|
|
|
|
|
2021-02-05 09:39:48 +13:00
|
|
|
func (manager *CaptureManagerCtx) Audio() types.StreamManager {
|
|
|
|
return manager.audio
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
|
|
|
|
2021-02-05 09:39:48 +13:00
|
|
|
func (manager *CaptureManagerCtx) Video() types.StreamManager {
|
|
|
|
return manager.video
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *CaptureManagerCtx) StartStream() {
|
2020-11-15 11:14:48 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-02-05 09:39:48 +13:00
|
|
|
manager.logger.Info().Msgf("starting stream pipelines")
|
2020-11-02 04:09:48 +13:00
|
|
|
|
2021-02-06 00:41:02 +13:00
|
|
|
var err error
|
|
|
|
err = manager.Video().Start()
|
|
|
|
if err != nil {
|
|
|
|
manager.logger.Panic().Err(err).Msg("unable to start video pipeline")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = manager.Audio().Start()
|
|
|
|
if err != nil {
|
|
|
|
manager.logger.Panic().Err(err).Msg("unable to start audio pipeline")
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
manager.streaming = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *CaptureManagerCtx) StopStream() {
|
2020-11-15 11:14:48 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-02-05 09:39:48 +13:00
|
|
|
manager.logger.Info().Msgf("stopping stream pipelines")
|
2020-11-08 05:22:25 +13:00
|
|
|
|
2021-02-05 09:39:48 +13:00
|
|
|
manager.Video().Stop()
|
|
|
|
manager.Audio().Stop()
|
2020-11-02 04:09:48 +13:00
|
|
|
manager.streaming = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *CaptureManagerCtx) Streaming() bool {
|
2020-11-15 11:14:48 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
return manager.streaming
|
|
|
|
}
|