mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-28 20:40:06 +00:00
Add video encoder tests
This commit is contained in:
parent
2a1bdbb3c9
commit
a7d8e53dac
3 changed files with 39 additions and 20 deletions
|
|
@ -84,9 +84,6 @@ func NewEncoder(width, height int, options ...Option) (encoder *H264, err error)
|
|||
width: int32(width),
|
||||
}
|
||||
|
||||
var picIn Picture
|
||||
PictureInit(&picIn)
|
||||
|
||||
if encoder.ref = EncoderOpen(¶m); encoder.ref == nil {
|
||||
err = fmt.Errorf("x264: cannot open the encoder")
|
||||
return
|
||||
|
|
|
|||
|
|
@ -46,7 +46,8 @@ func (vp *VideoPipe) Start() {
|
|||
|
||||
yuvProc := yuv.NewYuvImgProcessor(vp.w, vp.h)
|
||||
for img := range vp.Input {
|
||||
frame := vp.encoder.Encode(yuvProc.Process(img.Image).Get())
|
||||
yCbCr := yuvProc.Process(img.Image).Get()
|
||||
frame := vp.encoder.Encode(yCbCr)
|
||||
if len(frame) > 0 {
|
||||
vp.Output <- OutFrame{Data: frame, Timestamp: img.Timestamp}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ package room
|
|||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
|
|
@ -13,17 +12,31 @@ import (
|
|||
"github.com/giongto35/cloud-game/v2/pkg/encoder/vpx"
|
||||
)
|
||||
|
||||
func BenchmarkH264(b *testing.B) {
|
||||
benchmarkEncoder(1920, 1080, codec.H264, b)
|
||||
func TestEncoders(t *testing.T) {
|
||||
tests := []struct {
|
||||
n int
|
||||
w, h int
|
||||
codec codec.VideoCodec
|
||||
frames int
|
||||
}{
|
||||
{n: 3, w: 1920, h: 1080, codec: codec.H264, frames: 60 * 2},
|
||||
{n: 3, w: 1920, h: 1080, codec: codec.VPX, frames: 60 * 2},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
a := genTestImage(test.w, test.h, rand.New(rand.NewSource(int64(1))).Float32())
|
||||
b := genTestImage(test.w, test.h, rand.New(rand.NewSource(int64(2))).Float32())
|
||||
for i := 0; i < test.n; i++ {
|
||||
run(test.w, test.h, test.codec, test.frames, a, b, t)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkVP8(b *testing.B) {
|
||||
benchmarkEncoder(1920, 1080, codec.VPX, b)
|
||||
}
|
||||
func BenchmarkH264(b *testing.B) { run(1920, 1080, codec.H264, b.N, nil, nil, b) }
|
||||
func BenchmarkVP8(b *testing.B) { run(1920, 1080, codec.VPX, b.N, nil, nil, b) }
|
||||
|
||||
func benchmarkEncoder(w, h int, cod codec.VideoCodec, b *testing.B) {
|
||||
func run(w, h int, cod codec.VideoCodec, count int, a *image.RGBA, b *image.RGBA, backend testing.TB) {
|
||||
var enc encoder.Encoder
|
||||
|
||||
if cod == codec.H264 {
|
||||
enc, _ = h264.NewEncoder(w, h)
|
||||
} else {
|
||||
|
|
@ -34,19 +47,23 @@ func benchmarkEncoder(w, h int, cod codec.VideoCodec, b *testing.B) {
|
|||
go pipe.Start()
|
||||
defer pipe.Stop()
|
||||
|
||||
image1 := genTestImage(w, h, rand.New(rand.NewSource(int64(1))).Float32())
|
||||
image2 := genTestImage(w, h, rand.New(rand.NewSource(int64(2))).Float32())
|
||||
if a == nil {
|
||||
a = genTestImage(w, h, rand.New(rand.NewSource(int64(1))).Float32())
|
||||
}
|
||||
if b == nil {
|
||||
b = genTestImage(w, h, rand.New(rand.NewSource(int64(2))).Float32())
|
||||
}
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
im := image1
|
||||
for i := 0; i < count; i++ {
|
||||
im := a
|
||||
if i%2 == 0 {
|
||||
im = image2
|
||||
im = b
|
||||
}
|
||||
pipe.Input <- encoder.InFrame{Image: im}
|
||||
select {
|
||||
case <-pipe.Output:
|
||||
case <-time.After(5 * time.Second):
|
||||
b.Fatalf("encoder didn't produce an image")
|
||||
backend.Fatalf("encoder didn't produce an image")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -55,8 +72,12 @@ func genTestImage(w, h int, seed float32) *image.RGBA {
|
|||
img := image.NewRGBA(image.Rectangle{Max: image.Point{X: w, Y: h}})
|
||||
for x := 0; x < w; x++ {
|
||||
for y := 0; y < h; y++ {
|
||||
col := color.RGBA{R: uint8(seed * 255), G: uint8(seed * 255), B: uint8(seed * 255), A: 0xff}
|
||||
img.Set(x, y, col)
|
||||
i := img.PixOffset(x, y)
|
||||
s := img.Pix[i : i+4 : i+4]
|
||||
s[0] = uint8(seed * 255)
|
||||
s[1] = uint8(seed * 255)
|
||||
s[2] = uint8(seed * 255)
|
||||
s[3] = 0xff
|
||||
}
|
||||
}
|
||||
return img
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue