Allocation inplace

This commit is contained in:
giongto35 2019-05-15 02:52:00 +08:00
parent b3e9fd91e3
commit edba449500
3 changed files with 12 additions and 2 deletions

View file

@ -3,6 +3,7 @@ package room
import (
"log"
"github.com/giongto35/cloud-game/config"
"github.com/giongto35/cloud-game/emulator"
"github.com/giongto35/cloud-game/util"
"gopkg.in/hraban/opus.v2"
@ -77,6 +78,8 @@ func (r *Room) startAudio() {
}
func (r *Room) startVideo() {
size := int(float32(config.Width*config.Height) * 1.5)
yuv := make([]byte, size, size)
// fanout Screen
for {
image, ok := <-r.imageChannel
@ -91,7 +94,7 @@ func (r *Room) startVideo() {
}
// TODO: Use worker pool for encoding
yuv := util.RgbaToYuv(image)
util.RgbaToYuvInplace(image, yuv)
// TODO: r.rtcSessions is rarely updated. Lock will hold down perf
//r.sessionsLock.Lock()
for _, webRTC := range r.rtcSessions {

View file

@ -4,6 +4,8 @@ package util
import (
"image"
"unsafe"
"github.com/giongto35/cloud-game/config"
)
// https://stackoverflow.com/questions/9465815/rgb-to-yuv420-algorithm-efficiency
@ -51,3 +53,9 @@ func RgbaToYuv(rgba *image.RGBA) []byte {
C.rgba2yuv(unsafe.Pointer(&yuv[0]), unsafe.Pointer(&rgba.Pix[0]), C.int(w), C.int(h), C.int(stride))
return yuv
}
// RgbaToYuvInplace convert to yuv from rgba inplace to yuv. Avoid reallocation
func RgbaToYuvInplace(rgba *image.RGBA, yuv []byte) {
stride := rgba.Stride - config.Width*4
C.rgba2yuv(unsafe.Pointer(&yuv[0]), unsafe.Pointer(&rgba.Pix[0]), C.int(config.Width), C.int(config.Height), C.int(stride))
}

View file

@ -174,7 +174,6 @@ func (v *VpxEncoder) Release() {
if v.IsRunning {
v.IsRunning = false
log.Println("Releasing encoder")
log.Println("Close output", v.Output)
C.vpx_img_free(&v.vpxImage)
C.vpx_codec_destroy(&v.vpxCodexCtx)
// TODO: Bug here, after close it will signal