Search code examples
c++audiochatvoice

C++ Streaming audio from microphone


I have got a classic client-server environment in c++.
Now i want to implement a voice chat between specific clients.
My problem is: How to stream without "pauses" the audio. I'm interestend only to the theory.
My first idea was capture some bytes every 0,5 seconds to a buffer and then send it to the server, which re-send it to the interested clients.
But I don't know how to do this as in-real-time as possible!

Edit Client is Windows and Server is Linux


Solution

  • You would pick a codec and stream based on the clock rate specified by that codec. For example, the G.711 codec specifies a clock rate of 8000 Hz (meaning the microphone input source will be sampled 8000 times per second). It also specifies that (by default) each packet should contain 20 milliseconds of audio, so over one second you would send 50 audio packets (1 second = 1000 millseconds / 20 millseconds per packet = 50 packets per second).

    Implementation-wise (for real-time) you would have a separate thread that has "realtime" priority that would be responsible for sampling the audio from the microphone, wrapping it in a RTP packet, and shipping that packet off to your server. Your server would likewise have a separate "realtime" priority thread that would receive each RTP packet and forward it to each client that is subscribed.