First, I'm trying to send audio data from one process to other via the UDP protocol on localhost.
First, the program reads voice from the microphone and sends it via UDP socket:
#include <QApplication>
#include <QIODevice>
#include <QtMultimediaKit/QAudioOutput>
#include <QtMultimediaKit/QAudioInput>
#include <QtMultimediaKit/QAudioFormat>
#include <QUdpSocket>
int main(int argc, char** argv){
QApplication app(argc, argv);
QAudioFormat format;
format.setSampleRate(128000);
format.setChannelCount(1);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
QAudioInput* input = new QAudioInput(format);
QUdpSocket* socket = new QUdpSocket();
socket->connectToHost("127.0.0.1", 1002);
input->start(socket);
return app.exec();
}
I check data sending in by Wireshark, and I think data is sent. So many UDP packages per second on the 1002 port.
The second program should read UDP packages and play it in the output device:
#include "UDPPlayer.h"
#include <QDebug>
UDPPlayer::UDPPlayer(){
socket = new QUdpSocket();
socket->bind(1002);
QAudioFormat format;
format.setSampleRate(128000);
format.setChannelCount(1);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
output = new QAudioOutput(format);
connect(socket, SIGNAL(readyRead()), this, SLOT(playData()));
}
void UDPPlayer::playData(){
qDebug() << "data";
output->start(socket);
}
The problem is the following: playData()
is never called ("data" is never printed). Does it mean that the socket is never readyRead
? But I see these packages by Wireshark. What am I doing wrong?