0

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
}
Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
  • 1
    Can you edit your question to show the code that creates and configures `socketUDP`? – G.M. Dec 26 '17 at 21:50
  • G.M, edited and showing code that creates socketUDP. – Mohammad Kanan Dec 27 '17 at 11:11
  • Where is `socketUDP` reading data from? Or have I misunderstood something? I think you need to call [`bind`](http://doc.qt.io/qt-5/qabstractsocket.html#bind) with suitable host/port parameters in order to receive datagrams. – G.M. Dec 27 '17 at 11:37
  • G.M, thats in the constructor. Ok now I edited the code snippet to show constructor. – Mohammad Kanan Dec 27 '17 at 15:26
  • G.M, when the client starts streaming towards the server, the slot "playStreamedAudio" is fired -at least once - ... I can see that by debug(). So client is able to connect to server ... but I am not sure if client really keeps shooting the datagrams .. nor that server keeps receiving them, I have to say that. – Mohammad Kanan Dec 27 '17 at 15:34
  • 1
    I think it's the `connect` that's causing (at least some of) the problem. As it stands each time the socket receives data the `readyRead` signal will be emitted, `playStreamedAudio` will be called and a new `QAudioOutput` created using the same socket -- `socketUDP`. By way of a quick test, try replacing the call to `connect` with simply `playStreamedAudio();`. – G.M. Dec 27 '17 at 16:00
  • G.M, thats a strange thing I noticed when I implemented the streaming with TCP socket (and it works fine) ... your conclusion is what I did in the beginning with TCP socket , but guess what ! without attaching the Audio Output device in the slot ... you never get anything .. that behavior is mysterious but that was only way I got i working .. so naturally I cloned same logic replacing TCP with UDP socket! I will give a try – Mohammad Kanan Dec 27 '17 at 16:14
  • G.M, I withdraw my last comment! What was causing me to think as such s that the underlying IO device (QIODevice) was actually not playing because it has to be reset "seek(0)" in order to play correctly. Well, my knowledge is building up around those issues ... may be the socket should play correctly .. but I am getting glued to develop more robust with buffers and transactions. – Mohammad Kanan Dec 30 '17 at 16:59

1 Answers1

0

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.

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47