neko/server/internal/capture/broadcast.go

122 lines
2.2 KiB
Go
Raw Normal View History

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"
"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
pipelineFn func(url string) (*gst.Pipeline, error)
url string
started bool
2020-04-06 15:42:42 +12:00
}
2022-09-17 22:43:17 +12:00
func broadcastNew(pipelineFn func(url string) (*gst.Pipeline, error), url string, started bool) *BroacastManagerCtx {
logger := log.With().
Str("module", "capture").
Str("submodule", "broadcast").
Logger()
return &BroacastManagerCtx{
logger: logger,
pipelineFn: pipelineFn,
url: url,
started: started,
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()
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
manager.logger.Info().
Str("url", manager.url).
Msgf("creating pipeline")
manager.pipeline, err = manager.pipelineFn(manager.url)
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.logger.Info().
Str("url", manager.url).
Str("src", manager.pipeline.Src).
Msgf("created pipeline")
2020-09-27 11:10:34 +13: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
}