AAAAAAAAAAAAAAAAAAA
All checks were successful
Create and publish a Docker image 🚀 / build-and-push-image (push) Successful in 2m25s
All checks were successful
Create and publish a Docker image 🚀 / build-and-push-image (push) Successful in 2m25s
This commit is contained in:
73
peer.go
73
peer.go
@@ -10,93 +10,67 @@ import (
|
||||
|
||||
type Signal struct {
|
||||
Type string `json:"type"`
|
||||
Data json.RawMessage `json:"data"`
|
||||
Data json.RawMessage `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
type Peer struct {
|
||||
ID string
|
||||
Conn *websocket.Conn
|
||||
WS *websocket.Conn
|
||||
PC *webrtc.PeerConnection
|
||||
Room *Room
|
||||
}
|
||||
|
||||
func NewPeer(conn *websocket.Conn, room *Room) *Peer {
|
||||
pc, err := webrtc.NewPeerConnection(webrtc.Configuration{
|
||||
func NewPeer(ws *websocket.Conn, room *Room) *Peer {
|
||||
pc, _ := webrtc.NewPeerConnection(webrtc.Configuration{
|
||||
ICEServers: []webrtc.ICEServer{
|
||||
{URLs: []string{"stun:stun.l.google.com:19302"}},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// 🔥 ОБЯЗАТЕЛЬНО для SFU
|
||||
_, _ = pc.AddTransceiverFromKind(
|
||||
webrtc.RTPCodecTypeAudio,
|
||||
webrtc.RTPTransceiverInit{
|
||||
Direction: webrtc.RTPTransceiverDirectionSendrecv,
|
||||
},
|
||||
)
|
||||
|
||||
_, _ = pc.AddTransceiverFromKind(
|
||||
webrtc.RTPCodecTypeVideo,
|
||||
webrtc.RTPTransceiverInit{
|
||||
Direction: webrtc.RTPTransceiverDirectionSendrecv,
|
||||
},
|
||||
)
|
||||
// ОБЯЗАТЕЛЬНО для SFU
|
||||
pc.AddTransceiverFromKind(webrtc.RTPCodecTypeAudio)
|
||||
pc.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo)
|
||||
|
||||
p := &Peer{
|
||||
ID: uuid.NewString(),
|
||||
Conn: conn,
|
||||
WS: ws,
|
||||
PC: pc,
|
||||
Room: room,
|
||||
}
|
||||
|
||||
pc.OnICECandidate(func(c *webrtc.ICECandidate) {
|
||||
if c == nil {
|
||||
return
|
||||
if c != nil {
|
||||
b, _ := json.Marshal(c.ToJSON())
|
||||
p.Send("ice", b)
|
||||
}
|
||||
data, _ := json.Marshal(c.ToJSON())
|
||||
p.Send("ice", data)
|
||||
})
|
||||
|
||||
pc.OnTrack(func(remote *webrtc.TrackRemote, _ *webrtc.RTPReceiver) {
|
||||
local := NewTrack(remote)
|
||||
|
||||
// 🔥 регистрируем трек в комнате
|
||||
p.Room.AddTrack(p, local)
|
||||
track := NewTrack(remote)
|
||||
p.Room.AddTrack(p, track)
|
||||
|
||||
for {
|
||||
pkt, _, err := remote.ReadRTP()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
local.Write(pkt)
|
||||
track.Write(pkt)
|
||||
}
|
||||
})
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *Peer) AddTrack(track *Track) {
|
||||
_, err := p.PC.AddTrack(track.Local)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
p.Send("renegotiate", nil)
|
||||
}
|
||||
|
||||
func (p *Peer) ReadLoop() {
|
||||
defer func() {
|
||||
p.Conn.Close()
|
||||
p.Room.RemovePeer(p.ID)
|
||||
p.PC.Close()
|
||||
p.WS.Close()
|
||||
}()
|
||||
|
||||
for {
|
||||
var msg Signal
|
||||
if err := p.Conn.ReadJSON(&msg); err != nil {
|
||||
if err := p.WS.ReadJSON(&msg); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -104,13 +78,13 @@ func (p *Peer) ReadLoop() {
|
||||
case "offer":
|
||||
var offer webrtc.SessionDescription
|
||||
json.Unmarshal(msg.Data, &offer)
|
||||
|
||||
p.PC.SetRemoteDescription(offer)
|
||||
|
||||
answer, _ := p.PC.CreateAnswer(nil)
|
||||
p.PC.SetLocalDescription(answer)
|
||||
|
||||
data, _ := json.Marshal(answer)
|
||||
p.Send("answer", data)
|
||||
b, _ := json.Marshal(answer)
|
||||
p.Send("answer", b)
|
||||
|
||||
case "ice":
|
||||
var c webrtc.ICECandidateInit
|
||||
@@ -120,6 +94,11 @@ func (p *Peer) ReadLoop() {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Peer) Send(t string, data []byte) {
|
||||
p.Conn.WriteJSON(Signal{Type: t, Data: data})
|
||||
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})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user