mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-25 11:03:56 +00:00
Update encoding checking at webrtc initialization
This commit is contained in:
parent
265c6248c2
commit
d831592ddc
5 changed files with 33 additions and 22 deletions
|
|
@ -25,10 +25,6 @@ var ProdEnv = "prod"
|
|||
|
||||
const NumKeys = 10
|
||||
|
||||
var Codec = CODEC_H264
|
||||
|
||||
//var Codec = CODEC_VP8
|
||||
|
||||
var FileTypeToEmulator = map[string]string{
|
||||
"gba": "gba",
|
||||
"cue": "pcsx",
|
||||
|
|
|
|||
5
static/js/ws.js
vendored
5
static/js/ws.js
vendored
|
|
@ -209,8 +209,7 @@ function startWebRTC(iceservers) {
|
|||
session = btoa(JSON.stringify(pc.localDescription));
|
||||
log("Send SDP to remote peer");
|
||||
// TODO: Fix curPacketID
|
||||
//conn.send(JSON.stringify({"id": "initwebrtc", "data": session, "packet_id": curPacketID}));
|
||||
conn.send(JSON.stringify({"id": "initwebrtc", "data": session}));
|
||||
conn.send(JSON.stringify({"id": "initwebrtc", "data": JSON.stringify({"sdp": session, "is_mobile": isMobileDevice()})}));
|
||||
iceSent = true
|
||||
}
|
||||
} else {
|
||||
|
|
@ -221,7 +220,7 @@ function startWebRTC(iceservers) {
|
|||
if (!iceSent) {
|
||||
log("Ice gathering timeout, send anyway")
|
||||
session = btoa(JSON.stringify(pc.localDescription));
|
||||
conn.send(JSON.stringify({"id": "initwebrtc", "data": session}));
|
||||
conn.send(JSON.stringify({"id": "initwebrtc", "data": JSON.stringify({"sdp": session, "is_mobile": isMobileDevice()})}));
|
||||
iceSent = true;
|
||||
}
|
||||
}, ICE_TIMEOUT)
|
||||
|
|
|
|||
12
util/util.go
12
util/util.go
|
|
@ -6,6 +6,8 @@ import (
|
|||
"log"
|
||||
"os/user"
|
||||
"unsafe"
|
||||
|
||||
"github.com/giongto35/cloud-game/config"
|
||||
)
|
||||
|
||||
// https://stackoverflow.com/questions/9465815/rgb-to-yuv420-algorithm-efficiency
|
||||
|
|
@ -78,3 +80,13 @@ func GetSavePath(roomID string) string {
|
|||
func savePath(hash string) string {
|
||||
return homeDir + "/.cr/save/" + hash + ".dat"
|
||||
}
|
||||
|
||||
// GetVideoEncoder returns video encoder based on some qualification.
|
||||
// Actually Android is only supporting VP8 but H264 has better encoding performance
|
||||
// TODO: Better use useragent attribute from frontend
|
||||
func GetVideoEncoder(isMobile bool) string {
|
||||
if isMobile == true {
|
||||
return config.CODEC_VP8
|
||||
}
|
||||
return config.CODEC_H264
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/giongto35/cloud-game/config"
|
||||
"github.com/giongto35/cloud-game/util"
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/pion/webrtc/v2"
|
||||
"github.com/pion/webrtc/v2/pkg/media"
|
||||
|
|
@ -92,7 +93,7 @@ type WebRTC struct {
|
|||
}
|
||||
|
||||
// StartClient start webrtc
|
||||
func (w *WebRTC) StartClient(remoteSession string, iceCandidates []string) (string, error) {
|
||||
func (w *WebRTC) StartClient(remoteSession string, isMobile bool, iceCandidates []string) (string, error) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
log.Println(err)
|
||||
|
|
@ -114,7 +115,7 @@ func (w *WebRTC) StartClient(remoteSession string, iceCandidates []string) (stri
|
|||
return "", err
|
||||
}
|
||||
|
||||
if config.Codec == config.CODEC_H264 {
|
||||
if util.GetVideoEncoder(isMobile) == config.CODEC_H264 {
|
||||
videoTrack, err = w.connection.NewTrack(webrtc.DefaultPayloadTypeH264, rand.Uint32(), "video", "pion2")
|
||||
} else {
|
||||
videoTrack, err = w.connection.NewTrack(webrtc.DefaultPayloadTypeVP8, rand.Uint32(), "video", "pion2")
|
||||
|
|
@ -122,6 +123,7 @@ func (w *WebRTC) StartClient(remoteSession string, iceCandidates []string) (stri
|
|||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
_, err = w.connection.AddTrack(videoTrack)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
|
|||
|
|
@ -2,10 +2,11 @@ package worker
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/giongto35/cloud-game/config"
|
||||
"github.com/giongto35/cloud-game/cws"
|
||||
"github.com/giongto35/cloud-game/util"
|
||||
"github.com/giongto35/cloud-game/webrtc"
|
||||
"github.com/giongto35/cloud-game/worker/room"
|
||||
"github.com/gorilla/websocket"
|
||||
|
|
@ -50,8 +51,19 @@ func (h *Handler) RouteOverlord() {
|
|||
"initwebrtc",
|
||||
func(resp cws.WSPacket) (req cws.WSPacket) {
|
||||
log.Println("Received relay SDP of a browser from overlord")
|
||||
|
||||
peerconnection := webrtc.NewWebRTC()
|
||||
localSession, err := peerconnection.StartClient(resp.Data, iceCandidates[resp.SessionID])
|
||||
var initPacket struct {
|
||||
SDP string `json:"sdp"`
|
||||
IsMobile bool `json:"is_mobile"`
|
||||
}
|
||||
fmt.Println("HIHIHIHI!!!!", resp.Data)
|
||||
err := json.Unmarshal([]byte(resp.Data), &initPacket)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("HIHIHIHI!!!!", initPacket)
|
||||
localSession, err := peerconnection.StartClient(initPacket.SDP, initPacket.IsMobile, iceCandidates[resp.SessionID])
|
||||
//h.peerconnections[resp.SessionID] = peerconnection
|
||||
|
||||
// Create new sessions when we have new peerconnection initialized
|
||||
|
|
@ -92,7 +104,7 @@ func (h *Handler) RouteOverlord() {
|
|||
panic(err)
|
||||
}
|
||||
|
||||
room := h.startGameHandler(startPacket.GameName, resp.RoomID, resp.PlayerIndex, peerconnection, getVideoEncoder(startPacket.IsMobile))
|
||||
room := h.startGameHandler(startPacket.GameName, resp.RoomID, resp.PlayerIndex, peerconnection, util.GetVideoEncoder(startPacket.IsMobile))
|
||||
session.RoomID = room.ID
|
||||
// TODO: can data race
|
||||
h.rooms[room.ID] = room
|
||||
|
|
@ -206,16 +218,6 @@ func getServerIDOfRoom(oc *OverlordClient, roomID string) string {
|
|||
return packet.Data
|
||||
}
|
||||
|
||||
// getVideoEncoder returns video encoder based on some qualification.
|
||||
// Actually Android is only supporting VP8 but H264 has better encoding performance
|
||||
// TODO: Better use useragent attribute from frontend
|
||||
func getVideoEncoder(isMobile bool) string {
|
||||
if isMobile == true {
|
||||
return config.CODEC_VP8
|
||||
}
|
||||
return config.CODEC_H264
|
||||
}
|
||||
|
||||
func (h *Handler) startGameHandler(gameName, roomID string, playerIndex int, peerconnection *webrtc.WebRTC, videoEncoderType string) *room.Room {
|
||||
log.Println("Starting game", gameName)
|
||||
// If we are connecting to overlord, request corresponding serverID based on roomID
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue