2020-11-01 16:09:48 +01:00
|
|
|
package capture
|
|
|
|
|
|
|
|
import (
|
2021-02-05 13:58:02 +01:00
|
|
|
"strings"
|
2021-02-14 14:40:17 +01:00
|
|
|
"sync"
|
2021-02-05 12:49:02 +01:00
|
|
|
|
2021-01-22 14:09:47 +01:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2022-03-20 11:43:00 +01:00
|
|
|
"gitlab.com/demodesk/neko/server/pkg/gst"
|
|
|
|
"gitlab.com/demodesk/neko/server/pkg/types"
|
2020-11-01 16:09:48 +01:00
|
|
|
)
|
|
|
|
|
2021-01-22 14:09:47 +01:00
|
|
|
type BroacastManagerCtx struct {
|
2021-09-30 20:06:43 +02:00
|
|
|
logger zerolog.Logger
|
|
|
|
mu sync.Mutex
|
|
|
|
|
2021-02-14 14:40:17 +01:00
|
|
|
pipeline *gst.Pipeline
|
2021-09-30 20:06:43 +02:00
|
|
|
pipelineStr string
|
|
|
|
pipelineMu sync.Mutex
|
|
|
|
|
|
|
|
url string
|
|
|
|
started bool
|
2021-01-22 14:09:47 +01:00
|
|
|
}
|
|
|
|
|
2021-02-05 13:58:02 +01:00
|
|
|
func broadcastNew(pipelineStr string) *BroacastManagerCtx {
|
2021-09-30 20:06:43 +02:00
|
|
|
logger := log.With().
|
|
|
|
Str("module", "capture").
|
|
|
|
Str("submodule", "broadcast").
|
|
|
|
Logger()
|
|
|
|
|
2021-01-22 14:09:47 +01:00
|
|
|
return &BroacastManagerCtx{
|
2021-09-30 20:06:43 +02:00
|
|
|
logger: logger,
|
2021-02-14 14:40:17 +01:00
|
|
|
pipelineStr: pipelineStr,
|
|
|
|
url: "",
|
2021-09-30 20:06:43 +02:00
|
|
|
started: false,
|
2021-01-22 14:09:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 12:18:46 +01:00
|
|
|
func (manager *BroacastManagerCtx) shutdown() {
|
2021-09-02 00:00:29 +02:00
|
|
|
manager.logger.Info().Msgf("shutdown")
|
2021-02-05 12:18:46 +01:00
|
|
|
|
|
|
|
manager.destroyPipeline()
|
|
|
|
}
|
|
|
|
|
2021-01-22 14:09:47 +01:00
|
|
|
func (manager *BroacastManagerCtx) Start(url string) error {
|
2021-02-05 12:49:02 +01:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-02-05 12:41:02 +01:00
|
|
|
err := manager.createPipeline()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-22 14:09:47 +01:00
|
|
|
manager.url = url
|
2021-02-05 14:03:53 +01:00
|
|
|
manager.started = true
|
2021-02-05 12:41:02 +01:00
|
|
|
return nil
|
2020-11-14 13:46:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-22 14:09:47 +01:00
|
|
|
func (manager *BroacastManagerCtx) Stop() {
|
2021-02-05 12:49:02 +01:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-02-05 14:03:53 +01:00
|
|
|
manager.started = false
|
2021-01-22 14:09:47 +01:00
|
|
|
manager.destroyPipeline()
|
2020-11-14 13:46:12 +01:00
|
|
|
}
|
|
|
|
|
2021-02-05 14:03:53 +01:00
|
|
|
func (manager *BroacastManagerCtx) Started() bool {
|
2021-09-30 20:06:43 +02:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-02-05 14:03:53 +01:00
|
|
|
return manager.started
|
2020-11-14 13:46:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-22 14:09:47 +01:00
|
|
|
func (manager *BroacastManagerCtx) Url() string {
|
2021-09-30 20:06:43 +02:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-01-22 14:09:47 +01:00
|
|
|
return manager.url
|
2020-11-14 13:46:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-22 14:09:47 +01:00
|
|
|
func (manager *BroacastManagerCtx) createPipeline() error {
|
2021-09-30 20:06:43 +02:00
|
|
|
manager.pipelineMu.Lock()
|
|
|
|
defer manager.pipelineMu.Unlock()
|
|
|
|
|
2021-02-05 12:49:02 +01:00
|
|
|
if manager.pipeline != nil {
|
2021-08-29 17:09:13 +02:00
|
|
|
return types.ErrCapturePipelineAlreadyExists
|
2021-02-05 12:49:02 +01:00
|
|
|
}
|
|
|
|
|
2020-11-01 16:09:48 +01:00
|
|
|
var err error
|
|
|
|
|
2021-02-05 13:58:02 +01:00
|
|
|
// replace {url} with valid URL
|
|
|
|
pipelineStr := strings.Replace(manager.pipelineStr, "{url}", manager.url, 1)
|
|
|
|
|
2020-11-01 16:09:48 +01:00
|
|
|
manager.logger.Info().
|
2021-08-28 20:15:54 +02:00
|
|
|
Str("src", pipelineStr).
|
2021-02-05 13:58:02 +01:00
|
|
|
Msgf("starting pipeline")
|
2020-11-01 16:09:48 +01:00
|
|
|
|
2021-02-05 13:58:02 +01:00
|
|
|
manager.pipeline, err = gst.CreatePipeline(pipelineStr)
|
2020-11-01 16:09:48 +01:00
|
|
|
if err != nil {
|
2020-11-18 21:34:39 +01:00
|
|
|
return err
|
2020-11-01 16:09:48 +01:00
|
|
|
}
|
|
|
|
|
2021-01-22 14:09:47 +01:00
|
|
|
manager.pipeline.Play()
|
2020-11-18 21:34:39 +01:00
|
|
|
return nil
|
2020-11-01 16:09:48 +01:00
|
|
|
}
|
|
|
|
|
2021-01-22 14:09:47 +01:00
|
|
|
func (manager *BroacastManagerCtx) destroyPipeline() {
|
2021-09-30 20:06:43 +02:00
|
|
|
manager.pipelineMu.Lock()
|
|
|
|
defer manager.pipelineMu.Unlock()
|
|
|
|
|
2021-01-22 14:09:47 +01:00
|
|
|
if manager.pipeline == nil {
|
2020-11-01 16:09:48 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-05 18:16:26 +01:00
|
|
|
manager.pipeline.Destroy()
|
2021-02-05 13:58:02 +01:00
|
|
|
manager.logger.Info().Msgf("destroying pipeline")
|
2021-01-22 14:09:47 +01:00
|
|
|
manager.pipeline = nil
|
2020-11-01 16:09:48 +01:00
|
|
|
}
|