qtqudpsocket

Qt QUdpsocket audiostreaming


I am trying to clone the audio streaming model of QTCpsocket but now using QUdpsocket (virtual connection), though it looks like the code is being executed , nevertheless, effectively its not doing the job, I cant get streamed audio captured;

Main focus point is: is it possible to start a QAudioOutput with a QUDpsocket ???

Yet to clearly mention that this code works fine with a TCP socket!

Code snippet:

in server.h file

private:
    QUdpSocket *socketUDP;

and in .CPP file

udpServer::udpServer(QObject *parent) : QObject(parent)
{
    socketUDP = new QUdpSocket(this);
    serverAddress = QHostAddress("192.168.1.8");
    //socketUDP->bind(serverAddress, 1357);
    socketUDP->bind(1357, QUdpSocket::ShareAddress);
    socketUDP->open(QIODevice::ReadOnly);
    connect(socketUDP, &QUdpSocket::readyRead, this, &udpServer::playStreamedAudio);

}

then the playstream() method:

    void udpServer::playStreamedAudio() {
      // set the QAudioFormat parameters of output audio device.
      my_QAudioFormat = new QAudioFormat;
      my_QAudioFormat->setSampleRate(48000);
      my_QAudioFormat->setChannelCount(1);
      my_QAudioFormat->setSampleSize(8);
      my_QAudioFormat->setCodec("audio/pcm");
      my_QAudioFormat->setByteOrder(QAudioFormat::LittleEndian);
      my_QAudioFormat->setSampleType(QAudioFormat::UnSignedInt);
      //
    // get default audio output device 
      audiOutputDevice = QAudioDeviceInfo::defaultOutputDevice();

      audiooutput = new QAudioOutput(audiOutputDevice,my_QAudioFormat, this);
  // attach to socket!
    qDebug() << "Playaing AudioStream";
    socketUDP->open(QIODevice::ReadOnly);
    audiooutput->start(socketUDP); // the Audio output device shall listen to server socket for audio
}

Solution

  • It turns out that UDP sockets may not suitability be interfaced as QioDevices... seems they are not intended to be, the packets are better be written to a file and then processed.