]> git.alsa-project.org Git - alsa-plugins.git/commitdiff
aaf: Refactor timeout routines
authorAndre Guedes <andre.guedes@intel.com>
Sat, 8 Dec 2018 01:55:48 +0000 (17:55 -0800)
committerTakashi Iwai <tiwai@suse.de>
Mon, 10 Dec 2018 08:42:17 +0000 (09:42 +0100)
The functions aaf_mclk_timeout_playback() and aaf_mclk_timeout_capture()
have some common code so this patch does a code refactoring by moving
the shared code into a new function called aaf_timer_timeout().

After the refactoring, aaf_mclk_timeout_playback() and aaf_mclk_timeout_
capture() ended up having no code related to timeout so they were
renamed to better represent what they really do (send frames and present
frames, respectively).

Signed-off-by: Andre Guedes <andre.guedes@intel.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
aaf/pcm_aaf.c

index 49d5bedbfc03c5d14d072bbd0066ef1795035143..1d7ebcad36634c7a408bacdc39d8661a5df071b1 100644 (file)
@@ -895,51 +895,64 @@ static int aaf_flush_rx_buf(snd_pcm_aaf_t *aaf)
        return 0;
 }
 
-static int aaf_mclk_timeout_playback(snd_pcm_aaf_t *aaf)
+static int aaf_tx_frames(snd_pcm_aaf_t *aaf)
 {
        int res;
-       ssize_t n;
-       uint64_t expirations;
        snd_pcm_uframes_t hw_avail;
        snd_pcm_ioplug_t *io = &aaf->io;
 
-       n = read(aaf->timer_fd, &expirations, sizeof(uint64_t));
-       if (n < 0) {
-               SNDERR("Failed to read() timer");
-               return -errno;
+       hw_avail = snd_pcm_ioplug_hw_avail(io, aaf->hw_ptr, io->appl_ptr);
+       if (hw_avail < aaf->frames_per_pdu) {
+               /* If the number of available frames is less than number of
+                * frames needed to fill an AVTPDU, we reached an underrun
+                * state.
+                */
+               return -EPIPE;
        }
 
-       if (expirations != 1)
-               pr_debug("Missed %llu tx interval(s) ", expirations - 1);
+       res = aaf_tx_pdus(aaf, 1);
+       if (res < 0)
+               return res;
 
-       while (expirations--) {
-               aaf->timer_expirations++;
+       aaf_inc_ptr(&aaf->hw_ptr, aaf->frames_per_pdu, aaf->boundary);
+       return 0;
+}
 
-               hw_avail = snd_pcm_ioplug_hw_avail(io, aaf->hw_ptr, io->appl_ptr);
-               if (hw_avail < aaf->frames_per_pdu) {
-                       /* If the number of available frames is less than
-                        * number of frames needed to fill an AVTPDU, we
-                        * reached an underrun state.
-                        */
-                       return -EPIPE;
-               }
+static int aaf_present_frames(snd_pcm_aaf_t *aaf)
+{
+       snd_pcm_sframes_t len;
+       snd_pcm_ioplug_t *io = &aaf->io;
 
-               res = aaf_tx_pdus(aaf, 1);
-               if (res < 0)
-                       return res;
+       len = aaf->hw_virt_ptr - aaf->hw_ptr;
+       if (len < 0)
+               len += aaf->boundary;
 
-               aaf_inc_ptr(&aaf->hw_ptr, aaf->frames_per_pdu, aaf->boundary);
+       if ((snd_pcm_uframes_t) len > io->buffer_size) {
+               /* If the distance between hw virtual pointer and hw
+                * pointer is greater than the buffer size, it means we
+                * had an overrun error so -EPIPE is returned.
+                */
+               return -EPIPE;
        }
 
+       aaf_inc_ptr(&aaf->hw_ptr, aaf->frames_per_pdu, aaf->boundary);
        return 0;
 }
 
-static int aaf_mclk_timeout_capture(snd_pcm_aaf_t *aaf)
+static int aaf_process_frames(snd_pcm_aaf_t *aaf)
 {
+       snd_pcm_ioplug_t *io = &aaf->io;
+
+       return (io->stream == SND_PCM_STREAM_PLAYBACK) ?
+              aaf_tx_frames(aaf) :
+              aaf_present_frames(aaf);
+}
+
+static int aaf_timer_timeout(snd_pcm_aaf_t *aaf)
+{
+       int res;
        ssize_t n;
        uint64_t expirations;
-       snd_pcm_sframes_t len;
-       snd_pcm_ioplug_t *io = &aaf->io;
 
        n = read(aaf->timer_fd, &expirations, sizeof(uint64_t));
        if (n < 0) {
@@ -948,24 +961,14 @@ static int aaf_mclk_timeout_capture(snd_pcm_aaf_t *aaf)
        }
 
        if (expirations != 1)
-               pr_debug("Missed %llu presentation time(s) ", expirations - 1);
+               pr_debug("Missed %llu expirations ", expirations - 1);
 
        while (expirations--) {
                aaf->timer_expirations++;
 
-               len = aaf->hw_virt_ptr - aaf->hw_ptr;
-               if (len < 0)
-                       len += aaf->boundary;
-
-               if ((snd_pcm_uframes_t) len > io->buffer_size) {
-                       /* If the distance between hw virtual pointer and hw
-                        * pointer is greater than the buffer size, it means we
-                        * had an overrun error so -EPIPE is returned.
-                        */
-                       return -EPIPE;
-               }
-
-               aaf_inc_ptr(&aaf->hw_ptr, aaf->frames_per_pdu, aaf->boundary);
+               res = aaf_process_frames(aaf);
+               if (res < 0)
+                       return res;
        }
 
        return 0;
@@ -1152,7 +1155,7 @@ static int aaf_poll_revents(snd_pcm_ioplug_t *io, struct pollfd *pfd,
                        return -EINVAL;
 
                if (pfd[0].revents & POLLIN) {
-                       res = aaf_mclk_timeout_playback(aaf);
+                       res = aaf_timer_timeout(aaf);
                        if (res < 0)
                                return res;
 
@@ -1163,7 +1166,7 @@ static int aaf_poll_revents(snd_pcm_ioplug_t *io, struct pollfd *pfd,
                        return -EINVAL;
 
                if (pfd[0].revents & POLLIN) {
-                       res = aaf_mclk_timeout_capture(aaf);
+                       res = aaf_timer_timeout(aaf);
                        if (res < 0)
                                return res;