green-alligator-25439
03/01/2025, 11:40 PMroom = new LivekitClient.Room(); // Create a new LiveKit Room instance
gray-monkey-83525
03/01/2025, 11:40 PMautoSubscribe
option when connecting to the room. This allows you to manually control which tracks to subscribe to. Here's how you can do it:
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.
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.green-alligator-25439
03/02/2025, 1:38 AMasync 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 heregray-monkey-83525
03/02/2025, 1:38 AMgetTrackPublication
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:
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.green-alligator-25439
03/02/2025, 1:41 AMgray-monkey-83525
03/02/2025, 1:41 AMtrackPublications
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:
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.green-alligator-25439
03/02/2025, 1:48 AMgray-monkey-83525
03/02/2025, 1:48 AM