updated flow, room & participant id created by server

This commit is contained in:
David Zhao
2020-11-28 00:18:34 -08:00
parent bd667cfe39
commit bea1a87cb8
24 changed files with 675 additions and 624 deletions
+42 -55
View File
@@ -5,10 +5,10 @@ import (
"time"
"github.com/google/uuid"
"github.com/pion/webrtc/v3"
"github.com/pkg/errors"
"github.com/livekit/livekit-server/pkg/logger"
"github.com/livekit/livekit-server/pkg/utils"
"github.com/livekit/livekit-server/proto/livekit"
)
@@ -16,10 +16,10 @@ type Room struct {
livekit.Room
config WebRTCConfig
lock sync.RWMutex
// map of peerId -> WebRTCPeer
peers map[string]*WebRTCPeer
// map of participantId -> Participant
participants map[string]*Participant
// Client ID => list of tracks they are publishing
//tracks map[string][]PeerTrack
//tracks map[string][]Track
}
func NewRoomForRequest(req *livekit.CreateRoomRequest, config *WebRTCConfig) (*Room, error) {
@@ -28,106 +28,93 @@ func NewRoomForRequest(req *livekit.CreateRoomRequest, config *WebRTCConfig) (*R
return nil, err
}
if req.RoomId == "" {
return nil, ErrRoomIdMissing
}
return &Room{
Room: livekit.Room{
RoomId: req.RoomId,
Id: utils.NewGuid(utils.RoomPrefix),
EmptyTimeout: req.EmptyTimeout,
MaxParticipants: req.MaxParticipants,
CreationTime: time.Now().Unix(),
Token: id.String(),
},
config: *config,
lock: sync.RWMutex{},
peers: make(map[string]*WebRTCPeer),
config: *config,
lock: sync.RWMutex{},
participants: make(map[string]*Participant),
}, nil
}
func (r *Room) GetParticipant(id string) *Participant {
r.lock.RLock()
defer r.lock.RUnlock()
return r.participants[id]
}
func (r *Room) ToRoomInfo(node *livekit.Node) *livekit.RoomInfo {
return &livekit.RoomInfo{
RoomId: r.RoomId,
Id: r.Id,
NodeIp: node.Ip,
NodeRtcPort: node.RtcPort,
CreationTime: r.CreationTime,
Token: r.Token,
}
}
func (r *Room) Join(peerId string, sdp string) (peer *WebRTCPeer, err error) {
func (r *Room) Join(participantName string) (participant *Participant, err error) {
r.lock.Lock()
defer r.lock.Unlock()
_, ok := r.peers[peerId]
if ok {
// already exists, return error
return nil, ErrPeerExists
}
logger.GetLogger().Infow("new peer joined", "peerId", peerId,
"roomId", r.RoomId)
offer := webrtc.SessionDescription{
Type: webrtc.SDPTypeOffer,
SDP: sdp,
}
me := MediaEngine{}
if err := me.PopulateFromSDP(offer); err != nil {
return nil, errors.Wrapf(err, "could not parse SDP")
}
peer, err = NewWebRTCPeer(peerId, &me, r.config)
participant, err = NewParticipant(r.config, participantName)
if err != nil {
return nil, errors.Wrap(err, "could not create peer")
return nil, errors.Wrap(err, "could not create participant")
}
peer.OnPeerTrack = r.onTrackAdded
participant.OnPeerTrack = r.onTrackAdded
// subscribe peer to existing tracks
for _, p := range r.peers {
if err := p.AddSubscriber(peer); err != nil {
logger.GetLogger().Infow("new participant joined",
"id", participant.ID(),
"name", participantName,
"roomId", r.Id)
// subscribe participant to existing tracks
for _, p := range r.participants {
if err := p.AddSubscriber(participant); err != nil {
// TODO: log error? or disconnect?
logger.GetLogger().Errorw("could not subscribe to peer",
"dstPeer", peer.ID(),
"srcPeer", p.ID())
logger.GetLogger().Errorw("could not subscribe to participant",
"dstParticipant", participant.ID(),
"srcParticipant", p.ID())
}
}
r.peers[peerId] = peer
r.participants[participant.ID()] = participant
return
}
func (r *Room) RemovePeer(peerId string) {
func (r *Room) RemoveParticipant(id string) {
r.lock.Lock()
defer r.lock.Unlock()
if p, ok := r.peers[peerId]; ok {
if p, ok := r.participants[id]; ok {
// also stop connection if needed
p.Close()
}
delete(r.peers, peerId)
delete(r.participants, id)
}
// a peer in the room added a new track, subscribe other peers to it
func (r *Room) onTrackAdded(peer *WebRTCPeer, track *PeerTrack) {
// a peer in the room added a new mediaTrack, subscribe other participants to it
func (r *Room) onTrackAdded(peer *Participant, track *Track) {
r.lock.RLock()
defer r.lock.RUnlock()
// subscribe all existing peers to this track
for _, p := range r.peers {
// subscribe all existing participants to this mediaTrack
for _, p := range r.participants {
if p == peer {
// skip publishing peer
continue
}
if err := track.AddSubscriber(peer); err != nil {
logger.GetLogger().Errorw("could not subscribe to track",
"srcPeer", peer.ID(),
"track", track.id,
"dstPeer", p.ID())
logger.GetLogger().Errorw("could not subscribe to mediaTrack",
"srcParticipant", peer.ID(),
"mediaTrack", track.id,
"dstParticipant", p.ID())
}
}
}