title: Alsa snd_pcm_avail always returning a 0
body: <p>I am trying to read data from my codec.  For reasons in my project I would like to do nonblocking,  but every time I read the number of bytes available on my codec it says zero.</p>
  
  <p>The algorithm is pretty simple:  wait 1ms then check to see if there are 160+ samples available in the codec to read and then read the samples.  But every time I do a read it says the sample count is zero.</p>
  
  <p>Can someone help me understand why "rc = snd_pcm_avail(inputCodecHandle);" is always returning a zero?</p>
  
  <p>Here is the thread with the code in it.</p>
  
  <pre><code>void CRadioStack::rcvThread() {
      ChannelBuffer_t *buffer_p = NULL;
      int8_t *inputBuf_p;
      int rc;
      int16_t *inputBuf16_p;
      int  samplesToRead;
      const int rxFrameSize = 160;
      snd_pcm_sframes_t delay;
  
      snd_pcm_nonblock(inputCodecHandle, 1);
      snd_pcm_prepare(inputCodecHandle);
  
      while (true) {
          TWTime::msleep(1);
  
          // get the number of samples available
          snd_pcm_delay(inputCodecHandle, &amp;delay);
          rc = snd_pcm_avail(inputCodecHandle);
          if (rc &lt; 0) {
              myLog-&gt;warn("Error in getting sample count: %s", snd_strerror(rc));
              snd_pcm_prepare(outputCodecHandle);
              continue;
          }
          samplesToRead = rc;
  
          // if number of samples &gt; 160 then get 160 samples
          if (samplesToRead &lt;= rxFrameSize) {
              continue;
          }
  
          // read the from the codec into the Channel Buffer.
          rc = snd_pcm_readi(inputCodecHandle, inputBuf_p, rxFrameSize);
          if (rc &lt; 0) {
              myLog-&gt;warn("Error reading Codec: %s", snd_strerror(rc));
              continue;
          } else if (rc != rxFrameSize) {  // nothing to get
              myLog-&gt;warn("Input samples on codec not 160");
          }
  
          pushToInputQueue(inputBuf_p);
      }
  }
  </code></pre>
  
  <p>And here is the code to open the codec.</p>
  
  <pre><code>bool CRadioStack::openInputCodec()
  {
      unsigned int val;
      int dir;
      const int NUM_OF_CHAN = 1;
      codecRunning = false;
      snd_pcm_uframes_t frames;
  
      int rc;
      snd_pcm_t *handle;
      snd_pcm_hw_params_t *params;
  
      inputCodecHandle = nullptr;
  
      // Open pcm device for output
      rc = snd_pcm_open(&amp;handle, "hw:0,0", SND_PCM_STREAM_CAPTURE, 0);
      if (rc &lt; 0) {
          myLog-&gt;error("Unable to open input codec: %s", snd_strerror(rc));
          return false;
      }
  
      // allocate a hardware parameters object
      snd_pcm_hw_params_alloca(&amp;params);
  
      // fill with default values
      snd_pcm_hw_params_any(handle, params);
  
      // now setup the hardware paramters
      snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);    // interleaved
      snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);            // 16bin linear little-endian
      snd_pcm_hw_params_set_channels(handle, params, NUM_OF_CHAN);                    // one channel
      val = 0;
      snd_pcm_hw_params_set_channels_near(handle, params, &amp;val);                      // one channel
      val = 8000;
      dir = 0;
      snd_pcm_hw_params_set_rate_near(handle, params, &amp;val, &amp;dir);                    // 8k sample rate.
      frames = 160;
      snd_pcm_hw_params_set_period_size_near(handle, params, &amp;frames, &amp;dir);          // period size = 160 frames
  
      // save the hardware parameters
      rc = snd_pcm_hw_params(handle, params);
      if (rc &lt; 0) {
          myLog-&gt;error("Unable to save hardware parameters to output codec.");
          return false;
      }
  
      // ready to write to output codec.
      // so save the handle so that it can be used elsewhere.
      inputCodecHandle = handle;
      return true;
  }
  </code></pre>
  
  <p>Thanks!</p>