title: Qt Stream Audio Over TCP Socket In Realtime
body: <p>I'm trying to figure out how to stream audio in realtime over TCP sockets in Qt.  What I'm using is a <code>QAudioInput</code> on the client and <code>QAudioOutput</code> on the server. Both are using the following format:</p>
  
  <pre><code>QAudioFormat format;
  format.setChannelCount(1);
  format.setSampleRate(8000);
  format.setSampleSize(8);
  format.setCodec("audio/pcm");
  format.setByteOrder(QAudioFormat::LittleEndian);
  format.setSampleType(QAudioFormat::UnSignedInt);
  </code></pre>
  
  <p>I have a simple socket server setup already and managed to stream the audio from the client to the server using:</p>
  
  <pre><code>//client
  QAudioInput *audio = new QAudioInput(format, this);
  audio-&gt;setBufferSize(1024);
  audio-&gt;start(socket);
  
  
  //server
  QAudioOutput *audio = new QAudioOutput(format, this);
  audio-&gt;setBufferSize(1024);
  </code></pre>
  
  <p>Then on the server I'm receiving the data and appending it to a <code>QByteArray</code></p>
  
  <p>On the server, I create a <code>QBuffer</code> and give it the <code>QByteArray</code> once the client closes, and then play it like this:</p>
  
  <pre><code>QByteArray *data = new QByteArray();
  
  while(1)
  {
      if(socket-&gt;waitForReadyRead(30000))
          data-&gt;append(socket-&gt;readAll());
      else
          break;
  }
  
  QBuffer *buffer = new QBuffer(data);
  QEventLoop *loop = new QEventLoop(this);
  buffer-&gt;open(QIODevice::ReadOnly);
  audio-&gt;start(buffer);
  
  loop-&gt;exec();
  </code></pre>
  
  <p>This will play the entire stream AFTER the client closes. What I'm looking for is to modify the server to play it in realtime, but I can't figure out how. I've gotten close to realtime but it had loud clicks between packets and was delayed several seconds.</p>
  
  <p>I've tried playing the stream like how I used to send it:</p>
  
  <pre><code>audio-&gt;start(socket);
  </code></pre>
  
  <p>but this doesn't do anything. Maybe if I use <code>QDataStream</code> instead of directly using the sockets?</p>