From 141e092b67f589a096046de36add4b71b5b6a839 Mon Sep 17 00:00:00 2001 From: giongto35 Date: Thu, 4 Apr 2019 02:33:32 +0800 Subject: [PATCH] add screenshot package --- Dockerfile | 1 + main.go | 2 +- screenshot/screenshot.go | 82 +++++++++++++++++++++++++++++++++++ screenshot/screenshot_test.go | 13 ++++++ webrtc/webrtc.go | 2 +- 5 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 screenshot/screenshot.go create mode 100644 screenshot/screenshot_test.go diff --git a/Dockerfile b/Dockerfile index 71b7cbbc..b11a41bb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,3 +5,4 @@ COPY . /go/src/github.com/giongto35/game-online/ # Install server dependencies RUN go get github.com/pions/webrtc +RUN go get github.com/gordonklaus/portaudio diff --git a/main.go b/main.go index dddad270..b73b988f 100644 --- a/main.go +++ b/main.go @@ -10,10 +10,10 @@ import ( "net/http" "time" + "github.com/giongto35/game-online/screenshot" "github.com/giongto35/game-online/ui" "github.com/giongto35/game-online/webrtc" "github.com/gorilla/mux" - "github.com/poi5305/go-yuv2webRTC/screenshot" ) var webRTC *webrtc.WebRTC diff --git a/screenshot/screenshot.go b/screenshot/screenshot.go new file mode 100644 index 00000000..3182c563 --- /dev/null +++ b/screenshot/screenshot.go @@ -0,0 +1,82 @@ +package screenshot + +import ( + "image" + "unsafe" + + "github.com/kbinani/screenshot" + "github.com/nfnt/resize" +) + +// https://stackoverflow.com/questions/9465815/rgb-to-yuv420-algorithm-efficiency + +/* +void rgba2yuv(void *destination, void *source, int width, int height, int stride) { + const int image_size = width * height; + unsigned char *rgba = source; + unsigned char *dst_y = destination; + unsigned char *dst_u = destination + image_size; + unsigned char *dst_v = destination + image_size + image_size/4; + + // Y plane + for( int y=0; y> 8 ) + 16; + } + } + // U plane + for( int y=0; y> 8 ) + 128; + } + } + // V plane + for( int y=0; y> 8 ) + 128; + } + } +} +*/ +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 + h := rgba.Rect.Max.Y + size := int(float32(w*h) * 1.5) + stride := rgba.Stride - w*4 + yuv := make([]byte, size, size) + C.rgba2yuv(unsafe.Pointer(&yuv[0]), unsafe.Pointer(&rgba.Pix[0]), C.int(w), C.int(h), C.int(stride)) + return yuv +} diff --git a/screenshot/screenshot_test.go b/screenshot/screenshot_test.go new file mode 100644 index 00000000..fc4296a8 --- /dev/null +++ b/screenshot/screenshot_test.go @@ -0,0 +1,13 @@ +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)) +} diff --git a/webrtc/webrtc.go b/webrtc/webrtc.go index c8fa10be..e52d9840 100644 --- a/webrtc/webrtc.go +++ b/webrtc/webrtc.go @@ -11,9 +11,9 @@ import ( "strconv" "time" + vpxEncoder "github.com/giongto35/game-online/vpx-encoder" "github.com/pions/webrtc" "github.com/pions/webrtc/pkg/media" - vpxEncoder "github.com/poi5305/go-yuv2webRTC/vpx-encoder" ) var config = webrtc.Configuration{ICEServers: []webrtc.ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}}}