2020-11-02 04:09:48 +13:00
|
|
|
package capture
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/kataras/go-events"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
|
|
|
"demodesk/neko/internal/types"
|
|
|
|
"demodesk/neko/internal/config"
|
|
|
|
"demodesk/neko/internal/capture/gst"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CaptureManagerCtx struct {
|
2020-11-08 05:22:25 +13:00
|
|
|
logger zerolog.Logger
|
|
|
|
video *gst.Pipeline
|
|
|
|
audio *gst.Pipeline
|
|
|
|
broadcast *gst.Pipeline
|
|
|
|
config *config.Capture
|
2020-11-14 10:22:44 +13:00
|
|
|
emit_update chan bool
|
|
|
|
emit_stop chan bool
|
|
|
|
video_sample chan types.Sample
|
|
|
|
audio_sample chan types.Sample
|
2020-11-08 05:22:25 +13:00
|
|
|
emmiter events.EventEmmiter
|
|
|
|
streaming bool
|
|
|
|
broadcasting bool
|
|
|
|
broadcast_url string
|
|
|
|
desktop types.DesktopManager
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(desktop types.DesktopManager, config *config.Capture) *CaptureManagerCtx {
|
|
|
|
return &CaptureManagerCtx{
|
2020-11-08 05:22:25 +13:00
|
|
|
logger: log.With().Str("module", "capture").Logger(),
|
2020-11-14 10:22:44 +13:00
|
|
|
emit_update: make(chan bool),
|
|
|
|
emit_stop: make(chan bool),
|
2020-11-08 05:22:25 +13:00
|
|
|
emmiter: events.New(),
|
|
|
|
config: config,
|
|
|
|
streaming: false,
|
|
|
|
broadcasting: false,
|
|
|
|
broadcast_url: "",
|
|
|
|
desktop: desktop,
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *CaptureManagerCtx) Start() {
|
2020-11-15 01:46:12 +13:00
|
|
|
manager.createBroadcastPipeline()
|
2020-11-02 04:09:48 +13:00
|
|
|
|
2020-11-08 05:22:25 +13:00
|
|
|
manager.desktop.OnBeforeScreenSizeChange(func() {
|
2020-11-14 09:23:48 +13:00
|
|
|
manager.destroyVideoPipeline()
|
2020-11-15 01:46:12 +13:00
|
|
|
manager.destroyBroadcastPipeline()
|
2020-11-08 05:22:25 +13:00
|
|
|
})
|
2020-11-02 04:09:48 +13:00
|
|
|
|
2020-11-08 05:22:25 +13:00
|
|
|
manager.desktop.OnAfterScreenSizeChange(func() {
|
2020-11-04 12:27:47 +13:00
|
|
|
manager.createVideoPipeline()
|
2020-11-15 01:46:12 +13:00
|
|
|
manager.createBroadcastPipeline()
|
2020-11-04 12:27:47 +13:00
|
|
|
})
|
2020-11-14 10:22:44 +13:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
manager.logger.Debug().Msg("started emitting samples")
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-manager.emit_stop:
|
|
|
|
manager.logger.Debug().Msg("stopped emitting samples")
|
|
|
|
return
|
|
|
|
case <-manager.emit_update:
|
|
|
|
manager.logger.Debug().Msg("update emitting samples")
|
|
|
|
case sample := <-manager.video_sample:
|
|
|
|
manager.emmiter.Emit("video", sample)
|
|
|
|
case sample := <-manager.audio_sample:
|
|
|
|
manager.emmiter.Emit("audio", sample)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *CaptureManagerCtx) Shutdown() error {
|
|
|
|
manager.logger.Info().Msgf("capture shutting down")
|
2020-11-08 05:22:25 +13:00
|
|
|
manager.StopStream()
|
2020-11-15 01:46:12 +13:00
|
|
|
|
|
|
|
manager.destroyBroadcastPipeline()
|
|
|
|
manager.emit_stop <- true
|
2020-11-02 04:09:48 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *CaptureManagerCtx) VideoCodec() string {
|
|
|
|
return manager.config.VideoCodec
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *CaptureManagerCtx) AudioCodec() string {
|
|
|
|
return manager.config.AudioCodec
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *CaptureManagerCtx) OnVideoFrame(listener func(sample types.Sample)) {
|
|
|
|
manager.emmiter.On("video", func(payload ...interface{}) {
|
|
|
|
listener(payload[0].(types.Sample))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *CaptureManagerCtx) OnAudioFrame(listener func(sample types.Sample)) {
|
|
|
|
manager.emmiter.On("audio", func(payload ...interface{}) {
|
|
|
|
listener(payload[0].(types.Sample))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *CaptureManagerCtx) StartStream() {
|
2020-11-08 05:22:25 +13:00
|
|
|
manager.logger.Info().Msgf("starting pipelines")
|
2020-11-02 04:09:48 +13:00
|
|
|
|
2020-11-02 08:18:19 +13:00
|
|
|
manager.createVideoPipeline()
|
|
|
|
manager.createAudioPipeline()
|
2020-11-02 04:09:48 +13:00
|
|
|
manager.streaming = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *CaptureManagerCtx) StopStream() {
|
2020-11-08 05:22:25 +13:00
|
|
|
manager.logger.Info().Msgf("stopping pipelines")
|
|
|
|
|
2020-11-14 09:23:48 +13:00
|
|
|
manager.destroyVideoPipeline()
|
|
|
|
manager.destroyAudioPipeline()
|
2020-11-02 04:09:48 +13:00
|
|
|
manager.streaming = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *CaptureManagerCtx) Streaming() bool {
|
|
|
|
return manager.streaming
|
|
|
|
}
|
|
|
|
|
2020-11-02 08:18:19 +13:00
|
|
|
func (manager *CaptureManagerCtx) createVideoPipeline() {
|
2020-11-02 04:09:48 +13:00
|
|
|
var err error
|
|
|
|
|
|
|
|
manager.logger.Info().
|
|
|
|
Str("video_codec", manager.config.VideoCodec).
|
|
|
|
Str("video_display", manager.config.Display).
|
|
|
|
Str("video_params", manager.config.VideoParams).
|
2020-11-08 05:22:25 +13:00
|
|
|
Msgf("creating video pipeline")
|
2020-11-02 04:09:48 +13:00
|
|
|
|
|
|
|
manager.video, err = gst.CreateAppPipeline(
|
|
|
|
manager.config.VideoCodec,
|
|
|
|
manager.config.Display,
|
|
|
|
manager.config.VideoParams,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
manager.logger.Panic().Err(err).Msg("unable to create video pipeline")
|
|
|
|
}
|
2020-11-02 08:18:19 +13:00
|
|
|
|
|
|
|
manager.logger.Info().
|
2020-11-08 05:22:25 +13:00
|
|
|
Str("src", manager.video.Src).
|
2020-11-14 05:42:04 +13:00
|
|
|
Msgf("starting video pipeline")
|
2020-11-02 08:18:19 +13:00
|
|
|
|
|
|
|
manager.video.Start()
|
|
|
|
|
2020-11-14 10:22:44 +13:00
|
|
|
manager.video_sample = manager.video.Sample
|
|
|
|
manager.emit_update <-true
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
|
|
|
|
2020-11-14 09:23:48 +13:00
|
|
|
func (manager *CaptureManagerCtx) destroyVideoPipeline() {
|
|
|
|
manager.logger.Info().Msgf("stopping video pipeline")
|
|
|
|
manager.video.Stop()
|
|
|
|
}
|
|
|
|
|
2020-11-02 08:18:19 +13:00
|
|
|
func (manager *CaptureManagerCtx) createAudioPipeline() {
|
2020-11-02 04:09:48 +13:00
|
|
|
var err error
|
|
|
|
|
|
|
|
manager.logger.Info().
|
|
|
|
Str("audio_codec", manager.config.AudioCodec).
|
|
|
|
Str("audio_display", manager.config.Device).
|
|
|
|
Str("audio_params", manager.config.AudioParams).
|
2020-11-08 05:22:25 +13:00
|
|
|
Msgf("creating audio pipeline")
|
2020-11-02 04:09:48 +13:00
|
|
|
|
|
|
|
manager.audio, err = gst.CreateAppPipeline(
|
|
|
|
manager.config.AudioCodec,
|
|
|
|
manager.config.Device,
|
|
|
|
manager.config.AudioParams,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
manager.logger.Panic().Err(err).Msg("unable to create audio pipeline")
|
|
|
|
}
|
|
|
|
|
2020-11-02 08:18:19 +13:00
|
|
|
manager.logger.Info().
|
2020-11-08 05:22:25 +13:00
|
|
|
Str("src", manager.audio.Src).
|
|
|
|
Msgf("starting audio pipeline")
|
2020-11-02 04:09:48 +13:00
|
|
|
|
2020-11-02 08:18:19 +13:00
|
|
|
manager.audio.Start()
|
|
|
|
|
2020-11-14 10:22:44 +13:00
|
|
|
manager.audio_sample = manager.audio.Sample
|
|
|
|
manager.emit_update <-true
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
2020-11-14 09:23:48 +13:00
|
|
|
|
|
|
|
func (manager *CaptureManagerCtx) destroyAudioPipeline() {
|
|
|
|
manager.logger.Info().Msgf("stopping audio pipeline")
|
|
|
|
manager.audio.Stop()
|
|
|
|
}
|