Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp1763826imm; Thu, 18 Oct 2018 03:58:27 -0700 (PDT) X-Google-Smtp-Source: ACcGV60j7WBwkNm+fR9FRme92goPCyUla/hNeAHayTS2Wb3HHwSM2q1JPPf/ilXkuBTp3q7o2Nxx X-Received: by 2002:a17:902:aa48:: with SMTP id c8-v6mr30136624plr.100.1539860307696; Thu, 18 Oct 2018 03:58:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539860307; cv=none; d=google.com; s=arc-20160816; b=ZKMAt6l6E+FMDWA+ucYhwh5n1xlzzzmEClsRZ+l5fTEIXx3/dZi33Vyzng5ESDalRw d50H11ELQxf7UeMi3YgCe/s+Z2XmwUrtCsj1Ba7ssEPBKZoeifFsOrU0C8neHiSd7Wmg BxQcYA0DQG4JRCxQQkqd2JHBALIJhj+TcvLUNinTRDbFR8YV6otWB5nJlp21CCrhIasy Nz+9SH0vSmB5FcmvZTX04TWl0qriVLRmAqGzODZNzaNuY9chNZ0bwkIulNMvQcT663iJ N8rGfuFnNnUnEMVcYPL7N5cxGlhY2u7ChEe5JrYCqas2NYtcLDOhsSgN/iH8STGyX1iu DcxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date; bh=O6dX+zj2b1Ate2tNIOTgjx7+HBDEdB9mRtSM5BbfFpA=; b=rV6DqO3wtAKE4PPucY5+3OpAO8aj+nj2R0BDBA3RKt7zqc+1aKQRmqguNeG6CUb5Ea vlixxl3tTlPd3CiRtwWfkVqeybL24tYODuLUFbMHBwTxcvDYuu6ZAOTezz2bb0edqWtZ O2JT1YK1GItc11Yjq3eZX5vmg6M1E0QqOFf8SaBJI1imjfJUWkQ6pe7lMTOvPmwiBGd/ 2F6mu+Xmy43SksQ9pJ1EezLZbpD+zzSaYtoag/SAc76qW/i3kXernMOJDplHVeXFY/9b TpskdzcYBDdLI4YeVUijg1HyJZBF7/Krg459wgoZKqe1JyJGScBdegEKX1FFtYmexPGN CetA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g20-v6si20315810plq.192.2018.10.18.03.58.10; Thu, 18 Oct 2018 03:58:27 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727676AbeJRS54 (ORCPT + 99 others); Thu, 18 Oct 2018 14:57:56 -0400 Received: from vie01a-dmta-pe08-1.mx.upcmail.net ([84.116.36.20]:53633 "EHLO vie01a-dmta-pe08-1.mx.upcmail.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727453AbeJRS54 (ORCPT ); Thu, 18 Oct 2018 14:57:56 -0400 Received: from [172.31.216.235] (helo=vie01a-pemc-psmtp-pe12.mail.upcmail.net) by vie01a-dmta-pe08.mx.upcmail.net with esmtp (Exim 4.88) (envelope-from ) id 1gD5zb-00088p-0N for linux-kernel@vger.kernel.org; Thu, 18 Oct 2018 12:57:27 +0200 Received: from helix.aillwee.com ([37.228.204.209]) by vie01a-pemc-psmtp-pe12.mail.upcmail.net with ESMTP id D5zZg67FmkosQD5zZgtk2W; Thu, 18 Oct 2018 12:57:26 +0200 X-Env-Mailfrom: mikebrady@eircom.net X-Env-Rcptto: linux-kernel@vger.kernel.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=tsLZIYwQTnFd6Zqp:21 a=uTyBaVoy6FBSJHVO:21 a=CjuIK1q_8ugA:10 Received: from ubuntu (apu.aillwee.com [192.168.50.1]) (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 B43254E607; Thu, 18 Oct 2018 11:57:24 +0100 (IST) Date: Thu, 18 Oct 2018 11:57:15 +0100 From: Mike Brady To: eric@anholt.net, stefan.wahren@i2se.com, gregkh@linuxfoundation.org Cc: f.fainelli@gmail.com, tiwai@suse.de, nishka.dasgupta_ug18@ashoka.edu.in, julia.lawall@lip6.fr, mikebrady@eircom.net, k.marinushkin@gmail.com, linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org, devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org Subject: [PATCH] staging: bcm2835-audio: interpolate audio delay Message-ID: <20181018105715.GA6660@ubuntu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) X-CMAE-Envelope: MS4wfFPbtpHfqRe+r+/EJILl8WBnHKBt9JGnBXv0OLnDBc64jYCKIrfudhSvcI+LEU87Lq9QvX5fFxC4Pr9uiFTrtOHNd7QRlLH2iIsH1yNQ/mssFd91Uw+4 vvuxugONH7DEPD1TZI12fRpTNPV9nsBJzvaEuxkG8E0psebmLKPP8qmJ/rf+nrUW2gpBNXSf6tbnzZOIKmrLWdbazJdBEP1cfAI5yD1r7l1u4FStMwB5lWji b2T3SztI6Bh185Iyb7Vzzw5iYc0hVEgbytxOrJJcBdNLGe2LNKdKaAA9Cmn/D/Y5fhLVZQ8rG2p/B73vlndVFp5xdPxZi/cmCetTtB4H1ykj7ZS/e7l/Lg1x pylMFmHdBWf1uKLg6IZjEB0jKUJ9Zd1tpF9tfT1S+Wiv/dlgqA5iAFVHWaGqfY5y5ZWqdye75tEJ7Jf8KGsXKbL0UMGUXEeNvKRaWM4SpDg5LLv2ZgNCbPDv sJW9KwQajV9RszgopkjbWGzMx9JaBNYbCV7M9zyWA9HCtev5GB/ril54KwB47btffZp9z0lrmEOMSRPLRFsTy+j9pqNngLxQEMXU2J03J8Qn4DDpZ3Da37BP wc50bz79rdG1NjsnuVwYphaj4233cB71mT+SUzAY34kn78Bd5guShAb6oMdj4XvPswE= Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- .../vc04_services/bcm2835-audio/bcm2835-pcm.c | 23 ++++++++++++++++++- .../vc04_services/bcm2835-audio/bcm2835.h | 1 + 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/drivers/staging/vc04_services/bcm2835-audio/bcm2835-pcm.c b/drivers/staging/vc04_services/bcm2835-audio/bcm2835-pcm.c index e66da11af5cf..250adf82385a 100644 --- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835-pcm.c +++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835-pcm.c @@ -12,7 +12,8 @@ static const struct snd_pcm_hardware snd_bcm2835_playback_hw = { .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID | - SNDRV_PCM_INFO_DRAIN_TRIGGER | SNDRV_PCM_INFO_SYNC_APPLPTR), + SNDRV_PCM_INFO_BATCH | SNDRV_PCM_INFO_DRAIN_TRIGGER | + SNDRV_PCM_INFO_SYNC_APPLPTR), .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE, .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000, .rate_min = 8000, @@ -74,6 +75,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 +245,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 +295,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; -- 2.17.1