Archived
2
0
This repository has been archived on 2024-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
neko-custom/server/internal/webrtc/peer.go

78 lines
1.6 KiB
Go
Raw Normal View History

2020-01-25 04:47:37 +13:00
package webrtc
import (
2023-03-18 12:49:25 +13:00
"encoding/json"
2020-02-04 03:46:30 +13:00
"sync"
2021-02-15 05:30:24 +13:00
"github.com/pion/webrtc/v3"
2020-01-25 04:47:37 +13:00
)
type Peer struct {
id string
mu sync.Mutex
manager *WebRTCManager
connection *webrtc.PeerConnection
2020-01-25 04:47:37 +13:00
}
2021-12-03 11:43:36 +13:00
func (peer *Peer) CreateOffer() (string, error) {
desc, err := peer.connection.CreateOffer(nil)
if err != nil {
return "", err
}
err = peer.connection.SetLocalDescription(desc)
if err != nil {
return "", err
}
return desc.SDP, nil
}
func (peer *Peer) CreateAnswer() (string, error) {
desc, err := peer.connection.CreateAnswer(nil)
if err != nil {
return "", err
}
err = peer.connection.SetLocalDescription(desc)
if err != nil {
return "", nil
}
return desc.SDP, nil
}
func (peer *Peer) SetOffer(sdp string) error {
return peer.connection.SetRemoteDescription(webrtc.SessionDescription{SDP: sdp, Type: webrtc.SDPTypeOffer})
}
func (peer *Peer) SetAnswer(sdp string) error {
2020-02-13 12:13:33 +13:00
return peer.connection.SetRemoteDescription(webrtc.SessionDescription{SDP: sdp, Type: webrtc.SDPTypeAnswer})
2020-01-25 04:47:37 +13:00
}
2023-03-18 12:49:25 +13:00
func (peer *Peer) SetCandidate(candidateString string) error {
var candidate webrtc.ICECandidateInit
err := json.Unmarshal([]byte(candidateString), &candidate)
if err != nil {
return err
}
return peer.connection.AddICECandidate(candidate)
}
2020-01-25 04:47:37 +13:00
func (peer *Peer) WriteData(v interface{}) error {
2020-02-04 03:46:30 +13:00
peer.mu.Lock()
defer peer.mu.Unlock()
2020-01-25 04:47:37 +13:00
return nil
}
func (peer *Peer) Destroy() error {
if peer.connection != nil && peer.connection.ConnectionState() != webrtc.PeerConnectionStateClosed {
2020-01-25 04:47:37 +13:00
if err := peer.connection.Close(); err != nil {
return err
}
}
return nil
}