2022-09-13 08:12:47 +12:00
|
|
|
package capture
|
2020-04-06 10:34:51 +12:00
|
|
|
|
|
|
|
import (
|
2022-09-17 22:43:17 +12:00
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2020-04-06 10:34:51 +12:00
|
|
|
|
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"
|
2021-10-06 09:38:24 +13:00
|
|
|
"m1k1o/neko/internal/types"
|
2020-04-06 10:34:51 +12:00
|
|
|
)
|
|
|
|
|
2022-09-13 08:12:47 +12:00
|
|
|
type CaptureManagerCtx struct {
|
2022-09-17 22:43:17 +12:00
|
|
|
logger zerolog.Logger
|
|
|
|
desktop types.DesktopManager
|
2020-04-06 10:34:51 +12:00
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
// sinks
|
|
|
|
broadcast *BroacastManagerCtx
|
|
|
|
audio *StreamSinkManagerCtx
|
|
|
|
video *StreamSinkManagerCtx
|
2020-04-06 10:34:51 +12:00
|
|
|
}
|
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
func New(desktop types.DesktopManager, config *config.Capture) *CaptureManagerCtx {
|
|
|
|
logger := log.With().Str("module", "capture").Logger()
|
2020-04-06 10:34:51 +12:00
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
return &CaptureManagerCtx{
|
|
|
|
logger: logger,
|
|
|
|
desktop: desktop,
|
|
|
|
|
|
|
|
// sinks
|
|
|
|
broadcast: broadcastNew(func(url string) (*gst.Pipeline, error) {
|
|
|
|
return NewBroadcastPipeline(config.AudioDevice, config.Display, config.BroadcastPipeline, url)
|
|
|
|
}, config.BroadcastUrl, config.BroadcastStarted),
|
|
|
|
audio: streamSinkNew(config.AudioCodec, func() (*gst.Pipeline, error) {
|
|
|
|
return NewAudioPipeline(config.AudioCodec, config.AudioDevice, config.AudioPipeline, config.AudioBitrate)
|
|
|
|
}, "audio"),
|
|
|
|
video: streamSinkNew(config.VideoCodec, func() (*gst.Pipeline, error) {
|
|
|
|
return NewVideoPipeline(config.VideoCodec, config.Display, config.VideoPipeline, config.VideoMaxFPS, config.VideoBitrate, config.VideoHWEnc)
|
|
|
|
}, "audio"),
|
|
|
|
}
|
2020-04-06 10:34:51 +12:00
|
|
|
}
|
|
|
|
|
2022-09-13 08:12:47 +12:00
|
|
|
func (manager *CaptureManagerCtx) Start() {
|
2022-09-17 22:43:17 +12:00
|
|
|
if manager.broadcast.Started() {
|
|
|
|
if err := manager.broadcast.createPipeline(); err != nil {
|
|
|
|
manager.logger.Panic().Err(err).Msg("unable to create broadcast pipeline")
|
|
|
|
}
|
2021-08-16 01:37:27 +12:00
|
|
|
}
|
2020-04-06 10:34:51 +12:00
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
manager.desktop.OnBeforeScreenSizeChange(func() {
|
|
|
|
if manager.video.Started() {
|
|
|
|
manager.video.destroyPipeline()
|
2020-04-06 10:34:51 +12:00
|
|
|
}
|
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
if manager.broadcast.Started() {
|
|
|
|
manager.broadcast.destroyPipeline()
|
|
|
|
}
|
|
|
|
})
|
2020-09-24 18:09:02 +12:00
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
manager.desktop.OnAfterScreenSizeChange(func() {
|
|
|
|
if manager.video.Started() {
|
|
|
|
err := manager.video.createPipeline()
|
|
|
|
if err != nil && !errors.Is(err, types.ErrCapturePipelineAlreadyExists) {
|
|
|
|
manager.logger.Panic().Err(err).Msg("unable to recreate video pipeline")
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 10:34:51 +12:00
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
if manager.broadcast.Started() {
|
|
|
|
err := manager.broadcast.createPipeline()
|
|
|
|
if err != nil && !errors.Is(err, types.ErrCapturePipelineAlreadyExists) {
|
|
|
|
manager.logger.Panic().Err(err).Msg("unable to recreate broadcast pipeline")
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 10:34:51 +12:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
func (manager *CaptureManagerCtx) Shutdown() error {
|
|
|
|
manager.logger.Info().Msgf("shutdown")
|
2020-04-06 10:34:51 +12:00
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
manager.broadcast.shutdown()
|
2020-04-06 10:34:51 +12:00
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
manager.audio.shutdown()
|
|
|
|
manager.video.shutdown()
|
2020-04-06 11:07:25 +12:00
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
return nil
|
2020-04-06 11:07:25 +12:00
|
|
|
}
|
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
func (manager *CaptureManagerCtx) Broadcast() types.BroadcastManager {
|
|
|
|
return manager.broadcast
|
2020-04-06 10:34:51 +12:00
|
|
|
}
|
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
func (manager *CaptureManagerCtx) Audio() types.StreamSinkManager {
|
|
|
|
return manager.audio
|
|
|
|
}
|
2020-09-24 18:09:02 +12:00
|
|
|
|
2022-09-17 22:43:17 +12:00
|
|
|
func (manager *CaptureManagerCtx) Video() types.StreamSinkManager {
|
|
|
|
return manager.video
|
2020-04-06 10:34:51 +12:00
|
|
|
}
|