add screen-size aware pipelines.

This commit is contained in:
Miroslav Šedivý 2021-02-09 20:36:22 +01:00
parent bac69a205f
commit c0c14b3ac2
2 changed files with 87 additions and 49 deletions

View File

@ -3,6 +3,7 @@ package capture
import ( import (
"fmt" "fmt"
"sync" "sync"
"math"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -58,58 +59,94 @@ func New(desktop types.DesktopManager, config *config.Capture) *CaptureManagerCt
) )
} }
audioPipeline := config.AudioPipeline
if audioPipeline == "" {
audioPipeline = fmt.Sprintf(
"pulsesrc device=%s " +
"! audio/x-raw,channels=2 " +
"! audioconvert " +
"! queue " +
"! %s " +
"! appsink name=appsink", config.Device, config.AudioCodec.Pipeline,
)
}
return &CaptureManagerCtx{ return &CaptureManagerCtx{
logger: logger, logger: logger,
desktop: desktop, desktop: desktop,
streaming: false, streaming: false,
broadcast: broadcastNew(broadcastPipeline), broadcast: broadcastNew(broadcastPipeline),
screencast: screencastNew(config.Screencast, screencastPipeline), screencast: screencastNew(config.Screencast, screencastPipeline),
audio: streamNew(config.AudioCodec, audioPipeline), audio: streamNew(config.AudioCodec, func() string {
if config.AudioPipeline != "" {
return config.AudioPipeline
}
return fmt.Sprintf(
"pulsesrc device=%s " +
"! audio/x-raw,channels=2 " +
"! audioconvert " +
"! queue " +
"! %s " +
"! appsink name=appsink", config.Device, config.AudioCodec.Pipeline,
)
}),
videos: map[string]*StreamManagerCtx{ videos: map[string]*StreamManagerCtx{
"hd": streamNew(codec.VP8(), fmt.Sprintf( "hd": streamNew(codec.VP8(), func() string {
"ximagesrc display-name=%s show-pointer=false use-damage=false " + screen := desktop.GetScreenSize()
"! video/x-raw,framerate=25/1 " + bitrate := screen.Width * screen.Height * 12
"! videoconvert " +
"! queue " + return fmt.Sprintf(
"! vp8enc target-bitrate=24576000 cpu-used=16 threads=4 deadline=1 error-resilient=partitions keyframe-max-dist=15 static-threshold=20 " + "ximagesrc display-name=%s show-pointer=false use-damage=false " +
"! appsink name=appsink", config.Display, "! video/x-raw,framerate=25/1 " +
)), "! videoconvert " +
"hq": streamNew(codec.VP8(), fmt.Sprintf( "! queue " +
"ximagesrc display-name=%s show-pointer=false use-damage=false " + "! vp8enc target-bitrate=%d cpu-used=16 threads=4 deadline=100000 error-resilient=partitions keyframe-max-dist=15 auto-alt-ref=true min-quantizer=6 max-quantizer=12 " +
"! video/x-raw,framerate=25/1 " + "! appsink name=appsink", config.Display, bitrate,
"! videoconvert " + )
"! queue " + }),
"! vp8enc target-bitrate=16588800 cpu-used=16 threads=4 deadline=1 error-resilient=partitions keyframe-max-dist=15 static-threshold=20 " + "hq": streamNew(codec.VP8(), func() string {
"! appsink name=appsink", config.Display, screen := desktop.GetScreenSize()
)), width := int(math.Ceil(float64(screen.Width) / 6) * 5)
"mq": streamNew(codec.VP8(), fmt.Sprintf( height := int(math.Ceil(float64(screen.Height) / 6) * 5)
"ximagesrc display-name=%s show-pointer=false use-damage=false " + bitrate := width * height * 12
"! video/x-raw,framerate=125/10 " +
"! videoconvert " + return fmt.Sprintf(
"! queue " + "ximagesrc display-name=%s show-pointer=false use-damage=false " +
"! vp8enc target-bitrate=9216000 cpu-used=16 threads=4 deadline=1 error-resilient=partitions keyframe-max-dist=15 static-threshold=20 " + "! video/x-raw,framerate=25/1 " +
"! appsink name=appsink", config.Display, "! videoconvert " +
)), "! queue " +
"lq": streamNew(codec.VP8(), fmt.Sprintf( "! videoscale " +
"ximagesrc display-name=%s show-pointer=false use-damage=false " + "! video/x-raw,width=%d,height=%d " +
"! video/x-raw,framerate=125/10 " + "! queue " +
"! videoconvert " + "! vp8enc target-bitrate=%d cpu-used=16 threads=4 deadline=100000 error-resilient=partitions keyframe-max-dist=15 auto-alt-ref=true min-quantizer=6 max-quantizer=12 " +
"! queue " + "! appsink name=appsink", config.Display, width, height, bitrate,
"! vp8enc target-bitrate=4608000 cpu-used=16 threads=4 deadline=1 error-resilient=partitions keyframe-max-dist=15 static-threshold=20 " + )
"! appsink name=appsink", config.Display, }),
)), "mq": streamNew(codec.VP8(), func() string {
screen := desktop.GetScreenSize()
width := int(math.Ceil(float64(screen.Width) / 6) * 4)
height := int(math.Ceil(float64(screen.Height) / 6) * 4)
bitrate := width * height * 8
return fmt.Sprintf(
"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 " +
"! vp8enc target-bitrate=%d cpu-used=16 threads=4 deadline=100000 error-resilient=partitions keyframe-max-dist=15 auto-alt-ref=true min-quantizer=12 max-quantizer=24 " +
"! appsink name=appsink", config.Display, width, height, bitrate,
)
}),
"lq": streamNew(codec.VP8(), func() string {
screen := desktop.GetScreenSize()
width := int(math.Ceil(float64(screen.Width) / 6) * 3)
height := int(math.Ceil(float64(screen.Height) / 6) * 3)
bitrate := width * height * 4
return fmt.Sprintf(
"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 " +
"! vp8enc target-bitrate=%d cpu-used=16 threads=4 deadline=100000 error-resilient=partitions keyframe-max-dist=15 auto-alt-ref=true min-quantizer=12 max-quantizer=24 " +
"! appsink name=appsink", config.Display, width, height, bitrate,
)
}),
}, },
videoIDs: []string{ "hd", "hq", "mq", "lq" }, videoIDs: []string{ "hd", "hq", "mq", "lq" },
} }

View File

@ -17,7 +17,7 @@ type StreamManagerCtx struct {
logger zerolog.Logger logger zerolog.Logger
mu sync.Mutex mu sync.Mutex
codec codec.RTPCodec codec codec.RTPCodec
pipelineStr string pipelineStr func() string
pipeline *gst.Pipeline pipeline *gst.Pipeline
sample chan types.Sample sample chan types.Sample
listeners map[uintptr]*func(sample types.Sample) listeners map[uintptr]*func(sample types.Sample)
@ -27,7 +27,7 @@ type StreamManagerCtx struct {
started bool started bool
} }
func streamNew(codec codec.RTPCodec, pipelineStr string) *StreamManagerCtx { func streamNew(codec codec.RTPCodec, pipelineStr func() string) *StreamManagerCtx {
manager := &StreamManagerCtx{ manager := &StreamManagerCtx{
logger: log.With().Str("module", "capture").Str("submodule", "stream").Logger(), logger: log.With().Str("module", "capture").Str("submodule", "stream").Logger(),
codec: codec, codec: codec,
@ -132,12 +132,13 @@ func (manager *StreamManagerCtx) createPipeline() error {
var err error var err error
codec := manager.Codec() codec := manager.Codec()
pipelineStr := manager.pipelineStr()
manager.logger.Info(). manager.logger.Info().
Str("codec", codec.Name). Str("codec", codec.Name).
Str("src", manager.pipelineStr). Str("src", pipelineStr).
Msgf("creating pipeline") Msgf("creating pipeline")
manager.pipeline, err = gst.CreatePipeline(manager.pipelineStr) manager.pipeline, err = gst.CreatePipeline(pipelineStr)
if err != nil { if err != nil {
return err return err
} }