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