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: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()
|
2023-01-22 11:43:04 +13:00
|
|
|
manager := &CaptureManagerCtx{
|
2022-09-17 22:43:17 +12:00
|
|
|
logger: logger,
|
|
|
|
desktop: desktop,
|
|
|
|
|
|
|
|
// sinks
|
2022-09-22 04:58:28 +12:00
|
|
|
broadcast: broadcastNew(func(url string) (string, error) {
|
2022-09-17 22:43:17 +12:00
|
|
|
return NewBroadcastPipeline(config.AudioDevice, config.Display, config.BroadcastPipeline, url)
|
2022-09-22 04:59:38 +12:00
|
|
|
}, config.BroadcastUrl),
|
2022-09-22 04:58:28 +12:00
|
|
|
audio: streamSinkNew(config.AudioCodec, func() (string, error) {
|
2022-09-17 22:43:17 +12:00
|
|
|
return NewAudioPipeline(config.AudioCodec, config.AudioDevice, config.AudioPipeline, config.AudioBitrate)
|
|
|
|
}, "audio"),
|
2022-09-22 04:58:28 +12:00
|
|
|
video: streamSinkNew(config.VideoCodec, func() (string, error) {
|
2022-09-17 22:43:17 +12:00
|
|
|
return NewVideoPipeline(config.VideoCodec, config.Display, config.VideoPipeline, config.VideoMaxFPS, config.VideoBitrate, config.VideoHWEnc)
|
2022-09-25 00:21:32 +12:00
|
|
|
}, "video"),
|
2022-09-17 22:43:17 +12:00
|
|
|
}
|
2023-01-22 11:43:04 +13:00
|
|
|
|
|
|
|
manager.Video().SetAdaptiveFramerate(config.VideoAdaptiveFramerate)
|
|
|
|
|
|
|
|
return manager
|
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
|
|
|
|
2023-01-22 11:43:04 +13:00
|
|
|
go func() {
|
|
|
|
for {
|
2023-01-29 10:08:36 +13:00
|
|
|
<-manager.desktop.GetBeforeScreenSizeChangeChannel()
|
|
|
|
|
2023-01-22 11:43:04 +13:00
|
|
|
if manager.video.Started() {
|
|
|
|
manager.video.destroyPipeline()
|
|
|
|
}
|
2020-09-24 18:09:02 +12:00
|
|
|
|
2023-01-22 11:43:04 +13:00
|
|
|
if manager.broadcast.Started() {
|
|
|
|
manager.broadcast.destroyPipeline()
|
2022-09-17 22:43:17 +12:00
|
|
|
}
|
|
|
|
}
|
2023-01-22 11:43:04 +13:00
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
2023-01-29 10:08:36 +13:00
|
|
|
framerate := <-manager.desktop.GetAfterScreenSizeChangeChannel()
|
|
|
|
|
2023-01-22 11:43:04 +13:00
|
|
|
if manager.video.Started() {
|
|
|
|
manager.video.SetChangeFramerate(framerate)
|
|
|
|
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
|
|
|
|
2023-01-22 11:43:04 +13: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")
|
|
|
|
}
|
2022-09-17 22:43:17 +12:00
|
|
|
}
|
|
|
|
}
|
2023-01-22 11:43:04 +13:00
|
|
|
}()
|
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
|
|
|
}
|