2020-04-06 15:42:42 +12:00
|
|
|
package broadcast
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/rs/zerolog"
|
2020-04-19 08:57:28 +12:00
|
|
|
"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
|
2020-09-27 23:06:13 +13:00
|
|
|
config *config.Broadcast
|
2020-09-27 11:10:34 +13:00
|
|
|
enabled bool
|
|
|
|
url string
|
2020-04-06 15:42:42 +12:00
|
|
|
}
|
|
|
|
|
2020-09-27 23:06:13 +13: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,
|
2020-09-27 23:06:13 +13:00
|
|
|
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-19 08:57:28 +12:00
|
|
|
}
|
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-04-19 08:57:28 +12:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|