2020-10-22 16:54:50 +02:00
|
|
|
package webrtc
|
|
|
|
|
2021-02-06 18:16:24 +01:00
|
|
|
import "github.com/pion/webrtc/v3"
|
2020-10-22 16:54:50 +02:00
|
|
|
|
2020-11-25 18:41:40 +01:00
|
|
|
type WebRTCPeerCtx struct {
|
2021-02-14 14:40:17 +01:00
|
|
|
api *webrtc.API
|
|
|
|
connection *webrtc.PeerConnection
|
|
|
|
dataChannel *webrtc.DataChannel
|
|
|
|
changeVideo func(videoID string) error
|
2020-10-22 16:54:50 +02:00
|
|
|
}
|
|
|
|
|
2021-02-14 17:11:21 +01:00
|
|
|
func (peer *WebRTCPeerCtx) SignalAnswer(sdp string) error {
|
|
|
|
return peer.connection.SetRemoteDescription(webrtc.SessionDescription{
|
2021-02-14 14:40:17 +01:00
|
|
|
SDP: sdp,
|
2020-11-01 16:09:48 +01:00
|
|
|
Type: webrtc.SDPTypeAnswer,
|
|
|
|
})
|
2020-10-22 16:54:50 +02:00
|
|
|
}
|
|
|
|
|
2021-02-14 17:11:21 +01:00
|
|
|
func (peer *WebRTCPeerCtx) SignalCandidate(candidate webrtc.ICECandidateInit) error {
|
|
|
|
return peer.connection.AddICECandidate(candidate)
|
2021-02-02 20:43:33 +01:00
|
|
|
}
|
|
|
|
|
2021-02-14 17:11:21 +01:00
|
|
|
func (peer *WebRTCPeerCtx) SetVideoID(videoID string) error {
|
|
|
|
return peer.changeVideo(videoID)
|
2021-02-04 20:39:48 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 17:11:21 +01:00
|
|
|
func (peer *WebRTCPeerCtx) Destroy() error {
|
|
|
|
if peer.connection == nil || peer.connection.ConnectionState() != webrtc.PeerConnectionStateConnected {
|
2020-11-01 16:09:48 +01:00
|
|
|
return nil
|
2020-10-22 16:54:50 +02:00
|
|
|
}
|
2021-02-02 18:28:32 +01:00
|
|
|
|
2021-02-14 17:11:21 +01:00
|
|
|
return peer.connection.Close()
|
2020-10-22 16:54:50 +02:00
|
|
|
}
|