neko/internal/capture/manager.go

178 lines
4.0 KiB
Go
Raw Normal View History

2020-11-02 04:09:48 +13:00
package capture
import (
"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 {
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,
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() {
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-02 04:09:48 +13:00
manager.desktop.OnBeforeScreenSizeChange(func() {
if manager.video.Enabled() {
manager.video.destroyPipeline()
}
if manager.broadcast.Enabled() {
manager.broadcast.destroyPipeline()
}
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-02 04:09:48 +13:00
manager.desktop.OnAfterScreenSizeChange(func() {
if manager.video.Enabled() {
if err := manager.video.createPipeline(); err != nil {
manager.logger.Panic().Err(err).Msg("unable to recreate video pipeline")
}
}
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-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
}
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
}
func (manager *CaptureManagerCtx) Audio() types.StreamManager {
return manager.audio
2020-11-02 04:09:48 +13:00
}
func (manager *CaptureManagerCtx) Video() types.StreamManager {
return manager.video
2020-11-02 04:09:48 +13:00
}
func (manager *CaptureManagerCtx) StartStream() {
manager.mu.Lock()
defer manager.mu.Unlock()
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() {
manager.mu.Lock()
defer manager.mu.Unlock()
manager.logger.Info().Msgf("stopping stream pipelines")
manager.Video().Stop()
manager.Audio().Stop()
2020-11-02 04:09:48 +13:00
manager.streaming = false
}
func (manager *CaptureManagerCtx) Streaming() bool {
manager.mu.Lock()
defer manager.mu.Unlock()
2020-11-02 04:09:48 +13:00
return manager.streaming
}