0133-bcm2835-interpolate-audio-delay.patch 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. From dffcc96ee1cf290f59191c715076c866e6019c17 Mon Sep 17 00:00:00 2001
  2. From: wm4 <wm4@nowhere>
  3. Date: Wed, 13 Jan 2016 19:44:47 +0100
  4. Subject: [PATCH 133/381] bcm2835: interpolate audio delay
  5. It appears the GPU only sends us a message all 10ms to update
  6. the playback progress. Other than this, the playback position
  7. (what SNDRV_PCM_IOCTL_DELAY will return) is not updated at all.
  8. Userspace will see jitter up to 10ms in the audio position.
  9. Make this a bit nicer for userspace by interpolating the
  10. position using the CPU clock.
  11. I'm not sure if setting snd_pcm_runtime.delay is the right
  12. approach for this. Or if there is maybe an already existing
  13. mechanism for position interpolation in the ALSA core.
  14. I only set SNDRV_PCM_INFO_BATCH because this appears to remove
  15. at least one situation snd_pcm_runtime.delay is used, so I have
  16. to worry less in which place I have to update this field, or
  17. how it interacts with the rest of ALSA.
  18. In the future, it might be nice to use VC_AUDIO_MSG_TYPE_LATENCY.
  19. One problem is that it requires sending a videocore message, and
  20. waiting for a reply, which could make the implementation much
  21. harder due to locking and synchronization requirements.
  22. ---
  23. sound/arm/bcm2835-pcm.c | 12 +++++++++++-
  24. sound/arm/bcm2835.h | 1 +
  25. 2 files changed, 12 insertions(+), 1 deletion(-)
  26. --- a/sound/arm/bcm2835-pcm.c
  27. +++ b/sound/arm/bcm2835-pcm.c
  28. @@ -25,7 +25,7 @@
  29. /* hardware definition */
  30. static struct snd_pcm_hardware snd_bcm2835_playback_hw = {
  31. .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
  32. - SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
  33. + SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH),
  34. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  35. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  36. .rate_min = 8000,
  37. @@ -99,6 +99,8 @@ static irqreturn_t bcm2835_playback_fifo
  38. alsa_stream->pos %= alsa_stream->buffer_size;
  39. }
  40. + alsa_stream->interpolate_start = ktime_get_ns();
  41. +
  42. if (alsa_stream->substream) {
  43. if (new_period)
  44. snd_pcm_period_elapsed(alsa_stream->substream);
  45. @@ -399,6 +401,7 @@ static int snd_bcm2835_pcm_prepare(struc
  46. alsa_stream->buffer_size = snd_pcm_lib_buffer_bytes(substream);
  47. alsa_stream->period_size = snd_pcm_lib_period_bytes(substream);
  48. alsa_stream->pos = 0;
  49. + alsa_stream->interpolate_start = ktime_get_ns();
  50. audio_debug("buffer_size=%d, period_size=%d pos=%d frame_bits=%d\n",
  51. alsa_stream->buffer_size, alsa_stream->period_size,
  52. @@ -495,6 +498,7 @@ snd_bcm2835_pcm_pointer(struct snd_pcm_s
  53. {
  54. struct snd_pcm_runtime *runtime = substream->runtime;
  55. bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  56. + u64 now = ktime_get_ns();
  57. audio_info(" .. IN\n");
  58. @@ -503,6 +507,12 @@ snd_bcm2835_pcm_pointer(struct snd_pcm_s
  59. frames_to_bytes(runtime, runtime->control->appl_ptr),
  60. alsa_stream->pos);
  61. + /* Give userspace better delay reporting by interpolating between GPU
  62. + * notifications, assuming audio speed is close enough to the clock
  63. + * used for ktime */
  64. + if (alsa_stream->interpolate_start && alsa_stream->interpolate_start < now)
  65. + runtime->delay = -(int)div_u64((now - alsa_stream->interpolate_start) * runtime->rate, 1000000000);
  66. +
  67. audio_info(" .. OUT\n");
  68. return snd_pcm_indirect_playback_pointer(substream,
  69. &alsa_stream->pcm_indirect,
  70. --- a/sound/arm/bcm2835.h
  71. +++ b/sound/arm/bcm2835.h
  72. @@ -137,6 +137,7 @@ typedef struct bcm2835_alsa_stream {
  73. unsigned int pos;
  74. unsigned int buffer_size;
  75. unsigned int period_size;
  76. + u64 interpolate_start;
  77. uint32_t enable_fifo_irq;
  78. irq_handler_t fifo_irq_handler;