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
+14 -2
View File
@@ -41,6 +41,7 @@ type MediaTrack struct {
streamID string
kind livekit.TrackType
codec webrtc.RTPCodecParameters
onClose func()
// channel to send RTCP packets to the source
rtcpCh chan []rtcp.Packet
@@ -97,6 +98,10 @@ func (t *MediaTrack) IsMuted() bool {
return t.muted
}
func (t *MediaTrack) OnClose(f func()) {
t.onClose = f
}
// subscribes participant to current remoteTrack
// creates and add necessary forwarders and starts them
func (t *MediaTrack) AddSubscriber(participant types.Participant) error {
@@ -138,10 +143,13 @@ func (t *MediaTrack) AddSubscriber(participant types.Participant) error {
delete(t.downtracks, participant.ID())
t.lock.Unlock()
// ignore if the subscribing participant disconnected
if participant.PeerConnection().ConnectionState() == webrtc.PeerConnectionStateClosed {
// ignore if the subscribing participant is not connected
if participant.PeerConnection().ConnectionState() != webrtc.PeerConnectionStateConnected {
return
}
// if the source has been terminated, we'll need to terminate all of the downtracks
// however, if the dest participant has disconnected, then we can skip
sender := transceiver.Sender()
if sender != nil {
logger.GetLogger().Debugw("removing peerconnection track",
@@ -229,6 +237,10 @@ func (t *MediaTrack) forwardRTPWorker() {
t.RemoveAllSubscribers()
// TODO: send unpublished events?
t.nackWorker.Stop()
if t.onClose != nil {
t.onClose()
}
}()
for pkt := range t.receiver.RTPChan() {