All checks were successful
Create and publish a Docker image 🚀 / build-and-push-image (push) Successful in 2m25s
105 lines
1.9 KiB
Go
105 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/gorilla/websocket"
|
|
"github.com/pion/webrtc/v4"
|
|
)
|
|
|
|
type Signal struct {
|
|
Type string `json:"type"`
|
|
Data json.RawMessage `json:"data,omitempty"`
|
|
}
|
|
|
|
type Peer struct {
|
|
ID string
|
|
WS *websocket.Conn
|
|
PC *webrtc.PeerConnection
|
|
Room *Room
|
|
}
|
|
|
|
func NewPeer(ws *websocket.Conn, room *Room) *Peer {
|
|
pc, _ := webrtc.NewPeerConnection(webrtc.Configuration{
|
|
ICEServers: []webrtc.ICEServer{
|
|
{URLs: []string{"stun:stun.l.google.com:19302"}},
|
|
},
|
|
})
|
|
|
|
// ОБЯЗАТЕЛЬНО для SFU
|
|
pc.AddTransceiverFromKind(webrtc.RTPCodecTypeAudio)
|
|
pc.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo)
|
|
|
|
p := &Peer{
|
|
ID: uuid.NewString(),
|
|
WS: ws,
|
|
PC: pc,
|
|
Room: room,
|
|
}
|
|
|
|
pc.OnICECandidate(func(c *webrtc.ICECandidate) {
|
|
if c != nil {
|
|
b, _ := json.Marshal(c.ToJSON())
|
|
p.Send("ice", b)
|
|
}
|
|
})
|
|
|
|
pc.OnTrack(func(remote *webrtc.TrackRemote, _ *webrtc.RTPReceiver) {
|
|
track := NewTrack(remote)
|
|
p.Room.AddTrack(p, track)
|
|
|
|
for {
|
|
pkt, _, err := remote.ReadRTP()
|
|
if err != nil {
|
|
return
|
|
}
|
|
track.Write(pkt)
|
|
}
|
|
})
|
|
|
|
return p
|
|
}
|
|
|
|
func (p *Peer) ReadLoop() {
|
|
defer func() {
|
|
p.Room.RemovePeer(p.ID)
|
|
p.PC.Close()
|
|
p.WS.Close()
|
|
}()
|
|
|
|
for {
|
|
var msg Signal
|
|
if err := p.WS.ReadJSON(&msg); err != nil {
|
|
return
|
|
}
|
|
|
|
switch msg.Type {
|
|
case "offer":
|
|
var offer webrtc.SessionDescription
|
|
json.Unmarshal(msg.Data, &offer)
|
|
p.PC.SetRemoteDescription(offer)
|
|
|
|
answer, _ := p.PC.CreateAnswer(nil)
|
|
p.PC.SetLocalDescription(answer)
|
|
|
|
b, _ := json.Marshal(answer)
|
|
p.Send("answer", b)
|
|
|
|
case "ice":
|
|
var c webrtc.ICECandidateInit
|
|
json.Unmarshal(msg.Data, &c)
|
|
p.PC.AddICECandidate(c)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (p *Peer) AddTrack(t *Track) {
|
|
p.PC.AddTrack(t.Local)
|
|
p.Send("renegotiate", nil)
|
|
}
|
|
|
|
func (p *Peer) Send(t string, data []byte) {
|
|
p.WS.WriteJSON(Signal{Type: t, Data: data})
|
|
}
|