I am trying to stop a stream but the source is und...
# ask-ai
g
I am trying to stop a stream but the source is undefined in the track json log: source: "unknown"
Copy code
async 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;
    }
}
g
It seems likely that the issue with the source being "unknown" in the track JSON log might be due to the track not being properly initialized with a source when it was created or published. According to the LiveKit documentation, when publishing a track, you can specify a source to identify it later. If the source is not set, it defaults to "unknown". To resolve this, ensure that when you publish the track, you specify the source explicitly. For example, when publishing a screen share track, you can set the source as
Track.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.
g
@millions-hair-24956 This is my publishStream function, include the source here then:
Copy code
async 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;
    }
}
g
It seems likely that you can specify the source when publishing the track by using the
publishTrack
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:
Copy code
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.
g
@millions-hair-24956
Copy code
let 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:
Copy code
Object.values(room.localParticipant.trackPublications)
it never enters the for loop even though
Copy code
room.localParticipant.trackPublications
is not empty
g
It seems likely that the issue might be related to how the
trackPublications
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:
Copy 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.