protocol to synchronize published tracks

This commit is contained in:
David Zhao
2020-12-05 00:03:26 -08:00
parent 0dcfffb9d8
commit abd17af4d4
5 changed files with 81 additions and 40 deletions
+8 -4
View File
@@ -30,8 +30,8 @@ type Participant struct {
once sync.Once
// callbacks & handlers
// OnPeerTrack - remote peer added a mediaTrack
OnPeerTrack func(*Participant, *Track)
// OnParticipantTrack - remote peer added a mediaTrack
OnParticipantTrack func(*Participant, *Track)
// OnOffer - offer is ready for remote peer
OnOffer func(webrtc.SessionDescription)
// OnIceCandidate - ice candidate discovered for local peer
@@ -122,6 +122,10 @@ func (p *Participant) ToProto() *livekit.ParticipantInfo {
Name: p.name,
State: p.state,
}
for _, t := range p.tracks {
info.Tracks = append(info.Tracks, t.ToProto())
}
return info
}
@@ -302,9 +306,9 @@ func (p *Participant) onTrack(track *webrtc.Track, rtpReceiver *webrtc.RTPReceiv
p.lock.Unlock()
pt.Start()
if p.OnPeerTrack != nil {
if p.OnParticipantTrack != nil {
// caller should hook up what happens when the peer mediaTrack is available
go p.OnPeerTrack(p, pt)
go p.OnParticipantTrack(p, pt)
}
}
+8 -4
View File
@@ -63,7 +63,7 @@ func (r *Room) Join(participant *Participant) error {
log := logger.GetLogger()
// it's important to set this before connection, we don't want to miss out on any tracks
participant.OnPeerTrack = r.onTrackAdded
participant.OnParticipantTrack = r.onTrackAdded
participant.OnStateChange = func(p *Participant, oldState livekit.ParticipantInfo_State) {
log.Debugw("participant state changed", "state", p.state, "participant", p.id)
r.broadcastParticipantState(p)
@@ -114,19 +114,23 @@ func (r *Room) RemoveParticipant(id string) {
}
// a peer in the room added a new mediaTrack, subscribe other participants to it
func (r *Room) onTrackAdded(peer *Participant, track *Track) {
func (r *Room) onTrackAdded(participant *Participant, track *Track) {
// publish participant update, since track state is changed
r.broadcastParticipantState(participant)
r.lock.RLock()
defer r.lock.RUnlock()
// subscribe all existing participants to this mediaTrack
// this is the default behavior. in the future this could be more selective
for _, existingParticipant := range r.participants {
if existingParticipant == peer {
if existingParticipant == participant {
// skip publishing peer
continue
}
if err := track.AddSubscriber(existingParticipant); err != nil {
logger.GetLogger().Errorw("could not subscribe to mediaTrack",
"srcParticipant", peer.ID(),
"srcParticipant", participant.ID(),
"mediaTrack", track.id,
"dstParticipant", existingParticipant.ID())
}
+21
View File
@@ -9,6 +9,7 @@ import (
"github.com/livekit/livekit-server/pkg/logger"
"github.com/livekit/livekit-server/pkg/utils"
"github.com/livekit/livekit-server/proto/livekit"
)
var (
@@ -57,6 +58,10 @@ func (t *Track) Start() {
go t.forwardWorker()
}
func (t *Track) Kind() webrtc.RTPCodecType {
return t.mediaTrack.Kind()
}
// subscribes participant to current mediaTrack
// creates and add necessary forwarders and starts them
func (t *Track) AddSubscriber(participant *Participant) error {
@@ -125,6 +130,22 @@ func (t *Track) RemoveAllSubscribers() {
}
}
func (t *Track) ToProto() *livekit.TrackInfo {
var kind livekit.TrackInfo_Type
switch t.Kind() {
case webrtc.RTPCodecTypeAudio:
kind = livekit.TrackInfo_AUDIO
case webrtc.RTPCodecTypeVideo:
kind = livekit.TrackInfo_VIDEO
}
return &livekit.TrackInfo{
Sid: t.id,
Type: kind,
Name: t.mediaTrack.Label(),
}
}
// forwardWorker reads from the receiver and writes to each sender
func (t *Track) forwardWorker() {
for pkt := range t.receiver.RTPChan() {