ALSA: pcm: playback silence - move silence variable updates to separate function
[linux-block.git] / sound / core / pcm_lib.c
index d21c73944efdeecaca7ddc86dd9fd0facd283638..670572c9a8cc789803d80ab506991698beea2f7a 100644 (file)
 static int fill_silence_frames(struct snd_pcm_substream *substream,
                               snd_pcm_uframes_t off, snd_pcm_uframes_t frames);
 
+
+static inline void update_silence_vars(struct snd_pcm_runtime *runtime,
+                                      snd_pcm_uframes_t ptr,
+                                      snd_pcm_uframes_t new_ptr)
+{
+       snd_pcm_sframes_t delta;
+
+       delta = new_ptr - ptr;
+       if (delta == 0)
+               return;
+       if (delta < 0)
+               delta += runtime->boundary;
+       if ((snd_pcm_uframes_t)delta < runtime->silence_filled)
+               runtime->silence_filled -= delta;
+       else
+               runtime->silence_filled = 0;
+       runtime->silence_start = new_ptr;
+}
+
 /*
  * fill ring buffer with silence
  * runtime->silence_start: starting pointer to silence area
@@ -42,56 +61,59 @@ static int fill_silence_frames(struct snd_pcm_substream *substream,
  *
  * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
  */
-void snd_pcm_playback_silence(struct snd_pcm_substream *substream)
+void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
 {
        struct snd_pcm_runtime *runtime = substream->runtime;
-       snd_pcm_uframes_t appl_ptr = READ_ONCE(runtime->control->appl_ptr);
-       snd_pcm_sframes_t added, hw_avail, frames;
-       snd_pcm_uframes_t noise_dist, ofs, transfer;
+       snd_pcm_uframes_t frames, ofs, transfer;
        int err;
 
-       added = appl_ptr - runtime->silence_start;
-       if (added) {
-               if (added < 0)
-                       added += runtime->boundary;
-               if (added < runtime->silence_filled)
-                       runtime->silence_filled -= added;
-               else
-                       runtime->silence_filled = 0;
-               runtime->silence_start = appl_ptr;
-       }
-
-       // This will "legitimately" turn negative on underrun, and will be mangled
-       // into a huge number by the boundary crossing handling. The initial state
-       // might also be not quite sane. The code below MUST account for these cases.
-       hw_avail = appl_ptr - runtime->status->hw_ptr;
-       if (hw_avail < 0)
-               hw_avail += runtime->boundary;
-
-       noise_dist = hw_avail + runtime->silence_filled;
        if (runtime->silence_size < runtime->boundary) {
-               frames = runtime->silence_threshold - noise_dist;
-               if (frames <= 0)
+               snd_pcm_sframes_t noise_dist;
+               snd_pcm_uframes_t appl_ptr = READ_ONCE(runtime->control->appl_ptr);
+               update_silence_vars(runtime, runtime->silence_start, appl_ptr);
+               /* initialization outside pointer updates */
+               if (new_hw_ptr == ULONG_MAX)
+                       new_hw_ptr = runtime->status->hw_ptr;
+               /* get hw_avail with the boundary crossing */
+               noise_dist = appl_ptr - new_hw_ptr;
+               if (noise_dist < 0)
+                       noise_dist += runtime->boundary;
+               /* total noise distance */
+               noise_dist += runtime->silence_filled;
+               if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
                        return;
+               frames = runtime->silence_threshold - noise_dist;
                if (frames > runtime->silence_size)
                        frames = runtime->silence_size;
        } else {
-               frames = runtime->buffer_size - noise_dist;
-               if (frames <= 0)
-                       return;
+               /*
+                * This filling mode aims at free-running mode (used for example by dmix),
+                * which doesn't update the application pointer.
+                */
+               if (new_hw_ptr == ULONG_MAX) {  /* initialization */
+                       snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
+                       if (avail > runtime->buffer_size)
+                               avail = runtime->buffer_size;
+                       runtime->silence_filled = avail > 0 ? avail : 0;
+                       runtime->silence_start = runtime->status->hw_ptr;
+               } else {
+                       update_silence_vars(runtime, runtime->status->hw_ptr, new_hw_ptr);
+               }
+               frames = runtime->buffer_size - runtime->silence_filled;
        }
-
        if (snd_BUG_ON(frames > runtime->buffer_size))
                return;
+       if (frames == 0)
+               return;
        ofs = (runtime->silence_start + runtime->silence_filled) % runtime->buffer_size;
-       do {
+       while (frames > 0) {
                transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
                err = fill_silence_frames(substream, ofs, transfer);
                snd_BUG_ON(err < 0);
                runtime->silence_filled += transfer;
                frames -= transfer;
                ofs = 0;
-       } while (frames > 0);
+       }
        snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
 }
 
@@ -425,6 +447,10 @@ static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
                return 0;
        }
 
+       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
+           runtime->silence_size > 0)
+               snd_pcm_playback_silence(substream, new_hw_ptr);
+
        if (in_interrupt) {
                delta = new_hw_ptr - runtime->hw_ptr_interrupt;
                if (delta < 0)
@@ -442,10 +468,6 @@ static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
                runtime->hw_ptr_wrap += runtime->boundary;
        }
 
-       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
-           runtime->silence_size > 0)
-               snd_pcm_playback_silence(substream);
-
        update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
 
        return snd_pcm_update_state(substream, runtime);