From 3812babc583f30e52f6bebe69d9fd858b1270944 Mon Sep 17 00:00:00 2001 From: TonyChyi Date: Mon, 17 Oct 2022 16:16:36 +0800 Subject: [PATCH] faster ! --- drivers/video/ppm.go | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/drivers/video/ppm.go b/drivers/video/ppm.go index 36a5b58..7cf666b 100644 --- a/drivers/video/ppm.go +++ b/drivers/video/ppm.go @@ -3,7 +3,7 @@ package video import ( "context" "image" - "image/color" + "image/draw" "io" "time" @@ -58,7 +58,7 @@ func (v *PPMStreamDriver) Properties() []prop.Media { func (v *PPMStreamDriver) VideoRecord(p prop.Media) (video.Reader, error) { logrus.Debug(p) - canvas := image.NewYCbCr( + /*canvas := image.NewYCbCr( image.Rect(0, 0, p.Width, p.Height), image.YCbCrSubsampleRatio420, ) @@ -70,7 +70,12 @@ func (v *PPMStreamDriver) VideoRecord(p prop.Media) (video.Reader, error) { canvas.Cb[canvas.COffset(x, y)] = Cb canvas.Cr[canvas.COffset(x, y)] = Cr } - } + }*/ + + canvas := image.NewRGBA(image.Rect(0, 0, p.Width, p.Height)) + var ( + prevHeight, prevWidth int + ) r := video.ReaderFunc(func() (img image.Image, release func(), err error) { select { @@ -79,30 +84,30 @@ func (v *PPMStreamDriver) VideoRecord(p prop.Media) (video.Reader, error) { case ppmF := <-v.PPMImage: defer ppmF.Image.Close() + // skip timeouted frame + if time.Since(ppmF.Time) > time.Second/time.Duration(p.FrameRate) { + return canvas, func() {}, nil + } + img, _, err := image.Decode(ppmF.Image) if err != nil { return nil, func() {}, err } - // resize image and draw it to canvas - //resized := resize.Resize(uint(p.Width), uint(p.Height), img, resize.Bilinear) + // screen geometroy change + if img.Bounds().Dx() != prevWidth || img.Bounds().Dy() != prevHeight { + draw.Draw(canvas, canvas.Rect, image.Black, image.Black.Bounds().Min, draw.Over) + prevWidth = img.Bounds().Dx() + prevHeight = img.Bounds().Dy() + } + offsetX := (canvas.Rect.Dx() - img.Bounds().Dx()) / 2 offsetY := (canvas.Rect.Dy() - img.Bounds().Dy()) / 2 - for y := 0; y < img.Bounds().Dy(); y++ { - for x := 0; x < img.Bounds().Dx(); x++ { - r, g, b, _ := img.At(x, y).RGBA() - - Y, Cb, Cr := color.RGBToYCbCr(uint8(r), uint8(g), uint8(b)) - - _x := offsetX + x - _y := offsetY + y - - canvas.Y[canvas.YOffset(_x, _y)] = Y - canvas.Cb[canvas.COffset(_x, _y)] = Cb - canvas.Cr[canvas.COffset(_x, _y)] = Cr - } - } + draw.Draw(canvas, image.Rect( + offsetX, offsetY, offsetX+img.Bounds().Dx(), offsetY+img.Bounds().Dy(), + ), img, img.Bounds().Min, draw.Over) + case <-time.After(time.Second / time.Duration(p.FrameRate)): default: } return canvas, func() {}, nil