neko/internal/websocket/handler/signal.go

102 lines
2.4 KiB
Go
Raw Normal View History

2020-11-02 04:09:48 +13:00
package handler
import (
2020-10-29 07:15:48 +13:00
"demodesk/neko/internal/types"
"demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message"
)
2021-05-01 02:05:07 +12:00
func (h *MessageHandlerCtx) signalRequest(session types.Session, payload *message.SignalVideo) error {
2021-08-30 04:23:58 +12:00
logger := h.logger.With().Str("session_id", session.ID()).Logger()
2021-03-14 12:45:51 +13:00
if !session.Profile().CanWatch {
2021-08-30 04:23:58 +12:00
logger.Debug().Msg("not allowed to watch")
2020-12-07 06:49:20 +13:00
return nil
}
2021-05-01 02:05:07 +12:00
// use default first video, if not provided
if payload.Video == "" {
videos := h.capture.VideoIDs()
payload.Video = videos[0]
}
2021-02-08 05:07:55 +13:00
2021-05-01 02:05:07 +12:00
offer, err := h.webrtc.CreatePeer(session, payload.Video)
if err != nil {
return err
}
2020-11-19 08:30:33 +13:00
return session.Send(
message.SignalProvide{
2021-03-18 03:47:49 +13:00
Event: event.SIGNAL_PROVIDE,
SDP: offer.SDP,
ICEServers: h.webrtc.ICEServers(),
2021-05-01 02:05:07 +12:00
Video: payload.Video,
2020-11-19 08:30:33 +13:00
})
}
2021-06-28 08:02:05 +12:00
func (h *MessageHandlerCtx) signalRestart(session types.Session) error {
2021-08-30 04:23:58 +12:00
logger := h.logger.With().Str("session_id", session.ID()).Logger()
2021-06-28 08:02:05 +12:00
peer := session.GetWebRTCPeer()
if peer == nil {
2021-08-30 04:23:58 +12:00
logger.Debug().Msg("webRTC peer does not exist")
2021-06-28 08:02:05 +12:00
return nil
}
offer, err := peer.CreateOffer(true)
2021-06-28 08:02:05 +12:00
if err != nil {
return err
}
return session.Send(
message.SignalAnswer{
Event: event.SIGNAL_RESTART,
SDP: offer.SDP,
})
}
2020-11-02 04:09:48 +13:00
func (h *MessageHandlerCtx) signalAnswer(session types.Session, payload *message.SignalAnswer) error {
2021-08-30 04:23:58 +12:00
logger := h.logger.With().Str("session_id", session.ID()).Logger()
2021-02-06 06:14:45 +13:00
peer := session.GetWebRTCPeer()
if peer == nil {
2021-08-30 04:23:58 +12:00
logger.Debug().Msg("webRTC peer does not exist")
2021-02-06 06:14:45 +13:00
return nil
}
return peer.SignalAnswer(payload.SDP)
}
2021-02-03 08:43:33 +13:00
func (h *MessageHandlerCtx) signalCandidate(session types.Session, payload *message.SignalCandidate) error {
2021-08-30 04:23:58 +12:00
logger := h.logger.With().Str("session_id", session.ID()).Logger()
2021-02-06 06:14:45 +13:00
peer := session.GetWebRTCPeer()
if peer == nil {
2021-08-30 04:23:58 +12:00
logger.Debug().Msg("webRTC peer does not exist")
2021-02-06 06:14:45 +13:00
return nil
}
return peer.SignalCandidate(*payload.ICECandidateInit)
2021-02-03 08:43:33 +13:00
}
2021-02-06 08:35:30 +13:00
func (h *MessageHandlerCtx) signalVideo(session types.Session, payload *message.SignalVideo) error {
2021-08-30 04:23:58 +12:00
logger := h.logger.With().Str("session_id", session.ID()).Logger()
2021-02-06 08:35:30 +13:00
peer := session.GetWebRTCPeer()
if peer == nil {
2021-08-30 04:23:58 +12:00
logger.Debug().Msg("webRTC peer does not exist")
2021-02-06 08:35:30 +13:00
return nil
}
2021-02-08 05:29:40 +13:00
err := peer.SetVideoID(payload.Video)
if err != nil {
return err
}
return session.Send(
message.SignalVideo{
2021-02-15 02:40:17 +13:00
Event: event.SIGNAL_VIDEO,
Video: payload.Video,
2021-02-08 05:29:40 +13:00
})
2021-02-06 08:35:30 +13:00
}