From patchwork Mon Jan 24 21:07:44 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Hoffmann X-Patchwork-Id: 80252 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 176C4B7107 for ; Tue, 25 Jan 2011 08:19:46 +1100 (EST) Received: from localhost ([127.0.0.1]:45887 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PhTp7-00053H-Ul for incoming@patchwork.ozlabs.org; Mon, 24 Jan 2011 16:19:42 -0500 Received: from [140.186.70.92] (port=38357 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PhTdq-0008RH-4v for qemu-devel@nongnu.org; Mon, 24 Jan 2011 16:08:36 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PhTdj-0006lq-GJ for qemu-devel@nongnu.org; Mon, 24 Jan 2011 16:08:00 -0500 Received: from mx1.redhat.com ([209.132.183.28]:12790) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PhTdj-0006lS-9A for qemu-devel@nongnu.org; Mon, 24 Jan 2011 16:07:55 -0500 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id p0OL7o4E030909 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 24 Jan 2011 16:07:51 -0500 Received: from rincewind.home.kraxel.org (vpn1-5-238.ams2.redhat.com [10.36.5.238]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p0OL7mhN029539; Mon, 24 Jan 2011 16:07:49 -0500 Received: by rincewind.home.kraxel.org (Postfix, from userid 500) id 02E01412A6; Mon, 24 Jan 2011 22:07:46 +0100 (CET) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Mon, 24 Jan 2011 22:07:44 +0100 Message-Id: <1295903266-6458-1-git-send-email-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: Gerd Hoffmann Subject: [Qemu-devel] [RESEND PATCH 1/3] pulseaudio: process 1/4 buffer max at once X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Limit the size of data pieces processed by the pulseaudio worker threads. Never ever process more than 1/4 of the buffer at once. Background: The buffer area currently processed by the pulseaudio thread is blocked, i.e. the main thread (or iothread) can't fill in more data there. The buffer processing time is roughly real-time due to the pa_simple_write() call blocking when the output queue to the pulse server is full. Thus processing big chunks at once means blocking a large part of the buffer for a long time. This brings high latency and can lead to dropouts. When processing the buffer in smaller chunks the rpos handling becomes a problem though. The thread reads hw->rpos without knowing whenever qpa_run_out has already seen the last (small) chunk processed and updated rpos accordingly. There is no point in reading hw->rpos though, pa->rpos can be used instead. We just need to take care to initialize pa->rpos before kicking the thread. Signed-off-by: Gerd Hoffmann --- audio/paaudio.c | 22 +++++++++------------- 1 files changed, 9 insertions(+), 13 deletions(-) diff --git a/audio/paaudio.c b/audio/paaudio.c index 9cf685d..858ca81 100644 --- a/audio/paaudio.c +++ b/audio/paaudio.c @@ -57,9 +57,6 @@ static void *qpa_thread_out (void *arg) { PAVoiceOut *pa = arg; HWVoiceOut *hw = &pa->hw; - int threshold; - - threshold = conf.divisor ? hw->samples / conf.divisor : 0; if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) { return NULL; @@ -73,7 +70,7 @@ static void *qpa_thread_out (void *arg) goto exit; } - if (pa->live > threshold) { + if (pa->live > 0) { break; } @@ -82,8 +79,8 @@ static void *qpa_thread_out (void *arg) } } - decr = to_mix = pa->live; - rpos = hw->rpos; + decr = to_mix = audio_MIN (pa->live, conf.samples >> 2); + rpos = pa->rpos; if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) { return NULL; @@ -110,8 +107,8 @@ static void *qpa_thread_out (void *arg) return NULL; } - pa->live = 0; pa->rpos = rpos; + pa->live -= decr; pa->decr += decr; } @@ -152,9 +149,6 @@ static void *qpa_thread_in (void *arg) { PAVoiceIn *pa = arg; HWVoiceIn *hw = &pa->hw; - int threshold; - - threshold = conf.divisor ? hw->samples / conf.divisor : 0; if (audio_pt_lock (&pa->pt, AUDIO_FUNC)) { return NULL; @@ -168,7 +162,7 @@ static void *qpa_thread_in (void *arg) goto exit; } - if (pa->dead > threshold) { + if (pa->dead > 0) { break; } @@ -177,8 +171,8 @@ static void *qpa_thread_in (void *arg) } } - incr = to_grab = pa->dead; - wpos = hw->wpos; + incr = to_grab = audio_MIN (pa->dead, conf.samples >> 2); + wpos = pa->wpos; if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) { return NULL; @@ -323,6 +317,7 @@ static int qpa_init_out (HWVoiceOut *hw, struct audsettings *as) audio_pcm_init_info (&hw->info, &obt_as); hw->samples = conf.samples; pa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift); + pa->rpos = hw->rpos; if (!pa->pcm_buf) { dolog ("Could not allocate buffer (%d bytes)\n", hw->samples << hw->info.shift); @@ -377,6 +372,7 @@ static int qpa_init_in (HWVoiceIn *hw, struct audsettings *as) audio_pcm_init_info (&hw->info, &obt_as); hw->samples = conf.samples; pa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift); + pa->wpos = hw->wpos; if (!pa->pcm_buf) { dolog ("Could not allocate buffer (%d bytes)\n", hw->samples << hw->info.shift);