Received: by 2002:ac0:aa62:0:0:0:0:0 with SMTP id w31-v6csp2805344ima; Mon, 22 Oct 2018 16:39:51 -0700 (PDT) X-Google-Smtp-Source: ACcGV63kr9KCmVsUhdIVHzbsNiZMhKSjvwetxK/fDmRihp0MvA4otHKkunlRY0rawvG0zmcusT9x X-Received: by 2002:a17:902:b706:: with SMTP id d6-v6mr46557788pls.53.1540251590989; Mon, 22 Oct 2018 16:39:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540251590; cv=none; d=google.com; s=arc-20160816; b=s8SZRGyEK4DacBgGCOWpAIgw8ciyRvSkPnfW26bqN9dDrIVJTbWeWGIq4WcKYaVrKk GE9114xRd+TQoh100Odsk8vNzzIjj/UYp0GxABMvzEH2yPQfSg/iDm2egXwKk0BZIj0m Gfn62+Gzk2MqumkBI7jKXfT/E0hEimn6uhNr7kJlo6VMEqx9pCricZ6s1GAzU85BgS0C X8PXwc26hgfh+d7y38xqUhMQKnkdqahNyT5ZrkzkPDCDt80/7nxnEtfbGqsQsunpu4aZ kFgPDUX6mBFnMqJngzacV71it0W/TA1jmA1mfK0lCl3PAMzNHr5k0Rk5YR5YjDUyichN UwHw== 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=7FQiy3sQG0LtXMgoaOdjUeATeJRuS+E1psgYa/NO8/4=; b=jCYqA3txKOK/suZrVw3fswzzYyysxvLyUR/mjP6ew+GzFHMSoD0cMajNnVdKMatURx U8vRBSc+wsMWBUKOlsj48Hkr/JngJo+ndyDK9llDv/HYpjJpXzVlTPkMi4WcZgzyKx6q 6+FS08r6WZTTGPH77Q3oeuQxpWSMVC73JdohI4g+2QmDi+fNGPvKdSDk0Iv1V3VNUWOg GkEc6MZB/N7mvjxjpX0Pfp0pVS1km+Pdv4fPmuw+i17T3bpT0ejNwXcGNjdixnPuPTrD Urw07qB5SrZIDw+KwMGrekzlKD4PQe/Fr2OkLcPFDanPO0WKZ0KYZAeFg9qscs3IiUi1 xRyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=tGIWPAMo; 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 z1-v6si37736633pfc.11.2018.10.22.16.39.35; Mon, 22 Oct 2018 16:39:50 -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=tGIWPAMo; 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 S1729114AbeJWGoA (ORCPT + 99 others); Tue, 23 Oct 2018 02:44:00 -0400 Received: from mail-ed1-f65.google.com ([209.85.208.65]:45283 "EHLO mail-ed1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728921AbeJWGoA (ORCPT ); Tue, 23 Oct 2018 02:44:00 -0400 Received: by mail-ed1-f65.google.com with SMTP id t10-v6so6734115eds.12 for ; Mon, 22 Oct 2018 15:23:34 -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=7FQiy3sQG0LtXMgoaOdjUeATeJRuS+E1psgYa/NO8/4=; b=tGIWPAMoI7whVes8hJIAy3H1GShJ1IikQSc34+Ltg1rRwg8hqAAuUTBEOWfGvH8yiZ q2tHQfvbcPXsS4PRQrkyD7ItmljdzeTMPmSKft5G0HVSEDFCv6qTMGujeAK8L8/Nkd0q G1mmPM1I0kOHyUWN42qpDQsI0NEHac9oatemVYAP4xdcmvtXVRCr2KSwrk6tI0UErmoO k5M1gDjd4aPXDUTgRC+3foQFpnvsdtGY5fBOeVyjIZP5QGZAO8aUGoRlpJnTuZfvigQD BdEK30nOEnMtDZpMbEqmMPnDPgdlJkQ1oeRSIaPTHiEMRM0emukMf7Hdr7v1Fl3+JWIl +TjA== 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=7FQiy3sQG0LtXMgoaOdjUeATeJRuS+E1psgYa/NO8/4=; b=ByEbMRhmo8iE+3+a33nW0rg5ZXazBB/bbawZ6FA/0Ckm64Ar5hOj083YMlBxQVqMpV N3uy4OgDK/cUfL+8HxX/6WE/bs08AtEYLZkrmfH3ZMePYaq43Otf9+9WyA/7+RO1BsPu Wzayw9BeVGfC/eck0K12Y1/MEW7muqZDKskqrhQl9/VdRRB2UJ92mDe+8rXa4wNVecQB vFBqX+HTqB0GC6MOzFI0/ywGbSpuTl+Xt6MpofeyAMJhlHjQwcMSHhKOOtZ5699T7/5v /zO/YDsOOwW8sbDvfNcRNqICqMxpTpq9oS4gJDyQ4q8Qzdo0Axu8tRwkxilmGXGPOU4m xm9Q== X-Gm-Message-State: ABuFfogoCAg0M9dc1TI5q+2LD+H8Db7VTaX0m6kjYoee9oFAJ0bkUamX ihBArWb3aAwS5hwzvxRRVBZybPBYeD8= X-Received: by 2002:a17:906:d55a:: with SMTP id gk26-v6mr37240953ejb.209.1540247013524; Mon, 22 Oct 2018 15:23:33 -0700 (PDT) Received: from [192.168.1.3] (x4d0f6cdc.dyn.telefonica.de. [77.15.108.220]) by smtp.gmail.com with ESMTPSA id w14-v6sm627177eds.16.2018.10.22.15.23.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 22 Oct 2018 15:23:32 -0700 (PDT) Subject: Re: [PATCH v2] staging: bcm2835-audio: interpolate audio delay To: Mike Brady Cc: eric@anholt.net, stefan.wahren@i2se.com, gregkh@linuxfoundation.org, f.fainelli@gmail.com, tiwai@suse.de, nishka.dasgupta_ug18@ashoka.edu.in, julia.lawall@lip6.fr, linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org, devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org, alsa-devel@alsa-project.org References: <20181022191708.GA4659@ubuntu> From: Kirill Marinushkin Message-ID: Date: Tue, 23 Oct 2018 00:25:14 +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: <20181022191708.GA4659@ubuntu> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello Mike, 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. 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. As a result, userspace will recieve the correct delay, instead of these crazy 10 ms. > 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; >