From e393819edadd5b767c6ed4447dabaa30a0492ed8 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Sun, 3 Jan 2021 17:00:58 +0100 Subject: [PATCH] Carry raw SDP in the protocol. --- rtpconn/webclient.go | 69 ++++++++++++++++++++++---------------------- static/protocol.js | 29 +++++++++++-------- 2 files changed, 51 insertions(+), 47 deletions(-) diff --git a/rtpconn/webclient.go b/rtpconn/webclient.go index cd2ca41..fc36d0a 100644 --- a/rtpconn/webclient.go +++ b/rtpconn/webclient.go @@ -162,24 +162,23 @@ func (v rateMap) MarshalJSON() ([]byte, error) { } type clientMessage struct { - Type string `json:"type"` - Kind string `json:"kind,omitempty"` - Id string `json:"id,omitempty"` - Source string `json:"source,omitempty"` - Dest string `json:"dest,omitempty"` - Username string `json:"username,omitempty"` - Password string `json:"password,omitempty"` - Privileged bool `json:"privileged,omitempty"` - Permissions *group.ClientPermissions `json:"permissions,omitempty"` - Group string `json:"group,omitempty"` - Value interface{} `json:"value,omitempty"` - Time int64 `json:"time,omitempty"` - Offer *webrtc.SessionDescription `json:"offer,omitempty"` - Answer *webrtc.SessionDescription `json:"answer,omitempty"` - Candidate *webrtc.ICECandidateInit `json:"candidate,omitempty"` - Labels map[string]string `json:"labels,omitempty"` - Request rateMap `json:"request,omitempty"` - RTCConfiguration *webrtc.Configuration `json:"rtcConfiguration,omitempty"` + Type string `json:"type"` + Kind string `json:"kind,omitempty"` + Id string `json:"id,omitempty"` + Source string `json:"source,omitempty"` + Dest string `json:"dest,omitempty"` + Username string `json:"username,omitempty"` + Password string `json:"password,omitempty"` + Privileged bool `json:"privileged,omitempty"` + Permissions *group.ClientPermissions `json:"permissions,omitempty"` + Group string `json:"group,omitempty"` + Value interface{} `json:"value,omitempty"` + Time int64 `json:"time,omitempty"` + SDP string `json:"sdp,omitempty"` + Candidate *webrtc.ICECandidateInit `json:"candidate,omitempty"` + Labels map[string]string `json:"labels,omitempty"` + Request rateMap `json:"request,omitempty"` + RTCConfiguration *webrtc.Configuration `json:"rtcConfiguration,omitempty"` } type closeMessage struct { @@ -464,7 +463,7 @@ func negotiate(c *webClient, down *rtpDownConnection, renegotiate, restartIce bo Id: down.id, Source: source, Username: username, - Offer: &offer, + SDP: offer.SDP, Labels: labels, }) } @@ -481,7 +480,7 @@ func sendICE(c *webClient, id string, candidate *webrtc.ICECandidate) error { }) } -func gotOffer(c *webClient, id string, offer webrtc.SessionDescription, renegotiate bool, labels map[string]string) error { +func gotOffer(c *webClient, id string, sdp string, renegotiate bool, labels map[string]string) error { if !renegotiate { // unless the client indicates that this is a compatible // renegotiation, tear down the existing connection. @@ -496,12 +495,15 @@ func gotOffer(c *webClient, id string, offer webrtc.SessionDescription, renegoti up.userId = c.Id() up.username = c.Username() - err = up.pc.SetRemoteDescription(offer) + err = up.pc.SetRemoteDescription(webrtc.SessionDescription{ + Type: webrtc.SDPTypeOffer, + SDP: sdp, + }) if err != nil { if renegotiate && !isnew { // create a new PC from scratch log.Printf("SetRemoteDescription(offer): %v", err) - return gotOffer(c, id, offer, false, labels) + return gotOffer(c, id, sdp, false, labels) } return err } @@ -522,20 +524,23 @@ func gotOffer(c *webClient, id string, offer webrtc.SessionDescription, renegoti } return c.write(clientMessage{ - Type: "answer", - Id: id, - Answer: &answer, + Type: "answer", + Id: id, + SDP: answer.SDP, }) } var ErrUnknownId = errors.New("unknown id") -func gotAnswer(c *webClient, id string, answer webrtc.SessionDescription) error { +func gotAnswer(c *webClient, id string, sdp string) error { down := getDownConn(c, id) if down == nil { return ErrUnknownId } - err := down.pc.SetRemoteDescription(answer) + err := down.pc.SetRemoteDescription(webrtc.SessionDescription{ + Type: webrtc.SDPTypeAnswer, + SDP: sdp, + }) if err != nil { return err } @@ -1128,21 +1133,15 @@ func handleClientMessage(c *webClient, m clientMessage) error { }) return c.error(group.UserError("not authorised")) } - if m.Offer == nil { - return group.ProtocolError("null offer") - } err := gotOffer( - c, m.Id, *m.Offer, m.Kind == "renegotiate", m.Labels, + c, m.Id, m.SDP, m.Kind == "renegotiate", m.Labels, ) if err != nil { log.Printf("gotOffer: %v", err) return failUpConnection(c, m.Id, "negotiation failed") } case "answer": - if m.Answer == nil { - return group.ProtocolError("null answer") - } - err := gotAnswer(c, m.Id, *m.Answer) + err := gotAnswer(c, m.Id, m.SDP) if err != nil { log.Printf("gotAnswer: %v", err) message := "" diff --git a/static/protocol.js b/static/protocol.js index 922bdbd..ae44e4b 100644 --- a/static/protocol.js +++ b/static/protocol.js @@ -183,8 +183,7 @@ function ServerConnection() { * @property {Object} [permissions] * @property {string} [group] * @property {unknown} [value] - * @property {RTCSessionDescriptionInit} [offer] - * @property {RTCSessionDescriptionInit} [answer] + * @property {string} [sdp] * @property {RTCIceCandidate} [candidate] * @property {Object} [labels] * @property {Object} [request] @@ -265,10 +264,10 @@ ServerConnection.prototype.connect = async function(url) { break; case 'offer': sc.gotOffer(m.id, m.labels, m.source, m.username, - m.offer, m.kind === 'renegotiate'); + m.sdp, m.kind === 'renegotiate'); break; case 'answer': - sc.gotAnswer(m.id, m.answer); + sc.gotAnswer(m.id, m.sdp); break; case 'renegotiate': sc.gotRenegotiate(m.id) @@ -525,11 +524,11 @@ ServerConnection.prototype.groupAction = function(kind, message) { * @param {Object} labels * @param {string} source * @param {string} username - * @param {RTCSessionDescriptionInit} offer + * @param {string} sdp * @param {boolean} renegotiate * @function */ -ServerConnection.prototype.gotOffer = async function(id, labels, source, username, offer, renegotiate) { +ServerConnection.prototype.gotOffer = async function(id, labels, source, username, sdp, renegotiate) { let sc = this; let c = sc.down[id]; if(c && !renegotiate) { @@ -598,7 +597,10 @@ ServerConnection.prototype.gotOffer = async function(id, labels, source, usernam sc.ondownstream.call(sc, c); try { - await c.pc.setRemoteDescription(offer); + await c.pc.setRemoteDescription({ + type: 'offer', + sdp: sdp, + }); await c.flushRemoteIceCandidates(); @@ -609,7 +611,7 @@ ServerConnection.prototype.gotOffer = async function(id, labels, source, usernam this.send({ type: 'answer', id: id, - answer: answer, + sdp: answer.sdp, }); } catch(e) { try { @@ -631,15 +633,18 @@ ServerConnection.prototype.gotOffer = async function(id, labels, source, usernam * Called when we receive an answer from the server. Don't call this. * * @param {string} id - * @param {RTCSessionDescriptionInit} answer + * @param {string} sdp * @function */ -ServerConnection.prototype.gotAnswer = async function(id, answer) { +ServerConnection.prototype.gotAnswer = async function(id, sdp) { let c = this.up[id]; if(!c) throw new Error('unknown up stream'); try { - await c.pc.setRemoteDescription(answer); + await c.pc.setRemoteDescription({ + type: 'answer', + sdp: sdp, + }); } catch(e) { try { if(c.onerror) @@ -1038,7 +1043,7 @@ Stream.prototype.negotiate = async function (restartIce) { kind: this.localDescriptionSent ? 'renegotiate' : '', id: c.id, labels: c.labelsByMid, - offer: offer, + sdp: offer.sdp, }); this.localDescriptionSent = true; c.flushLocalIceCandidates();