QT voice chat Application on windows and android



  • I develop a voice chat program using QTcpSokcet and QTcpServer in which client can send and receive sound in real time to use it in local network and it worked and voice is seems to be clear acceptable but when I try to release it to android the sound is not clear as on windows there is a sound distribution
    I use this code to send and receive (client.cpp)

    #include "widget.h"
    #include "ui_widget.h"
    #include <QMediaPlayer>
    
    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget)
    {
        ui->setupUi(this);
    }
    
    Widget::~Widget()
    {
         delete ui;
    }
    
    void Widget::on_pushButton_clicked()
    {
       socket = new QTcpSocket(this);
       socket->connectToHost(ui->lineEdit->text(),quint16(5002));           connect(socket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(error()));
    
    QAudioFormat format;
    format.setSampleRate(44100);
    format.setChannelCount(2);
    format.setSampleSize(16);
    format.setCodec("audio/pcm");
    format.setByteOrder(QAudioFormat::LittleEndian);
    format.setSampleType(QAudioFormat::SignedInt);
    
    //If format isn't supported find the nearest supported
    QAudioDeviceInfo info(QAudioDeviceInfo::defaultInputDevice());
    if (!info.isFormatSupported(format))
        format = info.nearestFormat(format);
    ui->label_2->setText(info.deviceName()); // the supported Input device
    
    
    QAudioFormat format2;
    format2.setSampleRate(44100);
    format2.setChannelCount(2);
    format2.setSampleSize(16);
    format2.setCodec("audio/pcm");
    format2.setByteOrder(QAudioFormat::LittleEndian);
    format2.setSampleType(QAudioFormat::SignedInt);
    
    QAudioDeviceInfo info2(QAudioDeviceInfo::defaultOutputDevice());
    if (!info2.isFormatSupported(format2))
        format2 = info2.nearestFormat(format2);
    ui->label->setText(info2.deviceName()); // the supported output device
    
    input = new QAudioInput(format);
    input->setBufferSize(16384); //  set buffere size to solve sound distribution on windows but not solve this problem when use it in android
    
    output = new QAudioOutput(format2);
    output->setBufferSize(16384);
    
    device = output->start();
    
    input->start(socket);
    
    connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
    }
    
    void Widget::error()
    {
       ui->label->setText(socket->errorString());
    }
    
    void Widget::readyRead()
    {
        while (socket->bytesAvailable() > 0){
        QByteArray arr = socket->readAll();
        device->write(arr.data(),arr.size());
    }
    }
    

    and this code for server

    #include "server.h"
    
    Server::Server(MainWindow *w, QObject *parent) :
         QTcpServer(parent),
          window(w)
    {
    
    }
    
     void Server::incomingConnection(int desc)
    {
       QTcpSocket *socket = new QTcpSocket(); // get the new connection
       socket->setSocketDescriptor(desc); // set desc
       clients.insert(socket); // but the new connection to set 
       connect(socket,SIGNAL(readyRead()),this,SLOT(readRead()));
       window->setToPlain("CLient connected "+QString::number(socket->socketDescriptor()));
    }
    
    void Server::readRead()
    {
       QTcpSocket *socket = (QTcpSocket*)sender(); // get the sender
       while (socket->bytesAvailable() > 0){ // if bytes send more than zero 
           QByteArray arr = socket->readAll(); // read all bytes
           for (auto p : clients){ // loop throw all clients 
               if (p != socket){ // if the socket = the socket that send the voice not to send to him again
                   p->write(arr.data(),arr.size()); //write data
               }
           }
       }
     }
    
    void Server::disconnect()
    {
        QTcpSocket *socket = (QTcpSocket*)sender();
        clients.remove(socket);
        window->setToPlain("client disccont");
    }
    

    when I make search for a same app or example I found this Here
    I download this code and test it on both windows and android it work 100% fine, but I can not understand the code written and I it's not as I need I need the client to make both send and receive voice and server just to regulate the data

    I try to change buffer size of QAudioOutput or QAudioInput and change the format of voice and nothing help
    i have the same problem of sound distribution on windows but i solve it by set a buffered size of QAudioOutput and **QAudioInput **

    another try
    i put the server on and android mobile and clients on 2 computer using windows and the sound seems to work fine i don't know why when i use the client the problem found ?
    so I need any help to improve my code and make it work on android as windows or any help on how to modify the code in the project Here to add the two way for client i try on this but i cant
    thanks in advance



  • Hello i wish if i could help you. please i need to send voice over network using tcp socket and i believe you can help me if you give me your code (server and client side both )i will be very thankful.thank you soo much in advance.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.