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 (
"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