Received: by 2002:ac0:aa62:0:0:0:0:0 with SMTP id w31-v6csp909655ima; Wed, 24 Oct 2018 11:05:30 -0700 (PDT) X-Google-Smtp-Source: AJdET5eb9pfvc3NYEcCGDHlmKCOT9GBw7mD1P54HRY5DJqKcYR0h/uaMR686RiEFatiOt9yF72dO X-Received: by 2002:a65:40c2:: with SMTP id u2-v6mr3448560pgp.123.1540404330272; Wed, 24 Oct 2018 11:05:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540404330; cv=none; d=google.com; s=arc-20160816; b=o+2ELlHNJUTpv+xUzfZkace2tX88EARtxzij314TyKlKrqhI7/0QAAIw+BMXzMTMKt NaK0NAFYzjCc2ZuxdG4TOuQWghU+pc8sVC5V5odGnjO04o5CNTWAU/XuL5Hm7FJpnHMM 7hcW5edHnqtEti6zTrr2sgOAIig/zlvqYgbrdf09p5OkInifMFncyDL0ULZ+48hyuUSq bwD8cx/3OEHlTODIZuYdRv7Ayt7JUPJDIIt7vkXbdeTyRgmAwl1JonqwYaBzw0R5hcIs aR16x6zS+kwtYU8Q59HMJEbq4UGwaFPnr6icVxa4S+pIWW2O8jXKCk2yKcumSlFrRnVJ XgkA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=WY6TIVv81WhoZlHK4x/iJKFTYGnYqE4UtLZj0KUe0oQ=; b=kJqiqC2NiVbqtWdhOPEQ/I4PSK/HFWOzVP4Knua34N34bQhofpN3PE8ueaxYqTOm6n HCoqxGark0zpIKCAdWm/YEtezJIv1KiN97cJ+/wrGc9jzEYQhv72tajJ3uST21KqXb15 QcRd6o87KKXtlPQlJ54hJqvAORZNhOx4tVTFaGnKNyMFXHScf3l1MNt8xGwSTikw67on 54waRSzzhIBfVxXwtctYBr02LKmqt4svitC2iQpPfM3nepoalkt2jg9cm2twa8BzCmQ2 m3csUqoEPjwiHJcgKATNgHW671fxNJibwnH8LL5g2sH0+ZPp90K2rvBdmPmRYw0z+LEa kI1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=kWpFJerf; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w16-v6si5490687pge.9.2018.10.24.11.05.13; Wed, 24 Oct 2018 11:05:30 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=kWpFJerf; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727106AbeJYCdl (ORCPT + 99 others); Wed, 24 Oct 2018 22:33:41 -0400 Received: from mail-ed1-f68.google.com ([209.85.208.68]:40789 "EHLO mail-ed1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726742AbeJYCdl (ORCPT ); Wed, 24 Oct 2018 22:33:41 -0400 Received: by mail-ed1-f68.google.com with SMTP id r1-v6so5924629edd.7 for ; Wed, 24 Oct 2018 11:04:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=WY6TIVv81WhoZlHK4x/iJKFTYGnYqE4UtLZj0KUe0oQ=; b=kWpFJerfFmu9EqP1l2JnmdpXpRW3RGPBNv4pEmAqfv7FyLdXkJ36sKDfC29qVHZ3z3 eLYKnqyl70aRoNlXTX0L3jPThzbZVRv1SEAW0b9wpTJZNYxo44TZ8aahlOwplUfEBUX4 2FhGd8EabbmAmbJjPeFDS8C3E4p8bkJIEM+p/6OiQUXM71/OMr+6WgyoXg1DUGyMz6Ax 9TSO9F/ECFJgPMKJ/+ZglQUgK04enJr0IWGk8AjYE50sZ8H/eyvIvgVPXuDivDMO1fw3 zBoVNNvWJeTDoKY2vzy4rD/R/FqX6aUmIPdLKYjPVHd9K+LlZU4Bf+kZM66wNc/psmIy 2VSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=WY6TIVv81WhoZlHK4x/iJKFTYGnYqE4UtLZj0KUe0oQ=; b=bee9Mh4mioxlrDLM/AugAYbaCT3acAequMHTFAZttle0M4JzgjDBa20jSMv5a2FhqX wLnBYO7FoefspLaHFe8uyFlUNWJi50Z8YBNKWsIo7Sh48ebQIrHun+IQnMC7RkilcqkS diXAoQ9PRYrWtlCUlyeIDVA3X6a9udm5LpmkhlaYwV4P/IKVhphJfk1JifWFrD5ECw5E xKpsvbTIA5bEFUBITBbOTgbplN5FOIOZMBHV734OKOsiuQ3TZPWlQd0vUZPp4qGyRSwA kRAszrsbaeaounryQ1B0kGvZfc5cvtahq39aecvMcHkxqwZ7aYde8EaOrK4lUD1WmcI9 FHhw== X-Gm-Message-State: ABuFfojXoWrthVt7nTYhynfFFYB+s5w2Mr5k3XrJWjKQEe6JWPHbcHQa 5UTPZlMhMrYbApvKzHtSgxc= X-Received: by 2002:a05:6402:6d8:: with SMTP id n24mr18619925edy.99.1540404277030; Wed, 24 Oct 2018 11:04:37 -0700 (PDT) Received: from [192.168.1.3] (x4dbb3c80.dyn.telefonica.de. [77.187.60.128]) by smtp.gmail.com with ESMTPSA id c24-v6sm1954362ede.73.2018.10.24.11.04.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 24 Oct 2018 11:04:36 -0700 (PDT) Subject: Re: [alsa-devel] [PATCH v2] staging: bcm2835-audio: interpolate audio delay To: Mike Brady Cc: stefan.wahren@i2se.com, devel@driverdev.osuosl.org, alsa-devel@alsa-project.org, f.fainelli@gmail.com, julia.lawall@lip6.fr, tiwai@suse.de, gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, eric@anholt.net, linux-rpi-kernel@lists.infradead.org, nishka.dasgupta_ug18@ashoka.edu.in, linux-arm-kernel@lists.infradead.org References: <20181022191708.GA4659@ubuntu> From: Kirill Marinushkin Message-ID: <046aea96-e0d3-60f4-c61a-c26bb1b5c193@gmail.com> Date: Wed, 24 Oct 2018 20:06:18 +0200 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello Mike, On 10/24/18 10:20, Mike Brady wrote: > Hi Kirill. Thanks for your comments. > >> On 22 Oct 2018, at 23:25, Kirill Marinushkin wrote: >> >> AFAIU, this patch is wrong. Please correct me, maybe I misunderstand something. >> >>> The problem that this patch seeks to resolve is that when userland asks for >>> the delay >> >> The userspace asks not for delay, but for the pointer. > > The call in question is snd_pcm_delay. I presume this delay is calculated from knowledge of the stream position “pos", the period (buffer?) number (and period/buffer size) and the snd_pcm_runtime structure’s “delay" field (“runtime->delay”). > In kernel, this delay is calculated in `snd_pcm_calc_delay()`, defined at `sound/core/pcm_native.c:889`. If you analyze how this calculation is done, you will see that the returned value is a summary of the following fields: * runtime->status->hw_ptr * runtime->buffer_size * runtime->control->appl_ptr * runtime->boundary * runtime->delay >> You modify the function, which is called `snd_bcm2835_pcm_pointer`. Here you are >> supposed to increase `alsa_stream->pos` with the proper offset. Instead, you >> imitate a delay, but in fact the delay is not increased. >> >> So, the proper solution should be to fix the reported pointer. > > I think there is a difficulty with this. The “pos” pointer looks to have to be modulo the buffer size. This causes a problem, as I see it, in that if the calculated (pos + interpolated delay in bytes) is longer than the buffer size There is no "interpolated delay". The concept of "interpolated delay" is incorrect. When you play sound - the pointer increments. But in this commit you increment the delay, as if sound doesn't play. >> As a result, >> userspace will recieve the correct delay, instead of these crazy 10 ms. > > Just to point out that with the proposed patch, it appears that the correct delay is being reported, (apart, possibly, from any delay originally set in the snd_pcm_delay field, as mentioned above). Then I would like to point out the alsa-lib function `snd_pcm_avail()` - it will return the wrong value. > > All the best, > Mike > >> FYI, there is >>> a discussion of the effects of a downstream equivalent of this suggested patch >>> at: >>> https://github.com/raspberrypi/firmware/issues/1026#issuecomment-415746016. >> >> Thank you for the link, it clarified for me what you try to achieve. >> >> On 10/22/18 21:17, Mike Brady wrote: >>> 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; >>> >> _______________________________________________ >> Alsa-devel mailing list >> Alsa-devel@alsa-project.org >> http://mailman.alsa-project.org/mailman/listinfo/alsa-devel >