2022-09-13 08:12:47 +12:00
|
|
|
package capture
|
2020-04-06 10:34:51 +12:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2022-09-13 08:16:08 +12:00
|
|
|
"m1k1o/neko/internal/capture/gst"
|
2022-09-13 08:18:18 +12:00
|
|
|
"m1k1o/neko/internal/config"
|
2022-09-13 08:12:47 +12:00
|
|
|
"m1k1o/neko/internal/desktop/xorg"
|
2021-10-06 09:38:24 +13:00
|
|
|
"m1k1o/neko/internal/types"
|
|
|
|
|
2020-04-06 10:34:51 +12:00
|
|
|
"github.com/kataras/go-events"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
2022-09-13 08:12:47 +12:00
|
|
|
type CaptureManagerCtx struct {
|
2020-04-06 11:07:25 +12:00
|
|
|
logger zerolog.Logger
|
|
|
|
video *gst.Pipeline
|
|
|
|
audio *gst.Pipeline
|
2022-09-13 08:12:47 +12:00
|
|
|
config *config.Capture
|
2020-09-27 10:04:15 +13:00
|
|
|
broadcast types.BroadcastManager
|
2022-09-13 08:12:47 +12:00
|
|
|
desktop types.DesktopManager
|
2020-04-06 11:07:25 +12:00
|
|
|
cleanup *time.Ticker
|
|
|
|
shutdown chan bool
|
|
|
|
emmiter events.EventEmmiter
|
|
|
|
streaming bool
|
2020-04-06 10:34:51 +12:00
|
|
|
}
|
|
|
|
|
2022-09-13 08:12:47 +12:00
|
|
|
func New(desktop types.DesktopManager, broadcast types.BroadcastManager, config *config.Capture) *CaptureManagerCtx {
|
|
|
|
return &CaptureManagerCtx{
|
|
|
|
logger: log.With().Str("module", "capture").Logger(),
|
2020-04-06 11:07:25 +12:00
|
|
|
cleanup: time.NewTicker(1 * time.Second),
|
|
|
|
shutdown: make(chan bool),
|
|
|
|
emmiter: events.New(),
|
|
|
|
config: config,
|
2020-09-24 18:09:02 +12:00
|
|
|
broadcast: broadcast,
|
2022-09-13 08:12:47 +12:00
|
|
|
desktop: desktop,
|
2020-04-06 11:07:25 +12:00
|
|
|
streaming: false,
|
2020-04-06 10:34:51 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 08:12:47 +12:00
|
|
|
func (manager *CaptureManagerCtx) VideoCodec() string {
|
2020-04-06 10:34:51 +12:00
|
|
|
return manager.config.VideoCodec
|
|
|
|
}
|
|
|
|
|
2022-09-13 08:12:47 +12:00
|
|
|
func (manager *CaptureManagerCtx) AudioCodec() string {
|
2020-04-06 10:34:51 +12:00
|
|
|
return manager.config.AudioCodec
|
|
|
|
}
|
|
|
|
|
2022-09-13 08:12:47 +12:00
|
|
|
func (manager *CaptureManagerCtx) Start() {
|
2020-04-06 11:07:25 +12:00
|
|
|
manager.createPipelines()
|
2021-08-16 01:37:27 +12:00
|
|
|
if err := manager.broadcast.Start(); err != nil {
|
|
|
|
manager.logger.Panic().Err(err).Msg("unable to create rtmp pipeline")
|
|
|
|
}
|
2020-04-06 10:34:51 +12:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
manager.logger.Info().Msg("shutdown")
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-manager.shutdown:
|
|
|
|
return
|
|
|
|
case sample := <-manager.video.Sample:
|
|
|
|
manager.emmiter.Emit("video", sample)
|
|
|
|
case sample := <-manager.audio.Sample:
|
|
|
|
manager.emmiter.Emit("audio", sample)
|
|
|
|
case <-manager.cleanup.C:
|
2022-09-13 08:12:47 +12:00
|
|
|
// TODO: refactor.
|
2020-04-06 10:34:51 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2022-09-13 08:12:47 +12:00
|
|
|
func (manager *CaptureManagerCtx) Shutdown() error {
|
|
|
|
manager.logger.Info().Msgf("capture shutting down")
|
2020-04-06 10:34:51 +12:00
|
|
|
manager.video.Stop()
|
|
|
|
manager.audio.Stop()
|
2020-09-27 10:04:15 +13:00
|
|
|
manager.broadcast.Stop()
|
2020-09-24 18:09:02 +12:00
|
|
|
|
2020-04-06 10:34:51 +12:00
|
|
|
manager.cleanup.Stop()
|
|
|
|
manager.shutdown <- true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-13 08:12:47 +12:00
|
|
|
func (manager *CaptureManagerCtx) OnVideoFrame(listener func(sample types.Sample)) {
|
2020-04-06 10:34:51 +12:00
|
|
|
manager.emmiter.On("video", func(payload ...interface{}) {
|
|
|
|
listener(payload[0].(types.Sample))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-13 08:12:47 +12:00
|
|
|
func (manager *CaptureManagerCtx) OnAudioFrame(listener func(sample types.Sample)) {
|
2020-04-06 10:34:51 +12:00
|
|
|
manager.emmiter.On("audio", func(payload ...interface{}) {
|
|
|
|
listener(payload[0].(types.Sample))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-13 08:12:47 +12:00
|
|
|
func (manager *CaptureManagerCtx) StartStream() {
|
2020-09-27 10:04:15 +13:00
|
|
|
manager.createPipelines()
|
|
|
|
|
2020-04-06 10:34:51 +12:00
|
|
|
manager.logger.Info().
|
|
|
|
Str("video_display", manager.config.Display).
|
|
|
|
Str("video_codec", manager.config.VideoCodec).
|
|
|
|
Str("audio_device", manager.config.Device).
|
|
|
|
Str("audio_codec", manager.config.AudioCodec).
|
|
|
|
Str("audio_pipeline_src", manager.audio.Src).
|
|
|
|
Str("video_pipeline_src", manager.video.Src).
|
2020-04-06 11:07:25 +12:00
|
|
|
Msgf("Pipelines starting...")
|
2020-04-06 10:34:51 +12:00
|
|
|
|
|
|
|
manager.video.Start()
|
|
|
|
manager.audio.Start()
|
2020-04-06 11:07:25 +12:00
|
|
|
manager.streaming = true
|
2020-04-06 10:34:51 +12:00
|
|
|
}
|
|
|
|
|
2022-09-13 08:12:47 +12:00
|
|
|
func (manager *CaptureManagerCtx) StopStream() {
|
2020-04-06 11:07:25 +12:00
|
|
|
manager.logger.Info().Msgf("Pipelines shutting down...")
|
2020-04-06 10:34:51 +12:00
|
|
|
manager.video.Stop()
|
|
|
|
manager.audio.Stop()
|
2020-04-06 11:07:25 +12:00
|
|
|
manager.streaming = false
|
|
|
|
}
|
|
|
|
|
2022-09-13 08:12:47 +12:00
|
|
|
func (manager *CaptureManagerCtx) Streaming() bool {
|
2020-04-06 11:07:25 +12:00
|
|
|
return manager.streaming
|
|
|
|
}
|
|
|
|
|
2022-09-13 08:12:47 +12:00
|
|
|
func (manager *CaptureManagerCtx) createPipelines() {
|
2021-02-16 02:59:20 +13:00
|
|
|
// handle maximum fps
|
2022-09-13 08:12:47 +12:00
|
|
|
rate := int(manager.desktop.GetScreenSize().Rate)
|
|
|
|
if manager.config.MaxFPS != 0 && manager.config.MaxFPS < rate {
|
2021-02-16 02:59:20 +13:00
|
|
|
rate = manager.config.MaxFPS
|
|
|
|
}
|
|
|
|
|
2020-04-06 11:07:25 +12:00
|
|
|
var err error
|
2020-04-06 15:42:42 +12:00
|
|
|
manager.video, err = gst.CreateAppPipeline(
|
2020-04-06 11:07:25 +12:00
|
|
|
manager.config.VideoCodec,
|
|
|
|
manager.config.Display,
|
|
|
|
manager.config.VideoParams,
|
2021-02-16 02:59:20 +13:00
|
|
|
rate,
|
2021-02-15 09:07:56 +13:00
|
|
|
manager.config.VideoBitrate,
|
2022-03-18 08:25:17 +13:00
|
|
|
manager.config.VideoHWEnc,
|
2020-04-06 11:07:25 +12:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
manager.logger.Panic().Err(err).Msg("unable to create video pipeline")
|
|
|
|
}
|
|
|
|
|
2020-04-06 15:42:42 +12:00
|
|
|
manager.audio, err = gst.CreateAppPipeline(
|
2020-04-06 11:07:25 +12:00
|
|
|
manager.config.AudioCodec,
|
|
|
|
manager.config.Device,
|
|
|
|
manager.config.AudioParams,
|
2021-02-16 02:59:20 +13:00
|
|
|
0, // fps: n/a for audio
|
2021-02-15 09:07:56 +13:00
|
|
|
manager.config.AudioBitrate,
|
2022-03-18 08:25:17 +13:00
|
|
|
"", // hwenc: n/a for audio
|
2020-04-06 11:07:25 +12:00
|
|
|
)
|
|
|
|
if err != nil {
|
2020-09-24 18:09:02 +12:00
|
|
|
manager.logger.Panic().Err(err).Msg("unable to create audio pipeline")
|
|
|
|
}
|
2020-04-06 10:34:51 +12:00
|
|
|
}
|
|
|
|
|
2022-09-13 08:12:47 +12:00
|
|
|
func (manager *CaptureManagerCtx) ChangeResolution(width int, height int, rate int) error {
|
2020-04-06 10:34:51 +12:00
|
|
|
if !xorg.ValidScreenSize(width, height, rate) {
|
|
|
|
return fmt.Errorf("unknown configuration")
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.video.Stop()
|
2020-09-27 10:04:15 +13:00
|
|
|
manager.broadcast.Stop()
|
2020-09-24 18:09:02 +12:00
|
|
|
|
2020-04-06 10:34:51 +12:00
|
|
|
defer func() {
|
|
|
|
manager.video.Start()
|
2021-08-16 01:37:27 +12:00
|
|
|
if err := manager.broadcast.Start(); err != nil {
|
|
|
|
manager.logger.Panic().Err(err).Msg("unable to create rtmp pipeline")
|
|
|
|
}
|
2020-09-24 18:09:02 +12:00
|
|
|
|
2020-04-06 10:34:51 +12:00
|
|
|
manager.logger.Info().Msg("starting video pipeline...")
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := xorg.ChangeScreenSize(width, height, rate); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-16 02:59:20 +13:00
|
|
|
// handle maximum fps
|
|
|
|
if manager.config.MaxFPS != 0 && manager.config.MaxFPS < rate {
|
|
|
|
rate = manager.config.MaxFPS
|
|
|
|
}
|
|
|
|
|
2020-09-27 10:04:15 +13:00
|
|
|
var err error
|
|
|
|
manager.video, err = gst.CreateAppPipeline(
|
2020-04-06 10:34:51 +12:00
|
|
|
manager.config.VideoCodec,
|
|
|
|
manager.config.Display,
|
|
|
|
manager.config.VideoParams,
|
2021-02-16 02:59:20 +13:00
|
|
|
rate,
|
2021-02-15 09:07:56 +13:00
|
|
|
manager.config.VideoBitrate,
|
2022-03-18 08:25:17 +13:00
|
|
|
manager.config.VideoHWEnc,
|
2020-04-06 10:34:51 +12:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
manager.logger.Panic().Err(err).Msg("unable to create new video pipeline")
|
|
|
|
}
|
2020-09-24 18:09:02 +12:00
|
|
|
|
2020-04-06 10:34:51 +12:00
|
|
|
return nil
|
|
|
|
}
|