Sending silence on RTC causes noticable per-client background CPU usage
I've noticed that when the AI isn't speaking, speech-to-speech has a noticable CPU usage. I've found two causes for this, the major one being the fact that, when on a WebRTC connection, s2s constantly sends 50 packets/s of silent audio (I think this is standard in WebRTC).
The network usage seems to be negligeble (~150 bytes/s).
This is quite annoying in setups where s2s is running 24/7, only speaking every now and then.
I have managed to mitigate this locally by disabling the sender when it runs out of data to send: https://github.com/huggingface/speech-to-speech/blob/16d7f98ff712fb082d53497937f5456667c84680/src/speech_to_speech/api/openai_realtime/webrtc_session.py#L146-L147
and enabling it when there's more data to send: https://github.com/huggingface/speech-to-speech/blob/16d7f98ff712fb082d53497937f5456667c84680/src/speech_to_speech/api/openai_realtime/webrtc_session.py#L120-L121
What I'm not sure about is the best way to disable it.
The way to completely eliminate the CPU usage would be to call stop on the RTCRtpSender, then recreate it when it's needed. I'm not sure if this requires renegotiation, or if there's too much of an overhead to starting/stopping a sender too often (once every few seconds in a normal conversation).
Another way would be to use replaceTrack to add/remove the track. Currently in aiortc, this will result in a task that wakes up 50 times per second to check the track, which is not ideal, but is much better than the current implementation.
Would you accept a PR that implements one of these approaches?
Source: huggingface/speech-to-speech