neko/server/internal/broadcast/manager.go

84 lines
1.6 KiB
Go
Raw Normal View History

2020-04-06 15:42:42 +12:00
package broadcast
import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
2021-03-30 00:03:25 +13:00
2020-04-06 15:42:42 +12:00
"n.eko.moe/neko/internal/gst"
"n.eko.moe/neko/internal/types/config"
)
type BroadcastManager struct {
logger zerolog.Logger
pipeline *gst.Pipeline
2021-03-30 00:03:25 +13:00
remote *config.Remote
2020-04-06 15:42:42 +12:00
config *config.Broadcast
2021-03-30 00:03:25 +13:00
enabled bool
url string
2020-04-06 15:42:42 +12:00
}
2021-03-30 00:03:25 +13:00
func New(remote *config.Remote, config *config.Broadcast) *BroadcastManager {
2020-04-06 15:42:42 +12:00
return &BroadcastManager{
2021-03-30 00:03:25 +13:00
logger: log.With().Str("module", "remote").Logger(),
remote: remote,
config: config,
enabled: false,
url: "",
2020-04-06 15:42:42 +12:00
}
}
func (manager *BroadcastManager) Start() {
2021-03-30 00:03:25 +13:00
if !manager.enabled || manager.IsActive() {
return
}
2020-04-06 15:42:42 +12:00
var err error
manager.pipeline, err = gst.CreateRTMPPipeline(
2021-03-30 00:03:25 +13:00
manager.remote.Device,
manager.remote.Display,
manager.config.Pipeline,
manager.url,
2020-04-06 15:42:42 +12:00
)
2021-03-30 00:03:25 +13:00
manager.logger.Info().
Str("audio_device", manager.remote.Device).
Str("video_display", manager.remote.Display).
Str("rtmp_pipeline_src", manager.pipeline.Src).
Msgf("RTMP pipeline is starting...")
2020-04-06 15:42:42 +12:00
if err != nil {
manager.logger.Panic().Err(err).Msg("unable to create rtmp pipeline")
2021-03-30 00:03:25 +13:00
return
}
2020-04-06 15:42:42 +12:00
2021-03-30 00:03:25 +13:00
manager.pipeline.Play()
2020-04-06 15:42:42 +12:00
}
2021-03-30 00:03:25 +13:00
func (manager *BroadcastManager) Stop() {
if !manager.IsActive() {
return
}
2021-03-30 00:03:25 +13:00
manager.pipeline.Stop()
manager.pipeline = nil
}
func (manager *BroadcastManager) IsActive() bool {
return manager.pipeline != nil
}
func (manager *BroadcastManager) Create(url string) {
manager.url = url
manager.enabled = true
manager.Start()
}
func (manager *BroadcastManager) Destroy() {
manager.Stop()
manager.enabled = false
}
func (manager *BroadcastManager) GetUrl() string {
return manager.url
2020-04-06 15:42:42 +12:00
}