Manage the lifecycle of source and sink tracks
mainFactory helpers like create_video_source_track(), create_audio_source_track(), create_video_sink_track(), create_audio_sink_track(), and create_pcm_audio_source_track() cache their objects in st.session_state using the provided key. This ensures tracks survive Streamlit reruns.
By default, these tracks are scoped to the active WebRTC session. When the session ends (e.g., user clicks STOP or closes the page), the object is stopped and removed from st.session_state.
To keep a track alive across multiple WebRTC sessions within the same Streamlit session, set lifecycle_scope="streamlit-session".
from streamlit_webrtc import create_video_source_track
# Default: scoped to the WebRTC session
video_track = create_video_source_track(
callback=video_source_callback,
key="video-source",
)
# Opt-out: survives multiple WebRTC sessions in the same Streamlit session
video_track = create_video_source_track(
callback=video_source_callback,
key="video-source",
lifecycle_scope="streamlit-session",
)