green-alligator-25439
03/02/2025, 1:49 AMasync function stopStream() {
if (!room) {
console.log("Room does not exist, cannot stop stream.");
return;
}
console.log(room.localParticipant.trackPublications)
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;
}
}
gray-monkey-83525
03/02/2025, 1:49 AMTrack.Source.ScreenShare
. This will help in identifying the track correctly when you attempt to stop or unpublish it.
Would you like more details on how to set the source when publishing a track?
Sources:
- LiveKit documentation 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.green-alligator-25439
03/02/2025, 1:51 AMasync function publishStream() {
// Check if room exists before proceeding
if (!room) {
console.log("Room does not exist, cannot publish stream.");
return;
}
try {
// Get screen-sharing stream instead of webcam video
const stream = await navigator.mediaDevices.getDisplayMedia({ video: true });
// Create a video track for LiveKit
const videoTrack = new LivekitClient.LocalVideoTrack(stream.getVideoTracks()[0]);
// Publish the video track to the LiveKit room
await room.localParticipant.publishTrack(videoTrack);
console.log("Screen sharing started successfully!");
// Set up video display for local user (optional)
appendStreamToContainer(videoTrack)
//appendStreamToContainer(videoTrack, document.querySelector(`.user-preview[data-username="${username}"]`));
// Stop sharing when user manually stops
stream.getVideoTracks()[0].addEventListener("ended", stopStream);
updateVoiceState("stream");
} catch (error) {
console.error("Error starting screen sharing:", error);
throw error;
}
}
gray-monkey-83525
03/02/2025, 1:51 AMpublishTrack
method with additional options. According to the LiveKit documentation, you can set the source of the track to Track.Source.ScreenShare
to ensure it is correctly identified as a screen share track.
Here's how you can modify your publishStream
function to include the source:
async function publishStream() {
if (!room) {
console.log("Room does not exist, cannot publish stream.");
return;
}
try {
const stream = await navigator.mediaDevices.getDisplayMedia({ video: true });
const videoTrack = new LivekitClient.LocalVideoTrack(stream.getVideoTracks()[0]);
// Publish the video track with the source specified
await room.localParticipant.publishTrack(videoTrack, {
source: LivekitClient.Track.Source.ScreenShare
});
console.log("Screen sharing started successfully!");
appendStreamToContainer(videoTrack);
stream.getVideoTracks()[0].addEventListener("ended", stopStream);
updateVoiceState("stream");
} catch (error) {
console.error("Error starting screen sharing:", error);
throw error;
}
}
Would you like more details on this implementation?
Sources:
- LiveKit documentation 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.green-alligator-25439
03/02/2025, 2:01 AMlet screenTrackPub;
for (const pub of Object.values(room.localParticipant.trackPublications)) {
console.warn(pub)
if (pub.track && pub.source === LivekitClient.Track.Source.ScreenShare) {
screenTrackPub = pub;
break;
}
}
This doesn't return anything:
Object.values(room.localParticipant.trackPublications)
it never enters the for loop even though
room.localParticipant.trackPublications
is not emptygray-monkey-83525
03/02/2025, 2:01 AMtrackPublications
are being accessed or filtered. According to the LiveKit documentation, trackPublications
is a map of track SIDs to their corresponding TrackPublication
objects. When iterating over trackPublications
, ensure that you are correctly accessing the properties of each TrackPublication
.
The condition pub.track && pub.source === LivekitClient.Track.Source.ScreenShare
might not be evaluating as expected. The source
property should be accessed from the track
object within the TrackPublication
, not directly from pub
. Here's how you can adjust your code:
let screenTrackPub;
for (const pub of Object.values(room.localParticipant.trackPublications)) {
console.warn(pub);
if (pub.track && pub.track.source === LivekitClient.Track.Source.ScreenShare) {
screenTrackPub = pub;
break;
}
}
This change ensures that you are checking the source
property on the track
object within each TrackPublication
. Would you like more details on this approach?
Sources:
- LiveKit documentation 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.