2020-11-02 04:09:48 +13:00
|
|
|
package capture
|
|
|
|
|
|
|
|
import (
|
2021-02-06 03:10:41 +13:00
|
|
|
"fmt"
|
2021-02-10 08:36:22 +13:00
|
|
|
"math"
|
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"
|
|
|
|
|
2021-02-15 02:40:17 +13:00
|
|
|
"demodesk/neko/internal/config"
|
2020-11-02 04:09:48 +13:00
|
|
|
"demodesk/neko/internal/types"
|
2021-02-07 06:18:49 +13:00
|
|
|
"demodesk/neko/internal/types/codec"
|
2020-11-02 04:09:48 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
type CaptureManagerCtx struct {
|
2021-02-15 02:40:17 +13:00
|
|
|
logger zerolog.Logger
|
|
|
|
desktop types.DesktopManager
|
|
|
|
streaming bool
|
|
|
|
broadcast *BroacastManagerCtx
|
|
|
|
screencast *ScreencastManagerCtx
|
|
|
|
audio *StreamManagerCtx
|
|
|
|
videos map[string]*StreamManagerCtx
|
|
|
|
videoIDs []string
|
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()
|
|
|
|
|
2021-02-06 03:10:41 +13:00
|
|
|
broadcastPipeline := config.BroadcastPipeline
|
|
|
|
if broadcastPipeline == "" {
|
|
|
|
broadcastPipeline = fmt.Sprintf(
|
2021-02-15 02:40:17 +13:00
|
|
|
"flvmux name=mux ! rtmpsink location='{url} live=1' "+
|
|
|
|
"pulsesrc device=%s "+
|
|
|
|
"! audio/x-raw,channels=2 "+
|
|
|
|
"! audioconvert "+
|
|
|
|
"! queue "+
|
2021-03-04 07:13:53 +13:00
|
|
|
"! voaacenc bitrate=128000 "+
|
2021-02-15 02:40:17 +13:00
|
|
|
"! mux. "+
|
|
|
|
"ximagesrc display-name=%s show-pointer=true use-damage=false "+
|
|
|
|
"! video/x-raw "+
|
|
|
|
"! videoconvert "+
|
|
|
|
"! queue "+
|
2021-03-04 07:13:53 +13:00
|
|
|
"! x264enc threads=4 bitrate=4096 key-int-max=15 byte-stream=true tune=zerolatency speed-preset=veryfast "+
|
2021-03-12 05:55:13 +13:00
|
|
|
"! mux.", config.AudioDevice, config.Display,
|
2021-02-06 03:10:41 +13:00
|
|
|
)
|
|
|
|
}
|
2021-02-06 01:58:02 +13:00
|
|
|
|
2021-02-06 03:10:41 +13:00
|
|
|
screencastPipeline := config.ScreencastPipeline
|
|
|
|
if screencastPipeline == "" {
|
|
|
|
screencastPipeline = fmt.Sprintf(
|
2021-02-15 02:40:17 +13:00
|
|
|
"ximagesrc display-name=%s show-pointer=true use-damage=false "+
|
|
|
|
"! video/x-raw,framerate=%s "+
|
|
|
|
"! videoconvert "+
|
|
|
|
"! queue "+
|
|
|
|
"! jpegenc quality=%s "+
|
2021-02-06 03:10:41 +13:00
|
|
|
"! appsink name=appsink", config.Display, config.ScreencastRate, config.ScreencastQuality,
|
|
|
|
)
|
2021-02-06 01:58:02 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
return &CaptureManagerCtx{
|
2021-02-15 02:40:17 +13:00
|
|
|
logger: logger,
|
|
|
|
desktop: desktop,
|
|
|
|
streaming: false,
|
|
|
|
broadcast: broadcastNew(broadcastPipeline),
|
|
|
|
screencast: screencastNew(config.Screencast, screencastPipeline),
|
|
|
|
audio: streamNew(config.AudioCodec, func() string {
|
2021-02-10 08:36:22 +13:00
|
|
|
if config.AudioPipeline != "" {
|
|
|
|
return config.AudioPipeline
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2021-02-10 08:36:22 +13:00
|
|
|
)
|
|
|
|
}),
|
2021-02-15 02:40:17 +13:00
|
|
|
videos: map[string]*StreamManagerCtx{
|
2021-02-10 08:36:22 +13:00
|
|
|
"hd": streamNew(codec.VP8(), func() string {
|
2021-02-15 02:40:17 +13:00
|
|
|
screen := desktop.GetScreenSize()
|
2021-03-04 10:30:45 +13:00
|
|
|
bitrate := int((screen.Width * screen.Height * 5) / 3)
|
2021-02-10 08:36:22 +13:00
|
|
|
|
|
|
|
return fmt.Sprintf(
|
2021-02-15 02:40:17 +13:00
|
|
|
"ximagesrc display-name=%s show-pointer=false use-damage=false "+
|
|
|
|
"! video/x-raw,framerate=25/1 "+
|
|
|
|
"! videoconvert "+
|
|
|
|
"! queue "+
|
2021-03-04 07:10:48 +13:00
|
|
|
"! vp8enc end-usage=cbr target-bitrate=%d cpu-used=16 threads=4 deadline=100000 undershoot=95 error-resilient=partitions keyframe-max-dist=15 auto-alt-ref=true min-quantizer=6 max-quantizer=12 "+
|
2021-02-10 08:36:22 +13:00
|
|
|
"! appsink name=appsink", config.Display, bitrate,
|
|
|
|
)
|
|
|
|
}),
|
|
|
|
"hq": streamNew(codec.VP8(), func() string {
|
2021-02-15 02:40:17 +13:00
|
|
|
screen := desktop.GetScreenSize()
|
|
|
|
width := int(math.Ceil(float64(screen.Width)/6) * 5)
|
|
|
|
height := int(math.Ceil(float64(screen.Height)/6) * 5)
|
2021-03-04 10:30:45 +13:00
|
|
|
bitrate := int((width * height * 5) / 3)
|
2021-02-10 08:36:22 +13:00
|
|
|
|
|
|
|
return fmt.Sprintf(
|
2021-02-15 02:40:17 +13:00
|
|
|
"ximagesrc display-name=%s show-pointer=false use-damage=false "+
|
|
|
|
"! video/x-raw,framerate=25/1 "+
|
|
|
|
"! videoconvert "+
|
|
|
|
"! queue "+
|
|
|
|
"! videoscale "+
|
|
|
|
"! video/x-raw,width=%d,height=%d "+
|
|
|
|
"! queue "+
|
2021-03-04 07:10:48 +13:00
|
|
|
"! vp8enc end-usage=cbr target-bitrate=%d cpu-used=16 threads=4 deadline=100000 undershoot=95 error-resilient=partitions keyframe-max-dist=15 auto-alt-ref=true min-quantizer=6 max-quantizer=12 "+
|
2021-02-10 08:36:22 +13:00
|
|
|
"! appsink name=appsink", config.Display, width, height, bitrate,
|
|
|
|
)
|
|
|
|
}),
|
|
|
|
"mq": streamNew(codec.VP8(), func() string {
|
2021-02-15 02:40:17 +13:00
|
|
|
screen := desktop.GetScreenSize()
|
|
|
|
width := int(math.Ceil(float64(screen.Width)/6) * 4)
|
|
|
|
height := int(math.Ceil(float64(screen.Height)/6) * 4)
|
2021-03-04 10:30:45 +13:00
|
|
|
bitrate := int((width * height * 5) / 3)
|
2021-02-10 08:36:22 +13:00
|
|
|
|
|
|
|
return fmt.Sprintf(
|
2021-02-15 02:40:17 +13:00
|
|
|
"ximagesrc display-name=%s show-pointer=false use-damage=false "+
|
|
|
|
"! video/x-raw,framerate=125/10 "+
|
|
|
|
"! videoconvert "+
|
|
|
|
"! queue "+
|
|
|
|
"! videoscale "+
|
|
|
|
"! video/x-raw,width=%d,height=%d "+
|
|
|
|
"! queue "+
|
2021-03-04 07:10:48 +13:00
|
|
|
"! vp8enc end-usage=cbr target-bitrate=%d cpu-used=16 threads=4 deadline=100000 undershoot=95 error-resilient=partitions keyframe-max-dist=15 auto-alt-ref=true min-quantizer=12 max-quantizer=24 "+
|
2021-02-10 08:36:22 +13:00
|
|
|
"! appsink name=appsink", config.Display, width, height, bitrate,
|
|
|
|
)
|
|
|
|
}),
|
|
|
|
"lq": streamNew(codec.VP8(), func() string {
|
2021-02-15 02:40:17 +13:00
|
|
|
screen := desktop.GetScreenSize()
|
|
|
|
width := int(math.Ceil(float64(screen.Width)/6) * 3)
|
|
|
|
height := int(math.Ceil(float64(screen.Height)/6) * 3)
|
2021-03-04 10:30:45 +13:00
|
|
|
bitrate := int((width * height * 5) / 3)
|
2021-02-10 08:36:22 +13:00
|
|
|
|
|
|
|
return fmt.Sprintf(
|
2021-02-15 02:40:17 +13:00
|
|
|
"ximagesrc display-name=%s show-pointer=false use-damage=false "+
|
|
|
|
"! video/x-raw,framerate=125/10 "+
|
|
|
|
"! videoconvert "+
|
|
|
|
"! queue "+
|
|
|
|
"! videoscale "+
|
|
|
|
"! video/x-raw,width=%d,height=%d "+
|
|
|
|
"! queue "+
|
2021-03-04 07:10:48 +13:00
|
|
|
"! vp8enc end-usage=cbr target-bitrate=%d cpu-used=16 threads=4 deadline=100000 undershoot=95 error-resilient=partitions keyframe-max-dist=15 auto-alt-ref=true min-quantizer=12 max-quantizer=24 "+
|
2021-02-10 08:36:22 +13:00
|
|
|
"! appsink name=appsink", config.Display, width, height, bitrate,
|
|
|
|
)
|
|
|
|
}),
|
2021-02-06 05:40:29 +13:00
|
|
|
},
|
2021-02-15 02:40:17 +13:00
|
|
|
videoIDs: []string{"hd", "hq", "mq", "lq"},
|
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() {
|
2021-02-06 05:40:29 +13:00
|
|
|
for _, video := range manager.videos {
|
|
|
|
if video.Started() {
|
|
|
|
video.destroyPipeline()
|
|
|
|
}
|
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() {
|
2021-02-06 05:40:29 +13:00
|
|
|
for _, video := range manager.videos {
|
|
|
|
if video.Started() {
|
|
|
|
if err := video.createPipeline(); err != nil {
|
|
|
|
manager.logger.Panic().Err(err).Msg("unable to recreate video pipeline")
|
|
|
|
}
|
2021-02-05 09:39:48 +13:00
|
|
|
}
|
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
|
|
|
if err := manager.broadcast.createPipeline(); err != nil {
|
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-01-23 06:13:32 +13:00
|
|
|
if err := manager.screencast.createPipeline(); err != nil {
|
|
|
|
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 {
|
|
|
|
manager.logger.Info().Msgf("capture shutting down")
|
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()
|
2021-02-06 05:40:29 +13:00
|
|
|
|
|
|
|
for _, video := range manager.videos {
|
|
|
|
video.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-02-05 09:39:48 +13:00
|
|
|
func (manager *CaptureManagerCtx) Audio() types.StreamManager {
|
|
|
|
return manager.audio
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
|
|
|
|
2021-02-06 06:07:58 +13:00
|
|
|
func (manager *CaptureManagerCtx) Video(videoID string) (types.StreamManager, bool) {
|
|
|
|
video, ok := manager.videos[videoID]
|
|
|
|
return video, ok
|
2021-02-06 05:40:29 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *CaptureManagerCtx) VideoIDs() []string {
|
|
|
|
return manager.videoIDs
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|