From patchwork Mon Oct 22 19:17:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Brady X-Patchwork-Id: 10652405 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C3DA417D4 for ; Mon, 22 Oct 2018 19:17:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7092829147 for ; Mon, 22 Oct 2018 19:17:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 63A912917F; Mon, 22 Oct 2018 19:17:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,FREEMAIL_FROM, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 66CF229147 for ; Mon, 22 Oct 2018 19:17:28 +0000 (UTC) Received: from alsa0.perex.cz (localhost [127.0.0.1]) by alsa0.perex.cz (Postfix) with ESMTP id 310DF2679F4; Mon, 22 Oct 2018 21:17:27 +0200 (CEST) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa0.perex.cz (Postfix, from userid 1000) id D88B2267A95; Mon, 22 Oct 2018 21:17:23 +0200 (CEST) Received: from vie01a-dmta-pe05-2.mx.upcmail.net (vie01a-dmta-pe05-2.mx.upcmail.net [84.116.36.12]) by alsa0.perex.cz (Postfix) with ESMTP id 6125C2677D0 for ; Mon, 22 Oct 2018 21:17:21 +0200 (CEST) Received: from [172.31.216.235] (helo=vie01a-pemc-psmtp-pe12.mail.upcmail.net) by vie01a-dmta-pe05.mx.upcmail.net with esmtp (Exim 4.88) (envelope-from ) id 1gEfhZ-0008IQ-0J for alsa-devel@alsa-project.org; Mon, 22 Oct 2018 21:17:21 +0200 Received: from helix.aillwee.com ([37.228.204.209]) by vie01a-pemc-psmtp-pe12.mail.upcmail.net with ESMTP id EfhXgbVGYkosQEfhXgxGDN; Mon, 22 Oct 2018 21:17:20 +0200 X-Env-Mailfrom: mikebrady@eircom.net X-Env-Rcptto: alsa-devel@alsa-project.org X-SourceIP: 37.228.204.209 X-CNFS-Analysis: v=2.3 cv=NNQEBHyg c=1 sm=1 tr=0 a=/+iDkf0alGTUGXENEoGzTg==:117 a=/+iDkf0alGTUGXENEoGzTg==:17 a=kj9zAlcOel0A:10 a=smKx5t2vBNcA:10 a=k7f7euTfAAAA:8 a=rj1QaQtciXKptpJ9f5IA:9 a=oUWUQTMoM2F0x7kr:21 a=T7wL1U6q3Ylreetz:21 a=CjuIK1q_8ugA:10 Received: from ubuntu (unknown [77.75.241.10]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by helix.aillwee.com (Postfix) with ESMTPSA id 08DCB4E607; Mon, 22 Oct 2018 20:17:17 +0100 (IST) Date: Mon, 22 Oct 2018 20:17:08 +0100 From: Mike Brady To: eric@anholt.net, stefan.wahren@i2se.com, gregkh@linuxfoundation.org Message-ID: <20181022191708.GA4659@ubuntu> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20181018105715.GA6660@ubuntu> User-Agent: Mutt/1.9.4 (2018-02-28) X-CMAE-Envelope: MS4wfGWHcEgnY9/MsLzWJIuIQewDpIpciBKOouwGp1jOu/KXst+TtbTKxd6osEMtDq/xlPqDiUTB7otsccDAXJO9DmPeVBGkojgqeAuqa39gUu/XuVm/uXVH CA/fm0bikC/B8ko/vHlHeCKgorTMFfmCboe7lrEnt4GccJbK/gVLq4ynsPUf2xNVxthWqpMoGQxPas7nwGMn7PS0F6kSyL0hyamIhdsHaxFXUPjJzIRR6F86 MhLvAN2vPm2DZ67/SEpPy6Q94UIDsD3ic8e95POTYCO9IulECwq53F6arICnjPBpLs4FzEflUKsRUW2le7tMO4ff6RH5HgUmK9CA6I4ks+m1MNbXHPwj0VWQ TymuhDarR9k3bYnbB5S6mAbrH9RLlsePWCJK2TJ/hXDRJ8kzpNN/1TPcmmtyffuMJVJjdcaqD5BCU1tHCIJBaDaTTfY89ElVakvyQ50hcMea8hk2XMHShXqV zR98vVciTCtLYAT4At7xXeA8gk9XkywjctIQSnA0cMz/emIh2HznzBDeoJzObbqqFhPEH6ru+J41eTMnX0wgKzo1nEC/kpgC9aBxRN8otnCRZZxgNNJvi1f7 lvISY1HklTjs+3ZebCn2jH6MAf0qGAveZxzVm6olZdNI29nxVw+DmMFN5ONq50NBrnY/ZXUby4BhNS13gyuzpA5q Cc: linux-arm-kernel@lists.infradead.org, devel@driverdev.osuosl.org, alsa-devel@alsa-project.org, f.fainelli@gmail.com, tiwai@suse.de, mikebrady@eircom.net, linux-kernel@vger.kernel.org, julia.lawall@lip6.fr, linux-rpi-kernel@lists.infradead.org, nishka.dasgupta_ug18@ashoka.edu.in, k.marinushkin@gmail.com Subject: [alsa-devel] [PATCH v2] staging: bcm2835-audio: interpolate audio delay X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: alsa-devel-bounces@alsa-project.org X-Virus-Scanned: ClamAV using ClamSMTP When the BCM2835 audio output is used, userspace sees a jitter up to 10ms in the audio position, aka "delay" -- the number of frames that must be output before a new frame would be played. Make this a bit nicer for userspace by interpolating the position using the CPU clock. The overhead is small -- an extra ktime_get() every time a GPU message is sent -- and another call and a few calculations whenever the delay is sought from userland. At 48,000 frames per second, i.e. approximately 20 microseconds per frame, it would take a clock inaccuracy of 20 microseconds in 10 milliseconds -- 2,000 parts per million -- to result in an inaccurate estimate, whereas crystal- or resonator-based clocks typically have an inaccuracy of 10s to 100s of parts per million. Signed-off-by: Mike Brady --- Changes in v2 -- remove inappropriate addition of SNDRV_PCM_INFO_BATCH flag .../vc04_services/bcm2835-audio/bcm2835-pcm.c | 20 +++++++++++++++++++ .../vc04_services/bcm2835-audio/bcm2835.h | 1 + 2 files changed, 21 insertions(+) diff --git a/drivers/staging/vc04_services/bcm2835-audio/bcm2835-pcm.c b/drivers/staging/vc04_services/bcm2835-audio/bcm2835-pcm.c index e66da11af5cf..9053b996cada 100644 --- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835-pcm.c +++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835-pcm.c @@ -74,6 +74,7 @@ void bcm2835_playback_fifo(struct bcm2835_alsa_stream *alsa_stream, atomic_set(&alsa_stream->pos, pos); alsa_stream->period_offset += bytes; + alsa_stream->interpolate_start = ktime_get(); if (alsa_stream->period_offset >= alsa_stream->period_size) { alsa_stream->period_offset %= alsa_stream->period_size; snd_pcm_period_elapsed(substream); @@ -243,6 +244,7 @@ static int snd_bcm2835_pcm_prepare(struct snd_pcm_substream *substream) atomic_set(&alsa_stream->pos, 0); alsa_stream->period_offset = 0; alsa_stream->draining = false; + alsa_stream->interpolate_start = ktime_get(); return 0; } @@ -292,6 +294,24 @@ snd_bcm2835_pcm_pointer(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime = substream->runtime; struct bcm2835_alsa_stream *alsa_stream = runtime->private_data; + ktime_t now = ktime_get(); + + /* Give userspace better delay reporting by interpolating between GPU + * notifications, assuming audio speed is close enough to the clock + * used for ktime + */ + + if ((ktime_to_ns(alsa_stream->interpolate_start)) && + (ktime_compare(alsa_stream->interpolate_start, now) < 0)) { + u64 interval = + (ktime_to_ns(ktime_sub(now, + alsa_stream->interpolate_start))); + u64 frames_output_in_interval = + div_u64((interval * runtime->rate), 1000000000); + snd_pcm_sframes_t frames_output_in_interval_sized = + -frames_output_in_interval; + runtime->delay = frames_output_in_interval_sized; + } return snd_pcm_indirect_playback_pointer(substream, &alsa_stream->pcm_indirect, diff --git a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.h b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.h index e13435d1c205..595ad584243f 100644 --- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.h +++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.h @@ -78,6 +78,7 @@ struct bcm2835_alsa_stream { unsigned int period_offset; unsigned int buffer_size; unsigned int period_size; + ktime_t interpolate_start; struct bcm2835_audio_instance *instance; int idx;