neko/server/internal/broadcast/manager.go

105 lines
1.9 KiB
Go
Raw Normal View History

2020-04-06 15:42:42 +12:00
package broadcast
import (
2021-08-16 01:37:27 +12:00
"sync"
2020-04-06 15:42:42 +12:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
2020-09-27 10:04:15 +13:00
2021-10-06 09:38:24 +13:00
"m1k1o/neko/internal/gst"
"m1k1o/neko/internal/types/config"
2020-04-06 15:42:42 +12:00
)
type BroadcastManager struct {
2021-08-16 01:37:27 +12:00
mu sync.Mutex
2020-04-06 15:42:42 +12:00
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,
enabled: config.Enabled,
url: config.URL,
2020-04-06 15:42:42 +12:00
}
}
2021-10-06 10:10:10 +13:00
func (manager *BroadcastManager) Shutdown() error {
manager.Destroy()
return nil
}
2021-08-16 01:37:27 +12:00
func (manager *BroadcastManager) Start() error {
2020-09-27 11:10:34 +13:00
if !manager.enabled || manager.IsActive() {
2021-08-16 01:37:27 +12:00
return nil
2020-09-27 10:04:15 +13:00
}
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
2021-08-16 01:37:27 +12:00
if err != nil {
manager.pipeline = nil
return err
}
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...")
manager.pipeline.Play()
2021-08-16 01:37:27 +12:00
return nil
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
}
2021-08-16 01:37:27 +12:00
func (manager *BroadcastManager) Create(url string) error {
manager.mu.Lock()
defer manager.mu.Unlock()
2020-09-27 11:10:34 +13:00
manager.url = url
manager.enabled = true
2021-08-16 01:37:27 +12:00
err := manager.Start()
if err != nil {
manager.enabled = false
}
return err
2020-09-27 11:10:34 +13:00
}
func (manager *BroadcastManager) Destroy() {
2021-08-16 01:37:27 +12:00
manager.mu.Lock()
defer manager.mu.Unlock()
2020-09-27 11:10:34 +13:00
manager.Stop()
manager.enabled = false
}
func (manager *BroadcastManager) GetUrl() string {
return manager.url
}