Building a gstreamer app with streamlit

Gstreamer support in Streamlit

As the topic suggests I am looking for some solution if gstreamer framework support is in Streamlit or not.

I am able to make some sample gstreamer apps on Streamlit but all of them runs on the same thread

Steps to reproduce

Code snippet:

class GstreamerPlayer:
    def __init__(self):
        # Initialize GStreamer
        Gst.init(None)

        # Define the GStreamer pipeline string
        if "pipeline_str" not in st.session_state:
            st.session_state.pipeline_str = """videotestsrc pattern="ball" ! autovideosink"""

        # Define the GStreamer pipeline status
        if "status" not in st.session_state:
            st.session_state.status = "πŸ”΄"

        # Define GStreamer pipeline need to update or not
        # Also create/(update with defualt) the pipeline for the 1st time
        if "update_pipeline_enable" not in st.session_state:
            st.session_state.update_pipeline_enable = True
            self.update_pipeline()

    def consol(self):
        # Define the Streamlit app
        st.title(st.session_state.status+" Gst-Launch App "+st.session_state.status)

        # Input the gst-pipeline string 
        st.text_input(' ',value=st.session_state.pipeline_str,key="input_pipeline",on_change=self.update_pipeline_str,label_visibility="collapsed")

        # Define the start and stop buttons
        col1,col2,col3,col4 = st.columns(4)
        col1.button("Start",on_click=self.start)
        col2.button("Pause",on_click=self.pause)
        col3.button("Stop",on_click=self.stop)
        col4.button("Restart",on_click=self.restart)

    def update_pipeline(self):
        if st.session_state.update_pipeline_enable:
            try:
                # Define the new GStreamer pipeline
                st.session_state.pipeline = Gst.parse_launch(st.session_state.pipeline_str)
            except Exception as e:
                st.error(str(e))

            # Disable the pipeline updation until next update call
            st.session_state.update_pipeline_enable = False

    def start(self):
        # Start the pipeline
        st.session_state.pipeline.set_state(Gst.State.PLAYING)
        st.session_state.status = "🟒"

    def pause(self):
        # Pause the pipeline
        st.session_state.pipeline.set_state(Gst.State.PAUSED)
        st.session_state.status = "🟑"

    def stop(self):
        # Stop the pipeline when the stop button is clicked
        st.session_state.pipeline.set_state(Gst.State.NULL)
        st.session_state.status = "πŸ”΄"

    def restart(self):
        # Stop the current pipeline
        self.stop()
        # Start the current pipeline
        self.start()

    def update_pipeline_str(self):
        # Stop the current pipeline
        self.stop()
        
        # Update the pipeline string
        st.session_state.pipeline_str = st.session_state.input_pipeline.strip().replace("gst-launch-1.0 ", "")

        # Updating the pipeline
        st.session_state.update_pipeline_enable = True
        self.update_pipeline()

        # Start the pipeline with new pipeline string
        self.start()

## I thought the bellow method is enough to run the gstreamer pipeline in  a separate thread in the background
def gstreamer_player_thread():
    # Initialize threads and GStreamer
    Gst.init(None)
    
    # Create an instance of the GTK_Main class
    player = GstreamerPlayer()
    
    # Start the GTK_Main instance in a new thread
    thread = threading.Thread(target=player.consol)
    add_script_run_ctx(thread)
    thread.start()
    thread.join()

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

The app should start/stop/restart the test-stream on click.

Actual behavior:

The app is working fine when there is no work is being in the main thread.
if i add a sample code like below in main thread … then the behaviour of the app is random

while True:
    print("wait.....")
    time.sleep(2)

which means the app is not truly running in multithreading mode safely

Debug info

My doubt is that since gstreamer is also a multithreaded framework… so due to this some inconsistency is occurring.

So is it possible to run the gstreamer pipeline in the background thread with Streamlit app??

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.