QTcpSocket & QAudioInput
-
Hi all, i'm developing an app that allow to communicate between PCs by socket and I would to send audio from microphone.
I asked myself if I can use QTcpSocket & QAudioInput
So I tried this:@void Server::makeStream() {
QAudioInput* audio; QAudioFormat format; format.setFrequency(8000); format.setChannels(1); format.setSampleSize(8); format.setCodec("audio/pcm"); format.setByteOrder(QAudioFormat::LittleEndian); format.setSampleType(QAudioFormat::UnSignedInt); QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice(); if (!info.isFormatSupported(format)) { qWarning()<<"default format not supported try to use nearest"; format = info.nearestFormat(format); } audio = new QAudioInput(format, this); this->ToSocket("Listen Me"); audio->start(this->SocketServer);
}@
So I think that maybe Server sends "audio", but I don't know how can I do to save data from socket to a .wav file on client pc.
I tried this:
@void Client::readSocket()
{
QDataStream in(Socket);
in.setVersion(QDataStream::Qt_4_0);
if (audioincoming)
{
QFile f;
f.setFileName("/tmp/test.wav");
f.open( QIODevice::Append );
in >> f;
f.close();
return;
}
}@
But I can't use >> operator.any suggestions?
thanks all and sorry for my horrible english. -
I modified the Client::readSocket() function to this:
@void Client::readSocket()
{if (audioincoming) { QAudioFormat format; // Set up the format, eg. format.setFrequency(24000); format.setChannels(1); format.setSampleSize(8); format.setCodec("audio/pcm"); format.setByteOrder(QAudioFormat::LittleEndian); format.setSampleType(QAudioFormat::UnSignedInt); QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice()); if (!info.isFormatSupported(format)) { qWarning()<<"raw audio format not supported by backend, cannot play audio."; return; } audio = new QAudioOutput(format, this); connect(audio,SIGNAL(stateChanged(QAudio::State)),SLOT(finishedPlaying(QAudio::State))); audio->setBufferSize(512000); audio->start(Socket); return; }
}@
Server sends audio to the client but audio lags and is very horrible.
after about 30 secs the transferring of audio unexpectedly stops, client and server remain connected but server doesn’t send audio, or client doesn’t play audio from socket.moreover i receive in the client warnings:
@ QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -1536
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -1536
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -1536
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -1536
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -1024
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -1536
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -1536
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -1536
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -1536
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -512
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -2048
QIODevice::seek: Invalid pos: -512@ -
@Client::Client(QObject *parent) :
QObject(parent)
{
Socket = new QTcpSocket(this);
connect (this->Socket,SIGNAL(connected()),this, SLOT(SendTocToc()));
connect (this->Socket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(getError()));
connect (this->Socket,SIGNAL(readyRead()), this, SLOT(readSocket()));
QAudioFormat format;
// Set up the format, eg.
format.setFrequency(32000);
format.setChannels(1);
format.setSampleSize(8);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice()); if (!info.isFormatSupported(format)) { qWarning()<<"raw audio format not supported by backend, cannot play audio."; return; } audio = new QAudioOutput(format, this); connect(audio,SIGNAL(stateChanged(QAudio::State)),SLOT(finishedPlaying(QAudio::State))); audio->setBufferSize(512000);
}
void Client::readSocket()
{
if (audioincoming)
{
if (!audio->state() == 0 )
QTimer::singleShot(3000,this,SLOT(startAudio()));
return;
}
//[...]
}
void Client::startAudio()
{
audio->start(this->Socket);
} @Now it works. I initialize QAudioOutput only on class constructor but I must delay a bit to "load buffer". So I tried to start QAudioOutput object after 3 secs, but I would to use a more efficient way. If I don't delay, audio lags and QAudioOutput crashes.
Any suggestions?