Skip to content

Commit

Permalink
Add video encoder tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sergystepanov committed Apr 17, 2021
1 parent 2a1bdbb commit a7d8e53
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
3 changes: 0 additions & 3 deletions pkg/encoder/h264/x264.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(&param); encoder.ref == nil {
err = fmt.Errorf("x264: cannot open the encoder")
return
Expand Down
3 changes: 2 additions & 1 deletion pkg/encoder/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down
53 changes: 37 additions & 16 deletions pkg/worker/room/media_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package room

import (
"image"
"image/color"
"math/rand"
"testing"
"time"
Expand All @@ -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},
}

func BenchmarkVP8(b *testing.B) {
benchmarkEncoder(1920, 1080, codec.VPX, b)
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 benchmarkEncoder(w, h int, cod codec.VideoCodec, b *testing.B) {
var enc encoder.Encoder
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 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 {
Expand All @@ -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")
}
}
}
Expand All @@ -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
Expand Down

0 comments on commit a7d8e53

Please sign in to comment.