add screenshot package

This commit is contained in:
giongto35 2019-04-04 02:33:32 +08:00
parent 948bbbddf7
commit 141e092b67
5 changed files with 98 additions and 2 deletions

View file

@ -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

View file

@ -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

82
screenshot/screenshot.go Normal file
View file

@ -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<height; ++y ) {
for( int x=0; x<width; ++x ) {
const int i = y*(width+stride) + x;
*dst_y++ = ( ( 66*rgba[4*i] + 129*rgba[4*i+1] + 25*rgba[4*i+2] ) >> 8 ) + 16;
}
}
// U plane
for( int y=0; y<height; y+=2 ) {
for( int x=0; x<width; x+=2 ) {
const int i = y*(width+stride) + x;
*dst_u++ = ( ( -38*rgba[4*i] + -74*rgba[4*i+1] + 112*rgba[4*i+2] ) >> 8 ) + 128;
}
}
// V plane
for( int y=0; y<height; y+=2 ) {
for( int x=0; x<width; x+=2 ) {
const int i = y*(width+stride) + x;
*dst_v++ = ( ( 112*rgba[4*i] + -94*rgba[4*i+1] + -18*rgba[4*i+2] ) >> 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
}

View file

@ -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))
}

View file

@ -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"}}}}