2020-11-02 04:09:48 +13:00
|
|
|
package handler
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
import (
|
2021-09-02 09:10:18 +12:00
|
|
|
"errors"
|
|
|
|
|
2022-07-14 10:58:22 +12:00
|
|
|
"github.com/demodesk/neko/pkg/types"
|
|
|
|
"github.com/demodesk/neko/pkg/types/event"
|
|
|
|
"github.com/demodesk/neko/pkg/types/message"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
2021-05-01 02:05:07 +12:00
|
|
|
func (h *MessageHandlerCtx) signalRequest(session types.Session, payload *message.SignalVideo) error {
|
2021-03-14 12:45:51 +13:00
|
|
|
if !session.Profile().CanWatch {
|
2021-09-02 09:10:18 +12:00
|
|
|
return errors.New("not allowed to watch")
|
2020-12-07 06:49:20 +13:00
|
|
|
}
|
|
|
|
|
2021-05-01 02:05:07 +12:00
|
|
|
// use default first video, if not provided
|
|
|
|
if payload.Video == "" {
|
2022-10-18 00:39:31 +13:00
|
|
|
videos := h.capture.Video().IDs()
|
2021-05-01 02:05:07 +12:00
|
|
|
payload.Video = videos[0]
|
|
|
|
}
|
2021-02-08 05:07:55 +13:00
|
|
|
|
2022-10-26 07:25:00 +13:00
|
|
|
var err error
|
|
|
|
if payload.Bitrate == 0 {
|
|
|
|
// get bitrate from video id
|
|
|
|
payload.Bitrate, err = h.capture.GetBitrateFromVideoID(payload.Video)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 07:45:51 +13:00
|
|
|
offer, err := h.webrtc.CreatePeer(session, payload.Bitrate, payload.VideoAuto)
|
2020-10-23 03:54:50 +13:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-26 07:25:00 +13:00
|
|
|
if webrtcPeer := session.GetWebRTCPeer(); webrtcPeer != nil {
|
|
|
|
// set webrtc as paused if session has private mode enabled
|
|
|
|
if session.PrivateModeEnabled() {
|
|
|
|
webrtcPeer.SetPaused(true)
|
|
|
|
}
|
|
|
|
|
2023-02-07 07:45:51 +13:00
|
|
|
payload.Video = webrtcPeer.GetVideoID()
|
2023-02-27 09:54:10 +13:00
|
|
|
payload.VideoAuto = webrtcPeer.VideoAuto()
|
2022-03-27 11:20:38 +13:00
|
|
|
}
|
|
|
|
|
2021-09-02 07:58:39 +12:00
|
|
|
session.Send(
|
|
|
|
event.SIGNAL_PROVIDE,
|
2020-11-19 08:30:33 +13:00
|
|
|
message.SignalProvide{
|
2021-03-18 03:47:49 +13:00
|
|
|
SDP: offer.SDP,
|
|
|
|
ICEServers: h.webrtc.ICEServers(),
|
2023-02-07 07:45:51 +13:00
|
|
|
Video: payload.Video, // TODO: Refactor
|
|
|
|
Bitrate: payload.Bitrate,
|
|
|
|
VideoAuto: payload.VideoAuto,
|
2020-11-19 08:30:33 +13:00
|
|
|
})
|
2021-09-02 07:58:39 +12:00
|
|
|
|
|
|
|
return nil
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-06-28 08:02:05 +12:00
|
|
|
func (h *MessageHandlerCtx) signalRestart(session types.Session) error {
|
|
|
|
peer := session.GetWebRTCPeer()
|
|
|
|
if peer == nil {
|
2021-09-02 09:10:18 +12:00
|
|
|
return errors.New("webRTC peer does not exist")
|
2021-06-28 08:02:05 +12:00
|
|
|
}
|
|
|
|
|
2021-06-28 08:05:37 +12:00
|
|
|
offer, err := peer.CreateOffer(true)
|
2021-06-28 08:02:05 +12:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-07 07:45:51 +13:00
|
|
|
// TODO: Use offer event instead.
|
2021-09-02 07:58:39 +12:00
|
|
|
session.Send(
|
|
|
|
event.SIGNAL_RESTART,
|
2021-11-25 12:07:17 +13:00
|
|
|
message.SignalDescription{
|
2021-09-02 07:58:39 +12:00
|
|
|
SDP: offer.SDP,
|
2021-06-28 08:02:05 +12:00
|
|
|
})
|
2021-09-02 07:58:39 +12:00
|
|
|
|
|
|
|
return nil
|
2021-06-28 08:02:05 +12:00
|
|
|
}
|
|
|
|
|
2021-11-25 12:07:17 +13:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2021-11-29 06:36:47 +13:00
|
|
|
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
|
2021-11-25 12:07:17 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *MessageHandlerCtx) signalAnswer(session types.Session, payload *message.SignalDescription) error {
|
2021-02-06 06:14:45 +13:00
|
|
|
peer := session.GetWebRTCPeer()
|
|
|
|
if peer == nil {
|
2021-09-02 09:10:18 +12:00
|
|
|
return errors.New("webRTC peer does not exist")
|
2021-02-06 06:14:45 +13:00
|
|
|
}
|
|
|
|
|
2021-11-29 06:36:47 +13:00
|
|
|
return peer.SetAnswer(payload.SDP)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
2021-02-03 08:43:33 +13:00
|
|
|
|
|
|
|
func (h *MessageHandlerCtx) signalCandidate(session types.Session, payload *message.SignalCandidate) error {
|
2021-02-06 06:14:45 +13:00
|
|
|
peer := session.GetWebRTCPeer()
|
|
|
|
if peer == nil {
|
2021-09-02 09:10:18 +12:00
|
|
|
return errors.New("webRTC peer does not exist")
|
2021-02-06 06:14:45 +13:00
|
|
|
}
|
|
|
|
|
2021-11-29 06:36:47 +13:00
|
|
|
return peer.SetCandidate(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 {
|
|
|
|
peer := session.GetWebRTCPeer()
|
|
|
|
if peer == nil {
|
2021-09-02 09:10:18 +12:00
|
|
|
return errors.New("webRTC peer does not exist")
|
2021-02-06 08:35:30 +13:00
|
|
|
}
|
|
|
|
|
2023-02-07 07:45:51 +13:00
|
|
|
peer.SetVideoAuto(payload.VideoAuto)
|
2022-10-26 07:25:00 +13:00
|
|
|
|
2023-02-07 07:45:51 +13:00
|
|
|
if payload.Video != "" {
|
|
|
|
if err := peer.SetVideoID(payload.Video); err != nil {
|
|
|
|
h.logger.Error().Err(err).Msg("failed to set video id")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := peer.SetVideoBitrate(payload.Bitrate); err != nil {
|
|
|
|
h.logger.Error().Err(err).Msg("failed to set video bitrate")
|
|
|
|
}
|
2021-02-08 05:29:40 +13:00
|
|
|
}
|
|
|
|
|
2021-09-02 07:58:39 +12:00
|
|
|
return nil
|
2021-02-06 08:35:30 +13:00
|
|
|
}
|