New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use AioRTC with just Async / Loop / Threading calls. #792
Comments
I've been looking everywhere as well for any code examples of people using AioRTC with Flask, haven't been able to. |
I was able to solve this by setting up threading as such: |
|
@R0NAM1 could you reopen this issue? In the Flask sync mode, manually create and manage an event loop works. Now Flask also supports async views but I still can't make it work with aiortc properly... Following is my
ICE connection state stuck at "WAITING":
|
Same quesion here, seams like flask thread stop when return response. "Thread(target=loop.run_forever).start()" solved my problem, but question is should I manually call loop.stop() to avoid keeping creat threads in backend? |
Turn's out I am also still having an issue, @namoshizun I don't know how to fix that off that bat, sorry. I might see if I can restructure my rtc code to be like it though, in case that works. @ZoroLH I've noticed the issue seems to but not with stopping loogs for threads, they usually close fine, I have AioRTC tasks being left behind for some reason, that's the real issue because that will take up memory and such. My current issue is that when I call rtcPeer.close() it just sits there and never does anything, and for some reason AioRTC objects never seem to stop or go out of memory into a garbage collector. I have a github discussion here about it: |
@namoshizun I don't know what I was talking about before with trying some code from yours, I think I misread how you do your asyncio stuff. My guess is either that your STUN server is unreachable / down or that something is just running forever, try putting print lines between everything that's important, and see if the SDP is ever returned to the client, if so then it just might need to run forever or something else is wrong. |
Hi @R0NAM1 , Have you experienced the same issue? |
I don't use Gunicorn, I use Waitress and I know the Webrtc Streams run fine, but my issue is with closing WebRTC objects and Threads, right now I've figured out how to close objects, but that only works if the client streams for a short time. Got a question, using threading.active_count() could you see if your version of the AioRTC code with Flask closes everything properly, even after, say a 24 hour stream? If you application permits this kind of testing, of course. If you think it does, could you share your source so I can learn how to do it properly? |
@R0NAM1 |
Thanks, it's entirely possible part of my issue is the server I'm using with flask, I'll move back to the inbuilt dev server just to test. |
pc is my RTCPeerConnection, when it closes on the frontend, the backend site will automatically stop
|
I mean more the context it is called in. For instance, (I haven't committed it yet) my code is called when a client is offering SDP to start a WebRTC session:
That's where flask comes in, below is singleWebRtcStart (Everything relevant)
(requestCameraPlayer is a modified AioRTC MediaPlayer that can support multiple WebRTC consumers, the buffer is a single object instead of a queue, so no retransmissions!) How the RTCPeerClient is CLOSED (The context I want you to provide the most) in my code right now:
The issue right now is that when a client is connected for a short time when RTC.close() runs two or three threads stay open for about 5 minutes then close, but when I run it for a long time (Above an hour) it gets stuck on RTC.close(), never progressing. It used to always do that, so I guess progress. Thanks! |
A good thing an LLM is used for is querying large datasets in a human way, basically talking with data. This is useful for coding especially because the LLM can reword things very well and provide very good examples. I bring this up because I just asked one 'Show me how to close an AioRTC RTCPeerConnection with a flask application' and it mentions that I need to close all the tracks it's connected to, and then close the RTCPeer! I'll try that, but that is interesting, isn't it? from phind.com:
In this example, when a POST request is made to Remember, the
|
Realization! |
Turns out that's what pc.close() does anyway, but I'm delving deeper, for some reason it's the RTPSender not closing, something in there isn't working correctly but I need to wait a few hours, maybe overnight to let the code run |
I think I found the issue, the code gets stuck because the RTCP loop never exits:
I'm very sure it gets stuck at await self._send_rtcp(packets) and does NOT continue because it will wait forever, if it continued it would exit. I'm testing this with
to see if I can fix this issue, if this does I might be able to open a pull request and contribute to open source, I guess that's naturally when bending how a library is suppost to work. |
(It could also be me not closing the datachannel, as I haven't tried that, I will later... I don't think RTCPeerConnection.close() does from what I looked at) |
Are you experiencing performance issues with Flask? I switched to aiohttp because, with Flask, the video stream FPS would drop significantly when multiple video clients were opened. I haven't encountered this issue with aiohttp. |
I don't believe so, just some normal performance issues when using a mid gaming PC and a bloated copy of Debian running 2 4k 24/7 cameras recordings and testing webrtc streaming all day. I also found out where my issues lie, RTCP never stops!
Even after everything closes 'Send RTCP' still prints, so something is keeping it alive. |
I may have just fixed it, it looks like asyncio cancelling isn't reliable in this context, causing RTCP to never exit. Switching over to an asyncio.Event() fixes this:
Resulting output:
I'll let this run until I go to bed and see what the behavior is, hope is this fixes it. |
I think it works, eventually the threads always stop and if they don't they seem to be reused by other clients (?). I'll make a pull request soon and see if it gets approved. |
Before filing an issue please verify the following:
(Not from what I could find)
aiortc
. The goalof the issue tracker is not to provide general guidance about WebRTC or free
debugging of your code.
(Yes, it's explicitly related on how to call AioRTC properly when not using AioHTTP,)
of the examples provided with
aiortc
without any changes.(I was able to take the SERVER example and convert it to Flask, the only difference seems to be how it's called.)
aiortc
is provided on a best-effort,there is no guarantee your issue will be addressed.
I've been attempting to use AioRTC with Flask to stream video to HTML5 Clients, so far everything works correctly EXCEPT the server refuses to accept the User Bind Request from the client (Verified via Wireshark). I believe the issue is with how I'm executing some of the async code, it seems examples using AioHTTP can do this no problem, it takes care of it for them, but for Flask (Which is what I'm using), you need to make the async calls manually.
In the logs, running the loop until completion the final debug messages for ICE are:
REQUEST
RESPONSE
But if after I put loop.run_forever, then I get the following:
REQUEST
RESPONSE
WAITING
IN_PROGRESS
SUCCEEDED
The issue with running it forever is that I don't get my SDP return for the client, so even though the server can now actually get ICE candidates, now the client can't.
How can this be addressed?
The text was updated successfully, but these errors were encountered: