decouple BroadcastManager from CaptureManager.

This commit is contained in:
Miroslav Šedivý 2021-01-22 14:09:47 +01:00
parent afd3dd2f56
commit 407853eeb1
5 changed files with 82 additions and 53 deletions

View File

@ -14,9 +14,10 @@ type BroadcastStatusPayload struct {
} }
func (h *RoomHandler) broadcastStatus(w http.ResponseWriter, r *http.Request) { func (h *RoomHandler) broadcastStatus(w http.ResponseWriter, r *http.Request) {
broadcast := h.capture.Broadcast()
utils.HttpSuccess(w, BroadcastStatusPayload{ utils.HttpSuccess(w, BroadcastStatusPayload{
IsActive: h.capture.BroadcastEnabled(), IsActive: broadcast.Enabled(),
URL: h.capture.BroadcastUrl(), URL: broadcast.Url(),
}) })
} }
@ -31,12 +32,13 @@ func (h *RoomHandler) boradcastStart(w http.ResponseWriter, r *http.Request) {
return return
} }
if h.capture.BroadcastEnabled() { broadcast := h.capture.Broadcast()
if broadcast.Enabled() {
utils.HttpUnprocessableEntity(w, "Server is already broadcasting.") utils.HttpUnprocessableEntity(w, "Server is already broadcasting.")
return return
} }
if err := h.capture.StartBroadcast(data.URL); err != nil { if err := broadcast.Start(data.URL); err != nil {
utils.HttpInternalServerError(w, err) utils.HttpInternalServerError(w, err)
return return
} }
@ -44,26 +46,27 @@ func (h *RoomHandler) boradcastStart(w http.ResponseWriter, r *http.Request) {
h.sessions.AdminBroadcast( h.sessions.AdminBroadcast(
message.BroadcastStatus{ message.BroadcastStatus{
Event: event.BORADCAST_STATUS, Event: event.BORADCAST_STATUS,
IsActive: h.capture.BroadcastEnabled(), IsActive: broadcast.Enabled(),
URL: h.capture.BroadcastUrl(), URL: broadcast.Url(),
}, nil) }, nil)
utils.HttpSuccess(w) utils.HttpSuccess(w)
} }
func (h *RoomHandler) boradcastStop(w http.ResponseWriter, r *http.Request) { func (h *RoomHandler) boradcastStop(w http.ResponseWriter, r *http.Request) {
if !h.capture.BroadcastEnabled() { broadcast := h.capture.Broadcast()
if !broadcast.Enabled() {
utils.HttpUnprocessableEntity(w, "Server is not broadcasting.") utils.HttpUnprocessableEntity(w, "Server is not broadcasting.")
return return
} }
h.capture.StopBroadcast() broadcast.Stop()
h.sessions.AdminBroadcast( h.sessions.AdminBroadcast(
message.BroadcastStatus{ message.BroadcastStatus{
Event: event.BORADCAST_STATUS, Event: event.BORADCAST_STATUS,
IsActive: h.capture.BroadcastEnabled(), IsActive: broadcast.Enabled(),
URL: h.capture.BroadcastUrl(), URL: broadcast.Url(),
}, nil) }, nil)
utils.HttpSuccess(w) utils.HttpSuccess(w)

View File

@ -1,59 +1,80 @@
package capture package capture
import ( import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"demodesk/neko/internal/config"
"demodesk/neko/internal/capture/gst" "demodesk/neko/internal/capture/gst"
) )
func (manager *CaptureManagerCtx) StartBroadcast(url string) error { type BroacastManagerCtx struct {
manager.broadcast_url = url logger zerolog.Logger
manager.broadcasting = true config *config.Capture
return manager.createBroadcastPipeline() pipeline *gst.Pipeline
enabled bool
url string
} }
func (manager *CaptureManagerCtx) StopBroadcast() { func broadcastNew(config *config.Capture) *BroacastManagerCtx {
manager.broadcasting = false return &BroacastManagerCtx{
manager.destroyBroadcastPipeline() logger: log.With().Str("module", "capture").Str("submodule", "broadcast").Logger(),
config: config,
enabled: false,
url: "",
}
} }
func (manager *CaptureManagerCtx) BroadcastEnabled() bool { func (manager *BroacastManagerCtx) Start(url string) error {
return manager.broadcasting manager.url = url
manager.enabled = true
return manager.createPipeline()
} }
func (manager *CaptureManagerCtx) BroadcastUrl() string { func (manager *BroacastManagerCtx) Stop() {
return manager.broadcast_url manager.enabled = false
manager.destroyPipeline()
} }
func (manager *CaptureManagerCtx) createBroadcastPipeline() error { func (manager *BroacastManagerCtx) Enabled() bool {
return manager.enabled
}
func (manager *BroacastManagerCtx) Url() string {
return manager.url
}
func (manager *BroacastManagerCtx) createPipeline() error {
var err error var err error
manager.logger.Info(). manager.logger.Info().
Str("audio_device", manager.config.Device). Str("audio_device", manager.config.Device).
Str("video_display", manager.config.Display). Str("video_display", manager.config.Display).
Str("broadcast_pipeline", manager.config.BroadcastPipeline). Str("broadcast_pipeline", manager.config.BroadcastPipeline).
Msgf("creating broadcast pipeline") Msgf("creating pipeline")
manager.broadcast, err = gst.CreateRTMPPipeline( manager.pipeline, err = gst.CreateRTMPPipeline(
manager.config.Device, manager.config.Device,
manager.config.Display, manager.config.Display,
manager.config.BroadcastPipeline, manager.config.BroadcastPipeline,
manager.broadcast_url, manager.url,
) )
if err != nil { if err != nil {
return err return err
} }
manager.broadcast.Play() manager.pipeline.Play()
manager.logger.Info().Msgf("starting broadcast pipeline") manager.logger.Info().Msgf("starting pipeline")
return nil return nil
} }
func (manager *CaptureManagerCtx) destroyBroadcastPipeline() { func (manager *BroacastManagerCtx) destroyPipeline() {
if manager.broadcast == nil { if manager.pipeline == nil {
return return
} }
manager.broadcast.Stop() manager.pipeline.Stop()
manager.logger.Info().Msgf("stopping broadcast pipeline") manager.logger.Info().Msgf("stopping pipeline")
manager.broadcast = nil manager.pipeline = nil
} }

View File

@ -17,7 +17,6 @@ type CaptureManagerCtx struct {
mu sync.Mutex mu sync.Mutex
video *gst.Pipeline video *gst.Pipeline
audio *gst.Pipeline audio *gst.Pipeline
broadcast *gst.Pipeline
config *config.Capture config *config.Capture
emit_update chan bool emit_update chan bool
emit_stop chan bool emit_stop chan bool
@ -25,9 +24,8 @@ type CaptureManagerCtx struct {
audio_sample chan types.Sample audio_sample chan types.Sample
emmiter events.EventEmmiter emmiter events.EventEmmiter
streaming bool streaming bool
broadcasting bool
broadcast_url string
desktop types.DesktopManager desktop types.DesktopManager
broadcast *BroacastManagerCtx
} }
func New(desktop types.DesktopManager, config *config.Capture) *CaptureManagerCtx { func New(desktop types.DesktopManager, config *config.Capture) *CaptureManagerCtx {
@ -39,15 +37,14 @@ func New(desktop types.DesktopManager, config *config.Capture) *CaptureManagerCt
emmiter: events.New(), emmiter: events.New(),
config: config, config: config,
streaming: false, streaming: false,
broadcasting: false,
broadcast_url: "",
desktop: desktop, desktop: desktop,
broadcast: broadcastNew(config),
} }
} }
func (manager *CaptureManagerCtx) Start() { func (manager *CaptureManagerCtx) Start() {
if manager.BroadcastEnabled() { if manager.broadcast.Enabled() {
if err := manager.createBroadcastPipeline(); err != nil { if err := manager.broadcast.createPipeline(); err != nil {
manager.logger.Panic().Err(err).Msg("unable to create broadcast pipeline") manager.logger.Panic().Err(err).Msg("unable to create broadcast pipeline")
} }
} }
@ -57,8 +54,8 @@ func (manager *CaptureManagerCtx) Start() {
manager.destroyVideoPipeline() manager.destroyVideoPipeline()
} }
if manager.BroadcastEnabled() { if manager.broadcast.Enabled() {
manager.destroyBroadcastPipeline() manager.broadcast.destroyPipeline()
} }
}) })
@ -67,8 +64,8 @@ func (manager *CaptureManagerCtx) Start() {
manager.createVideoPipeline() manager.createVideoPipeline()
} }
if manager.BroadcastEnabled() { if manager.broadcast.Enabled() {
if err := manager.createBroadcastPipeline(); err != nil { if err := manager.broadcast.createPipeline(); err != nil {
manager.logger.Panic().Err(err).Msg("unable to create broadcast pipeline") manager.logger.Panic().Err(err).Msg("unable to create broadcast pipeline")
} }
} }
@ -100,14 +97,18 @@ func (manager *CaptureManagerCtx) Shutdown() error {
manager.StopStream() manager.StopStream()
} }
if manager.BroadcastEnabled() { if manager.broadcast.Enabled() {
manager.destroyBroadcastPipeline() manager.broadcast.destroyPipeline()
} }
manager.emit_stop <- true manager.emit_stop <- true
return nil return nil
} }
func (manager *CaptureManagerCtx) Broadcast() types.BroadcastManager {
return manager.broadcast
}
func (manager *CaptureManagerCtx) VideoCodec() string { func (manager *CaptureManagerCtx) VideoCodec() string {
return manager.config.VideoCodec return manager.config.VideoCodec
} }

View File

@ -5,10 +5,19 @@ type Sample struct {
Samples uint32 Samples uint32
} }
type BroadcastManager interface {
Start(url string) error
Stop()
Enabled() bool
Url() string
}
type CaptureManager interface { type CaptureManager interface {
Start() Start()
Shutdown() error Shutdown() error
Broadcast() BroadcastManager
VideoCodec() string VideoCodec() string
AudioCodec() string AudioCodec() string
@ -18,10 +27,4 @@ type CaptureManager interface {
StartStream() StartStream()
StopStream() StopStream()
Streaming() bool Streaming() bool
// broacast
StartBroadcast(url string) error
StopBroadcast()
BroadcastEnabled() bool
BroadcastUrl() string
} }

View File

@ -75,13 +75,14 @@ func (h *MessageHandlerCtx) systemAdmin(session types.Session) error {
} }
} }
broadcast := h.capture.Broadcast()
return session.Send( return session.Send(
message.SystemAdmin{ message.SystemAdmin{
Event: event.SYSTEM_ADMIN, Event: event.SYSTEM_ADMIN,
ScreenSizesList: screenSizesList, ScreenSizesList: screenSizesList,
BroadcastStatus: message.BroadcastStatus{ BroadcastStatus: message.BroadcastStatus{
IsActive: h.capture.BroadcastEnabled(), IsActive: broadcast.Enabled(),
URL: h.capture.BroadcastUrl(), URL: broadcast.Url(),
}, },
}) })
} }