livekit / client-sdk-unity-web

Client SDK for Unity WebGL
Apache License 2.0
47 stars 15 forks source link

After an user is diconnected, the unpublish track has no information about the participant or the track #39

Closed D4rWiNSS closed 11 months ago

D4rWiNSS commented 11 months ago

Hi there!

Imagine that there is 2 users in the same room. They are user A and user B

If the user B quit the platform, the user A received that the tracks form user B has been unpublished, however those tracks comes with the Track.Sid as null or the parcicipant.Identity as null as well.

So I can't clean all the things related to the tracks cince I don't know which track to dispose

The log No track id will be shown in the following sniped

  private void HandleUnpub(RemoteTrackPublication publication, RemoteParticipant participant)
        {
            // Note: there is a bug in livekit where there the tracks doens't come with the ID, so we just try it and if not, just let it go
            try
            {
                if (publication.Kind == TrackKind.Video)
                    VideoTrackUnpublished(publication.Track.Sid);
                else if (publication.Kind == TrackKind.Audio)
                {
                    Debug.Log("Audio unpublished");
                    Debug.Log("Audio unpublished track " + publication.Track);
                    try
                    {
                        Debug.Log("Audio unpublished track " + publication.Track.Sid));
                    }
                    catch (Exception e)
                    {
                        Debug.Log("No track id");
                    }
                    Debug.Log("Audio unpublished owner id " + Convert.ToInt32(participant.Identity));

                    // Note: There is a bug that the track Sid comes empty, so we use the participant id to solve the issue
                    VoiceChatTrackUnpublished(Convert.ToInt32(participant.Identity));
                }
            }
            catch { }
        }

Code to create the room and connect


    m_Room = new LiveKit.Room();
            m_Room.ParticipantConnected += (p) =>
            {
                Debug.Log($"Participant connected: {p.Sid}");
            };

            m_Room.TrackMuted += VideoTrackMuteChanged;
            m_Room.TrackUnmuted += VideoTrackMuteChanged;
            m_Room.LocalTrackPublished += (publication, participant) => HandleRawTrack(publication.Track, publication);
            m_Room.TrackSubscribed += (track, publication, participant) => HandleOthersRawTrack(track, publication, participant); 
            m_Room.TrackUnsubscribed += (track, publication, participant) => HandleUnsubTrack(track, publication, participant);
            m_Room.TrackUnpublished += (track, publication) => HandleUnpub(track, publication);
            m_Room.LocalTrackUnpublished += (track, publication) => LocalTrackUnpublished(track, publication);

             var c = m_Room.Connect(_serverUrl, _roomToken, new RoomConnectOptions()
            {
                AutoSubscribe = true
            });
            yield return c;

            if (c.IsError)
            {
                Debug.Log("Failed to connect to the room !");
                yield break;
            }

            yield return m_Room.LocalParticipant.EnableCameraAndMicrophone();
theomonnom commented 11 months ago

Hey, it seems like your trying to access the Track when it is not subscribed/published, so the track should be null in that case. (The track member is only valid if you're subscribed to the publication) Can you try to directly access the sid from the publication: publication.sid

D4rWiNSS commented 11 months ago

Yes that solved the issue. Thanks! I was subscribed to the publication but seems like when the track stops it is null