Does livekit automatically subscribe to all audio ...
# ask-ai
g
Does livekit automatically subscribe to all audio and video tracks when a user joins a room? How can I prevent automatically subscribing to a stream? I would also like to add adaptive stream if possible
Copy code
room = new LivekitClient.Room(); // Create a new LiveKit Room instance
g
Yes, by default, LiveKit automatically subscribes new participants to all available tracks when they join a room. To prevent this automatic subscription, you can disable the
autoSubscribe
option when connecting to the room. This allows you to manually control which tracks to subscribe to. Here's how you can do it:
Copy code
let room = await room.connect(url, token, { autoSubscribe: false });
room.on(RoomEvent.TrackPublished, (publication, participant) => {
  publication.setSubscribed(true); // Manually subscribe to specific tracks
});
For adaptive streaming, you can enable it by setting the
adaptiveStream
option to
true
when creating the room instance. This optimizes video quality based on the size and visibility of the video elements.
Copy code
const room = new LivekitClient.Room({
  adaptiveStream: true,
});
Would you like more details on implementing these features? Sources: - LiveKit Docs - Subscribing to Tracks - LiveKit Docs - Adaptive Stream 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
g
@millions-hair-24956
Copy code
async function stopStream() {
    if (!room) {
        console.log("Room does not exist, cannot stop stream.");
        return;
    }

    try {
        // Find the screen-sharing track publication
        const screenTrackPub = room.localParticipant.getTrackPublication(LivekitClient.Track.Source.ScreenShare);
        console.error(screenTrackPub)
        if (screenTrackPub && screenTrackPub.track) {
            console.log("Stopping and unpublishing screen share track...");

            // 🔥 Stop the track first (triggers TrackUnsubscribed)
            screenTrackPub.track.stop();

            // Unpublish the track
            await room.localParticipant.unpublishTrack(screenTrackPub.track);

            console.log("Screen sharing stopped successfully.");
        }

         updateVoiceState("stream");

    } catch (error) {
        console.error("Error stopping screen sharing:", error);
        throw error;
    }
}
It seems that my syntax is wrong here. I am trying to stop a stream screen sharing track. screenTrackPub is returning undefined here
g
It seems likely that the issue is with how you're trying to access the screen-sharing track publication. The
getTrackPublication
method requires the track's SID (string identifier) rather than the track source. To find the screen-sharing track, you should iterate over the track publications and check the source. Here's how you can modify your code:
Copy code
async function stopStream() {
    if (!room) {
        console.log("Room does not exist, cannot stop stream.");
        return;
    }

    try {
        // Iterate over track publications to find the screen-sharing track
        const screenTrackPub = room.localParticipant.trackPublications.find(
            (pub) => pub.track && pub.track.source === LivekitClient.Track.Source.ScreenShare
        );

        if (screenTrackPub && screenTrackPub.track) {
            console.log("Stopping and unpublishing screen share track...");

            // Stop the track first (triggers TrackUnsubscribed)
            screenTrackPub.track.stop();

            // Unpublish the track
            await room.localParticipant.unpublishTrack(screenTrackPub.track);

            console.log("Screen sharing stopped successfully.");
        }

        updateVoiceState("stream");

    } catch (error) {
        console.error("Error stopping screen sharing:", error);
        throw error;
    }
}
This approach iterates over the track publications to find the one with the
ScreenShare
source. Would you like more details on this solution? Sources: - https://livekit-users.slack.com/archives/C07FY8WHGPM/p1730233417.503479 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
g
@millions-hair-24956 I am getting this error now: Error stopping screen sharing: TypeError: room.localParticipant.trackPublications.find is not a function
g
The error indicates that
trackPublications
is not an array, which means you cannot use the
find
method directly. Instead,
trackPublications
is likely a map or an object. You should iterate over its values to find the screen-sharing track. Here's how you can adjust your code:
Copy code
async function stopStream() {
    if (!room) {
        console.log("Room does not exist, cannot stop stream.");
        return;
    }

    try {
        // Iterate over track publications to find the screen-sharing track
        let screenTrackPub;
        for (const pub of Object.values(room.localParticipant.trackPublications)) {
            if (pub.track && pub.track.source === LivekitClient.Track.Source.ScreenShare) {
                screenTrackPub = pub;
                break;
            }
        }

        if (screenTrackPub && screenTrackPub.track) {
            console.log("Stopping and unpublishing screen share track...");

            // Stop the track first (triggers TrackUnsubscribed)
            screenTrackPub.track.stop();

            // Unpublish the track
            await room.localParticipant.unpublishTrack(screenTrackPub.track);

            console.log("Screen sharing stopped successfully.");
        }

        updateVoiceState("stream");

    } catch (error) {
        console.error("Error stopping screen sharing:", error);
        throw error;
    }
}
This code iterates over the values of
trackPublications
to find the screen-sharing track. Would you like more details on this solution? Sources: - https://docs.livekit.io/client-sdk-flutter/livekit_client/Participant/trackPublications.html - https://docs.livekit.io/client-sdk-swift/documentation/livekit/participant/trackpublications#1813652986 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
g
@millions-hair-24956 Problem is that source is unknown in the track json: source: "unknown". Why?
g
If you're looking for further help, please follow-up on one of the other channels available for community support!