mirror of
https://github.com/pion/mediadevices.git
synced 2025-10-03 07:46:35 +08:00
Compare commits
9 Commits
update
...
generic-re
Author | SHA1 | Date | |
---|---|---|---|
![]() |
3bd70869d7 | ||
![]() |
0cd870fd4b | ||
![]() |
13e6dcc437 | ||
![]() |
366885e01c | ||
![]() |
86e3a3f14c | ||
![]() |
b4c11d5a0c | ||
![]() |
18da7ff1c6 | ||
![]() |
f7068296d3 | ||
![]() |
6d07cc2a58 |
4
.github/workflows/ci.yaml
vendored
4
.github/workflows/ci.yaml
vendored
@@ -12,7 +12,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
go: [ '1.14', '1.13' ]
|
||||
go: [ '1.15', '1.14' ]
|
||||
name: Linux Go ${{ matrix.go }}
|
||||
steps:
|
||||
- name: Checkout
|
||||
@@ -47,7 +47,7 @@ jobs:
|
||||
runs-on: macos-latest
|
||||
strategy:
|
||||
matrix:
|
||||
go: [ '1.14', '1.13' ]
|
||||
go: [ '1.15', '1.14' ]
|
||||
name: Darwin Go ${{ matrix.go }}
|
||||
steps:
|
||||
- name: Checkout
|
||||
|
6
go.mod
6
go.mod
@@ -4,10 +4,10 @@ go 1.13
|
||||
|
||||
require (
|
||||
github.com/blackjack/webcam v0.0.0-20200313125108-10ed912a8539
|
||||
github.com/jfreymuth/pulse v0.0.0-20200804114219-7d61c4938214
|
||||
github.com/jfreymuth/pulse v0.0.0-20200817093420-a82ccdb5e8aa
|
||||
github.com/lherman-cs/opus v0.0.0-20200223204610-6a4b98199ea4
|
||||
github.com/pion/webrtc/v2 v2.2.23
|
||||
github.com/pion/webrtc/v2 v2.2.24
|
||||
github.com/satori/go.uuid v1.2.0
|
||||
golang.org/x/image v0.0.0-20200801110659-972c09e46d76
|
||||
golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1
|
||||
golang.org/x/sys v0.0.0-20200831180312-196b9ba8737a
|
||||
)
|
||||
|
20
go.sum
20
go.sum
@@ -15,8 +15,8 @@ github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
|
||||
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
|
||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||
github.com/jfreymuth/pulse v0.0.0-20200804114219-7d61c4938214 h1:2xVJKIumEUWeV3vczQwn61SHjNZ94Bwk+4CTjmcePxk=
|
||||
github.com/jfreymuth/pulse v0.0.0-20200804114219-7d61c4938214/go.mod h1:cpYspI6YljhkUf1WLXLLDmeaaPFc3CnGLjDZf9dZ4no=
|
||||
github.com/jfreymuth/pulse v0.0.0-20200817093420-a82ccdb5e8aa h1:qUZIj5+D3UDgfshNe8Cz/9maOxe8ddt43qwQH9vEEC8=
|
||||
github.com/jfreymuth/pulse v0.0.0-20200817093420-a82ccdb5e8aa/go.mod h1:cpYspI6YljhkUf1WLXLLDmeaaPFc3CnGLjDZf9dZ4no=
|
||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
@@ -33,8 +33,8 @@ github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs=
|
||||
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU=
|
||||
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
|
||||
github.com/pion/datachannel v1.4.19 h1:IcOmm5fdDzJVCMgFYDCMtFC+lrjG78KcMYXH+gOo6ys=
|
||||
github.com/pion/datachannel v1.4.19/go.mod h1:JzKF/zzeWgkOYwQ+KFb8JzbrUt8s63um+Qunu8VqTyw=
|
||||
github.com/pion/datachannel v1.4.20 h1:+uYUrxbhGuEt+9En81Necda5ul8M2h7mMsvGWkYZ/yI=
|
||||
github.com/pion/datachannel v1.4.20/go.mod h1:hsjWYdTW5fMmtM4hVIxUNYqViRPv2A6ixzkQFd82wSc=
|
||||
github.com/pion/dtls/v2 v2.0.1 h1:ddE7+V0faYRbyh4uPsRZ2vLdRrjVZn+wmCfI7jlBfaA=
|
||||
github.com/pion/dtls/v2 v2.0.1/go.mod h1:uMQkz2W0cSqY00xav7WByQ4Hb+18xeQh2oH2fRezr5U=
|
||||
github.com/pion/dtls/v2 v2.0.2 h1:FHCHTiM182Y8e15aFTiORroiATUI16ryHiQh8AIOJ1E=
|
||||
@@ -54,8 +54,8 @@ github.com/pion/rtcp v1.2.3 h1:2wrhKnqgSz91Q5nzYTO07mQXztYPtxL8a0XOss4rJqA=
|
||||
github.com/pion/rtcp v1.2.3/go.mod h1:zGhIv0RPRF0Z1Wiij22pUt5W/c9fevqSzT4jje/oK7I=
|
||||
github.com/pion/rtp v1.6.0 h1:4Ssnl/T5W2LzxHj9ssYpGVEQh3YYhQFNVmSWO88MMwk=
|
||||
github.com/pion/rtp v1.6.0/go.mod h1:QgfogHsMBVE/RFNno467U/KBqfUywEH+HK+0rtnwsdI=
|
||||
github.com/pion/sctp v1.7.8 h1:tEWel2BKXLZitU+LxY3GDeQXoKeTafYasiu/X+XBKNM=
|
||||
github.com/pion/sctp v1.7.8/go.mod h1:EhpTUQu1/lcK3xI+eriS6/96fWetHGCvBi9MSsnaBN0=
|
||||
github.com/pion/sctp v1.7.9 h1:n+A37cTMU08xL3Oodkz39XjtPReQliKyk01q96mGB5M=
|
||||
github.com/pion/sctp v1.7.9/go.mod h1:EhpTUQu1/lcK3xI+eriS6/96fWetHGCvBi9MSsnaBN0=
|
||||
github.com/pion/sdp/v2 v2.4.0 h1:luUtaETR5x2KNNpvEMv/r4Y+/kzImzbz4Lm1z8eQNQI=
|
||||
github.com/pion/sdp/v2 v2.4.0/go.mod h1:L2LxrOpSTJbAns244vfPChbciR/ReU1KWfG04OpkR7E=
|
||||
github.com/pion/srtp v1.5.1 h1:9Q3jAfslYZBt+C69SI/ZcONJh9049JUHZWYRRf5KEKw=
|
||||
@@ -73,8 +73,8 @@ github.com/pion/turn/v2 v2.0.4 h1:oDguhEv2L/4rxwbL9clGLgtzQPjtuZwCdoM7Te8vQVk=
|
||||
github.com/pion/turn/v2 v2.0.4/go.mod h1:1812p4DcGVbYVBTiraUmP50XoKye++AMkbfp+N27mog=
|
||||
github.com/pion/udp v0.1.0 h1:uGxQsNyrqG3GLINv36Ff60covYmfrLoxzwnCsIYspXI=
|
||||
github.com/pion/udp v0.1.0/go.mod h1:BPELIjbwE9PRbd/zxI/KYBnbo7B6+oA6YuEaNE8lths=
|
||||
github.com/pion/webrtc/v2 v2.2.23 h1:rZdOC95fwUCoQFVjHooPAayx/vhs3SLHFz8J/iRkAuk=
|
||||
github.com/pion/webrtc/v2 v2.2.23/go.mod h1:1lN/3EcATkQxc7GJSQbISCGC2l64Xu2VSLpwEG3c/tM=
|
||||
github.com/pion/webrtc/v2 v2.2.24 h1:l7q/iO96tMTElxuE2XGdNhCzklGcd9aVZ00XufASp0g=
|
||||
github.com/pion/webrtc/v2 v2.2.24/go.mod h1:U/m+nvG1t8gInf8PwiDyJEDcd7qfl+jmGQXqTX2zGvo=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
@@ -119,8 +119,8 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c h1:UIcGWL6/wpCfyGuJnRFJRurA+yj8RrW7Q6x2YMCXt6c=
|
||||
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1 h1:sIky/MyNRSHTrdxfsiUSS4WIAMvInbeXljJz+jDjeYE=
|
||||
golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200831180312-196b9ba8737a h1:i47hUS795cOydZI4AwJQCKXOr4BvxzvikwDoDtHhP2Y=
|
||||
golang.org/x/sys v0.0.0-20200831180312-196b9ba8737a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||
|
@@ -57,10 +57,11 @@ func init() {
|
||||
|
||||
func newCamera(path string) *camera {
|
||||
formats := map[webcam.PixelFormat]frame.Format{
|
||||
webcam.PixelFormat(C.V4L2_PIX_FMT_YUYV): frame.FormatYUYV,
|
||||
webcam.PixelFormat(C.V4L2_PIX_FMT_UYVY): frame.FormatUYVY,
|
||||
webcam.PixelFormat(C.V4L2_PIX_FMT_NV12): frame.FormatNV21,
|
||||
webcam.PixelFormat(C.V4L2_PIX_FMT_MJPEG): frame.FormatMJPEG,
|
||||
webcam.PixelFormat(C.V4L2_PIX_FMT_YUV420): frame.FormatI420,
|
||||
webcam.PixelFormat(C.V4L2_PIX_FMT_YUYV): frame.FormatYUYV,
|
||||
webcam.PixelFormat(C.V4L2_PIX_FMT_UYVY): frame.FormatUYVY,
|
||||
webcam.PixelFormat(C.V4L2_PIX_FMT_NV12): frame.FormatNV21,
|
||||
webcam.PixelFormat(C.V4L2_PIX_FMT_MJPEG): frame.FormatMJPEG,
|
||||
}
|
||||
|
||||
reversedFormats := make(map[frame.Format]webcam.PixelFormat)
|
||||
|
@@ -3,8 +3,6 @@ package frame
|
||||
type Format string
|
||||
|
||||
const (
|
||||
// YUV Formats
|
||||
|
||||
// FormatI420 https://www.fourcc.org/pixel-format/yuv-i420/
|
||||
FormatI420 Format = "I420"
|
||||
// FormatI444 is a YUV format without sub-sampling
|
||||
@@ -16,18 +14,11 @@ const (
|
||||
// FormatUYVY https://www.fourcc.org/pixel-format/yuv-uyvy/
|
||||
FormatUYVY = "UYVY"
|
||||
|
||||
// RGB Formats
|
||||
|
||||
// FormatRGBA https://www.fourcc.org/pixel-format/rgb-rgba/
|
||||
FormatRGBA Format = "RGBA"
|
||||
|
||||
// Compressed Formats
|
||||
|
||||
// FormatMJPEG https://www.fourcc.org/mjpg/
|
||||
FormatMJPEG = "MJPEG"
|
||||
)
|
||||
|
||||
// YUV aliases
|
||||
|
||||
// FormatYUYV is an alias of FormatYUY2
|
||||
const FormatYUYV = FormatYUY2
|
||||
|
@@ -5,7 +5,7 @@ import (
|
||||
)
|
||||
|
||||
func NewDecoder(f Format) (Decoder, error) {
|
||||
var decoder DecoderFunc
|
||||
var decoder decoderFunc
|
||||
|
||||
switch f {
|
||||
case FormatI420:
|
||||
|
@@ -7,8 +7,8 @@ type Decoder interface {
|
||||
}
|
||||
|
||||
// DecoderFunc is a proxy type for Decoder
|
||||
type DecoderFunc func(frame []byte, width, height int) (image.Image, error)
|
||||
type decoderFunc func(frame []byte, width, height int) (image.Image, error)
|
||||
|
||||
func (f DecoderFunc) Decode(frame []byte, width, height int) (image.Image, error) {
|
||||
func (f decoderFunc) Decode(frame []byte, width, height int) (image.Image, error) {
|
||||
return f(frame, width, height)
|
||||
}
|
||||
|
14
pkg/io/reader.go
Normal file
14
pkg/io/reader.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package io
|
||||
|
||||
// Reader is a generic reader. When generic is ready, interface{} will be replaced
|
||||
// with a generic type and will provide type safety.
|
||||
type Reader interface {
|
||||
Read() (interface{}, error)
|
||||
}
|
||||
|
||||
// ReaderFunc is a proxy type to make easier for users to implement Reader
|
||||
type ReaderFunc func() (interface{}, error)
|
||||
|
||||
func (f ReaderFunc) Read() (interface{}, error) {
|
||||
return f()
|
||||
}
|
214
pkg/io/video/framebuffer.go
Normal file
214
pkg/io/video/framebuffer.go
Normal file
@@ -0,0 +1,214 @@
|
||||
package video
|
||||
|
||||
import (
|
||||
"image"
|
||||
)
|
||||
|
||||
// FrameBuffer is a buffer that can store any image format.
|
||||
type FrameBuffer struct {
|
||||
buffer []uint8
|
||||
tmp image.Image
|
||||
}
|
||||
|
||||
// NewFrameBuffer creates a new FrameBuffer instance and initialize internal buffer
|
||||
// with initialSize
|
||||
func NewFrameBuffer(initialSize int) *FrameBuffer {
|
||||
return &FrameBuffer{
|
||||
buffer: make([]uint8, initialSize),
|
||||
}
|
||||
}
|
||||
|
||||
func (buff *FrameBuffer) storeInOrder(srcs ...[]uint8) {
|
||||
var neededSize int
|
||||
|
||||
for _, src := range srcs {
|
||||
neededSize += len(src)
|
||||
}
|
||||
|
||||
if len(buff.buffer) < neededSize {
|
||||
if cap(buff.buffer) >= neededSize {
|
||||
buff.buffer = buff.buffer[:neededSize]
|
||||
} else {
|
||||
buff.buffer = make([]uint8, neededSize)
|
||||
}
|
||||
}
|
||||
|
||||
var currentLen int
|
||||
for _, src := range srcs {
|
||||
copy(buff.buffer[currentLen:], src)
|
||||
currentLen += len(src)
|
||||
}
|
||||
}
|
||||
|
||||
// Load loads the current owned image
|
||||
func (buff *FrameBuffer) Load() image.Image {
|
||||
return buff.tmp
|
||||
}
|
||||
|
||||
// StoreCopy makes a copy of src and store its copy. StoreCopy will reuse as much memory as it can
|
||||
// from the previous copies. For example, if StoreCopy is given an image that has the same resolution
|
||||
// and format from the previous call, StoreCopy will not allocate extra memory and only copy the content
|
||||
// from src to the previous buffer.
|
||||
func (buff *FrameBuffer) StoreCopy(src image.Image) {
|
||||
switch src := src.(type) {
|
||||
case *image.Alpha:
|
||||
clone, ok := buff.tmp.(*image.Alpha)
|
||||
if ok {
|
||||
*clone = *src
|
||||
} else {
|
||||
copied := *src
|
||||
clone = &copied
|
||||
}
|
||||
|
||||
buff.storeInOrder(src.Pix)
|
||||
clone.Pix = buff.buffer[:len(src.Pix)]
|
||||
|
||||
buff.tmp = clone
|
||||
case *image.Alpha16:
|
||||
clone, ok := buff.tmp.(*image.Alpha16)
|
||||
if ok {
|
||||
*clone = *src
|
||||
} else {
|
||||
copied := *src
|
||||
clone = &copied
|
||||
}
|
||||
|
||||
buff.storeInOrder(src.Pix)
|
||||
clone.Pix = buff.buffer[:len(src.Pix)]
|
||||
|
||||
buff.tmp = clone
|
||||
case *image.CMYK:
|
||||
clone, ok := buff.tmp.(*image.CMYK)
|
||||
if ok {
|
||||
*clone = *src
|
||||
} else {
|
||||
copied := *src
|
||||
clone = &copied
|
||||
}
|
||||
|
||||
buff.storeInOrder(src.Pix)
|
||||
clone.Pix = buff.buffer[:len(src.Pix)]
|
||||
|
||||
buff.tmp = clone
|
||||
case *image.Gray:
|
||||
clone, ok := buff.tmp.(*image.Gray)
|
||||
if ok {
|
||||
*clone = *src
|
||||
} else {
|
||||
copied := *src
|
||||
clone = &copied
|
||||
}
|
||||
|
||||
buff.storeInOrder(src.Pix)
|
||||
clone.Pix = buff.buffer[:len(src.Pix)]
|
||||
|
||||
buff.tmp = clone
|
||||
case *image.Gray16:
|
||||
clone, ok := buff.tmp.(*image.Gray16)
|
||||
if ok {
|
||||
*clone = *src
|
||||
} else {
|
||||
copied := *src
|
||||
clone = &copied
|
||||
}
|
||||
|
||||
buff.storeInOrder(src.Pix)
|
||||
clone.Pix = buff.buffer[:len(src.Pix)]
|
||||
|
||||
buff.tmp = clone
|
||||
case *image.NRGBA:
|
||||
clone, ok := buff.tmp.(*image.NRGBA)
|
||||
if ok {
|
||||
*clone = *src
|
||||
} else {
|
||||
copied := *src
|
||||
clone = &copied
|
||||
}
|
||||
|
||||
buff.storeInOrder(src.Pix)
|
||||
clone.Pix = buff.buffer[:len(src.Pix)]
|
||||
|
||||
buff.tmp = clone
|
||||
case *image.NRGBA64:
|
||||
clone, ok := buff.tmp.(*image.NRGBA64)
|
||||
if ok {
|
||||
*clone = *src
|
||||
} else {
|
||||
copied := *src
|
||||
clone = &copied
|
||||
}
|
||||
|
||||
buff.storeInOrder(src.Pix)
|
||||
clone.Pix = buff.buffer[:len(src.Pix)]
|
||||
|
||||
buff.tmp = clone
|
||||
case *image.RGBA:
|
||||
clone, ok := buff.tmp.(*image.RGBA)
|
||||
if ok {
|
||||
*clone = *src
|
||||
} else {
|
||||
copied := *src
|
||||
clone = &copied
|
||||
}
|
||||
|
||||
buff.storeInOrder(src.Pix)
|
||||
clone.Pix = buff.buffer[:len(src.Pix)]
|
||||
|
||||
buff.tmp = clone
|
||||
case *image.RGBA64:
|
||||
clone, ok := buff.tmp.(*image.RGBA64)
|
||||
if ok {
|
||||
*clone = *src
|
||||
} else {
|
||||
copied := *src
|
||||
clone = &copied
|
||||
}
|
||||
|
||||
buff.storeInOrder(src.Pix)
|
||||
clone.Pix = buff.buffer[:len(src.Pix)]
|
||||
|
||||
buff.tmp = clone
|
||||
case *image.NYCbCrA:
|
||||
clone, ok := buff.tmp.(*image.NYCbCrA)
|
||||
if ok {
|
||||
*clone = *src
|
||||
} else {
|
||||
copied := *src
|
||||
clone = &copied
|
||||
}
|
||||
|
||||
var currentLen int
|
||||
buff.storeInOrder(src.Y, src.Cb, src.Cr, src.A)
|
||||
clone.Y = buff.buffer[currentLen : currentLen+len(src.Y) : currentLen+len(src.Y)]
|
||||
currentLen += len(src.Y)
|
||||
clone.Cb = buff.buffer[currentLen : currentLen+len(src.Cb) : currentLen+len(src.Cb)]
|
||||
currentLen += len(src.Cb)
|
||||
clone.Cr = buff.buffer[currentLen : currentLen+len(src.Cr) : currentLen+len(src.Cr)]
|
||||
currentLen += len(src.Cr)
|
||||
clone.A = buff.buffer[currentLen : currentLen+len(src.A) : currentLen+len(src.A)]
|
||||
|
||||
buff.tmp = clone
|
||||
case *image.YCbCr:
|
||||
clone, ok := buff.tmp.(*image.YCbCr)
|
||||
if ok {
|
||||
*clone = *src
|
||||
} else {
|
||||
copied := *src
|
||||
clone = &copied
|
||||
}
|
||||
|
||||
var currentLen int
|
||||
buff.storeInOrder(src.Y, src.Cb, src.Cr)
|
||||
clone.Y = buff.buffer[currentLen : currentLen+len(src.Y) : currentLen+len(src.Y)]
|
||||
currentLen += len(src.Y)
|
||||
clone.Cb = buff.buffer[currentLen : currentLen+len(src.Cb) : currentLen+len(src.Cb)]
|
||||
currentLen += len(src.Cb)
|
||||
clone.Cr = buff.buffer[currentLen : currentLen+len(src.Cr) : currentLen+len(src.Cr)]
|
||||
|
||||
buff.tmp = clone
|
||||
default:
|
||||
var converted image.RGBA
|
||||
imageToRGBA(&converted, src)
|
||||
buff.StoreCopy(&converted)
|
||||
}
|
||||
}
|
195
pkg/io/video/framebuffer_test.go
Normal file
195
pkg/io/video/framebuffer_test.go
Normal file
@@ -0,0 +1,195 @@
|
||||
package video
|
||||
|
||||
import (
|
||||
"image"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func randomize(arr []uint8) {
|
||||
for i := range arr {
|
||||
arr[i] = uint8(rand.Uint32())
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkFrameBufferCopyOptimized(b *testing.B) {
|
||||
frameBuffer := NewFrameBuffer(0)
|
||||
resolution := image.Rect(0, 0, 1920, 1080)
|
||||
src := image.NewYCbCr(resolution, image.YCbCrSubsampleRatio420)
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
frameBuffer.StoreCopy(src)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkFrameBufferCopyNaive(b *testing.B) {
|
||||
resolution := image.Rect(0, 0, 1920, 1080)
|
||||
src := image.NewYCbCr(resolution, image.YCbCrSubsampleRatio420)
|
||||
var dst image.Image
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
clone := *src
|
||||
clone.Cb = make([]uint8, len(src.Cb))
|
||||
clone.Cr = make([]uint8, len(src.Cr))
|
||||
clone.Y = make([]uint8, len(src.Y))
|
||||
|
||||
copy(clone.Cb, src.Cb)
|
||||
copy(clone.Cr, src.Cr)
|
||||
copy(clone.Y, src.Y)
|
||||
dst = &clone
|
||||
_ = dst
|
||||
}
|
||||
}
|
||||
|
||||
func TestFrameBufferStoreCopyAndLoad(t *testing.T) {
|
||||
resolution := image.Rect(0, 0, 16, 8)
|
||||
rgbaLike := image.NewRGBA64(resolution)
|
||||
randomize(rgbaLike.Pix)
|
||||
testCases := map[string]struct {
|
||||
New func() image.Image
|
||||
Update func(image.Image)
|
||||
}{
|
||||
"Alpha": {
|
||||
New: func() image.Image {
|
||||
return (*image.Alpha)(rgbaLike)
|
||||
},
|
||||
Update: func(src image.Image) {
|
||||
img := src.(*image.Alpha)
|
||||
randomize(img.Pix)
|
||||
},
|
||||
},
|
||||
"Alpha16": {
|
||||
New: func() image.Image {
|
||||
return (*image.Alpha16)(rgbaLike)
|
||||
},
|
||||
Update: func(src image.Image) {
|
||||
img := src.(*image.Alpha16)
|
||||
randomize(img.Pix)
|
||||
},
|
||||
},
|
||||
"CMYK": {
|
||||
New: func() image.Image {
|
||||
return (*image.CMYK)(rgbaLike)
|
||||
},
|
||||
Update: func(src image.Image) {
|
||||
img := src.(*image.CMYK)
|
||||
randomize(img.Pix)
|
||||
},
|
||||
},
|
||||
"Gray": {
|
||||
New: func() image.Image {
|
||||
return (*image.Gray)(rgbaLike)
|
||||
},
|
||||
Update: func(src image.Image) {
|
||||
img := src.(*image.Gray)
|
||||
randomize(img.Pix)
|
||||
},
|
||||
},
|
||||
"Gray16": {
|
||||
New: func() image.Image {
|
||||
return (*image.Gray16)(rgbaLike)
|
||||
},
|
||||
Update: func(src image.Image) {
|
||||
img := src.(*image.Gray16)
|
||||
randomize(img.Pix)
|
||||
},
|
||||
},
|
||||
"NRGBA": {
|
||||
New: func() image.Image {
|
||||
return (*image.NRGBA)(rgbaLike)
|
||||
},
|
||||
Update: func(src image.Image) {
|
||||
img := src.(*image.NRGBA)
|
||||
randomize(img.Pix)
|
||||
},
|
||||
},
|
||||
"NRGBA64": {
|
||||
New: func() image.Image {
|
||||
return (*image.NRGBA64)(rgbaLike)
|
||||
},
|
||||
Update: func(src image.Image) {
|
||||
img := src.(*image.NRGBA64)
|
||||
randomize(img.Pix)
|
||||
},
|
||||
},
|
||||
"RGBA": {
|
||||
New: func() image.Image {
|
||||
return (*image.RGBA)(rgbaLike)
|
||||
},
|
||||
Update: func(src image.Image) {
|
||||
img := src.(*image.RGBA)
|
||||
randomize(img.Pix)
|
||||
},
|
||||
},
|
||||
"RGBA64": {
|
||||
New: func() image.Image {
|
||||
return (*image.RGBA64)(rgbaLike)
|
||||
},
|
||||
Update: func(src image.Image) {
|
||||
img := src.(*image.RGBA64)
|
||||
randomize(img.Pix)
|
||||
},
|
||||
},
|
||||
"NYCbCrA": {
|
||||
New: func() image.Image {
|
||||
img := image.NewNYCbCrA(resolution, image.YCbCrSubsampleRatio420)
|
||||
randomize(img.Y)
|
||||
randomize(img.Cb)
|
||||
randomize(img.Cr)
|
||||
randomize(img.A)
|
||||
img.CStride = 10
|
||||
img.YStride = 5
|
||||
return img
|
||||
},
|
||||
Update: func(src image.Image) {
|
||||
img := src.(*image.NYCbCrA)
|
||||
randomize(img.Y)
|
||||
randomize(img.Cb)
|
||||
randomize(img.Cr)
|
||||
randomize(img.A)
|
||||
img.CStride = 3
|
||||
img.YStride = 2
|
||||
},
|
||||
},
|
||||
"YCbCr": {
|
||||
New: func() image.Image {
|
||||
img := image.NewYCbCr(resolution, image.YCbCrSubsampleRatio420)
|
||||
randomize(img.Y)
|
||||
randomize(img.Cb)
|
||||
randomize(img.Cr)
|
||||
img.CStride = 10
|
||||
img.YStride = 5
|
||||
return img
|
||||
},
|
||||
Update: func(src image.Image) {
|
||||
img := src.(*image.YCbCr)
|
||||
randomize(img.Y)
|
||||
randomize(img.Cb)
|
||||
randomize(img.Cr)
|
||||
img.CStride = 3
|
||||
img.YStride = 2
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
frameBuffer := NewFrameBuffer(0)
|
||||
|
||||
for name, testCase := range testCases {
|
||||
// Since the test also wants to make sure that Copier can convert from 1 type to another,
|
||||
// t.Run is not ideal since it'll run the tests separately
|
||||
t.Log("Testing", name)
|
||||
|
||||
src := testCase.New()
|
||||
frameBuffer.StoreCopy(src)
|
||||
if !reflect.DeepEqual(frameBuffer.Load(), src) {
|
||||
t.Fatal("Expected the copied image to be identical with the source")
|
||||
}
|
||||
|
||||
testCase.Update(src)
|
||||
frameBuffer.StoreCopy(src)
|
||||
if !reflect.DeepEqual(frameBuffer.Load(), src) {
|
||||
t.Fatal("Expected the copied image to be identical with the source after an update in source")
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user