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
|
|
|
|
audio_emit_stop chan bool
|
|
|
|
video_emit_stop chan bool
|
|
|
|
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(),
|
|
|
|
audio_emit_stop: make(chan bool),
|
|
|
|
video_emit_stop: make(chan bool),
|
|
|
|
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-04 12:27:47 +13:00
|
|
|
manager.StartBroadcastPipeline()
|
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-04 12:27:47 +13:00
|
|
|
manager.StopBroadcastPipeline()
|
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()
|
|
|
|
manager.StartBroadcastPipeline()
|
|
|
|
})
|
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-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()
|
|
|
|
|
|
|
|
go func() {
|
2020-11-08 05:22:25 +13:00
|
|
|
manager.logger.Debug().Msg("started emitting video samples")
|
2020-11-02 08:18:19 +13:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2020-11-08 05:22:25 +13:00
|
|
|
case <-manager.video_emit_stop:
|
|
|
|
manager.logger.Debug().Msg("stopped emitting video samples")
|
2020-11-02 08:18:19 +13:00
|
|
|
return
|
|
|
|
case sample := <-manager.video.Sample:
|
|
|
|
manager.emmiter.Emit("video", sample)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
|
|
|
|
2020-11-14 09:23:48 +13:00
|
|
|
func (manager *CaptureManagerCtx) destroyVideoPipeline() {
|
|
|
|
manager.video_emit_stop <- true
|
|
|
|
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()
|
|
|
|
|
|
|
|
go func() {
|
2020-11-08 05:22:25 +13:00
|
|
|
manager.logger.Debug().Msg("started emitting audio samples")
|
2020-11-02 08:18:19 +13:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2020-11-08 05:22:25 +13:00
|
|
|
case <-manager.audio_emit_stop:
|
|
|
|
manager.logger.Debug().Msg("stopped emitting audio samples")
|
2020-11-02 08:18:19 +13:00
|
|
|
return
|
|
|
|
case sample := <-manager.audio.Sample:
|
|
|
|
manager.emmiter.Emit("audio", sample)
|
|
|
|
}
|
|
|
|
}
|
2020-11-02 04:09:48 +13:00
|
|
|
}()
|
|
|
|
}
|
2020-11-14 09:23:48 +13:00
|
|
|
|
|
|
|
func (manager *CaptureManagerCtx) destroyAudioPipeline() {
|
|
|
|
manager.audio_emit_stop <- true
|
|
|
|
manager.logger.Info().Msgf("stopping audio pipeline")
|
|
|
|
manager.audio.Stop()
|
|
|
|
}
|