streamlit-webrtc icon indicating copy to clipboard operation
streamlit-webrtc copied to clipboard

Inconsistent issue with streamlit-webrtc in streamlit app

Open araii opened this issue 1 year ago • 40 comments

          A post in https://github.com/aiortc/aiortc/issues/85 says

Maybe the root cause is something weird happening in the python selector generated by others things that share the same event loop.


Hypothesis:

  • The WebRTC connection is properly closed as ICE failed under this network environment ("ICE connection state is closed")
  • So the event loop is closed or stopped in weird state.

Originally posted by @whitphx in https://github.com/whitphx/streamlit-webrtc/issues/552#issuecomment-987885401


A similar issue has occured again , it affects your component page in streamlit as well - https://webrtc.streamlit.app/

I checked if STUN server (stun.l.google.com:19302) is down on this site https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/

it seems that the stun server is working fine. Any idea what is causing the error? Please find attached logged screenshot of the error.

Do advise if this is something that can be fixed? maybe from asyncio lib? or what?

Screenshot 2023-03-08 at 12 40 42

araii avatar Mar 08 '23 07:03 araii

update -

tested server (stun.l.google.com:19302) on https://smarthome-webrtc-validator.withgoogle.com/ attached is the error i received from Devtools Screenshot 2023-03-08 at 15 41 32

stun server error: 701 - but I tried other free stun servers, all 5 random servers I chose did not worked...can you please help look into this?

araii avatar Mar 09 '23 04:03 araii

Update - tried running streamlit-webrtc component on my app page, it still fails, please find attached screenshot of the issue tracked on Chrome Dev tool - Screenshot 2023-03-13 at 12 02 25 hope it can help in figuring out what is going on - it seems that the 'index.ts' file...is getting connection closed after sending SDP offer.

I check STUN server on TrickleICE - its working fine. but i don't know why it is returning closeconnection - please see screenshot Screenshot 2023-03-13 at 21 36 41

I tested STUN server on webrtc-validator tool with google - it is also facing the same issue - connection state changed - disconnected. Screenshot 2023-03-13 at 12 23 12

by the way @whitphx - your page is showing the "error running app" page again.

araii avatar Mar 13 '23 04:03 araii

@araii Sorry for late reply. I confirmed it was not working, while it worked well weeks ago.

I am asking the Streamlit Cloud team member, and as the core team member mentioned in the issue you created (https://github.com/streamlit/streamlit/issues/6330), it looks like there has been some changes on the network of the community cloud.

First of all, the WebRTC connection is kind of "hack" that bypasses Streamlit's normal server-client connection, so we can't blame them for this issue. And I also think using a TURN server as written in the README should be the solution, in the scope of the WebRTC's standard.

Let me take time to consider the way to host a TURN server and use it from the community cloud in a security way.

whitphx avatar Mar 17 '23 16:03 whitphx

Hi @whitphx I was wondering if you had any updates on this? Any idea what is causing the issue?

Many thanks!

georgetanaselea avatar Mar 18 '23 16:03 georgetanaselea

Thank you @whitphx for responding.

Yes, was puzzled that STUN server stop working, so I did use a free TURN server from open relay. It worked but the latency was not good but at least it was working. But before I can demo, it fail again 2 days later, hence I open new ticket.

First of all, the WebRTC connection is kind of "hack" that bypasses Streamlit's normal server-client connection, so we can't blame them for this issue.

Yes but if webrtc component was working all this while, and given that all factors (network, device, urls, lib version) had remain the same - there is no reason for it to fail unless something changed in the cloud network settings.

Now page is stuck at "App is booting" - so can't test anything. I really hope this unstablity is due to the migration and can get resolved soon.

Let me take time to consider the way to host a TURN server and use it from the community cloud in a security way.

That would be great! Perhaps the latency would improve if that is the case. Thank you.

araii avatar Mar 20 '23 01:03 araii

Hi @araii could you please share more details on how you fixed the issue?

I have created a TURN server with metered.ca but I cannot seem to make it work as I get the same error as you.

Any help would be appreciated, thanks a lot!

georgetanaselea avatar Mar 20 '23 07:03 georgetanaselea

Hi @araii could you please share more details on how you fixed the issue?

I have created a TURN server with metered.ca but I cannot seem to make it work as I get the same error as you.

Any help would be appreciated, thanks a lot!

Hi @georgetanaselea , currently theres no fix for this. As stated here by @whitphx (https://github.com/whitphx/streamlit-webrtc/issues/1213#issuecomment-1474108148)

If i understand the situation correctly, do correct me if I am wrong:-

  1. there has been some changes in the network in community cloud causing the STUN server to stop working, so if https://webrtc.streamlit.app/ is not working in streamlit, most likely your app will also not work.
  2. @whitphx will think of a way to host a TURN server from the community cloud with security measures

Conclusion for now: STUN server use to work until streamlit changed some settings in the network in the community cloud and - that may have screw up the webrtc connection. Open ticket here https://github.com/streamlit/streamlit/issues/6330#issue-1628796485 Still waiting for it to be resolved..

I tried adding a TURN server, it worked for awhile, but then it stopped working 2 days later. Not sure if it will work for you but the code for adding TURN server can be found here. https://github.com/whitphx/streamlit-webrtc/issues/335#issuecomment-897326755

araii avatar Mar 22 '23 04:03 araii

Has anyone tested it on another cloud provider, like Heroku?

zivp100 avatar Mar 23 '23 18:03 zivp100

Hi @araii,

I tried adding a TURN server, it worked for awhile, but then it stopped working 2 days later. Not sure if it will work for you but the code for adding TURN server can be found here. #335 (comment)

I spent quite a lot of time on this issue and I have managed to make it work by using a turn server from these guys https://global.xirsys.net/. They have a 500mb limit for the free subscription but it should be enough for a few minutes demo.

@zivp100 I have tried Heroku too but didn't work because the problem is with the server that connects to the webcam in the streamlit app not with the app hosting itself.

I hope this helps.

georgetanaselea avatar Mar 23 '23 20:03 georgetanaselea

Facing the same issue, both heroku and streamlit cloud logs points to the one error, no matter do I use TURN or STUN server.

When the camera starts, the ICE connection is activated, but a bit later it ends up here INFO from streamlit_webrtc.webrtc:<MainThread> ICE connection state is closed

Which then leads to an error, because of socket object becomes none type: AttributeError: 'NoneType' object has no attribute 'sendto' Which later breaks the asyncio loop. Is there any ideas of how to fix all of that?

UPD: I have even tried to rollback to some streamlit-webrtc and streamlit versions back, did not help with versions:

streamlit==1.19.0
streamlit-webrtc==0.44.7

kirienkomaxym avatar Mar 25 '23 16:03 kirienkomaxym

Hi @araii,

I tried adding a TURN server, it worked for awhile, but then it stopped working 2 days later. Not sure if it will work for you but the code for adding TURN server can be found here. #335 (comment)

I spent quite a lot of time on this issue and I have managed to make it work by using a turn server from these guys https://global.xirsys.net/. They have a 500mb limit for the free subscription but it should be enough for a few minutes demo.

@zivp100 I have tried Heroku too but didn't work because the problem is with the server that connects to the webcam in the streamlit app not with the app hosting itself.

I hope this helps.

hi @georgetanaselea, thank you for sharing. I have tried out TURN server from https://global.xirxyx.com but it is still don't work when I upload it to the cloud. Is it still working for you?

Can everyone please also link your issue to this post here (https://github.com/streamlit/streamlit/issues/6330). I think this issue is with the network settings in streamlit cloud...

araii avatar Mar 27 '23 07:03 araii

hi @georgetanaselea, thank you for sharing. I have tried out TURN server from https://global.xirxyx.com but it is still don't work when I upload it to the cloud. Is it still working for you?

Yes, it still did on Saturday. Since then I have downgraded my subscription so I don't forget about it.

I use it in my python app, here is my config - hope it's going to help you. User and Cred are two variables that are added in my secrets.toml file.

RTC_CONFIGURATION = RTCConfiguration( {"iceServers":[{"urls": ["stun:eu-turn6.xirsys.com"]}, {"username": user, "credential": cred, "urls": ["turn:eu-turn6.xirsys.com:80?transport=udp", "turn:eu-turn6.xirsys.com:3478?transport=udp", "turn:eu-turn6.xirsys.com:80?transport=tcp", "turn:eu-turn6.xirsys.com:3478?transport=tcp", "turns:eu-turn6.xirsys.com:443?transport=tcp", "turns:eu-turn6.xirsys.com:5349?transport=tcp"]}]} )

georgetanaselea avatar Mar 27 '23 08:03 georgetanaselea

Is there any progress on this topic?

AhmetEnesYalcinkaya avatar Apr 01 '23 12:04 AhmetEnesYalcinkaya

Has anyone tested it on another cloud provider, like Heroku?

~~I switched the deployment method to Heroku and it works perfect.~~ Sorry, the audio/video capturing is not working in Safari, while it works fine with Chrome.

kishiyamat avatar Apr 06 '23 00:04 kishiyamat

Has anyone tested it on another cloud provider, like Heroku?

I switched the deployment method to Heroku and it works perfect.

How did you do it?

olabodeoa avatar Apr 13 '23 12:04 olabodeoa

How did you do it?

Updated: sorry, the following may not be not a valid solution since the video capturing does not work in Safari (it seems fine with Chrome).

Hi! Let me explain the steps. But I think Heroku no longer gives us free tiers. Here are the steps:

  1. Create a Procfile with the following content: web: sh setup.sh && streamlit run app.py. You need to change app.py based on the files you want to run.
  2. Create a file named setup.sh.
  3. Create a file named runtime.txt with the content python-3.9.16. The version should be one from the list at https://devcenter.heroku.com/articles/python-support.

The setup.sh file should contain the following code:

mkdir -p ~/.streamlit/

echo "\
[server]\n\
port = $PORT\n\
enableCORS = false\n\
headless = true\n\
\n\
" > ~/.streamlit/config.toml

A couple of years ago, I watched a video (https://www.youtube.com/watch?v=zK4Ch6e1zq8) and the method seems to still be working. After creating the files, I connected my repository to Heroku; the following steps are explained in the video.

kishiyamat avatar Apr 13 '23 14:04 kishiyamat

@kishiyamat Are you sure that your application is working from a remote host, e.g. camera starts, the video being send and the RTC connection does not drop all the way, also, can you please share your requirements.txt file, maybe you got some kind of special versions of aiortc or streamlit-webrtc that still works, many thanks!

kirienkomaxym avatar Apr 13 '23 15:04 kirienkomaxym

@kirienkomaxym I'm using audio capturing, so I'm not sure about the camera. But it works fine with the audio capturing, which doesn't work on Streamlit Cloud now. Here is my requirements.txt. (Sorry, this is for my lecture, and the versions are not well specified.)

audio-recorder-streamlit
librosa
plotly
streamlit
pydub
streamlit-webrtc==0.45.0

kishiyamat avatar Apr 13 '23 15:04 kishiyamat

I cloned the s2t app from the example (https://github.com/kishiyamat/streamlit-stt-app/tree/heroku), making the file changes above. Then, I moved to Heroku and followed the steps below.

  1. Create New App
  2. Choose the repository to connect
  3. Choose the deployment method.

The deployed app: https://streamlit-stt-app-clone.herokuapp.com/ The commit: https://github.com/whitphx/streamlit-stt-app/pull/131/commits/1d3c30d1b8e515bb735d06d997fc707edae14b38

The cloned app seems to be working fine. (I don't know how much it costs. I'll disable it in a few days maybe.) But the reason it works might be because this is about audio capturing. When it comes to cameras, it might not be the case as some have tried Heroku and failed.

kishiyamat avatar Apr 13 '23 16:04 kishiyamat

@kishiyamat your app seem to struggle the same issue, if the video mode is on, the video does not actually starts, you can check the heroku log for the same error, we all facing here. Thanks for the help! image

kirienkomaxym avatar Apr 13 '23 16:04 kirienkomaxym

Oh I see. Thank you for sharing the issue.

kishiyamat avatar Apr 13 '23 22:04 kishiyamat

@kirienkomaxym Hi, I checked the deployed app, and it's working. But once I switch the browser from Chrome to Safari, it doesn't work. What browser do you use?

kishiyamat avatar Apr 16 '23 04:04 kishiyamat

it worked on Heroku :)

AhmetEnesYalcinkaya avatar Apr 16 '23 14:04 AhmetEnesYalcinkaya

@AhmetEnesYalcinkaya Hi, What the costs to run your app on Heroku Eco and Basic is enough?

PeerapolHan avatar Apr 24 '23 10:04 PeerapolHan

Hi, I updated the WebRTC sample app on Community Cloud to use STUN/TURN servers provided by Twilio Network Traversal Service. I think the demo is working now. The source code looks like these: https://github.com/whitphx/streamlit-webrtc-example/blob/79ac65994a8c7f91475647d65e63b5040ea35863/pages/1_object_detection.py#L141 https://github.com/whitphx/streamlit-webrtc-example/blob/79ac65994a8c7f91475647d65e63b5040ea35863/sample_utils/turn.py#L10-L33

At this point, I'm using the service with a free trial account which has limitations on its balance, so the app can sometimes be down.

I also tried Open Relay Project as a free TURN server provider, but it was down when I gave it a try and actually some developer reported it is not stable such as https://github.com/aiortc/aiortc/issues/832#issuecomment-1482420656, so I gave up using it.

whitphx avatar Apr 25 '23 14:04 whitphx

@whitphx thank you for updated 🙏

PeerapolHan avatar Apr 25 '23 14:04 PeerapolHan

I updated the section in README.md about TURN servers: https://github.com/whitphx/streamlit-webrtc#configure-the-turn-server-if-necessary

whitphx avatar Apr 25 '23 17:04 whitphx

Hi @whitphx, thanks for your great work! Doing the same as you mentioned in using TURN server, still facing the same error using cloud, (both heroku and streamlit-cloud). Dont you think the issue may be with communication between streamlit-webrtc and aiortc, e.g. some conflicting libraries versions or unexpected behavior. Thanks for your reply!

image image

upd: Also checked the demo page you mentioned, its down :(

kirienkomaxym avatar May 04 '23 17:05 kirienkomaxym

Hey ,any updates on this issue as I am also facing the same issue while deploying on Streamlit Cloud I am using Chrome Browser Screenshot 2023-05-11 171250

imprasukjain avatar May 11 '23 11:05 imprasukjain

Hello, @whitphx despite the TURN server we still experiencing the same problem. you take too long to answer. how can we resolve this if you are not responding very well?

I am so frustrated, with how this problem is still unresolved. months passed but we still had this problem. come on!

FourthSyte avatar May 26 '23 17:05 FourthSyte