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

121 lines
3.1 KiB
Go
Raw Permalink 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
"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()
2023-01-30 05:51:39 +13:00
return &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) {
// use screen fps as default
fps := desktop.GetScreenSize().Rate
// if max fps is set, cap it to that value
if config.VideoMaxFPS > 0 && config.VideoMaxFPS < fps {
fps = config.VideoMaxFPS
}
return NewVideoPipeline(config.VideoCodec, config.Display, config.VideoPipeline, fps, 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
go gst.RunMainLoop()
2023-01-22 11:43:04 +13:00
go func() {
for {
2023-01-30 06:00:56 +13:00
before, ok := <-manager.desktop.GetScreenSizeChangeChannel()
if !ok {
2023-01-30 06:00:56 +13:00
manager.logger.Info().Msg("screen size change channel was closed")
return
}
2023-01-29 10:08:36 +13:00
2023-01-30 06:00:56 +13:00
if before {
// before screen size change, we need to destroy all pipelines
2020-09-24 18:09:02 +12:00
2023-01-30 06:00:56 +13:00
if manager.video.Started() {
manager.video.destroyPipeline()
}
2023-01-29 10:08:36 +13:00
2023-01-30 06:00:56 +13:00
if manager.broadcast.Started() {
manager.broadcast.destroyPipeline()
}
} else {
// after screen size change, we need to recreate all pipelines
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")
}
2023-01-22 11:43:04 +13:00
}
2020-04-06 10:34:51 +12:00
2023-01-30 06:00:56 +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")
}
2023-01-22 11:43:04 +13:00
}
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()
gst.QuitMainLoop()
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
}