handle unpublish track
This commit is contained in:
+12
-3
@@ -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
|
||||
|
||||
+14
-2
@@ -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() {
|
||||
|
||||
@@ -262,15 +262,6 @@ func (p *ParticipantImpl) AddTrack(clientId, name string, trackType livekit.Trac
|
||||
})
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) RemoveTrack(sid string) error {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
|
||||
// TODO: handle removal properly
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) HandleAnswer(sdp webrtc.SessionDescription) error {
|
||||
if sdp.Type != webrtc.SDPTypeAnswer {
|
||||
return ErrUnexpectedOffer
|
||||
@@ -539,6 +530,14 @@ func (p *ParticipantImpl) handleTrackPublished(track types.PublishedTrack) {
|
||||
|
||||
track.Start()
|
||||
|
||||
track.OnClose(func() {
|
||||
// cleanup
|
||||
p.lock.Lock()
|
||||
delete(p.publishedTracks, track.ID())
|
||||
p.lock.Unlock()
|
||||
p.onTrackUpdated(p, track)
|
||||
})
|
||||
|
||||
if p.onTrackPublished != nil {
|
||||
p.onTrackPublished(p, track)
|
||||
}
|
||||
|
||||
@@ -80,12 +80,13 @@ func TestRoomJoin(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("participant removal is broadcasted to others", func(t *testing.T) {
|
||||
rm := newRoomWithParticipants(t, numParticipants)
|
||||
t.Run("participant state change is broadcasted to others", func(t *testing.T) {
|
||||
rm := newRoomWithParticipants(t, 1)
|
||||
participants := rm.GetParticipants()
|
||||
p := participants[0].(*typesfakes.FakeParticipant)
|
||||
|
||||
rm.RemoveParticipant(p.ID())
|
||||
p.OnStateChangeArgsForCall(0)(p, livekit.ParticipantInfo_ACTIVE)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
|
||||
for _, op := range participants {
|
||||
|
||||
@@ -59,7 +59,6 @@ type Participant interface {
|
||||
RTCPChan() chan<- []rtcp.Packet
|
||||
|
||||
AddTrack(clientId, name string, trackType livekit.TrackType)
|
||||
RemoveTrack(sid string) error
|
||||
Answer(sdp webrtc.SessionDescription) (answer webrtc.SessionDescription, err error)
|
||||
HandleAnswer(sdp webrtc.SessionDescription) error
|
||||
AddICECandidate(candidate webrtc.ICECandidateInit) error
|
||||
@@ -100,6 +99,9 @@ type PublishedTrack interface {
|
||||
AddSubscriber(participant Participant) error
|
||||
RemoveSubscriber(participantId string)
|
||||
RemoveAllSubscribers()
|
||||
|
||||
// callbacks
|
||||
OnClose(func())
|
||||
}
|
||||
|
||||
//counterfeiter:generate . Receiver
|
||||
|
||||
@@ -167,17 +167,6 @@ type FakeParticipant struct {
|
||||
removeSubscriberArgsForCall []struct {
|
||||
arg1 string
|
||||
}
|
||||
RemoveTrackStub func(string) error
|
||||
removeTrackMutex sync.RWMutex
|
||||
removeTrackArgsForCall []struct {
|
||||
arg1 string
|
||||
}
|
||||
removeTrackReturns struct {
|
||||
result1 error
|
||||
}
|
||||
removeTrackReturnsOnCall map[int]struct {
|
||||
result1 error
|
||||
}
|
||||
SendJoinResponseStub func(*livekit.RoomInfo, []types.Participant) error
|
||||
sendJoinResponseMutex sync.RWMutex
|
||||
sendJoinResponseArgsForCall []struct {
|
||||
@@ -1092,67 +1081,6 @@ func (fake *FakeParticipant) RemoveSubscriberArgsForCall(i int) string {
|
||||
return argsForCall.arg1
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) RemoveTrack(arg1 string) error {
|
||||
fake.removeTrackMutex.Lock()
|
||||
ret, specificReturn := fake.removeTrackReturnsOnCall[len(fake.removeTrackArgsForCall)]
|
||||
fake.removeTrackArgsForCall = append(fake.removeTrackArgsForCall, struct {
|
||||
arg1 string
|
||||
}{arg1})
|
||||
stub := fake.RemoveTrackStub
|
||||
fakeReturns := fake.removeTrackReturns
|
||||
fake.recordInvocation("RemoveTrack", []interface{}{arg1})
|
||||
fake.removeTrackMutex.Unlock()
|
||||
if stub != nil {
|
||||
return stub(arg1)
|
||||
}
|
||||
if specificReturn {
|
||||
return ret.result1
|
||||
}
|
||||
return fakeReturns.result1
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) RemoveTrackCallCount() int {
|
||||
fake.removeTrackMutex.RLock()
|
||||
defer fake.removeTrackMutex.RUnlock()
|
||||
return len(fake.removeTrackArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) RemoveTrackCalls(stub func(string) error) {
|
||||
fake.removeTrackMutex.Lock()
|
||||
defer fake.removeTrackMutex.Unlock()
|
||||
fake.RemoveTrackStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) RemoveTrackArgsForCall(i int) string {
|
||||
fake.removeTrackMutex.RLock()
|
||||
defer fake.removeTrackMutex.RUnlock()
|
||||
argsForCall := fake.removeTrackArgsForCall[i]
|
||||
return argsForCall.arg1
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) RemoveTrackReturns(result1 error) {
|
||||
fake.removeTrackMutex.Lock()
|
||||
defer fake.removeTrackMutex.Unlock()
|
||||
fake.RemoveTrackStub = nil
|
||||
fake.removeTrackReturns = struct {
|
||||
result1 error
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) RemoveTrackReturnsOnCall(i int, result1 error) {
|
||||
fake.removeTrackMutex.Lock()
|
||||
defer fake.removeTrackMutex.Unlock()
|
||||
fake.RemoveTrackStub = nil
|
||||
if fake.removeTrackReturnsOnCall == nil {
|
||||
fake.removeTrackReturnsOnCall = make(map[int]struct {
|
||||
result1 error
|
||||
})
|
||||
}
|
||||
fake.removeTrackReturnsOnCall[i] = struct {
|
||||
result1 error
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) SendJoinResponse(arg1 *livekit.RoomInfo, arg2 []types.Participant) error {
|
||||
var arg2Copy []types.Participant
|
||||
if arg2 != nil {
|
||||
@@ -1490,8 +1418,6 @@ func (fake *FakeParticipant) Invocations() map[string][][]interface{} {
|
||||
defer fake.removeDownTrackMutex.RUnlock()
|
||||
fake.removeSubscriberMutex.RLock()
|
||||
defer fake.removeSubscriberMutex.RUnlock()
|
||||
fake.removeTrackMutex.RLock()
|
||||
defer fake.removeTrackMutex.RUnlock()
|
||||
fake.sendJoinResponseMutex.RLock()
|
||||
defer fake.sendJoinResponseMutex.RUnlock()
|
||||
fake.sendParticipantUpdateMutex.RLock()
|
||||
|
||||
@@ -60,6 +60,11 @@ type FakePublishedTrack struct {
|
||||
nameReturnsOnCall map[int]struct {
|
||||
result1 string
|
||||
}
|
||||
OnCloseStub func(func())
|
||||
onCloseMutex sync.RWMutex
|
||||
onCloseArgsForCall []struct {
|
||||
arg1 func()
|
||||
}
|
||||
RemoveAllSubscribersStub func()
|
||||
removeAllSubscribersMutex sync.RWMutex
|
||||
removeAllSubscribersArgsForCall []struct {
|
||||
@@ -69,11 +74,6 @@ type FakePublishedTrack struct {
|
||||
removeSubscriberArgsForCall []struct {
|
||||
arg1 string
|
||||
}
|
||||
SetNameStub func(string)
|
||||
setNameMutex sync.RWMutex
|
||||
setNameArgsForCall []struct {
|
||||
arg1 string
|
||||
}
|
||||
StartStub func()
|
||||
startMutex sync.RWMutex
|
||||
startArgsForCall []struct {
|
||||
@@ -355,6 +355,38 @@ func (fake *FakePublishedTrack) NameReturnsOnCall(i int, result1 string) {
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakePublishedTrack) OnClose(arg1 func()) {
|
||||
fake.onCloseMutex.Lock()
|
||||
fake.onCloseArgsForCall = append(fake.onCloseArgsForCall, struct {
|
||||
arg1 func()
|
||||
}{arg1})
|
||||
stub := fake.OnCloseStub
|
||||
fake.recordInvocation("OnClose", []interface{}{arg1})
|
||||
fake.onCloseMutex.Unlock()
|
||||
if stub != nil {
|
||||
fake.OnCloseStub(arg1)
|
||||
}
|
||||
}
|
||||
|
||||
func (fake *FakePublishedTrack) OnCloseCallCount() int {
|
||||
fake.onCloseMutex.RLock()
|
||||
defer fake.onCloseMutex.RUnlock()
|
||||
return len(fake.onCloseArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakePublishedTrack) OnCloseCalls(stub func(func())) {
|
||||
fake.onCloseMutex.Lock()
|
||||
defer fake.onCloseMutex.Unlock()
|
||||
fake.OnCloseStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakePublishedTrack) OnCloseArgsForCall(i int) func() {
|
||||
fake.onCloseMutex.RLock()
|
||||
defer fake.onCloseMutex.RUnlock()
|
||||
argsForCall := fake.onCloseArgsForCall[i]
|
||||
return argsForCall.arg1
|
||||
}
|
||||
|
||||
func (fake *FakePublishedTrack) RemoveAllSubscribers() {
|
||||
fake.removeAllSubscribersMutex.Lock()
|
||||
fake.removeAllSubscribersArgsForCall = append(fake.removeAllSubscribersArgsForCall, struct {
|
||||
@@ -411,38 +443,6 @@ func (fake *FakePublishedTrack) RemoveSubscriberArgsForCall(i int) string {
|
||||
return argsForCall.arg1
|
||||
}
|
||||
|
||||
func (fake *FakePublishedTrack) SetName(arg1 string) {
|
||||
fake.setNameMutex.Lock()
|
||||
fake.setNameArgsForCall = append(fake.setNameArgsForCall, struct {
|
||||
arg1 string
|
||||
}{arg1})
|
||||
stub := fake.SetNameStub
|
||||
fake.recordInvocation("SetName", []interface{}{arg1})
|
||||
fake.setNameMutex.Unlock()
|
||||
if stub != nil {
|
||||
fake.SetNameStub(arg1)
|
||||
}
|
||||
}
|
||||
|
||||
func (fake *FakePublishedTrack) SetNameCallCount() int {
|
||||
fake.setNameMutex.RLock()
|
||||
defer fake.setNameMutex.RUnlock()
|
||||
return len(fake.setNameArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakePublishedTrack) SetNameCalls(stub func(string)) {
|
||||
fake.setNameMutex.Lock()
|
||||
defer fake.setNameMutex.Unlock()
|
||||
fake.SetNameStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakePublishedTrack) SetNameArgsForCall(i int) string {
|
||||
fake.setNameMutex.RLock()
|
||||
defer fake.setNameMutex.RUnlock()
|
||||
argsForCall := fake.setNameArgsForCall[i]
|
||||
return argsForCall.arg1
|
||||
}
|
||||
|
||||
func (fake *FakePublishedTrack) Start() {
|
||||
fake.startMutex.Lock()
|
||||
fake.startArgsForCall = append(fake.startArgsForCall, struct {
|
||||
@@ -480,12 +480,12 @@ func (fake *FakePublishedTrack) Invocations() map[string][][]interface{} {
|
||||
defer fake.kindMutex.RUnlock()
|
||||
fake.nameMutex.RLock()
|
||||
defer fake.nameMutex.RUnlock()
|
||||
fake.onCloseMutex.RLock()
|
||||
defer fake.onCloseMutex.RUnlock()
|
||||
fake.removeAllSubscribersMutex.RLock()
|
||||
defer fake.removeAllSubscribersMutex.RUnlock()
|
||||
fake.removeSubscriberMutex.RLock()
|
||||
defer fake.removeSubscriberMutex.RUnlock()
|
||||
fake.setNameMutex.RLock()
|
||||
defer fake.setNameMutex.RUnlock()
|
||||
fake.startMutex.RLock()
|
||||
defer fake.startMutex.RUnlock()
|
||||
copiedInvocations := map[string][][]interface{}{}
|
||||
|
||||
@@ -167,8 +167,6 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
case *livekit.SignalRequest_Mute:
|
||||
participant.SetTrackMuted(msg.Mute.Sid, msg.Mute.Muted)
|
||||
case *livekit.SignalRequest_RemoveTrack:
|
||||
participant.RemoveTrack(msg.RemoveTrack.Sid)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user