2022-09-13 08:16:08 +12:00
|
|
|
package capture
|
2020-04-06 15:42:42 +12:00
|
|
|
|
|
|
|
import (
|
2021-08-16 01:37:27 +12:00
|
|
|
"sync"
|
|
|
|
|
2020-04-06 15:42:42 +12:00
|
|
|
"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
|
|
|
|
2022-09-13 08:16:08 +12:00
|
|
|
"m1k1o/neko/internal/capture/gst"
|
2022-09-13 08:18:18 +12:00
|
|
|
"m1k1o/neko/internal/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
|
2022-09-13 08:12:47 +12:00
|
|
|
capture *config.Capture
|
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
|
|
|
}
|
|
|
|
|
2022-09-13 08:16:08 +12:00
|
|
|
func NewBroadcast(capture *config.Capture, config *config.Broadcast) *BroadcastManager {
|
2020-04-06 15:42:42 +12:00
|
|
|
return &BroadcastManager{
|
2022-09-13 08:12:47 +12:00
|
|
|
logger: log.With().Str("module", "broadcast").Logger(),
|
|
|
|
capture: capture,
|
2020-09-27 23:06:13 +13:00
|
|
|
config: config,
|
2022-01-03 10:19:36 +13:00
|
|
|
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
|
2022-09-13 08:21:22 +12:00
|
|
|
manager.pipeline, err = CreateRTMPPipeline(
|
2022-09-13 08:12:47 +12:00
|
|
|
manager.capture.Device,
|
|
|
|
manager.capture.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().
|
2022-09-13 08:12:47 +12:00
|
|
|
Str("audio_device", manager.capture.Device).
|
|
|
|
Str("video_display", manager.capture.Display).
|
2020-09-27 10:04:15 +13:00
|
|
|
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-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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|