diff --git a/internal/capture/manager.go b/internal/capture/manager.go index cd83b89e..39990a72 100644 --- a/internal/capture/manager.go +++ b/internal/capture/manager.go @@ -11,41 +11,46 @@ import ( ) type CaptureManagerCtx struct { - logger zerolog.Logger - video *gst.Pipeline - audio *gst.Pipeline - broadcast *gst.Pipeline - config *config.Capture - audio_stop chan bool - video_stop chan bool - emmiter events.EventEmmiter - streaming bool - broadcasting bool - broadcast_url string - desktop types.DesktopManager + logger zerolog.Logger + video *gst.Pipeline + audio *gst.Pipeline + broadcast *gst.Pipeline + config *config.Capture + audio_emit_stop chan bool + video_emit_stop chan bool + emmiter events.EventEmmiter + streaming bool + broadcasting bool + broadcast_url string + desktop types.DesktopManager } func New(desktop types.DesktopManager, config *config.Capture) *CaptureManagerCtx { return &CaptureManagerCtx{ - logger: log.With().Str("module", "capture").Logger(), - audio_stop: make(chan bool), - video_stop: make(chan bool), - emmiter: events.New(), - config: config, - streaming: false, - broadcasting: false, - broadcast_url: "", - desktop: desktop, + logger: log.With().Str("module", "capture").Logger(), + audio_emit_stop: make(chan bool), + video_emit_stop: make(chan bool), + emmiter: events.New(), + config: config, + streaming: false, + broadcasting: false, + broadcast_url: "", + desktop: desktop, } } func (manager *CaptureManagerCtx) Start() { manager.StartBroadcastPipeline() - manager.desktop.OnScreenSizeChange(func(width int, height int, rate int) { - manager.video_stop <- true - manager.StopBroadcastPipeline() + manager.desktop.OnBeforeScreenSizeChange(func() { + manager.video_emit_stop <- true + manager.logger.Info().Msgf("stopping video pipeline") + manager.video.Stop() + manager.StopBroadcastPipeline() + }) + + manager.desktop.OnAfterScreenSizeChange(func() { manager.createVideoPipeline() manager.StartBroadcastPipeline() }) @@ -53,10 +58,7 @@ func (manager *CaptureManagerCtx) Start() { func (manager *CaptureManagerCtx) Shutdown() error { manager.logger.Info().Msgf("capture shutting down") - manager.audio_stop <- true - manager.video_stop <- true - manager.StopBroadcastPipeline() - + manager.StopStream() return nil } @@ -81,7 +83,7 @@ func (manager *CaptureManagerCtx) OnAudioFrame(listener func(sample types.Sample } func (manager *CaptureManagerCtx) StartStream() { - manager.logger.Info().Msgf("Pipelines starting...") + manager.logger.Info().Msgf("starting pipelines") manager.createVideoPipeline() manager.createAudioPipeline() @@ -89,10 +91,16 @@ func (manager *CaptureManagerCtx) StartStream() { } 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 } @@ -107,7 +115,7 @@ func (manager *CaptureManagerCtx) createVideoPipeline() { Str("video_codec", manager.config.VideoCodec). Str("video_display", manager.config.Display). Str("video_params", manager.config.VideoParams). - Msgf("Creating video pipeline...") + Msgf("creating video pipeline") manager.video, err = gst.CreateAppPipeline( manager.config.VideoCodec, @@ -120,23 +128,18 @@ func (manager *CaptureManagerCtx) createVideoPipeline() { } manager.logger.Info(). - Str("pipeline", manager.video.Src). - Msgf("Starting video pipeline...") + Str("src", manager.video.Src). + Msgf("starting video pipeline...") manager.video.Start() go func() { - manager.logger.Debug().Msg("started emitting video data") - - defer func() { - manager.logger.Debug().Msg("stopped emitting video data") - }() + manager.logger.Debug().Msg("started emitting video samples") for { select { - case <-manager.video_stop: - manager.logger.Info().Msgf("Stopping video pipeline...") - manager.video.Stop() + case <-manager.video_emit_stop: + manager.logger.Debug().Msg("stopped emitting video samples") return case sample := <-manager.video.Sample: manager.emmiter.Emit("video", sample) @@ -152,7 +155,7 @@ func (manager *CaptureManagerCtx) createAudioPipeline() { Str("audio_codec", manager.config.AudioCodec). Str("audio_display", manager.config.Device). Str("audio_params", manager.config.AudioParams). - Msgf("Creating audio pipeline...") + Msgf("creating audio pipeline") manager.audio, err = gst.CreateAppPipeline( manager.config.AudioCodec, @@ -165,23 +168,18 @@ func (manager *CaptureManagerCtx) createAudioPipeline() { } manager.logger.Info(). - Str("pipeline", manager.audio.Src). - Msgf("Starting audio pipeline...") + Str("src", manager.audio.Src). + Msgf("starting audio pipeline") manager.audio.Start() go func() { - manager.logger.Debug().Msg("started emitting audio data") - - defer func() { - manager.logger.Debug().Msg("stopped emitting audio data") - }() + manager.logger.Debug().Msg("started emitting audio samples") for { select { - case <-manager.audio_stop: - manager.logger.Info().Msgf("Stopping audio pipeline...") - manager.audio.Stop() + case <-manager.audio_emit_stop: + manager.logger.Debug().Msg("stopped emitting audio samples") return case sample := <-manager.audio.Sample: manager.emmiter.Emit("audio", sample) diff --git a/internal/desktop/manager.go b/internal/desktop/manager.go index 0238c085..a475cd7f 100644 --- a/internal/desktop/manager.go +++ b/internal/desktop/manager.go @@ -64,9 +64,15 @@ func (manager *DesktopManagerCtx) Start() { }() } -func (manager *DesktopManagerCtx) OnScreenSizeChange(listener func(width int, height int, rate int)) { - manager.emmiter.On("screen_size_change", func(payload ...interface{}) { - listener(payload[0].(int), payload[1].(int), payload[2].(int)) +func (manager *DesktopManagerCtx) OnBeforeScreenSizeChange(listener func()) { + manager.emmiter.On("before_screen_size_change", func(payload ...interface{}) { + listener() + }) +} + +func (manager *DesktopManagerCtx) OnAfterScreenSizeChange(listener func()) { + manager.emmiter.On("after_screen_size_change", func(payload ...interface{}) { + listener() }) } diff --git a/internal/desktop/xorg.go b/internal/desktop/xorg.go index 3f25e53a..c0f20d65 100644 --- a/internal/desktop/xorg.go +++ b/internal/desktop/xorg.go @@ -42,12 +42,10 @@ func (manager *DesktopManagerCtx) GetScreenSize() *types.ScreenSize { } func (manager *DesktopManagerCtx) ChangeScreenSize(width int, height int, rate int) error { - if err := xorg.ChangeScreenSize(width, height, rate); err != nil { - return err - } - - manager.emmiter.Emit("screen_size_change", width, height, rate) - return nil + manager.emmiter.Emit("before_screen_size_change") + err := xorg.ChangeScreenSize(width, height, rate) + manager.emmiter.Emit("after_screen_size_change") + return err } func (manager *DesktopManagerCtx) SetKeyboardLayout(layout string) { diff --git a/internal/types/desktop.go b/internal/types/desktop.go index f9168c23..69f888b8 100644 --- a/internal/types/desktop.go +++ b/internal/types/desktop.go @@ -15,7 +15,8 @@ type ScreenConfiguration struct { type DesktopManager interface { Start() Shutdown() error - OnScreenSizeChange(listener func(width int, height int, rate int)) + OnBeforeScreenSizeChange(listener func()) + OnAfterScreenSizeChange(listener func()) // xorg ChangeScreenSize(width int, height int, rate int) error