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
2021-12-03 23:54:07 +01:00

67 lines
1.3 KiB
Go

package webrtc
import (
"sync"
"github.com/pion/webrtc/v3"
)
type Peer struct {
id string
mu sync.Mutex
manager *WebRTCManager
connection *webrtc.PeerConnection
}
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 {
return peer.connection.SetRemoteDescription(webrtc.SessionDescription{SDP: sdp, Type: webrtc.SDPTypeAnswer})
}
func (peer *Peer) WriteData(v interface{}) error {
peer.mu.Lock()
defer peer.mu.Unlock()
return nil
}
func (peer *Peer) Destroy() error {
if peer.connection != nil && peer.connection.ConnectionState() != webrtc.PeerConnectionStateClosed {
if err := peer.connection.Close(); err != nil {
return err
}
}
return nil
}