2020-11-02 04:09:48 +13:00
|
|
|
package capture
|
|
|
|
|
|
|
|
import (
|
2021-02-06 00:49:02 +13:00
|
|
|
"fmt"
|
2021-02-06 01:58:02 +13:00
|
|
|
"strings"
|
2021-02-15 02:40:17 +13:00
|
|
|
"sync"
|
2021-02-06 00:49:02 +13:00
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
"demodesk/neko/internal/capture/gst"
|
|
|
|
)
|
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
type BroacastManagerCtx struct {
|
2021-02-15 02:40:17 +13:00
|
|
|
logger zerolog.Logger
|
|
|
|
mu sync.Mutex
|
|
|
|
pipelineStr string
|
|
|
|
pipeline *gst.Pipeline
|
|
|
|
started bool
|
|
|
|
url string
|
2021-01-23 02:09:47 +13:00
|
|
|
}
|
|
|
|
|
2021-02-06 01:58:02 +13:00
|
|
|
func broadcastNew(pipelineStr string) *BroacastManagerCtx {
|
2021-01-23 02:09:47 +13:00
|
|
|
return &BroacastManagerCtx{
|
2021-02-15 02:40:17 +13:00
|
|
|
logger: log.With().Str("module", "capture").Str("submodule", "broadcast").Logger(),
|
|
|
|
pipelineStr: pipelineStr,
|
|
|
|
started: false,
|
|
|
|
url: "",
|
2021-01-23 02:09:47 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 00:18:46 +13:00
|
|
|
func (manager *BroacastManagerCtx) shutdown() {
|
|
|
|
manager.logger.Info().Msgf("shutting down")
|
|
|
|
|
|
|
|
manager.destroyPipeline()
|
|
|
|
}
|
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
func (manager *BroacastManagerCtx) Start(url string) error {
|
2021-02-06 00:49:02 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-02-06 00:41:02 +13:00
|
|
|
err := manager.createPipeline()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
manager.url = url
|
2021-02-06 02:03:53 +13:00
|
|
|
manager.started = true
|
2021-02-06 00:41:02 +13:00
|
|
|
return nil
|
2020-11-15 01:46:12 +13:00
|
|
|
}
|
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
func (manager *BroacastManagerCtx) Stop() {
|
2021-02-06 00:49:02 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-02-06 02:03:53 +13:00
|
|
|
manager.started = false
|
2021-01-23 02:09:47 +13:00
|
|
|
manager.destroyPipeline()
|
2020-11-15 01:46:12 +13:00
|
|
|
}
|
|
|
|
|
2021-02-06 02:03:53 +13:00
|
|
|
func (manager *BroacastManagerCtx) Started() bool {
|
|
|
|
return manager.started
|
2020-11-15 01:46:12 +13:00
|
|
|
}
|
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
func (manager *BroacastManagerCtx) Url() string {
|
|
|
|
return manager.url
|
2020-11-15 01:46:12 +13:00
|
|
|
}
|
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
func (manager *BroacastManagerCtx) createPipeline() error {
|
2021-02-06 00:49:02 +13:00
|
|
|
if manager.pipeline != nil {
|
2021-02-06 01:58:02 +13:00
|
|
|
return fmt.Errorf("pipeline already exists")
|
2021-02-06 00:49:02 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
var err error
|
|
|
|
|
2021-02-06 01:58:02 +13:00
|
|
|
// replace {url} with valid URL
|
|
|
|
pipelineStr := strings.Replace(manager.pipelineStr, "{url}", manager.url, 1)
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
manager.logger.Info().
|
2021-08-29 06:15:54 +12:00
|
|
|
Str("src", pipelineStr).
|
2021-02-06 01:58:02 +13:00
|
|
|
Msgf("starting pipeline")
|
2020-11-02 04:09:48 +13:00
|
|
|
|
2021-02-06 01:58:02 +13:00
|
|
|
manager.pipeline, err = gst.CreatePipeline(pipelineStr)
|
2020-11-02 04:09:48 +13:00
|
|
|
if err != nil {
|
2020-11-19 09:34:39 +13:00
|
|
|
return err
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
manager.pipeline.Play()
|
2020-11-19 09:34:39 +13:00
|
|
|
return nil
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
func (manager *BroacastManagerCtx) destroyPipeline() {
|
|
|
|
if manager.pipeline == nil {
|
2020-11-02 04:09:48 +13:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-23 02:09:47 +13:00
|
|
|
manager.pipeline.Stop()
|
2021-02-06 01:58:02 +13:00
|
|
|
manager.logger.Info().Msgf("destroying pipeline")
|
2021-01-23 02:09:47 +13:00
|
|
|
manager.pipeline = nil
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|