how can I detect if the user stops screensharing a...
# ask-ai
c
how can I detect if the user stops screensharing and when they start again?
t
Thanks for reaching out, dexter. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to help you detect when a user starts or stops screen sharing in LiveKit! Here are some friendly methods you can use to keep track of screen sharing activity: • Using `screenShareObserver`: This handy utility from
@livekit/components-js
offers a reactive way to watch screen share tracks in a room. It listens for events like
TrackSubscribed
and
TrackUnsubscribed
to spot any changes. Here's a simple example:
Copy code
import { screenShareObserver } from '@livekit/components-core';

screenShareObserver(room).subscribe((screenShares) => {
  screenShares.forEach(({ participant, tracks }) => {
    console.log(`${participant.identity} is sharing screen with ${tracks.length} track(s)`);
  });
});
• Listening for Track Events: You can also listen directly for events like
LocalTrackPublished
and
LocalTrackUnpublished
for the local participant, or
TrackSubscribed
and
TrackUnsubscribed
for remote participants. Here’s how you might do it:
Copy code
room.localParticipant.on('trackPublished', (publication) => {
  if (publication.source === Track.Source.ScreenShare) {
    console.log('Local user started screen sharing');
  }
});

room.localParticipant.on('trackUnpublished', (publication) => {
  if (publication.source === Track.Source.ScreenShare) {
    console.log('Local user stopped screen sharing');
  }
});
• Checking Local Screen Share State: Another quick way is to check the
isScreenShareEnabled
property to see if the local participant is currently sharing their screen:
Copy code
const isEnabled = room.localParticipant.isScreenShareEnabled;
console.log(`Screen sharing is ${isEnabled ? 'on' : 'off'}`);
Using these methods, you'll be able to easily monitor and respond to screen sharing activity. If you have any questions or want to explore more, feel free to ask—I'm here to help! Sources: Sources/LiveKit/Participant/LocalParticipant.swift | packages/core/src/observables/room.ts | Web Applications & Integration Examples | livekit/livekit_composite | DeepW... | Screen sharing | LiveKit docs
c
in agents sdk python
t
I'm glad you're interested in detecting when a user starts or stops screen sharing using the LiveKit Python SDK! You can achieve this by listening to the
track_published
and
track_unpublished
events. These events are really helpful because they trigger whenever a participant publishes or unpublishes a track in the room. Here's a simple example to guide you:
Copy code
import asyncio
import logging
from livekit import rtc

async def main():
    room = rtc.Room()

    @room.on("track_published")
    def on_track_published(publication, participant):
        if publication.source == rtc.Track.Source.SCREEN_SHARE:
            logging.info(f"User {participant.identity} started screen sharing")
    
    @room.on("track_unpublished")
    def on_track_unpublished(publication, participant):
        if publication.source == rtc.Track.Source.SCREEN_SHARE:
            logging.info(f"User {participant.identity} stopped screen sharing")

    await room.connect("LIVEKIT_URL", "ACCESS_TOKEN")
    await asyncio.sleep(3600)  # Keep the app running

asyncio.run(main())
Let me break it down for you: • Event Handlers: The
track_published
event helps you catch when screen sharing starts, and
track_unpublished
lets you know when it stops. • Source Filtering: By checking
publication.source
, you can confirm the track is specifically for screen sharing (
SCREEN_SHARE
). • Connection: Just remember to replace
LIVEKIT_URL
and
ACCESS_TOKEN
with your actual server URL and access token. This approach will help you keep a real-time eye on screen sharing activity, making your application more responsive and user-friendly. If you have any questions or need further assistance, feel free to ask—I'm here to help! Sources: livekit-android-sdk/src/main/java/io/livekit/android/room/track/Track.kt | livekit-rtc/livekit/rtc/room.py
c
is there a hook to detect when they're screensharing and when they stopped
t
Absolutely! The LiveKit Python SDK is designed to help you easily detect when someone starts or stops screen sharing. You can listen for these helpful events: • `track_published`: This event fires when a remote participant begins screen sharing. • `track_unpublished`: This one triggers when a remote participant stops screen sharing. • `local_track_published`: This happens when you, the local participant, start screen sharing. • `local_track_unpublished`: And this triggers when you stop screen sharing. To make sure you’re catching screen sharing tracks specifically, just check the
source
property on the track publication. For screen sharing, it will be set to
rtc.TrackSource.SOURCE_SCREENSHARE
. Here’s a friendly example to guide you:
Copy code
import livekit.rtc as rtc

@room.on("track_published")
def on_track_published(publication, participant):
    if publication.source == rtc.TrackSource.SOURCE_SCREENSHARE:
        print(f"Screen sharing started by {participant.identity}")

@room.on("track_unpublished")
def on_track_unpublished(publication, participant):
    if publication.source == rtc.TrackSource.SOURCE_SCREENSHARE:
        print(f"Screen sharing stopped by {participant.identity}")

@room.on("local_track_published")
def on_local_track_published(publication, track):
    if publication.source == rtc.TrackSource.SOURCE_SCREENSHARE:
        print("Local screen sharing started")

@room.on("local_track_unpublished")
def on_local_track_unpublished(publication):
    if publication.source == rtc.TrackSource.SOURCE_SCREENSHARE:
        print("Local screen sharing stopped")
Using this method, you’ll be able to track screen sharing activity smoothly for both yourself and others in the room. If you have any more questions or need further help, I’m here for you! Sources: livekit-android-sdk/src/main/java/io/livekit/android/room/participant/Local... | lib/src/managers/broadcast_manager.dart | livekit-rtc/livekit/rtc/room.py | LiveKit Server Platform | livekit/livekit_composite | DeepWiki