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"
|
2023-01-30 09:29:16 +13:00
|
|
|
"fmt"
|
2022-09-17 22:43:17 +12:00
|
|
|
|
|
|
|
"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"
|
2023-01-30 09:29:16 +13:00
|
|
|
"m1k1o/neko/internal/types/codec"
|
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
|
2023-01-30 09:29:16 +13:00
|
|
|
|
|
|
|
// source-sinks
|
|
|
|
screenshare *StreamSrcSinkManagerCtx
|
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
|
|
|
|
2023-01-30 05:40:07 +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) {
|
2023-01-30 05:40:07 +13:00
|
|
|
// 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"),
|
2023-01-30 09:29:16 +13:00
|
|
|
|
|
|
|
// source-sinks
|
|
|
|
screenshare: streamSrcSinkNew(config.ScreenshareEnabled, map[string]string{
|
|
|
|
codec.VP8().Name: "appsrc format=time is-live=true do-timestamp=true name=appsrc " +
|
|
|
|
fmt.Sprintf("! application/x-rtp, payload=%d, encoding-name=VP8-DRAFT-IETF-01 ", codec.VP8().PayloadType) +
|
|
|
|
"! rtpvp8depay " +
|
|
|
|
"! appsink name=appsink",
|
|
|
|
// TODO: Add support for more codecs.
|
|
|
|
}, "webcam"),
|
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
|
|
|
|
2023-01-22 11:43:04 +13:00
|
|
|
go func() {
|
|
|
|
for {
|
2023-01-30 06:00:56 +13:00
|
|
|
before, ok := <-manager.desktop.GetScreenSizeChangeChannel()
|
2023-01-30 01:28:03 +13:00
|
|
|
if !ok {
|
2023-01-30 06:00:56 +13:00
|
|
|
manager.logger.Info().Msg("screen size change channel was closed")
|
2023-01-30 01:28:03 +13:00
|
|
|
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
|
|
|
|
2023-01-30 09:29:16 +13:00
|
|
|
manager.screenshare.shutdown()
|
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
|
|
|
}
|
2023-01-30 09:29:16 +13:00
|
|
|
|
|
|
|
func (manager *CaptureManagerCtx) Screenshare() types.StreamSrcSinkManager {
|
|
|
|
return manager.screenshare
|
|
|
|
}
|