neko/internal/capture/manager.go

285 lines
8.8 KiB
Go
Raw Normal View History

2020-11-01 16:09:48 +01:00
package capture
import (
"errors"
2021-02-05 15:10:41 +01:00
"fmt"
"strings"
2020-11-01 16:09:48 +01:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/demodesk/neko/internal/capture/buckets"
"github.com/demodesk/neko/internal/config"
"github.com/demodesk/neko/pkg/types"
"github.com/demodesk/neko/pkg/types/codec"
2020-11-01 16:09:48 +01:00
)
type CaptureManagerCtx struct {
logger zerolog.Logger
desktop types.DesktopManager
config *config.Capture
2021-12-01 22:36:45 +01:00
// sinks
2021-02-14 14:40:17 +01:00
broadcast *BroacastManagerCtx
screencast *ScreencastManagerCtx
2021-12-01 20:30:18 +01:00
audio *StreamSinkManagerCtx
video types.BucketsManager
2021-12-01 22:36:45 +01:00
// sources
webcam *StreamSrcManagerCtx
microphone *StreamSrcManagerCtx
2020-11-01 16:09:48 +01:00
}
func New(desktop types.DesktopManager, config *config.Capture) *CaptureManagerCtx {
2021-02-05 13:58:02 +01:00
logger := log.With().Str("module", "capture").Logger()
videos := map[string]types.StreamSinkManager{}
2021-08-28 20:15:54 +02:00
for video_id, cnf := range config.VideoPipelines {
pipelineConf := cnf
2021-03-29 00:58:51 +02:00
2022-09-17 17:52:55 +02:00
createPipeline := func() (string, error) {
if pipelineConf.GstPipeline != "" {
2022-09-17 18:00:25 +02:00
// replace {display} with valid display
2022-09-17 17:52:55 +02:00
return strings.Replace(pipelineConf.GstPipeline, "{display}", config.Display, 1), nil
}
2021-03-29 00:58:51 +02:00
screen := desktop.GetScreenSize()
pipeline, err := pipelineConf.GetPipeline(screen)
2021-03-29 00:58:51 +02:00
if err != nil {
2022-09-17 17:52:55 +02:00
return "", err
2021-03-29 00:58:51 +02:00
}
return fmt.Sprintf(
"ximagesrc display-name=%s show-pointer=false use-damage=false "+
2021-03-29 22:59:07 +02:00
"%s ! appsink name=appsink", config.Display, pipeline,
2022-09-17 17:52:55 +02:00
), nil
2021-03-29 00:58:51 +02:00
}
// trigger function to catch evaluation errors at startup
2022-09-17 17:52:55 +02:00
pipeline, err := createPipeline()
if err != nil {
logger.Panic().Err(err).
Str("video_id", video_id).
Msg("failed to create video pipeline")
}
logger.Info().
2021-08-28 20:15:54 +02:00
Str("video_id", video_id).
Str("pipeline", pipeline).
Msg("syntax check for video stream pipeline passed")
2021-03-29 00:58:51 +02:00
getVideoBitrate := pipelineConf.GetBitrateFn(desktop.GetScreenSize)
if _, err = getVideoBitrate(); err != nil {
logger.Panic().Err(err).Msg("unable to get video bitrate")
}
2021-03-29 00:58:51 +02:00
// append to videos
videos[video_id] = streamSinkNew(config.VideoCodec, createPipeline, video_id, getVideoBitrate)
2021-03-29 00:58:51 +02:00
}
2020-11-01 16:09:48 +01:00
return &CaptureManagerCtx{
logger: logger,
desktop: desktop,
config: config,
2021-12-01 22:36:45 +01:00
// sinks
2022-09-17 18:00:25 +02:00
broadcast: broadcastNew(func(url string) (string, error) {
if config.BroadcastPipeline != "" {
2022-09-21 19:05:42 +02:00
var pipeline = config.BroadcastPipeline
// replace {display} with valid display
pipeline = strings.Replace(pipeline, "{display}", config.Display, 1)
// replace {device} with valid device
pipeline = strings.Replace(pipeline, "{device}", config.AudioDevice, 1)
2022-09-17 18:00:25 +02:00
// replace {url} with valid URL
2022-09-21 19:05:42 +02:00
return strings.Replace(pipeline, "{url}", url, 1), nil
2022-09-17 18:00:25 +02:00
}
return fmt.Sprintf(
"flvmux name=mux ! rtmpsink location='%s live=1' "+
"pulsesrc device=%s "+
"! audio/x-raw,channels=2 "+
"! audioconvert "+
"! queue "+
"! voaacenc bitrate=%d "+
"! mux. "+
"ximagesrc display-name=%s show-pointer=true use-damage=false "+
"! video/x-raw "+
"! videoconvert "+
"! queue "+
"! x264enc threads=4 bitrate=%d key-int-max=15 byte-stream=true tune=zerolatency speed-preset=%s "+
"! mux.", url, config.AudioDevice, config.BroadcastAudioBitrate*1000, config.Display, config.BroadcastVideoBitrate, config.BroadcastPreset,
), nil
2022-09-21 19:08:29 +02:00
}, config.BroadcastUrl),
2022-09-21 19:05:42 +02:00
screencast: screencastNew(config.ScreencastEnabled, func() string {
if config.ScreencastPipeline != "" {
// replace {display} with valid display
return strings.Replace(config.ScreencastPipeline, "{display}", config.Display, 1)
}
return fmt.Sprintf(
"ximagesrc display-name=%s show-pointer=true use-damage=false "+
"! video/x-raw,framerate=%s "+
"! videoconvert "+
"! queue "+
"! jpegenc quality=%s "+
"! appsink name=appsink", config.Display, config.ScreencastRate, config.ScreencastQuality,
)
}()),
2022-09-17 17:52:55 +02:00
audio: streamSinkNew(config.AudioCodec, func() (string, error) {
2021-02-09 20:36:22 +01:00
if config.AudioPipeline != "" {
2022-09-17 18:00:25 +02:00
// replace {device} with valid device
return strings.Replace(config.AudioPipeline, "{device}", config.AudioDevice, 1), nil
2021-02-09 20:36:22 +01:00
}
return fmt.Sprintf(
2021-02-14 14:40:17 +01:00
"pulsesrc device=%s "+
"! audio/x-raw,channels=2 "+
"! audioconvert "+
"! queue "+
"! %s "+
2021-03-11 17:55:13 +01:00
"! appsink name=appsink", config.AudioDevice, config.AudioCodec.Pipeline,
2022-09-17 17:52:55 +02:00
), nil
}, "audio", nil),
video: buckets.BucketsNew(config.VideoCodec, videos, config.VideoIDs),
2021-12-01 22:36:45 +01:00
// sources
2021-12-09 23:22:24 +01:00
webcam: streamSrcNew(config.WebcamEnabled, map[string]string{
2021-12-05 22:06:42 +01:00
codec.VP8().Name: "appsrc format=time is-live=true do-timestamp=true name=appsrc " +
2021-12-02 23:06:21 +01:00
fmt.Sprintf("! application/x-rtp, payload=%d, encoding-name=VP8-DRAFT-IETF-01 ", codec.VP8().PayloadType) +
"! rtpvp8depay " +
"! decodebin " +
2021-12-05 16:35:04 +01:00
"! videoconvert " +
2021-12-05 22:25:35 +01:00
"! videorate " +
2022-01-08 23:53:45 +01:00
"! videoscale " +
fmt.Sprintf("! video/x-raw,width=%d,height=%d ", config.WebcamWidth, config.WebcamHeight) +
2021-12-05 22:11:52 +01:00
"! identity drop-allocation=true " +
2021-12-09 23:22:24 +01:00
fmt.Sprintf("! v4l2sink sync=false device=%s", config.WebcamDevice),
// TODO: Test this pipeline.
2021-12-05 22:06:42 +01:00
codec.VP9().Name: "appsrc format=time is-live=true do-timestamp=true name=appsrc " +
2021-12-02 23:06:21 +01:00
"! application/x-rtp " +
"! rtpvp9depay " +
"! decodebin " +
2021-12-05 16:35:04 +01:00
"! videoconvert " +
2021-12-05 22:25:35 +01:00
"! videorate " +
2022-01-08 23:53:45 +01:00
"! videoscale " +
fmt.Sprintf("! video/x-raw,width=%d,height=%d ", config.WebcamWidth, config.WebcamHeight) +
2021-12-05 22:11:52 +01:00
"! identity drop-allocation=true " +
2021-12-09 23:22:24 +01:00
fmt.Sprintf("! v4l2sink sync=false device=%s", config.WebcamDevice),
// TODO: Test this pipeline.
2021-12-05 22:06:42 +01:00
codec.H264().Name: "appsrc format=time is-live=true do-timestamp=true name=appsrc " +
2021-12-02 23:06:21 +01:00
"! application/x-rtp " +
"! rtph264depay " +
"! decodebin " +
2021-12-05 16:35:04 +01:00
"! videoconvert " +
2021-12-05 22:25:35 +01:00
"! videorate " +
2022-01-08 23:53:45 +01:00
"! videoscale " +
fmt.Sprintf("! video/x-raw,width=%d,height=%d ", config.WebcamWidth, config.WebcamHeight) +
2021-12-05 22:11:52 +01:00
"! identity drop-allocation=true " +
2021-12-09 23:22:24 +01:00
fmt.Sprintf("! v4l2sink sync=false device=%s", config.WebcamDevice),
2021-12-02 23:06:21 +01:00
}, "webcam"),
2021-12-09 23:22:24 +01:00
microphone: streamSrcNew(config.MicrophoneEnabled, map[string]string{
2021-12-05 22:06:42 +01:00
codec.Opus().Name: "appsrc format=time is-live=true do-timestamp=true name=appsrc " +
2021-12-02 23:06:21 +01:00
fmt.Sprintf("! application/x-rtp, payload=%d, encoding-name=OPUS ", codec.Opus().PayloadType) +
2021-12-01 22:36:45 +01:00
"! rtpopusdepay " +
"! decodebin " +
2021-12-09 23:22:24 +01:00
fmt.Sprintf("! pulsesink device=%s", config.MicrophoneDevice),
// TODO: Test this pipeline.
2021-12-05 22:06:42 +01:00
codec.G722().Name: "appsrc format=time is-live=true do-timestamp=true name=appsrc " +
2021-12-01 22:36:45 +01:00
"! application/x-rtp clock-rate=8000 " +
"! rtpg722depay " +
"! decodebin " +
2021-12-09 23:22:24 +01:00
fmt.Sprintf("! pulsesink device=%s", config.MicrophoneDevice),
2021-12-01 22:36:45 +01:00
}, "microphone"),
2020-11-01 16:09:48 +01:00
}
}
func (manager *CaptureManagerCtx) Start() {
2021-02-05 14:03:53 +01:00
if manager.broadcast.Started() {
if err := manager.broadcast.createPipeline(); err != nil {
2020-11-18 23:32:43 +01:00
manager.logger.Panic().Err(err).Msg("unable to create broadcast pipeline")
}
}
2020-11-01 16:09:48 +01:00
manager.desktop.OnBeforeScreenSizeChange(func() {
manager.video.DestroyAll()
2021-02-05 14:03:53 +01:00
if manager.broadcast.Started() {
manager.broadcast.destroyPipeline()
}
2021-01-22 18:13:32 +01:00
2021-01-23 15:17:52 +01:00
if manager.screencast.Started() {
2021-01-22 18:13:32 +01:00
manager.screencast.destroyPipeline()
}
})
2020-11-01 16:09:48 +01:00
manager.desktop.OnAfterScreenSizeChange(func() {
err := manager.video.RecreateAll()
if err != nil {
manager.logger.Panic().Err(err).Msg("unable to recreate video pipelines")
}
2021-02-05 14:03:53 +01:00
if manager.broadcast.Started() {
err := manager.broadcast.createPipeline()
if err != nil && !errors.Is(err, types.ErrCapturePipelineAlreadyExists) {
2021-01-22 18:13:32 +01:00
manager.logger.Panic().Err(err).Msg("unable to recreate broadcast pipeline")
}
}
2021-01-23 15:17:52 +01:00
if manager.screencast.Started() {
err := manager.screencast.createPipeline()
if err != nil && !errors.Is(err, types.ErrCapturePipelineAlreadyExists) {
2021-01-22 18:13:32 +01:00
manager.logger.Panic().Err(err).Msg("unable to recreate screencast pipeline")
2020-11-18 23:32:43 +01:00
}
}
2020-11-04 00:27:47 +01:00
})
2020-11-01 16:09:48 +01:00
}
func (manager *CaptureManagerCtx) Shutdown() error {
2021-09-02 00:00:29 +02:00
manager.logger.Info().Msgf("shutdown")
2021-02-02 18:28:32 +01:00
2021-02-05 12:18:46 +01:00
manager.broadcast.shutdown()
manager.screencast.shutdown()
manager.audio.shutdown()
manager.video.Shutdown()
2021-12-01 22:36:45 +01:00
manager.webcam.shutdown()
manager.microphone.shutdown()
2020-11-01 16:09:48 +01:00
return nil
}
func (manager *CaptureManagerCtx) GetBitrateFromVideoID(videoID string) (int, error) {
cfg, ok := manager.config.VideoPipelines[videoID]
if !ok {
return 0, fmt.Errorf("video config not found for %s", videoID)
}
return cfg.GetBitrateFn(manager.desktop.GetScreenSize)()
}
func (manager *CaptureManagerCtx) Broadcast() types.BroadcastManager {
return manager.broadcast
}
2021-01-22 18:13:32 +01:00
func (manager *CaptureManagerCtx) Screencast() types.ScreencastManager {
return manager.screencast
}
2021-12-01 20:30:18 +01:00
func (manager *CaptureManagerCtx) Audio() types.StreamSinkManager {
return manager.audio
2020-11-01 16:09:48 +01:00
}
func (manager *CaptureManagerCtx) Video() types.BucketsManager {
return manager.video
2020-11-01 16:09:48 +01:00
}
2021-12-01 22:36:45 +01:00
func (manager *CaptureManagerCtx) Webcam() types.StreamSrcManager {
return manager.webcam
}
func (manager *CaptureManagerCtx) Microphone() types.StreamSrcManager {
return manager.microphone
}