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-17 22:43:17 +12:00
|
|
|
"m1k1o/neko/internal/types"
|
2020-04-06 15:42:42 +12:00
|
|
|
)
|
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
type BroacastManagerCtx struct {
|
|
|
|
logger zerolog.Logger
|
|
|
|
mu sync.Mutex
|
|
|
|
|
|
|
|
pipeline *gst.Pipeline
|
|
|
|
pipelineMu sync.Mutex
|
2022-09-22 04:58:28 +12:00
|
|
|
pipelineFn func(url string) (string, error)
|
2022-09-17 22:43:17 +12:00
|
|
|
|
|
|
|
url string
|
|
|
|
started bool
|
2020-04-06 15:42:42 +12:00
|
|
|
}
|
|
|
|
|
2022-09-22 04:59:38 +12:00
|
|
|
func broadcastNew(pipelineFn func(url string) (string, error), defaultUrl string) *BroacastManagerCtx {
|
2022-09-17 22:43:17 +12:00
|
|
|
logger := log.With().
|
|
|
|
Str("module", "capture").
|
|
|
|
Str("submodule", "broadcast").
|
|
|
|
Logger()
|
|
|
|
|
|
|
|
return &BroacastManagerCtx{
|
|
|
|
logger: logger,
|
|
|
|
pipelineFn: pipelineFn,
|
2022-09-22 04:59:38 +12:00
|
|
|
url: defaultUrl,
|
|
|
|
started: defaultUrl != "",
|
2020-04-06 15:42:42 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
func (manager *BroacastManagerCtx) shutdown() {
|
|
|
|
manager.logger.Info().Msgf("shutdown")
|
2021-10-06 10:10:10 +13:00
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
manager.destroyPipeline()
|
|
|
|
}
|
2020-09-27 10:04:15 +13:00
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
func (manager *BroacastManagerCtx) Start(url string) error {
|
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
2020-09-27 10:04:15 +13:00
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
err := manager.createPipeline()
|
2021-08-16 01:37:27 +12:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
manager.url = url
|
|
|
|
manager.started = true
|
2021-08-16 01:37:27 +12:00
|
|
|
return nil
|
2020-04-06 15:42:42 +12:00
|
|
|
}
|
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
func (manager *BroacastManagerCtx) Stop() {
|
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
2020-04-19 08:57:28 +12:00
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
manager.started = false
|
|
|
|
manager.destroyPipeline()
|
2020-04-06 15:42:42 +12:00
|
|
|
}
|
2020-09-27 11:10:34 +13:00
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
func (manager *BroacastManagerCtx) Started() bool {
|
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
|
|
|
return manager.started
|
2020-09-27 11:10:34 +13:00
|
|
|
}
|
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
func (manager *BroacastManagerCtx) Url() string {
|
2021-08-16 01:37:27 +12:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
return manager.url
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *BroacastManagerCtx) createPipeline() error {
|
|
|
|
manager.pipelineMu.Lock()
|
|
|
|
defer manager.pipelineMu.Unlock()
|
2021-08-16 01:37:27 +12:00
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
if manager.pipeline != nil {
|
|
|
|
return types.ErrCapturePipelineAlreadyExists
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2022-09-22 04:58:28 +12:00
|
|
|
pipelineStr, err := manager.pipelineFn(manager.url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-17 22:43:17 +12:00
|
|
|
|
|
|
|
manager.logger.Info().
|
|
|
|
Str("url", manager.url).
|
2022-09-22 04:58:28 +12:00
|
|
|
Str("src", pipelineStr).
|
|
|
|
Msgf("starting pipeline")
|
2022-09-17 22:43:17 +12:00
|
|
|
|
2023-01-30 07:59:54 +13:00
|
|
|
manager.pipeline, err = gst.CreatePipeline(pipelineStr)
|
2021-08-16 01:37:27 +12:00
|
|
|
if err != nil {
|
2022-09-17 22:43:17 +12:00
|
|
|
return err
|
2021-08-16 01:37:27 +12:00
|
|
|
}
|
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
manager.pipeline.Play()
|
2021-08-16 01:37:27 +12:00
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
return nil
|
2020-09-27 11:10:34 +13:00
|
|
|
}
|
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
func (manager *BroacastManagerCtx) destroyPipeline() {
|
|
|
|
manager.pipelineMu.Lock()
|
|
|
|
defer manager.pipelineMu.Unlock()
|
|
|
|
|
|
|
|
if manager.pipeline == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.pipeline.Destroy()
|
|
|
|
manager.logger.Info().Msgf("destroying pipeline")
|
|
|
|
manager.pipeline = nil
|
2020-09-27 11:10:34 +13:00
|
|
|
}
|