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