Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT voice chat Application on windows and android
QtWS25 Last Chance

QT voice chat Application on windows and android

Scheduled Pinned Locked Moved Unsolved General and Desktop
mobile app devenetwork socketqt 5.6.1tcp
2 Posts 2 Posters 3.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AmrCoder
    wrote on 22 Aug 2016, 22:35 last edited by
    #1

    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

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christina
      wrote on 16 Oct 2016, 01:15 last edited by
      #2

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved