to be continued

This commit is contained in:
TonyChyi
2022-10-12 11:23:14 +08:00
parent f1fe154e01
commit c64db4a7a5
4 changed files with 46 additions and 19 deletions

View File

@@ -5,8 +5,8 @@ import (
"image"
"image/color"
"io"
"time"
"github.com/nfnt/resize"
"github.com/pion/mediadevices/pkg/frame"
"github.com/pion/mediadevices/pkg/io/video"
"github.com/pion/mediadevices/pkg/prop"
@@ -17,10 +17,15 @@ import (
const DefaultFPS float32 = 60.0
type Frame struct {
Time time.Time
Image io.ReadCloser
}
type PPMStreamDriver struct {
Height, Width int
FPS float32
PPMImage <-chan io.ReadCloser
PPMImage <-chan Frame
closed <-chan struct{}
cancel func()
}
@@ -57,28 +62,45 @@ func (v *PPMStreamDriver) VideoRecord(p prop.Media) (video.Reader, error) {
image.Rect(0, 0, p.Width, p.Height),
image.YCbCrSubsampleRatio420,
)
Y, Cb, Cr := color.RGBToYCbCr(0, 0, 0)
for y := 0; y < canvas.Rect.Dy(); y++ {
for x := 0; x < canvas.Rect.Dx(); x++ {
canvas.Y[canvas.YOffset(x, y)] = Y
canvas.Cb[canvas.COffset(x, y)] = Cb
canvas.Cr[canvas.COffset(x, y)] = Cr
}
}
r := video.ReaderFunc(func() (img image.Image, release func(), err error) {
select {
case <-v.closed:
return nil, func() {}, io.EOF
case ppmF := <-v.PPMImage:
defer ppmF.Close()
defer ppmF.Image.Close()
img, _, err := image.Decode(ppmF)
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)
for y := 0; y < resized.Bounds().Dy(); y++ {
for x := 0; x < resized.Bounds().Dx(); x++ {
r, g, b, _ := resized.At(x, y).RGBA()
//resized := resize.Resize(uint(p.Width), uint(p.Height), img, resize.Bilinear)
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))
canvas.Y[canvas.YOffset(x, y)] = Y
canvas.Cb[canvas.COffset(x, y)] = Cb
canvas.Cr[canvas.COffset(x, y)] = Cr
_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: