whitphx / streamlit-webrtc

Real-time video and audio streams over the network, with Streamlit.
https://discuss.streamlit.io/t/new-component-streamlit-webrtc-a-new-way-to-deal-with-real-time-media-streams/8669
MIT License
1.36k stars 182 forks source link

Initialising audio recording using rtc configuration is very slow #717

Open corticalstack opened 2 years ago

corticalstack commented 2 years ago

Hi,

Using streamlit-webrtc to record audio-only, for speech-to-text use case. Note the code snippet below. Between the call to instantiate the webrtc_audio_recorder and setting of status indicaotr as "Recording") takes about 30 seconds.

I've seen another (closed) issue where the stun server was changed. I've tried, still slow. When running locally without the explicit rtc_configuration it is fast to initialise, however the use case demands cloud-hosted, soassume stun config is required, and this is where it is very slow.

Grateful for any recommendations, and thanks for such a great package.

import os
from pathlib import Path
from aiortc.contrib.media import MediaRecorder
from streamlit_webrtc import WebRtcMode, webrtc_streamer, WebRtcStreamerContext
import streamlit as st

MEDIA_STREAM_CONSTRAINTS = {
    "video": False,
    "audio": {
        "echoCancellation": False,
        "noiseSuppression": True,
        "autoGainControl": True,
    },
}
}

class App:
    def __init__(self):

        if "recording_status" not in st.session_state:
            st.session_state["recording_status"] = None

        self.webrtc_ctx = None

    def webrtc_audio_recorder(self, wavpath):
        def recorder_factory():
            return MediaRecorder(wavpath)

        self.webrtc_ctx: WebRtcStreamerContext = webrtc_streamer(
            key="sendonly-audio",
            mode=WebRtcMode.SENDONLY,
            rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]},
            in_recorder_factory=recorder_factory,
            media_stream_constraints=MEDIA_STREAM_CONSTRAINTS,
        )

    def main(self):
        wavpath = "temp/speech_to_text_audio.wav"
        self.webrtc_audio_recorder(wavpath)
        st.write(self.webrtc_ctx.state)
        status_indicator = st.empty()
        if self.webrtc_ctx.state.playing:
            status_indicator.info("Recording...")

if __name__ == "__main__":
    app = App()
    app.main()
whitphx commented 2 years ago

Hello. Thank you for the detailed report and trying a different STUN server too. However, I don't have any clues for now, sorry.

whitphx commented 2 years ago

FYI, I deployed the code snippet you posted above to Streamlit Sharing cloud and tested it, then the problem didn't occur. In my environment, the app shows the "Recording..." message 2 or 3 seconds after clicking the "START" button.

corticalstack commented 2 years ago

@whitphx thanks for testing end feeding back. Was the test done as localhost or deployed to cloud? Chrome browser? Windows/Linux? Tested both locally, and deployed as web app on Azure, and both deployment scenarios slow. Will test with other client devices and browsers.

whitphx commented 2 years ago

@corticalstack Sorry for being late to reply. It was both on local and Streamlit Cloud. Browser: Brave (same engine to Chrome). OS: macOS

It's a bit weird that locally hosted app has the same connectivity problem... but sorry, I don't have a direct answer to it now.