Archived
2
0
This repository has been archived on 2024-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
neko-custom/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"
2020-09-27 10:04:15 +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
2020-09-27 10:04:15 +13:00
remote *config.Remote
config *config.Broadcast
2020-09-27 11:10:34 +13:00
enabled bool
url string
2020-04-06 15:42:42 +12:00
}
func New(remote *config.Remote, config *config.Broadcast) *BroadcastManager {
2020-04-06 15:42:42 +12:00
return &BroadcastManager{
2020-09-27 11:10:34 +13:00
logger: log.With().Str("module", "remote").Logger(),
remote: remote,
config: config,
2020-09-27 11:10:34 +13:00
enabled: false,
url: "",
2020-04-06 15:42:42 +12:00
}
}
func (manager *BroadcastManager) Start() {
2020-09-27 11:10:34 +13:00
if !manager.enabled || manager.IsActive() {
2020-09-27 10:04:15 +13:00
return
}
2020-04-06 15:42:42 +12:00
var err error
manager.pipeline, err = gst.CreateRTMPPipeline(
2020-09-27 10:04:15 +13:00
manager.remote.Device,
manager.remote.Display,
2020-09-27 23:09:45 +13:00
manager.config.Pipeline,
2020-09-27 11:10:34 +13:00
manager.url,
2020-04-06 15:42:42 +12:00
)
2020-09-27 10:04:15 +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")
2020-09-27 10:04:15 +13:00
return
}
2020-04-06 15:42:42 +12:00
2020-09-27 10:04:15 +13:00
manager.pipeline.Play()
2020-04-06 15:42:42 +12:00
}
2020-09-27 10:04:15 +13:00
func (manager *BroadcastManager) Stop() {
2020-09-27 11:10:34 +13:00
if !manager.IsActive() {
2020-09-27 10:04:15 +13:00
return
}
2020-09-27 10:04:15 +13:00
manager.pipeline.Stop()
manager.pipeline = nil
2020-04-06 15:42:42 +12:00
}
2020-09-27 11:10:34 +13:00
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
}