Clean code

This commit is contained in:
giongto35 2019-04-04 02:58:27 +08:00
parent 113a848e12
commit b7a8e15b41
6 changed files with 5 additions and 108 deletions

View file

@ -8,8 +8,8 @@ import (
"net/http"
"time"
"github.com/giongto35/game-online/screenshot"
"github.com/giongto35/game-online/ui"
"github.com/giongto35/game-online/util"
"github.com/giongto35/game-online/webrtc"
"github.com/gorilla/mux"
)
@ -69,8 +69,7 @@ func postSession(w http.ResponseWriter, r *http.Request) {
func screenshotLoop(imageChannel chan *image.RGBA) {
for image := range imageChannel {
if webRTC.IsConnected() {
//rgbaImg := randomImage(width, height)
yuv := screenshot.RgbaToYuv(image)
yuv := util.RgbaToYuv(image)
webRTC.ImageChannel <- yuv
}
time.Sleep(10 * time.Millisecond)

View file

@ -1,13 +0,0 @@
package screenshot
import (
"fmt"
"testing"
)
func TestScreenshotToYuv(t *testing.T) {
w, h := GetScreenSize()
rgbaImg := GetScreenshot(0, 0, w, h, w, h)
yuv := RgbaToYuv(rgbaImg)
fmt.Println("yuv len", len(yuv))
}

View file

@ -1,7 +1,6 @@
package ui
import (
"fmt"
"image"
"log"
"time"
@ -45,11 +44,8 @@ func (d *Director) SetView(view View) {
func (d *Director) Step() {
//gl.Clear(gl.COLOR_BUFFER_BIT)
timestamp := float64(time.Now().Nanosecond()) / float64(time.Second)
fmt.Println("Time stamp", timestamp)
dt := timestamp - d.timestamp
fmt.Println("dt", dt)
d.timestamp = timestamp
fmt.Println("view", d.view)
if d.view != nil {
d.view.Update(timestamp, dt)
}

View file

@ -1,52 +0,0 @@
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strings"
"github.com/giongto35/game-online/nes"
)
func testRom(path string) (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
console, err := nes.NewConsole(path)
if err != nil {
return err
}
console.StepSeconds(3)
return nil
}
func main() {
args := os.Args[1:]
if len(args) != 1 {
log.Fatalln("Usage: go run util/roms.go roms_directory")
}
dir := args[0]
infos, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}
for _, info := range infos {
name := info.Name()
if !strings.HasSuffix(name, ".nes") {
continue
}
name = path.Join(dir, name)
err := testRom(name)
if err == nil {
fmt.Println("OK ", name)
} else {
fmt.Println("FAIL", name)
fmt.Println(err)
}
}
}

View file

@ -1,11 +1,8 @@
package screenshot
package util
import (
"image"
"unsafe"
"github.com/kbinani/screenshot"
"github.com/nfnt/resize"
)
// https://stackoverflow.com/questions/9465815/rgb-to-yuv420-algorithm-efficiency
@ -43,33 +40,6 @@ void rgba2yuv(void *destination, void *source, int width, int height, int stride
*/
import "C"
// GetScreenSize return screen size width and height
func GetScreenSize() (int, int) {
bounds := screenshot.GetDisplayBounds(0)
return bounds.Max.X, bounds.Max.Y
}
// GetScreenshot return rgba format
func GetScreenshot(cx, cy, cw, ch, rw, rh int) *image.RGBA {
bounds := image.Rectangle{
Min: image.Point{
X: cx,
Y: cy,
},
Max: image.Point{
X: cx + cw,
Y: cy + ch,
},
}
img, err := screenshot.CaptureRect(bounds)
if err != nil {
panic(err)
}
img = resize.Resize(uint(rw), uint(rh), img, resize.Lanczos3).(*image.RGBA)
return img
}
// RgbaToYuv convert to yuv from rgba
func RgbaToYuv(rgba *image.RGBA) []byte {
w := rgba.Rect.Max.X

View file

@ -233,11 +233,8 @@ func (w *WebRTC) startStreaming(vp8Track *webrtc.Track) {
go func() {
for i := 0; w.isConnected; i++ {
bs := <-w.encoder.Output
if i%10 == 0 {
fmt.Println("On Frame", len(bs), i)
}
//if len(vp8Track.Samples) < cap(vp8Track.Samples) {
//vp8Track.Samples <- media.Sample{Data: bs, Samples: 1}
//if i%10 == 0 {
//fmt.Println("On Frame", len(bs), i)
//}
vp8Track.WriteSample(media.Sample{Data: bs, Samples: 1})
}