2022-09-28 06:14:26 +00:00
|
|
|
package video
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
"io"
|
2022-10-02 10:50:08 +00:00
|
|
|
"time"
|
2022-09-28 06:14:26 +00:00
|
|
|
|
2022-09-28 06:29:54 +00:00
|
|
|
"github.com/nfnt/resize"
|
2022-09-28 06:14:26 +00:00
|
|
|
"github.com/pion/mediadevices/pkg/frame"
|
|
|
|
"github.com/pion/mediadevices/pkg/io/video"
|
|
|
|
"github.com/pion/mediadevices/pkg/prop"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
_ "github.com/jbuchbinder/gopnm"
|
|
|
|
)
|
|
|
|
|
|
|
|
const DefaultFPS float32 = 60.0
|
|
|
|
|
|
|
|
type PPMStreamDriver struct {
|
|
|
|
Height, Width int
|
|
|
|
FPS float32
|
|
|
|
PPMImage <-chan io.ReadCloser
|
|
|
|
closed <-chan struct{}
|
|
|
|
cancel func()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *PPMStreamDriver) Open() error {
|
|
|
|
defer logrus.Debug("device opened")
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
v.closed = ctx.Done()
|
|
|
|
v.cancel = cancel
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *PPMStreamDriver) Close() error {
|
|
|
|
v.cancel()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *PPMStreamDriver) Properties() []prop.Media {
|
|
|
|
return []prop.Media{
|
|
|
|
{
|
|
|
|
Video: prop.Video{
|
|
|
|
Width: v.Width,
|
|
|
|
Height: v.Height,
|
|
|
|
FrameRate: v.FPS,
|
|
|
|
FrameFormat: frame.FormatYUYV,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *PPMStreamDriver) VideoRecord(p prop.Media) (video.Reader, error) {
|
2022-09-30 04:24:32 +00:00
|
|
|
logrus.Debug(p)
|
2022-10-02 10:50:08 +00:00
|
|
|
canvas := image.NewYCbCr(
|
|
|
|
image.Rect(0, 0, p.Width, p.Height),
|
|
|
|
image.YCbCrSubsampleRatio420,
|
|
|
|
)
|
2022-09-28 06:14:26 +00:00
|
|
|
r := video.ReaderFunc(func() (img image.Image, release func(), err error) {
|
2022-10-10 11:12:26 +00:00
|
|
|
ticker := time.NewTicker(time.Second / time.Duration(p.FrameRate))
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2022-09-28 06:14:26 +00:00
|
|
|
select {
|
|
|
|
case <-v.closed:
|
|
|
|
return nil, func() {}, io.EOF
|
2022-10-02 10:50:08 +00:00
|
|
|
case ppmF := <-v.PPMImage:
|
2022-09-28 06:14:26 +00:00
|
|
|
defer ppmF.Close()
|
|
|
|
|
|
|
|
img, _, err := image.Decode(ppmF)
|
|
|
|
if err != nil {
|
|
|
|
return nil, func() {}, err
|
|
|
|
}
|
|
|
|
|
2022-09-28 06:29:54 +00:00
|
|
|
// resize image and draw it to canvas
|
|
|
|
resized := resize.Resize(uint(p.Width), uint(p.Height), img, resize.Lanczos3)
|
2022-10-02 10:50:08 +00:00
|
|
|
for y := 0; y < resized.Bounds().Dy(); y++ {
|
|
|
|
for x := 0; x < resized.Bounds().Dx(); x++ {
|
2022-09-28 06:29:54 +00:00
|
|
|
r, g, b, _ := resized.At(x, y).RGBA()
|
2022-09-28 06:14:26 +00:00
|
|
|
Y, Cb, Cr := color.RGBToYCbCr(uint8(r), uint8(g), uint8(b))
|
|
|
|
|
2022-09-28 06:29:54 +00:00
|
|
|
canvas.Y[canvas.YOffset(x, y)] = Y
|
|
|
|
canvas.Cb[canvas.COffset(x, y)] = Cb
|
|
|
|
canvas.Cr[canvas.COffset(x, y)] = Cr
|
2022-09-28 06:14:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-10 11:12:26 +00:00
|
|
|
case <-ticker.C:
|
2022-09-28 06:14:26 +00:00
|
|
|
}
|
|
|
|
return canvas, func() {}, nil
|
|
|
|
})
|
|
|
|
return r, nil
|
|
|
|
}
|