2020-11-02 04:09:48 +13:00
|
|
|
package capture
|
|
|
|
|
|
|
|
import (
|
2021-01-23 02:09:47 +13:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
|
|
|
"demodesk/neko/internal/config"
|
2020-11-02 04:09:48 +13:00
|
|
|
"demodesk/neko/internal/capture/gst"
|
|
|
|
)
|
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
type BroacastManagerCtx struct {
|
|
|
|
logger zerolog.Logger
|
|
|
|
config *config.Capture
|
|
|
|
pipeline *gst.Pipeline
|
|
|
|
enabled bool
|
|
|
|
url string
|
|
|
|
}
|
|
|
|
|
|
|
|
func broadcastNew(config *config.Capture) *BroacastManagerCtx {
|
|
|
|
return &BroacastManagerCtx{
|
|
|
|
logger: log.With().Str("module", "capture").Str("submodule", "broadcast").Logger(),
|
|
|
|
config: config,
|
|
|
|
enabled: false,
|
|
|
|
url: "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *BroacastManagerCtx) Start(url string) error {
|
|
|
|
manager.url = url
|
|
|
|
manager.enabled = true
|
|
|
|
return manager.createPipeline()
|
2020-11-15 01:46:12 +13:00
|
|
|
}
|
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
func (manager *BroacastManagerCtx) Stop() {
|
|
|
|
manager.enabled = false
|
|
|
|
manager.destroyPipeline()
|
2020-11-15 01:46:12 +13:00
|
|
|
}
|
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
func (manager *BroacastManagerCtx) Enabled() bool {
|
|
|
|
return manager.enabled
|
2020-11-15 01:46:12 +13:00
|
|
|
}
|
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
func (manager *BroacastManagerCtx) Url() string {
|
|
|
|
return manager.url
|
2020-11-15 01:46:12 +13:00
|
|
|
}
|
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
func (manager *BroacastManagerCtx) createPipeline() error {
|
2020-11-02 04:09:48 +13:00
|
|
|
var err error
|
|
|
|
|
|
|
|
manager.logger.Info().
|
|
|
|
Str("audio_device", manager.config.Device).
|
|
|
|
Str("video_display", manager.config.Display).
|
2020-11-02 05:34:47 +13:00
|
|
|
Str("broadcast_pipeline", manager.config.BroadcastPipeline).
|
2021-01-23 02:09:47 +13:00
|
|
|
Msgf("creating pipeline")
|
2021-02-03 06:28:32 +13:00
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
manager.pipeline, err = gst.CreateRTMPPipeline(
|
2020-11-02 04:09:48 +13:00
|
|
|
manager.config.Device,
|
|
|
|
manager.config.Display,
|
|
|
|
manager.config.BroadcastPipeline,
|
2021-01-23 02:09:47 +13:00
|
|
|
manager.url,
|
2020-11-02 04:09:48 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-11-19 09:34:39 +13:00
|
|
|
return err
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
manager.pipeline.Play()
|
|
|
|
manager.logger.Info().Msgf("starting pipeline")
|
2020-11-19 09:34:39 +13:00
|
|
|
return nil
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
func (manager *BroacastManagerCtx) destroyPipeline() {
|
|
|
|
if manager.pipeline == nil {
|
2020-11-02 04:09:48 +13:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
manager.pipeline.Stop()
|
|
|
|
manager.logger.Info().Msgf("stopping pipeline")
|
|
|
|
manager.pipeline = nil
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|