mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-21 00:59:22 +00:00
Remove Base64 encoding of wire data
This commit is contained in:
parent
fa783dc7cd
commit
f72202684f
5 changed files with 20 additions and 33 deletions
|
|
@ -47,7 +47,7 @@ type (
|
|||
}
|
||||
WebrtcIceCandidateRequest struct {
|
||||
Stateful
|
||||
Candidate string `json:"candidate"` // Base64-encoded ICE candidate
|
||||
Candidate string `json:"candidate"`
|
||||
}
|
||||
InitWebrtcStreamRequest struct {
|
||||
// Stateful
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ package coordinator
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
|
@ -92,7 +91,7 @@ func RequestToHandshake(data string) (*api.ConnectionRequest[com.Uid], error) {
|
|||
if data == "" {
|
||||
return nil, api.ErrMalformed
|
||||
}
|
||||
handshake, err := api.UnwrapChecked[api.ConnectionRequest[com.Uid]](base64.URLEncoding.DecodeString(data))
|
||||
handshake, err := api.UnwrapChecked[api.ConnectionRequest[com.Uid]]([]byte(data), nil)
|
||||
if err != nil || handshake == nil {
|
||||
return nil, fmt.Errorf("%w (%v)", err, handshake)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
package worker
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
|
||||
"github.com/giongto35/cloud-game/v3/pkg/api"
|
||||
"github.com/giongto35/cloud-game/v3/pkg/com"
|
||||
"github.com/giongto35/cloud-game/v3/pkg/config"
|
||||
|
|
@ -17,7 +15,7 @@ import (
|
|||
// buildConnQuery builds initial connection data query to a coordinator.
|
||||
func buildConnQuery(id com.Uid, conf config.Worker, address string) (string, error) {
|
||||
addr := conf.GetPingAddr(address)
|
||||
return toBase64Json(api.ConnectionRequest[com.Uid]{
|
||||
return toJson(api.ConnectionRequest[com.Uid]{
|
||||
Addr: addr.Hostname(),
|
||||
Id: id,
|
||||
IsHTTPS: conf.Server.Https,
|
||||
|
|
@ -31,7 +29,7 @@ func buildConnQuery(id com.Uid, conf config.Worker, address string) (string, err
|
|||
func (c *coordinator) HandleInitWebrtcStream(rq api.InitWebrtcStreamRequest, w *Worker, factory *webrtc.ApiFactory) api.Out {
|
||||
peer := webrtc.New(c.log, factory)
|
||||
err := peer.NewConnection(w.conf.Encoder.Video.Codec, "opus", func(data any) {
|
||||
candidate, err := toBase64Json(data)
|
||||
candidate, err := toJson(data)
|
||||
if err != nil {
|
||||
c.log.Error().Err(err).Msgf("ICE candidate encode fail for [%v]", data)
|
||||
return
|
||||
|
|
@ -44,7 +42,7 @@ func (c *coordinator) HandleInitWebrtcStream(rq api.InitWebrtcStreamRequest, w *
|
|||
}
|
||||
|
||||
if rq.Initiator {
|
||||
if err := peer.SetRemoteSDP(rq.Sdp, fromBase64Json); err != nil {
|
||||
if err := peer.SetRemoteSDP(rq.Sdp, fromJson); err != nil {
|
||||
c.log.Error().Err(err).Msgf("cannot set remote SDP of peer [%v]", rq.Id)
|
||||
return api.EmptyPacket
|
||||
}
|
||||
|
|
@ -61,7 +59,7 @@ func (c *coordinator) HandleInitWebrtcStream(rq api.InitWebrtcStreamRequest, w *
|
|||
return api.EmptyPacket
|
||||
}
|
||||
|
||||
sdp, err := toBase64Json(*lsdp)
|
||||
sdp, err := toJson(*lsdp)
|
||||
if err != nil {
|
||||
c.log.Error().Err(err).Msgf("SDP encode fail for [%v]", *lsdp)
|
||||
return api.EmptyPacket
|
||||
|
|
@ -76,7 +74,7 @@ func (c *coordinator) HandleInitWebrtcStream(rq api.InitWebrtcStreamRequest, w *
|
|||
|
||||
func (c *coordinator) HandleWebrtcAnswer(rq api.WebrtcAnswerRequest, w *Worker) {
|
||||
if user := w.router.FindUser(rq.Id); user != nil {
|
||||
if err := room.WithWebRTC(user.Session).SetRemoteSDP(rq.Sdp, fromBase64Json); err != nil {
|
||||
if err := room.WithWebRTC(user.Session).SetRemoteSDP(rq.Sdp, fromJson); err != nil {
|
||||
c.log.Error().Err(err).Msgf("cannot set remote SDP of client [%v]", rq.Id)
|
||||
}
|
||||
}
|
||||
|
|
@ -84,7 +82,7 @@ func (c *coordinator) HandleWebrtcAnswer(rq api.WebrtcAnswerRequest, w *Worker)
|
|||
|
||||
func (c *coordinator) HandleWebrtcIceCandidate(rs api.WebrtcIceCandidateRequest, w *Worker) {
|
||||
if user := w.router.FindUser(rs.Id); user != nil {
|
||||
if err := room.WithWebRTC(user.Session).AddCandidate(rs.Candidate, fromBase64Json); err != nil {
|
||||
if err := room.WithWebRTC(user.Session).AddCandidate(rs.Candidate, fromJson); err != nil {
|
||||
c.log.Error().Err(err).Msgf("cannot add ICE candidate of the client [%v]", rs.Id)
|
||||
}
|
||||
}
|
||||
|
|
@ -305,21 +303,11 @@ func (c *coordinator) HandleRecordGame(rq api.RecordGameRequest, w *Worker) api.
|
|||
return api.OkPacket
|
||||
}
|
||||
|
||||
// fromBase64Json decodes data from a URL-encoded Base64+JSON string.
|
||||
func fromBase64Json(data string, obj any) error {
|
||||
b, err := base64.URLEncoding.DecodeString(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = json.Unmarshal(b, obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
func fromJson(data string, obj any) error {
|
||||
return json.Unmarshal([]byte(data), obj)
|
||||
}
|
||||
|
||||
// toBase64Json encodes data to a URL-encoded Base64+JSON string.
|
||||
func toBase64Json(data any) (string, error) {
|
||||
func toJson(data any) (string, error) {
|
||||
if data == nil {
|
||||
return "", nil
|
||||
}
|
||||
|
|
@ -327,5 +315,5 @@ func toBase64Json(data any) (string, error) {
|
|||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.URLEncoding.EncodeToString(b), nil
|
||||
return string(b), nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -318,8 +318,8 @@ const libretro = (function () {
|
|||
})();
|
||||
|
||||
// data type converters
|
||||
const toBase64 = (val) => btoa(JSON.stringify(val));
|
||||
const fromBase64 = (val) => JSON.parse(atob(val));
|
||||
const toJson = (val) => JSON.stringify(val);
|
||||
const fromJson = (val) => JSON.parse(val);
|
||||
const fromBytes = (val) =>
|
||||
JSON.parse(String.fromCharCode.apply(null, new Uint8Array(val)));
|
||||
|
||||
|
|
@ -335,7 +335,7 @@ export const api = {
|
|||
endpoint: endpoints,
|
||||
endpointName: endpointName,
|
||||
fromBytes,
|
||||
fromBase64,
|
||||
fromJson,
|
||||
server: {
|
||||
/** Initializes the stream with the given config.
|
||||
* @property {boolean} initiator - whether the user is an initiator or not.
|
||||
|
|
@ -344,11 +344,11 @@ export const api = {
|
|||
initWebrtcStream: ({ initiator = false, sdpOffer = "" } = {}) =>
|
||||
packet(endpoints.INIT_WEBRTC_STREAM, {
|
||||
initiator,
|
||||
...(sdpOffer && { sdp: toBase64(sdpOffer) }),
|
||||
...(sdpOffer && { sdp: toJson(sdpOffer) }),
|
||||
}),
|
||||
sendIceCandidate: (candidate) =>
|
||||
packet(endpoints.ICE_CANDIDATE, toBase64(candidate)),
|
||||
sendSdp: (sdp) => packet(endpoints.ANSWER, toBase64(sdp)),
|
||||
packet(endpoints.ICE_CANDIDATE, toJson(candidate)),
|
||||
sendSdp: (sdp) => packet(endpoints.ANSWER, toJson(sdp)),
|
||||
latencyCheck: (id, list) => packet(endpoints.LATENCY_CHECK, list, id),
|
||||
getWorkerList: () => packet(endpoints.GET_WORKER_LIST),
|
||||
},
|
||||
|
|
|
|||
|
|
@ -185,10 +185,10 @@ const onMessage = (m) => {
|
|||
handleWebrtcStart({ data: payload, initiator });
|
||||
break;
|
||||
case api.endpoint.OFFER:
|
||||
webrtc.answer(api.fromBase64(payload));
|
||||
webrtc.answer(api.fromJson(payload));
|
||||
break;
|
||||
case api.endpoint.ICE_CANDIDATE:
|
||||
webrtc.candidate(payload ? api.fromBase64(payload) : "");
|
||||
webrtc.candidate(payload ? api.fromJson(payload) : "");
|
||||
break;
|
||||
case api.endpoint.GAME_START:
|
||||
if (payload.av) pub(APP_VIDEO_CHANGED, payload.av);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue