fixes "screen size change is sometimes failing".

This commit is contained in:
Miroslav Šedivý 2020-11-07 17:22:25 +01:00
parent e4fa855f7b
commit a6fd0f3d25
4 changed files with 67 additions and 64 deletions

View File

@ -11,41 +11,46 @@ import (
) )
type CaptureManagerCtx struct { type CaptureManagerCtx struct {
logger zerolog.Logger logger zerolog.Logger
video *gst.Pipeline video *gst.Pipeline
audio *gst.Pipeline audio *gst.Pipeline
broadcast *gst.Pipeline broadcast *gst.Pipeline
config *config.Capture config *config.Capture
audio_stop chan bool audio_emit_stop chan bool
video_stop chan bool video_emit_stop chan bool
emmiter events.EventEmmiter emmiter events.EventEmmiter
streaming bool streaming bool
broadcasting bool broadcasting bool
broadcast_url string broadcast_url string
desktop types.DesktopManager desktop types.DesktopManager
} }
func New(desktop types.DesktopManager, config *config.Capture) *CaptureManagerCtx { func New(desktop types.DesktopManager, config *config.Capture) *CaptureManagerCtx {
return &CaptureManagerCtx{ return &CaptureManagerCtx{
logger: log.With().Str("module", "capture").Logger(), logger: log.With().Str("module", "capture").Logger(),
audio_stop: make(chan bool), audio_emit_stop: make(chan bool),
video_stop: make(chan bool), video_emit_stop: make(chan bool),
emmiter: events.New(), emmiter: events.New(),
config: config, config: config,
streaming: false, streaming: false,
broadcasting: false, broadcasting: false,
broadcast_url: "", broadcast_url: "",
desktop: desktop, desktop: desktop,
} }
} }
func (manager *CaptureManagerCtx) Start() { func (manager *CaptureManagerCtx) Start() {
manager.StartBroadcastPipeline() manager.StartBroadcastPipeline()
manager.desktop.OnScreenSizeChange(func(width int, height int, rate int) { manager.desktop.OnBeforeScreenSizeChange(func() {
manager.video_stop <- true manager.video_emit_stop <- true
manager.StopBroadcastPipeline() manager.logger.Info().Msgf("stopping video pipeline")
manager.video.Stop()
manager.StopBroadcastPipeline()
})
manager.desktop.OnAfterScreenSizeChange(func() {
manager.createVideoPipeline() manager.createVideoPipeline()
manager.StartBroadcastPipeline() manager.StartBroadcastPipeline()
}) })
@ -53,10 +58,7 @@ func (manager *CaptureManagerCtx) Start() {
func (manager *CaptureManagerCtx) Shutdown() error { func (manager *CaptureManagerCtx) Shutdown() error {
manager.logger.Info().Msgf("capture shutting down") manager.logger.Info().Msgf("capture shutting down")
manager.audio_stop <- true manager.StopStream()
manager.video_stop <- true
manager.StopBroadcastPipeline()
return nil return nil
} }
@ -81,7 +83,7 @@ func (manager *CaptureManagerCtx) OnAudioFrame(listener func(sample types.Sample
} }
func (manager *CaptureManagerCtx) StartStream() { func (manager *CaptureManagerCtx) StartStream() {
manager.logger.Info().Msgf("Pipelines starting...") manager.logger.Info().Msgf("starting pipelines")
manager.createVideoPipeline() manager.createVideoPipeline()
manager.createAudioPipeline() manager.createAudioPipeline()
@ -89,10 +91,16 @@ func (manager *CaptureManagerCtx) StartStream() {
} }
func (manager *CaptureManagerCtx) StopStream() { func (manager *CaptureManagerCtx) StopStream() {
manager.logger.Info().Msgf("Pipelines stopping...") manager.logger.Info().Msgf("stopping pipelines")
manager.audio_emit_stop <- true
manager.logger.Info().Msgf("stopping video pipeline")
manager.audio.Stop()
manager.video_emit_stop <- true
manager.logger.Info().Msgf("stopping audio pipeline")
manager.video.Stop()
manager.audio_stop <- true
manager.video_stop <- true
manager.streaming = false manager.streaming = false
} }
@ -107,7 +115,7 @@ func (manager *CaptureManagerCtx) createVideoPipeline() {
Str("video_codec", manager.config.VideoCodec). Str("video_codec", manager.config.VideoCodec).
Str("video_display", manager.config.Display). Str("video_display", manager.config.Display).
Str("video_params", manager.config.VideoParams). Str("video_params", manager.config.VideoParams).
Msgf("Creating video pipeline...") Msgf("creating video pipeline")
manager.video, err = gst.CreateAppPipeline( manager.video, err = gst.CreateAppPipeline(
manager.config.VideoCodec, manager.config.VideoCodec,
@ -120,23 +128,18 @@ func (manager *CaptureManagerCtx) createVideoPipeline() {
} }
manager.logger.Info(). manager.logger.Info().
Str("pipeline", manager.video.Src). Str("src", manager.video.Src).
Msgf("Starting video pipeline...") Msgf("starting video pipeline...")
manager.video.Start() manager.video.Start()
go func() { go func() {
manager.logger.Debug().Msg("started emitting video data") manager.logger.Debug().Msg("started emitting video samples")
defer func() {
manager.logger.Debug().Msg("stopped emitting video data")
}()
for { for {
select { select {
case <-manager.video_stop: case <-manager.video_emit_stop:
manager.logger.Info().Msgf("Stopping video pipeline...") manager.logger.Debug().Msg("stopped emitting video samples")
manager.video.Stop()
return return
case sample := <-manager.video.Sample: case sample := <-manager.video.Sample:
manager.emmiter.Emit("video", sample) manager.emmiter.Emit("video", sample)
@ -152,7 +155,7 @@ func (manager *CaptureManagerCtx) createAudioPipeline() {
Str("audio_codec", manager.config.AudioCodec). Str("audio_codec", manager.config.AudioCodec).
Str("audio_display", manager.config.Device). Str("audio_display", manager.config.Device).
Str("audio_params", manager.config.AudioParams). Str("audio_params", manager.config.AudioParams).
Msgf("Creating audio pipeline...") Msgf("creating audio pipeline")
manager.audio, err = gst.CreateAppPipeline( manager.audio, err = gst.CreateAppPipeline(
manager.config.AudioCodec, manager.config.AudioCodec,
@ -165,23 +168,18 @@ func (manager *CaptureManagerCtx) createAudioPipeline() {
} }
manager.logger.Info(). manager.logger.Info().
Str("pipeline", manager.audio.Src). Str("src", manager.audio.Src).
Msgf("Starting audio pipeline...") Msgf("starting audio pipeline")
manager.audio.Start() manager.audio.Start()
go func() { go func() {
manager.logger.Debug().Msg("started emitting audio data") manager.logger.Debug().Msg("started emitting audio samples")
defer func() {
manager.logger.Debug().Msg("stopped emitting audio data")
}()
for { for {
select { select {
case <-manager.audio_stop: case <-manager.audio_emit_stop:
manager.logger.Info().Msgf("Stopping audio pipeline...") manager.logger.Debug().Msg("stopped emitting audio samples")
manager.audio.Stop()
return return
case sample := <-manager.audio.Sample: case sample := <-manager.audio.Sample:
manager.emmiter.Emit("audio", sample) manager.emmiter.Emit("audio", sample)

View File

@ -64,9 +64,15 @@ func (manager *DesktopManagerCtx) Start() {
}() }()
} }
func (manager *DesktopManagerCtx) OnScreenSizeChange(listener func(width int, height int, rate int)) { func (manager *DesktopManagerCtx) OnBeforeScreenSizeChange(listener func()) {
manager.emmiter.On("screen_size_change", func(payload ...interface{}) { manager.emmiter.On("before_screen_size_change", func(payload ...interface{}) {
listener(payload[0].(int), payload[1].(int), payload[2].(int)) listener()
})
}
func (manager *DesktopManagerCtx) OnAfterScreenSizeChange(listener func()) {
manager.emmiter.On("after_screen_size_change", func(payload ...interface{}) {
listener()
}) })
} }

View File

@ -42,12 +42,10 @@ func (manager *DesktopManagerCtx) GetScreenSize() *types.ScreenSize {
} }
func (manager *DesktopManagerCtx) ChangeScreenSize(width int, height int, rate int) error { func (manager *DesktopManagerCtx) ChangeScreenSize(width int, height int, rate int) error {
if err := xorg.ChangeScreenSize(width, height, rate); err != nil { manager.emmiter.Emit("before_screen_size_change")
return err err := xorg.ChangeScreenSize(width, height, rate)
} manager.emmiter.Emit("after_screen_size_change")
return err
manager.emmiter.Emit("screen_size_change", width, height, rate)
return nil
} }
func (manager *DesktopManagerCtx) SetKeyboardLayout(layout string) { func (manager *DesktopManagerCtx) SetKeyboardLayout(layout string) {

View File

@ -15,7 +15,8 @@ type ScreenConfiguration struct {
type DesktopManager interface { type DesktopManager interface {
Start() Start()
Shutdown() error Shutdown() error
OnScreenSizeChange(listener func(width int, height int, rate int)) OnBeforeScreenSizeChange(listener func())
OnAfterScreenSizeChange(listener func())
// xorg // xorg
ChangeScreenSize(width int, height int, rate int) error ChangeScreenSize(width int, height int, rate int) error