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