Archived
2
0
This repository has been archived on 2024-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
neko-custom/server/internal/capture/manager.go

99 lines
2.6 KiB
Go
Raw Normal View History

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()
2020-04-06 10:34:51 +12:00
2022-09-17 22:43:17 +12:00
return &CaptureManagerCtx{
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
}
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()
2022-09-17 22:43:17 +12:00
return nil
}
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
}