fix: keep the direction of call for webrtc handshake

pull/1/head
Benyamin Azarkhazin 2023-08-04 18:13:48 +03:30
parent 53d51d446a
commit eda46cb7ef
Signed by: benyamin
GPG Key ID: 3AE44F5623C70269
1 changed files with 23 additions and 10 deletions

View File

@ -24,8 +24,8 @@ type Peer struct {
ID uint64 ID uint64
Conn *webrtc.PeerConnection Conn *webrtc.PeerConnection
CanPublish bool CanPublish bool
IsCaller bool
HandshakeLock *sync.Mutex HandshakeLock *sync.Mutex
didOnce bool
} }
type Room struct { type Room struct {
@ -68,7 +68,7 @@ func (r *RoomRepository) doesPeerExists(roomId string, id uint64) bool {
return false return false
} }
func (r *RoomRepository) CreatePeer(roomId string, id uint64, canPublish, isCaller bool) error { func (r *RoomRepository) CreatePeer(roomId string, id uint64, canPublish bool, isCaller bool) error {
r.Lock() r.Lock()
if !r.doesRoomExists(roomId) { if !r.doesRoomExists(roomId) {
@ -89,16 +89,16 @@ func (r *RoomRepository) CreatePeer(roomId string, id uint64, canPublish, isCall
continue continue
} }
go func(recv *webrtc.RTPReceiver) { go func(peerConn *webrtc.PeerConnection, recv *webrtc.RTPReceiver) {
err := peer.Conn.WriteRTCP([]rtcp.Packet{ err := peerConn.WriteRTCP([]rtcp.Packet{
&rtcp.PictureLossIndication{ &rtcp.PictureLossIndication{
MediaSSRC: uint32(recv.Track().SSRC()), MediaSSRC: uint32(recv.Track().SSRC()),
}, },
}) })
if err != nil { if err != nil {
println(`[E] [rtcp] `, err.Error()) println(`[E] [rtcp][PLI] `, err.Error())
} }
}(receiver) }(peer.Conn, receiver)
} }
} }
@ -135,6 +135,7 @@ func (r *RoomRepository) CreatePeer(roomId string, id uint64, canPublish, isCall
Conn: peerConn, Conn: peerConn,
HandshakeLock: &sync.Mutex{}, HandshakeLock: &sync.Mutex{},
CanPublish: canPublish, CanPublish: canPublish,
IsCaller: isCaller,
} }
go r.updatePCTracks(roomId) go r.updatePCTracks(roomId)
return nil return nil
@ -274,19 +275,28 @@ func (r *RoomRepository) updatePCTracks(roomId string) {
println(err.Error()) println(err.Error())
return return
} }
_, err = peer.Conn.AddTransceiverFromTrack(track.TrackLocal)
if err != nil {
println("t", err.Error())
return
}
} }
} }
for trackId, rtpSender := range alreadySentTracks { for trackId, rtpSender := range alreadySentTracks {
if _, exists := room.Tracks[trackId]; !exists { if _, exists := room.Tracks[trackId]; !exists {
renegotiate = true
println("[PC] remove track", trackId, "from", peer.ID) println("[PC] remove track", trackId, "from", peer.ID)
//_ = rtpSender.Stop() err := peer.Conn.RemoveTrack(rtpSender)
_ = peer.Conn.RemoveTrack(rtpSender) if err != nil {
println(err.Error())
return
}
} }
} }
room.trackLock.Unlock() room.trackLock.Unlock()
if renegotiate { if renegotiate {
go func(peer *Peer, roomId string) { go func(p *Peer, rid string) {
err := r.offerPeer(peer, roomId) err := r.offerPeer(p, rid)
if err != nil { if err != nil {
println(`[E]`, err.Error()) println(`[E]`, err.Error())
return return
@ -357,6 +367,9 @@ func (r *RoomRepository) SetPeerOffer(roomId string, id uint64, offer webrtc.Ses
peer := room.Peers[id] peer := room.Peers[id]
room.Unlock() room.Unlock()
if !peer.IsCaller {
return nil, models.NewError("only caller can offer", 403, nil)
}
peer.HandshakeLock.Lock() peer.HandshakeLock.Lock()
defer peer.HandshakeLock.Unlock() defer peer.HandshakeLock.Unlock()
err = peer.Conn.SetRemoteDescription(offer) err = peer.Conn.SetRemoteDescription(offer)