title: How I can play streaming audio over Ethernet in Qt?
body: <p>My goal is to transmit *.wav file over LAN network without delay or with minimal one.</p>
  
  <p>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?</p>
  
  <p>Here is the sender:</p>
  
  <pre><code>void MainWindow::on_start_tx_triggered()
  {
      timer1 = new QTimer (this);
      udpSocketout = new QUdpSocket(this);
      qDebug()&lt;&lt;"Start";
      for (int i = 0; i &lt; playlist.size(); ++i)
      {
          inputFile.setFileName(playlist.at(i));
          qDebug()&lt;&lt;inputFile.fileName();
          if (!inputFile.open(QIODevice::ReadOnly))
          {
              qDebug()&lt;&lt; "file not found;";
          }
      }
      connect(timer1, SIGNAL(timeout()), this, SLOT(writeDatagrams()));
      timer1-&gt;start(5);
  }
  
  void MainWindow::writeDatagrams()
  {
      if(!inputFile.atEnd()){
          payloadout = inputFile.read(320);
      }
      qDebug()&lt;&lt;payloadout;
      QDataStream out(&amp;datagramout, QIODevice::WriteOnly);
      out.setVersion(QDataStream::Qt_4_7);
      out &lt;&lt; qint64(0);
      out &lt;&lt; payloadout;
      out.device()-&gt;seek(qint64(0));
      out &lt;&lt; qint64(datagramout.size() - sizeof(qint64));
      qint64 writtenBytes = udpSocketout-&gt;writeDatagram(datagramout, remoteHOST, remotePORT);
      qDebug() &lt;&lt; "Sent " &lt;&lt; writtenBytes &lt;&lt; " bytes.";
  }
  </code></pre>
  
  <p>Here is the receiver and player:</p>
  
  <pre><code>void MainWindow::on_start_rx_triggered()
  {
      udpSocketin = new QUdpSocket(this);
      udpSocketin-&gt;bind(localHOST, localPORT);
      connect(udpSocketin, SIGNAL(readyRead()),
              this, SLOT(readDatagrams()));
      QDataStream out(&amp;datagramout, QIODevice::WriteOnly);
      out.setVersion(QDataStream::Qt_4_7);
      timer2 =  new QTimer (this);
      connect(timer2, SIGNAL(timeout()), this, SLOT(playbuff()));
      timer2-&gt;start(50);
      audioout = new QAudioOutput(format, this);
  }
  
  void MainWindow::readDatagrams()
  {
      datagramin.resize(udpSocketin-&gt;pendingDatagramSize());
      qint64 receiveBytes = udpSocketin-&gt;readDatagram(datagramin.data(), datagramin.size());
      qDebug() &lt;&lt; "Receive " &lt;&lt; receiveBytes &lt;&lt; " bytes.";
      QDataStream in(&amp;datagramin, QIODevice::ReadOnly);
      in.setVersion(QDataStream::Qt_4_7);
      quint64 size = 0;
      if(in.device()-&gt;size() &gt; sizeof(quint64))
      {
          in &gt;&gt; size;
      }
      else
          return;
      if(in.device()-&gt;size() &lt; size)
          return;
      in &gt;&gt; payloadin;
      qDebug() &lt;&lt; payloadin.size();
      emit jitterbuff();
  }
  
  void MainWindow::jitterbuff()
  {
      if (buff_pos &lt; SIZE_OF_BUF)
      {
          QDataStream out(&amp;buffered, QIODevice::WriteOnly);
          out.setVersion(QDataStream::Qt_4_7);
          out &lt;&lt; payloadin;
          buff_pos++;
      }
      else
          buff_pos = 0;
  }
  
  void MainWindow::playbuff()
  {
      qDebug() &lt;&lt; "YES!!!";
      buffer = new QBuffer(&amp;buffered);
      buffer-&gt;open(QIODevice::ReadOnly);
      audioout-&gt;start(buffer);
      QEventLoop loop;
      QTimer::singleShot(50, &amp;loop, SLOT(quit()));
      loop.exec();
      buffer-&gt;close();
  }
  </code></pre>