handle unpublish track

This commit is contained in:
David Zhao
2021-01-12 17:07:01 -08:00
parent 7c3b0c5202
commit bd885b1de9
10 changed files with 221 additions and 361 deletions
+12 -3
View File
@@ -24,6 +24,7 @@ type DataTrack struct {
lock sync.RWMutex
once sync.Once
msgChan chan livekit.DataMessage
onClose func()
// map of target participantId -> DownDataChannel
subscribers map[string]*DownDataChannel
@@ -46,6 +47,13 @@ func NewDataTrack(trackId, participantId string, dc *webrtc.DataChannel) *DataTr
t.msgChan <- dm
})
dc.OnClose(func() {
t.RemoveAllSubscribers()
if t.onClose != nil {
t.onClose()
}
})
return t
}
@@ -72,6 +80,10 @@ func (t *DataTrack) IsMuted() bool {
return false
}
func (t *DataTrack) OnClose(f func()) {
t.onClose = f
}
func (t *DataTrack) AddSubscriber(participant types.Participant) error {
label := PackDataTrackLabel(t.participantId, t.ID(), t.dataChannel.Label())
downChannel, err := participant.PeerConnection().CreateDataChannel(label, t.dataChannelOptions())
@@ -116,9 +128,6 @@ func (t *DataTrack) RemoveAllSubscribers() {
func (t *DataTrack) forwardWorker() {
defer Recover()
defer func() {
t.RemoveAllSubscribers()
}()
for {
msg := <-t.msgChan