add SDP answer.

This commit is contained in:
Miroslav Šedivý 2021-11-28 18:36:47 +01:00
parent 096efe9b38
commit 7b2c1570bf
4 changed files with 63 additions and 15 deletions

View File

@ -19,9 +19,10 @@ type ICEServer struct {
type WebRTCPeer interface {
CreateOffer(ICERestart bool) (*webrtc.SessionDescription, error)
SignalOffer(sdp string) error
SignalAnswer(sdp string) error
SignalCandidate(candidate webrtc.ICECandidateInit) error
CreateAnswer() (*webrtc.SessionDescription, error)
SetOffer(sdp string) error
SetAnswer(sdp string) error
SetCandidate(candidate webrtc.ICECandidateInit) error
SetVideoID(videoID string) error
SendCursorPosition(x, y int) error
@ -36,5 +37,5 @@ type WebRTCManager interface {
ICEServers() []ICEServer
CreatePeer(session Session, videoID string) (*webrtc.SessionDescription, error)
CreatePeer(session Session, videoID string) (WebRTCPeer, error)
}

View File

@ -73,7 +73,7 @@ func (manager *WebRTCManagerCtx) ICEServers() []types.ICEServer {
return manager.config.ICEServers
}
func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID string) (*webrtc.SessionDescription, error) {
func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID string) (types.WebRTCPeer, error) {
// add session id to logger context
logger := manager.logger.With().Str("session_id", session.ID()).Logger()
logger.Info().Msg("creating webrtc peer")
@ -238,5 +238,5 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin
})
session.SetWebRTCPeer(peer)
return peer.CreateOffer(false)
return peer, nil
}

View File

@ -27,17 +27,33 @@ func (peer *WebRTCPeerCtx) CreateOffer(ICERestart bool) (*webrtc.SessionDescript
return nil, err
}
return peer.setLocalDescription(offer)
}
func (peer *WebRTCPeerCtx) CreateAnswer() (*webrtc.SessionDescription, error) {
peer.mu.Lock()
defer peer.mu.Unlock()
answer, err := peer.connection.CreateAnswer(nil)
if err != nil {
return nil, err
}
return peer.setLocalDescription(answer)
}
func (peer *WebRTCPeerCtx) setLocalDescription(description webrtc.SessionDescription) (*webrtc.SessionDescription, error) {
if !peer.iceTrickle {
// Create channel that is blocked until ICE Gathering is complete
gatherComplete := webrtc.GatheringCompletePromise(peer.connection)
if err := peer.connection.SetLocalDescription(offer); err != nil {
if err := peer.connection.SetLocalDescription(description); err != nil {
return nil, err
}
<-gatherComplete
} else {
if err := peer.connection.SetLocalDescription(offer); err != nil {
if err := peer.connection.SetLocalDescription(description); err != nil {
return nil, err
}
}
@ -45,21 +61,30 @@ func (peer *WebRTCPeerCtx) CreateOffer(ICERestart bool) (*webrtc.SessionDescript
return peer.connection.LocalDescription(), nil
}
func (peer *WebRTCPeerCtx) SignalOffer(sdp string) error {
func (peer *WebRTCPeerCtx) SetOffer(sdp string) error {
peer.mu.Lock()
defer peer.mu.Unlock()
return peer.connection.SetRemoteDescription(webrtc.SessionDescription{
SDP: sdp,
Type: webrtc.SDPTypeOffer,
})
}
func (peer *WebRTCPeerCtx) SignalAnswer(sdp string) error {
func (peer *WebRTCPeerCtx) SetAnswer(sdp string) error {
peer.mu.Lock()
defer peer.mu.Unlock()
return peer.connection.SetRemoteDescription(webrtc.SessionDescription{
SDP: sdp,
Type: webrtc.SDPTypeAnswer,
})
}
func (peer *WebRTCPeerCtx) SignalCandidate(candidate webrtc.ICECandidateInit) error {
func (peer *WebRTCPeerCtx) SetCandidate(candidate webrtc.ICECandidateInit) error {
peer.mu.Lock()
defer peer.mu.Unlock()
return peer.connection.AddICECandidate(candidate)
}

View File

@ -19,7 +19,12 @@ func (h *MessageHandlerCtx) signalRequest(session types.Session, payload *messag
payload.Video = videos[0]
}
offer, err := h.webrtc.CreatePeer(session, payload.Video)
peer, err := h.webrtc.CreatePeer(session, payload.Video)
if err != nil {
return err
}
offer, err := peer.CreateOffer(false)
if err != nil {
return err
}
@ -46,6 +51,7 @@ func (h *MessageHandlerCtx) signalRestart(session types.Session) error {
return err
}
// TODO: Use offer event intead.
session.Send(
event.SIGNAL_RESTART,
message.SignalDescription{
@ -61,7 +67,23 @@ func (h *MessageHandlerCtx) signalOffer(session types.Session, payload *message.
return errors.New("webRTC peer does not exist")
}
return peer.SignalOffer(payload.SDP)
err := peer.SetOffer(payload.SDP)
if err != nil {
return err
}
answer, err := peer.CreateAnswer()
if err != nil {
return err
}
session.Send(
event.SIGNAL_ANSWER,
message.SignalDescription{
SDP: answer.SDP,
})
return nil
}
func (h *MessageHandlerCtx) signalAnswer(session types.Session, payload *message.SignalDescription) error {
@ -70,7 +92,7 @@ func (h *MessageHandlerCtx) signalAnswer(session types.Session, payload *message
return errors.New("webRTC peer does not exist")
}
return peer.SignalAnswer(payload.SDP)
return peer.SetAnswer(payload.SDP)
}
func (h *MessageHandlerCtx) signalCandidate(session types.Session, payload *message.SignalCandidate) error {
@ -79,7 +101,7 @@ func (h *MessageHandlerCtx) signalCandidate(session types.Session, payload *mess
return errors.New("webRTC peer does not exist")
}
return peer.SignalCandidate(payload.ICECandidateInit)
return peer.SetCandidate(payload.ICECandidateInit)
}
func (h *MessageHandlerCtx) signalVideo(session types.Session, payload *message.SignalVideo) error {