My goal is to transmit *.wav file over LAN network without delay or with minimal one.
Also we read a file on a server machine by parts, 320 bytes both. After this we send packets by UDP and write receiving in jitter-buffer. The size of the jitter-buffer is 10.
What delays should I set on timers for clear sound?
Here is the sender:
void MainWindow::on_start_tx_triggered()
{
timer1 = new QTimer (this);
udpSocketout = new QUdpSocket(this);
qDebug()<<"Start";
for (int i = 0; i < playlist.size(); ++i)
{
inputFile.setFileName(playlist.at(i));
qDebug()<<inputFile.fileName();
if (!inputFile.open(QIODevice::ReadOnly))
{
qDebug()<< "file not found;";
}
}
connect(timer1, SIGNAL(timeout()), this, SLOT(writeDatagrams()));
timer1->start(5);
}
void MainWindow::writeDatagrams()
{
if(!inputFile.atEnd()){
payloadout = inputFile.read(320);
}
qDebug()<<payloadout;
QDataStream out(&datagramout, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
out << qint64(0);
out << payloadout;
out.device()->seek(qint64(0));
out << qint64(datagramout.size() - sizeof(qint64));
qint64 writtenBytes = udpSocketout->writeDatagram(datagramout, remoteHOST, remotePORT);
qDebug() << "Sent " << writtenBytes << " bytes.";
}
Here is the receiver and player:
void MainWindow::on_start_rx_triggered()
{
udpSocketin = new QUdpSocket(this);
udpSocketin->bind(localHOST, localPORT);
connect(udpSocketin, SIGNAL(readyRead()),
this, SLOT(readDatagrams()));
QDataStream out(&datagramout, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
timer2 = new QTimer (this);
connect(timer2, SIGNAL(timeout()), this, SLOT(playbuff()));
timer2->start(50);
audioout = new QAudioOutput(format, this);
}
void MainWindow::readDatagrams()
{
datagramin.resize(udpSocketin->pendingDatagramSize());
qint64 receiveBytes = udpSocketin->readDatagram(datagramin.data(), datagramin.size());
qDebug() << "Receive " << receiveBytes << " bytes.";
QDataStream in(&datagramin, QIODevice::ReadOnly);
in.setVersion(QDataStream::Qt_4_7);
quint64 size = 0;
if(in.device()->size() > sizeof(quint64))
{
in >> size;
}
else
return;
if(in.device()->size() < size)
return;
in >> payloadin;
qDebug() << payloadin.size();
emit jitterbuff();
}
void MainWindow::jitterbuff()
{
if (buff_pos < SIZE_OF_BUF)
{
QDataStream out(&buffered, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
out << payloadin;
buff_pos++;
}
else
buff_pos = 0;
}
void MainWindow::playbuff()
{
qDebug() << "YES!!!";
buffer = new QBuffer(&buffered);
buffer->open(QIODevice::ReadOnly);
audioout->start(buffer);
QEventLoop loop;
QTimer::singleShot(50, &loop, SLOT(quit()));
loop.exec();
buffer->close();
}
This problem was solved. QAdioOutput has two modes; there are "push" and "pull". I give a pointer to a QIODevice, and write data directly to this. Solution:
Reading UDP socket:
void MainWindow::on_start_rx_triggered()
{
udpSocketin = new QUdpSocket(this);
udpSocketin->bind(localPORT);
connect(udpSocketin, SIGNAL(readyRead()), this, SLOT(readDatagrams()));
QDataStream out(&datagramout, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
timer2 = new QTimer (this);
connect(timer2, SIGNAL(timeout()), this, SLOT(playbuff()));
timer2->setInterval(15*9);
audioout = new QAudioOutput(format, this);
input = audioout->start();
}
void MainWindow::readDatagrams()
{
if (udpSocketin->hasPendingDatagrams()){
datagramin.resize(udpSocketin->pendingDatagramSize());
qint64 receiveBytes = udpSocketin->readDatagram(datagramin.data(), datagramin.size());
if (receiveBytes <= 0)
{
msg.warning(this, "File ERROR", "The end!", QMessageBox::Ok);
emit on_stop_rx_triggered();
}
QDataStream in(&datagramin, QIODevice::ReadOnly);
in.setVersion(QDataStream::Qt_4_7);
quint64 size = 0;
if(in.device()->size() > sizeof(quint64))
{
in >> size;
}
else return;
in >> rxfilename;
in >> name;
in >> payloadin;
emit jitterbuff();
}
void MainWindow::jitterbuff()
{
if (buff_pos < SIZE_OF_BUF)
{
buffered.append(payloadin);
buff_pos++;
}
else
{
timer2->start();
buffered.clear();
buff_pos = 0;
}
}
void MainWindow::playbuff()
{
if (!buffered.isEmpty())
{
buffer = new QBuffer(&buffered);
buffer->open(QIODevice::ReadOnly);
input->write(buffered);
buffer->close();
}
}
Writing to UDP socket:
void MainWindow::on_start_tx_triggered()
{
timer1 = new QTimer (this);
udpSocketout = new QUdpSocket(this);
inputFile.setFileName(playlist.at(playIDfile));
if (!inputFile.open(QIODevice::ReadOnly))
{
msg.warning(this, "File ERROR", "File not found!", QMessageBox::Ok);
return;
}
fileinfo = new QFileInfo (inputFile);
txfilename = fileinfo->fileName();
ui->playedFile->setText("Now played: " + txfilename);
connect(timer1, SIGNAL(timeout()), this, SLOT(writeDatagrams()));
timer1->start(15);
}
void MainWindow::writeDatagrams()
{
if(!inputFile.atEnd()){
payloadout = inputFile.read(SIZE_OF_SOUND);
QDataStream out(&datagramout, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
out << qint64(0);
out << txfilename;
out << name;
out << payloadout;
out.device()->seek(qint64(0));
out << qint64(datagramout.size() - sizeof(qint64));
qint64 writtenBytes = udpSocketout->writeDatagram(datagramout, remoteHOST, remotePORT);
}
}
If somebody will have seems problem, I will try to help him/her.
Related
I have a service: GWT client calls QT QTcpSocket function that make a request to a device and gets responses(it cannot be only the one response. I should waiting for all of them).
According to the QT documentation I can't use waitForReadyRead() function because I use Windows platform.
Note: This function may fail randomly on Windows. Consider using the
event loop and the readyRead() signal if your software will run on
Windows. http://doc.qt.io/qt-5/qabstractsocket.html#waitForReadyRead
I have only one decision now:
pseudo code:
QString MainQTFunc() {
create new thread;
while (!thread.isStopped()) {
sleep(x);
}
return QString variable from thread to GWT client;
}
New Thread {
run() {
make a TcpRequest to the device...
}
boolean isStopped() {
if(we got the response!!!) {
return true;
}
}
}
Does it the best solution to do so? I can't understand how to send simply QString variable after I get the result. Is it really impossible to the powerful QT?
Now I have(without any threads):
// The function should to return QString to the GWT client
QString MainWindow::TcpConnect(QByteArray data) {
_pSocket = new QTcpSocket( this );
connect( _pSocket, SIGNAL(readyRead()), SLOT(readTcpData()) );
connect( _pSocket, SIGNAL(connected()), SLOT(connected()) );
connect( _pSocket, SIGNAL(disconnected()), SLOT(disconnected()) );
dataGlobal = data;
_pSocket->connectToHost("IP", port);
//waiting here for all responses and sendinig the last response
return responseHexGlobal;
}
void MainWindow::connected() {
qDebug() << "connected. " << QDateTime::currentDateTime();
_pSocket->write( dataGlobal );
}
void MainWindow::disconnected() {
qDebug() << "disconnected. " << QDateTime::currentDateTime();
}
void MainWindow::readTcpData()
{
QByteArray data = _pSocket->readAll();
QByteArray as_hex_string = data.toHex();
QString response = QString(as_hex_string);
if(some condition here...) {
responseHexGlobal = response;
_pSocket->disconnectFromHost();
}
}
It's the best solution I've found. It works, but I don't like it
QString JSPrinter::connectTcp(QByteArray data) {
QTimer timer;
timer.setSingleShot(true);
QEventLoop loop;
_pSocket = new QTcpSocket( this ); // <-- needs to be a member variable: QTcpSocket * _pSocket;
connect( _pSocket, SIGNAL(readyRead()), SLOT(readTcpData()) );
connect( _pSocket, SIGNAL(connected()), SLOT(connected()) );
connect( _pSocket, SIGNAL(disconnected()), SLOT(disconnected()) );
connect( &timer, SIGNAL(timeout()), &loop, SLOT(quit()) );
loop.connect( this, SIGNAL(exitLoop()), SLOT(quit()) );
dataGlobal = data;
_pSocket->connectToHost(ip_, port_);
timer.start(75000);
loop.exec();
if(timer.isActive())
logger()->info("ok");
else
logger()->info("timeout");
return responseHexGlobal;
}
void JSPrinter::connected() {
qDebug() << "connected. " << QDateTime::currentDateTime();
_pSocket->write( dataGlobal );
}
void JSPrinter::disconnected() {
qDebug() << "disconnected. " << QDateTime::currentDateTime();
}
void JSPrinter::readTcpData() {
QByteArray data = _pSocket->readAll();
QByteArray as_hex_string = data.toHex();
std::string stdString(as_hex_string.constData(), as_hex_string.length());
qDebug() << "readTcpData response " << QDateTime::currentDateTime() << QString::fromStdString(stdString);
// doing something...
if(condition) {
responseHexGlobal = received;
qDebug() << "responseHexGlobal " << responseHexGlobal;
emit exitLoop();
_pSocket->disconnectFromHost();
} else {
emit exitLoop();
_pSocket->disconnectFromHost();
return;
}
}
I am using QTCPSockets to talk to a program I've written in Qt for Raspberry Pi. The same software runs on my Mac (or Windows, whatever). The Pi is running a QTCPServer.
I send JSON data to it and most of the time this goes ok.
But sometimes, the Pi is not responding, the data does not seem to arrive. But then, when I send some more data, that data is not being handled, but the previous Json message is! And this stays like this. All the messages are now off by 1. Sending a new messages, triggers the previous one.
It feels a bit connected to this bugreport: https://bugreports.qt.io/browse/QTBUG-58262
But I'm not sure if it is the same.
I've tried waitForBytesWritten and flush and it seemed to work at first, but later I saw the issue again.
I expect that the TCP buffer on the Pi is not being flushed, but I do now know how to make sure that all data is handled right away.
As asked, here is some sourcecode:
This is the client software:
Client::Client() : tcpSocket(new QTcpSocket(this)), in(tcpSocket)
{
connect(tcpSocket, &QIODevice::readyRead, this, &Client::readData);
connect(tcpSocket, &QTcpSocket::connected, this, &Client::connected);
connect(tcpSocket, &QTcpSocket::stateChanged, this, &Client::onConnectionStateChanged);
void (QAbstractSocket:: *sig)(QAbstractSocket::SocketError) = &QAbstractSocket::error;
connect(tcpSocket, sig, this, &Client::error);
}
void Client::connectTo(QString ip, int port) {
this->ip = ip;
this->port = port;
tcpSocket->connectToHost(ip, port);
}
void Client::reconnect() {
connectTo(ip, port);
}
void Client::disconnect()
{
tcpSocket->disconnectFromHost();
}
void Client::connected()
{
qDebug() << TAG << "connected!";
}
void Client::error(QAbstractSocket::SocketError error)
{
qDebug() << TAG << error;
}
void Client::sendData(const QString& data)
{
bool connected = (tcpSocket->state() == QTcpSocket::ConnectedState);
if (!connected) {
qDebug() << TAG << "NOT CONNECTED!";
return;
}
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_7);
out << data;
tcpSocket->write(block);
tcpSocket->flush();
}
void Client::sendData(const QByteArray& data) {
bool connected = (tcpSocket->state() == QTcpSocket::ConnectedState);
if (!connected) {
qDebug() << TAG << " is NOT connected!";
return;
}
tcpSocket->write(data);
tcpSocket->flush();
}
void Client::readData()
{
in.startTransaction();
QString data;
in >> data;
if (!in.commitTransaction())
{
return;
}
emit dataReceived(data);
}
void Client::onConnectionStateChanged(QAbstractSocket::SocketState state)
{
switch (state) {
case QAbstractSocket::UnconnectedState:
connectionState = "Not connected";
break;
case QAbstractSocket::ConnectingState:
connectionState = "connecting";
break;
case QAbstractSocket::ConnectedState:
connectionState = "connected";
break;
default:
connectionState = QString::number(state);
}
qDebug() << TAG << " connecting state: " << state;
emit connectionStateChanged(connectionState);
if (state == QAbstractSocket::UnconnectedState) {
QTimer::singleShot(1000, this, &Client::reconnect);
}
}
and here the server part:
Server::Server()
{
tcpServer = new QTcpServer(this);
connect(tcpServer, &QTcpServer::newConnection, this, &Server::handleConnection);
tcpServer->listen(QHostAddress::Any, 59723);
QString ipAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
// use the first non-localhost IPv4 address
for (int i = 0; i < ipAddressesList.size(); ++i) {
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
ipAddressesList.at(i).toIPv4Address()) {
ipAddress = ipAddressesList.at(i).toString();
break;
}
}
// if we did not find one, use IPv4 localhost
if (ipAddress.isEmpty())
ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
qDebug() << TAG << "ip " << ipAddress << " serverport: " << tcpServer->serverPort();
}
void Server::clientDisconnected()
{
QTcpSocket *client = qobject_cast<QTcpSocket *>(QObject::sender());
int idx = clients.indexOf(client);
if (idx != -1) {
clients.removeAt(idx);
}
qDebug() << TAG << "client disconnected: " << client;
client->deleteLater();
}
void Server::handleConnection()
{
qDebug() << TAG << "incoming!";
QTcpSocket* clientConnection = tcpServer->nextPendingConnection();
connect(clientConnection, &QAbstractSocket::disconnected, this, &Server::clientDisconnected);
connect(clientConnection, &QIODevice::readyRead, this, &Server::readData);
clients.append(clientConnection);
broadcastUpdate(Assets().toJson());
}
void Server::readData()
{
QTcpSocket *client = qobject_cast<QTcpSocket *>(QObject::sender());
QDataStream in(client);
in.startTransaction();
QString data;
in >> data;
if (!in.commitTransaction())
{
return;
}
...
// here I do something with the data. I removed that code as it is
// not necessary for this issue
...
broadcastUpdate(data);
}
void Server::broadcastUpdate(const QString& data)
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_7);
out << data;
foreach(QTcpSocket* client, clients) {
bool connected = (client->state() == QTcpSocket::ConnectedState);
if (!connected) {
qDebug() << TAG << client << " is NOT connected!";
continue;
}
client->write(block);
}
}
I think the problem is with your void Client::readData(): you have to write it in such a way that you read all available data from the socket inside it (usually it's written with while (socket->bytesAvailable() > 0) { ... } loop).
It is because of the way the readyRead() signal is emitted: the remote peer may send any non-zero number of packets to you, and your socket will emit any non-zero number of readyRead() signals. In your case, it seems that the server sends two messages but they only cause one readyRead() signal to be emitted on the client.
I wanna make a simple Qt app which can perform task like image below:
I have learnt Fortune Server examples and actually I want this app is just like the combination of Fortune Server and Fortune Client.
dialog.cpp
void Dialog::on_btnListen_clicked(bool checked)
{
if(checked)
{
listener = new ServerSock(this);
if(!listener->listen(QHostAddress::LocalHost,ui->boxPort->value()))
{
QMessageBox::critical(this,tr("Error"),tr("Cannot listen: %1").arg(listener->errorString()));
ui->btnListen->setChecked(false);
return;
}
else
{
qDebug() << "Server is listening to port" << listener->serverPort();
ui->btnListen->setText(tr("Listening"));
this->setWindowTitle(tr("Listening and Running"));
}
}
else
{
listener->close();
listener->deleteLater();
ui->tmbListen->setText(tr("Listen again"));
this->setWindowTitle(tr("Taking a rest"));
}
}
serversock.cpp:
#include "serversock.h"
ServerSock::ServerSock(QObject *parent) :
QTcpServer(parent)
{
}
void ServerSock::incomingConnection(int descriptor)
{
thread = new Threading(descriptor,this);
connect(thread,SIGNAL(finished()),thread,SLOT(deleteLater()));
thread->start();
}
threading.cpp:
#include "threading.h"
Threading::Threading(int descriptor, QObject *parent) :
QThread(parent)
{
this->descriptorSocket = descriptor;
}
void Threading::run()
{
qDebug() << "Thread is started...";
socketThread = new QTcpSocket();
if(!socketThread->setSocketDescriptor(this->descriptorSocket))
{
emit error(socketThread->error());
return;
}
connect(socketThread,SIGNAL(readyRead()),this,SLOT(readyToRead()),Qt::DirectConnection);
connect(socketThread,SIGNAL(disconnected()),this,SLOT(unconnected()),Qt::DirectConnection);
qDebug() << descriptorSocket << "Connected";
exec(); //loop
}
void Threading::readyToRead()
{
QByteArray data = socketThread->readAll();
socketOut = new QTcpSocket();
socketOut->connectToHost("192.168.0.1",8080);
if(socketOut->waitForConnected(5000))
{
QByteArray query("GET http:// mysite. com/page HTTP/1.1\r\nHost: anothersite.com\r\r\r\n" + data); //example
socketOut->write(query);
socketOut->write("\r\r\r\n");
socketOut->waitForBytesWritten(1000);
socketOut->waitForReadyRead(3000);
outPut = socketOut->readAll();
if(socketOut->error() != socketOut->UnknownSocketError)
{
qDebug() << socketOut->errorString();
socketOut->disconnectFromHost();
this->quit();
socketThread->abort();
}
else
{
socketOut->close();
}
}
else
{
qDebug() << "Cannot connected";
}
if(!outPut.isNull())
socketThread->write(outPut);
qDebug() << descriptorSocket << " Data in: " << data;
void Threading::unconnected()
{
qDebug() << "Disconnected";
socketThread->abort();
exit(0);
deleteLater();
}
The problem is that I don't know how to return data from remote host to the local client, like the image below:
it sends request, but there is no response. How to accomplish this?
I have used QTcpSocket and QTcpServer class of qt to establish two way communication. I am able to send data from client to server. But am not getting the response back from server i.e my client.cpp never fires readyRead() signal. I have checked using Wireshark that my data from the server is available in specifed port.
I am posting my client.cpp code( Please help) :
Client::Client(QObject* parent): QObject(parent)
{
socket = new QTcpSocket(this);
connect(socket, SIGNAL(connected()),
this, SLOT(startTransfer()));
connect(socket, SIGNAL(readyRead()),this, SLOT(startRead()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(socketError(QAbstractSocket::SocketError)) );
}
Client::~Client()
{
socket->close();
}
void Client::start(QString address, quint16 port)
{
addr.setAddress(address);
socket->connectToHost(addr,port,QTcpSocket::ReadWrite);
}
void Client::startTransfer()
{
printf("Connection established.\n");
char buffer[1024];
forever
{
printf(">> ");
gets(buffer);
int len = strlen(buffer);
buffer[len] = '\n';
buffer[len+1] = '\0';
socket->write(buffer);
socket->flush();
}
}
void Client::startRead()
{
cout<<"inside startRead()<<endl";
while(socket->canReadLine())
{
QByteArray ba = socket->readLine();
if(strcmp(ba.constData(), "!exit\n") == 0)
{
socket->disconnectFromHost();
break;
}
printf(">> %s", ba.constData());
}
}
void Client::socketError(QAbstractSocket::SocketError )
{
qDebug()<<"error" ;
}
Looks like you have forever loop here. This means that your Qt main eventloop never gets the control back after you call startTransfer(). How do you suppose the Qt should run the startRead() code if you block your execution thread with infinite loop?
For Amartel adding the server code:
Server::Server(QObject* parent): QObject(parent)
{
// cout << "Before connect" << endl;
connect(&server, SIGNAL(newConnection()),
this, SLOT(acceptConnection()));
cout << "Listening.." << endl;
server.listen(QHostAddress::Any, 9999);
// cout << "Server started.." << endl;
}
Server::~Server()
{
server.close();
}
void Server::acceptConnection()
{
// cout << "In acceptConnection" << endl;
client = server.nextPendingConnection();
connect(client, SIGNAL(readyRead()),
this, SLOT(startRead()));
}
void Server::startRead()
{
while(client->canReadLine())
{
QByteArray ba = client->readLine();
if(strcmp(ba.constData(), "!exit\n") == 0)
{
client->disconnectFromHost();
break;
}
printf(">> %s", ba.constData());
int result = 0;
bool ack = true;
result = client->write("I Reached");
cout<<result<<endl;
if(result <= 0)
qDebug("Ack NOT sent to client!!!");
else
qDebug("Ack sent to client.");
// client->write("I Reached");
client->flush();
}
}
I do not know what the heck is wrong with my code that it never Dequeus the queue, instead isEmpty always meets true.
class ConcurrentQueue
{
private:
QQueue<QByteArray> dataStore;
public:
void Enqueue(QByteArray value);
QByteArray Dequeue();
bool isEmpty();
private:
QMutex mutex;
};
void ConcurrentQueue::Enqueue(QByteArray value)
{
qDebug() << dataStore.length();
mutex.lock();
dataStore.enqueue(value);
mutex.unlock();
qDebug() << dataStore.length();
}
QByteArray ConcurrentQueue::Dequeue()
{
// mutex.lock();
// return dataStore.dequeue();
// mutex.unlock();
QByteArray tmp;
mutex.lock();
if(!dataStore.isEmpty())
{
tmp = dataStore.dequeue();
}
mutex.unlock();
return tmp;
}
bool ConcurrentQueue::isEmpty()
{
return dataStore.isEmpty();
}
next a timer is connected to a slot to Enqueu a packet every lt's say 10 milliseconds:
QByteArray built((char*)data, len) ;
//qDebug() << built.toHex();
qDebug() << "EnQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ";
queue.Enqueue(built);
A thread regularly checks if this queue is not empty it dequeues the queue and does something, but isEmplty always returns true
if(queue.isEmpty())
{
qDebug() << "#####################################";
return;
}
//NEVER COMES HERE
QByteArray rd15Bytes = queue.Dequeue();
here's the thread:
Write::Write()
{
}
void Write::run(){
while(1)
{
rs.writeToSerialPort(); // in this function above always isemplty is true
this->msleep(10);
}
}