mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-22 17:47:11 +00:00
Merge pull request #27 from giongto35/optimize/reuse-buffer
Optimize/reuse buffer
This commit is contained in:
commit
7cda2b4db1
3 changed files with 13 additions and 3 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -172,8 +172,8 @@ func (v *VpxEncoder) startLooping() {
|
|||
// Release release memory and stop loop
|
||||
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
|
||||
|
|
@ -182,6 +182,5 @@ func (v *VpxEncoder) Release() {
|
|||
close(v.Input)
|
||||
}
|
||||
}
|
||||
v.IsRunning = false
|
||||
// TODO: Can we merge IsRunning and Done together
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue