This commit is contained in:
TonyChyi 2022-10-17 16:16:36 +08:00
parent c64db4a7a5
commit 3812babc58

View File

@ -3,7 +3,7 @@ package video
import ( import (
"context" "context"
"image" "image"
"image/color" "image/draw"
"io" "io"
"time" "time"
@ -58,7 +58,7 @@ func (v *PPMStreamDriver) Properties() []prop.Media {
func (v *PPMStreamDriver) VideoRecord(p prop.Media) (video.Reader, error) { func (v *PPMStreamDriver) VideoRecord(p prop.Media) (video.Reader, error) {
logrus.Debug(p) logrus.Debug(p)
canvas := image.NewYCbCr( /*canvas := image.NewYCbCr(
image.Rect(0, 0, p.Width, p.Height), image.Rect(0, 0, p.Width, p.Height),
image.YCbCrSubsampleRatio420, image.YCbCrSubsampleRatio420,
) )
@ -70,7 +70,12 @@ func (v *PPMStreamDriver) VideoRecord(p prop.Media) (video.Reader, error) {
canvas.Cb[canvas.COffset(x, y)] = Cb canvas.Cb[canvas.COffset(x, y)] = Cb
canvas.Cr[canvas.COffset(x, y)] = Cr 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) { r := video.ReaderFunc(func() (img image.Image, release func(), err error) {
select { select {
@ -79,30 +84,30 @@ func (v *PPMStreamDriver) VideoRecord(p prop.Media) (video.Reader, error) {
case ppmF := <-v.PPMImage: case ppmF := <-v.PPMImage:
defer ppmF.Image.Close() 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) img, _, err := image.Decode(ppmF.Image)
if err != nil { if err != nil {
return nil, func() {}, err return nil, func() {}, err
} }
// resize image and draw it to canvas // screen geometroy change
//resized := resize.Resize(uint(p.Width), uint(p.Height), img, resize.Bilinear) 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 offsetX := (canvas.Rect.Dx() - img.Bounds().Dx()) / 2
offsetY := (canvas.Rect.Dy() - img.Bounds().Dy()) / 2 offsetY := (canvas.Rect.Dy() - img.Bounds().Dy()) / 2
for y := 0; y < img.Bounds().Dy(); y++ { draw.Draw(canvas, image.Rect(
for x := 0; x < img.Bounds().Dx(); x++ { offsetX, offsetY, offsetX+img.Bounds().Dx(), offsetY+img.Bounds().Dy(),
r, g, b, _ := img.At(x, y).RGBA() ), img, img.Bounds().Min, draw.Over)
case <-time.After(time.Second / time.Duration(p.FrameRate)):
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
}
}
default: default:
} }
return canvas, func() {}, nil return canvas, func() {}, nil