I'm trying to figure out how to stream audio in realtime over TCP sockets in Qt. What I'm using is a QAudioInput
on the client and QAudioOutput
on the server. Both are using the following format:
QAudioFormat format;
format.setChannelCount(1);
format.setSampleRate(8000);
format.setSampleSize(8);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
I have a simple socket server setup already and managed to stream the audio from the client to the server using:
//client
QAudioInput *audio = new QAudioInput(format, this);
audio->setBufferSize(1024);
audio->start(socket);
//server
QAudioOutput *audio = new QAudioOutput(format, this);
audio->setBufferSize(1024);
Then on the server I'm receiving the data and appending it to a QByteArray
On the server, I create a QBuffer
and give it the QByteArray
once the client closes, and then play it like this:
QByteArray *data = new QByteArray();
while(1)
{
if(socket->waitForReadyRead(30000))
data->append(socket->readAll());
else
break;
}
QBuffer *buffer = new QBuffer(data);
QEventLoop *loop = new QEventLoop(this);
buffer->open(QIODevice::ReadOnly);
audio->start(buffer);
loop->exec();
This will play the entire stream AFTER the client closes. What I'm looking for is to modify the server to play it in realtime, but I can't figure out how. I've gotten close to realtime but it had loud clicks between packets and was delayed several seconds.
I've tried playing the stream like how I used to send it:
audio->start(socket);
but this doesn't do anything. Maybe if I use QDataStream
instead of directly using the sockets?
In order to solve that problem I simply increased the setBufferSize
from your 1024 to a higher value, for example i've used 8192, that way you give the chance to the devices read more data at a time, and I was able to stream audio over my wireless network.
My class design is a little bit different than yours, I have different classes for sockets and audio, and you can see it here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With