I am trying to understand how to transmit real-time audio over TCP sockets in Qt. What I'm using is QAudioInput on the client and QAudioOutput on the server. Both use 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 already have a simple socket server setup and managed to transfer 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 get the data and add it to the QByteArray
On the server, I create a QBuffer and give it a QByteArray as soon as 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. I am looking to change the server to play it in real time, but I cannot figure out how to do this. I approached real time, but it had loud clicks between packets and was delayed for several seconds.
I tried to play the stream as I sent it:
audio->start(socket);
but it does nothing. Maybe if I use QDataStream instead of using sockets directly?
source share