implement signal offer.

This commit is contained in:
Miroslav Šedivý 2021-11-25 00:07:17 +01:00
parent a260ce6a60
commit 44e74e3088
6 changed files with 27 additions and 4 deletions

View File

@ -10,6 +10,7 @@ const (
const ( const (
SIGNAL_REQUEST = "signal/request" SIGNAL_REQUEST = "signal/request"
SIGNAL_RESTART = "signal/restart" SIGNAL_RESTART = "signal/restart"
SIGNAL_OFFER = "signal/offer"
SIGNAL_ANSWER = "signal/answer" SIGNAL_ANSWER = "signal/answer"
SIGNAL_PROVIDE = "signal/provide" SIGNAL_PROVIDE = "signal/provide"
SIGNAL_CANDIDATE = "signal/candidate" SIGNAL_CANDIDATE = "signal/candidate"

View File

@ -56,7 +56,7 @@ type SignalCandidate struct {
webrtc.ICECandidateInit webrtc.ICECandidateInit
} }
type SignalAnswer struct { type SignalDescription struct {
SDP string `json:"sdp"` SDP string `json:"sdp"`
} }

View File

@ -19,6 +19,7 @@ type ICEServer struct {
type WebRTCPeer interface { type WebRTCPeer interface {
CreateOffer(ICERestart bool) (*webrtc.SessionDescription, error) CreateOffer(ICERestart bool) (*webrtc.SessionDescription, error)
SignalOffer(sdp string) error
SignalAnswer(sdp string) error SignalAnswer(sdp string) error
SignalCandidate(candidate webrtc.ICECandidateInit) error SignalCandidate(candidate webrtc.ICECandidateInit) error

View File

@ -45,6 +45,13 @@ func (peer *WebRTCPeerCtx) CreateOffer(ICERestart bool) (*webrtc.SessionDescript
return peer.connection.LocalDescription(), nil return peer.connection.LocalDescription(), nil
} }
func (peer *WebRTCPeerCtx) SignalOffer(sdp string) error {
return peer.connection.SetRemoteDescription(webrtc.SessionDescription{
SDP: sdp,
Type: webrtc.SDPTypeOffer,
})
}
func (peer *WebRTCPeerCtx) SignalAnswer(sdp string) error { func (peer *WebRTCPeerCtx) SignalAnswer(sdp string) error {
return peer.connection.SetRemoteDescription(webrtc.SessionDescription{ return peer.connection.SetRemoteDescription(webrtc.SessionDescription{
SDP: sdp, SDP: sdp,

View File

@ -56,8 +56,13 @@ func (h *MessageHandlerCtx) Message(session types.Session, data types.WebSocketM
}) })
case event.SIGNAL_RESTART: case event.SIGNAL_RESTART:
err = h.signalRestart(session) err = h.signalRestart(session)
case event.SIGNAL_OFFER:
payload := &message.SignalDescription{}
err = utils.Unmarshal(payload, data.Payload, func() error {
return h.signalOffer(session, payload)
})
case event.SIGNAL_ANSWER: case event.SIGNAL_ANSWER:
payload := &message.SignalAnswer{} payload := &message.SignalDescription{}
err = utils.Unmarshal(payload, data.Payload, func() error { err = utils.Unmarshal(payload, data.Payload, func() error {
return h.signalAnswer(session, payload) return h.signalAnswer(session, payload)
}) })

View File

@ -48,14 +48,23 @@ func (h *MessageHandlerCtx) signalRestart(session types.Session) error {
session.Send( session.Send(
event.SIGNAL_RESTART, event.SIGNAL_RESTART,
message.SignalAnswer{ message.SignalDescription{
SDP: offer.SDP, SDP: offer.SDP,
}) })
return nil return nil
} }
func (h *MessageHandlerCtx) signalAnswer(session types.Session, payload *message.SignalAnswer) error { func (h *MessageHandlerCtx) signalOffer(session types.Session, payload *message.SignalDescription) error {
peer := session.GetWebRTCPeer()
if peer == nil {
return errors.New("webRTC peer does not exist")
}
return peer.SignalOffer(payload.SDP)
}
func (h *MessageHandlerCtx) signalAnswer(session types.Session, payload *message.SignalDescription) error {
peer := session.GetWebRTCPeer() peer := session.GetWebRTCPeer()
if peer == nil { if peer == nil {
return errors.New("webRTC peer does not exist") return errors.New("webRTC peer does not exist")