From nobody Fri Nov 7 03:57:01 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1547683375226777.1800533326748; Wed, 16 Jan 2019 16:02:55 -0800 (PST) Received: from localhost ([127.0.0.1]:37163 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjv94-0006eS-4C for importer@patchew.org; Wed, 16 Jan 2019 19:02:54 -0500 Received: from eggs.gnu.org ([209.51.188.92]:58402) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gjul9-0004RG-Nr for qemu-devel@nongnu.org; Wed, 16 Jan 2019 18:38:15 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gjul4-0003Sz-Qn for qemu-devel@nongnu.org; Wed, 16 Jan 2019 18:38:11 -0500 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]:32773) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gjul4-00039l-EV for qemu-devel@nongnu.org; Wed, 16 Jan 2019 18:38:06 -0500 Received: by mail-wr1-x444.google.com with SMTP id c14so9004925wrr.0 for ; Wed, 16 Jan 2019 15:37:54 -0800 (PST) Received: from nullptr.home.dirty-ice.org (2a01-036c-0113-e4b0-0000-0000-0000-0005.pool6.digikabel.hu. [2a01:36c:113:e4b0::5]) by smtp.gmail.com with ESMTPSA id s66sm30760437wmf.34.2019.01.16.15.37.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 16 Jan 2019 15:37:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Mcju0fxZG2CfmaqB9Sb0ZdZGxm957N2Vwz/TnTv2mas=; b=q/7bcYuv6GDFauOz6KmyLoOT9y8WL8zSOKUPbM3fC7lnE2TPjdoMZ5R8rPwluKU42C DQga4DpfmETyktPWWk3GqnOMhKJdB9P2VDnzLSWpV7LzQGWfYnrsn4yEZ0n0mrhrDC2/ gArhtSF6o0mmwNc4JNsrOHXyiagmcz02XMbBxu9PlBN3NdoLP/Ao31AzkKXriZyO7qJg 0JhnRtomfYi8o0kotk0mrQ0pYU2xfEQK7bcEo3olKBope5T29TyWP0pyZ26sYMhYaqFN oOeSdaU4BHErASqCLzWIjPSYddSCSTq7KZ2Igl4a2NtQaiBQvP22SbK85WTQtIsnwCHo xs/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Mcju0fxZG2CfmaqB9Sb0ZdZGxm957N2Vwz/TnTv2mas=; b=tvcO2r0h/bxuNRdOWwypQ1eb3jWOh3oJ+KydmyLWvqyNWvw+YRGnsXHIAgZoDprBVD C/rr6WKmfGqju7naSs9j3loXXRr3No1IsZMDi8DpApyYe1nSrMXw9pRFAT5Mz2qCJTsp mlaWTkWiRRPYoce8Va5NdMFunWYWsRob2g5RJPOEHDaDCXtWiL+KPHqzpOzZvvwbJagN 3Pj5e0TCFmGLBWGcIvkNZ6pXa2KCYvHaIsMH4u6sOYAZPkHAYF9ohtwQp5P9+cTSAWSV QC/Nn1sssIVItBikUfU83Nfxwz4Zs9AcGZ2stQBTSy12+jzzyqDtSDLlqZXTPjcBKUX1 B7Gg== X-Gm-Message-State: AJcUuke1aDeZhwmUcpMZvMH+1zqmQ2mn0rVXVwTKvgQGC9XvqTFKS9x6 nzjqGFoRsqTZB5dxax+vPkzt+O2FIos= X-Google-Smtp-Source: ALg8bN7zrcBOHNer4k1q6qiYsRUsQj0s9RqaXSSv2W6uKt1/D/gY0cQw5ihJvnWiO10rtPbAOkOneA== X-Received: by 2002:adf:8421:: with SMTP id 30mr9754172wrf.153.1547681873496; Wed, 16 Jan 2019 15:37:53 -0800 (PST) From: "=?UTF-8?q?K=C5=91v=C3=A1g=C3=B3=2C=20Zolt=C3=A1n?=" X-Google-Original-From: =?UTF-8?q?K=C5=91v=C3=A1g=C3=B3=2C=20Zolt=C3=A1n?= To: qemu-devel@nongnu.org Date: Thu, 17 Jan 2019 00:37:04 +0100 Message-Id: X-Mailer: git-send-email 2.20.1 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::444 Subject: [Qemu-devel] [PATCH v3 31/50] ossaudio: port to the new audio backend api X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Gerd Hoffmann Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Signed-off-by: K=C5=91v=C3=A1g=C3=B3, Zolt=C3=A1n --- audio/ossaudio.c | 288 +++++++++++++++++------------------------------ 1 file changed, 104 insertions(+), 184 deletions(-) diff --git a/audio/ossaudio.c b/audio/ossaudio.c index c6d4086573..774a41fbf9 100644 --- a/audio/ossaudio.c +++ b/audio/ossaudio.c @@ -39,19 +39,15 @@ =20 typedef struct OSSVoiceOut { HWVoiceOut hw; - void *pcm_buf; int fd; - int wpos; int nfrags; int fragsize; int mmapped; - int pending; Audiodev *dev; } OSSVoiceOut; =20 typedef struct OSSVoiceIn { HWVoiceIn hw; - void *pcm_buf; int fd; int nfrags; int fragsize; @@ -370,98 +366,87 @@ static int oss_open(int in, struct oss_params *req, a= udsettings *as, return -1; } =20 -static void oss_write_pending (OSSVoiceOut *oss) +static size_t oss_get_available_bytes(OSSVoiceOut *oss) { - HWVoiceOut *hw =3D &oss->hw; + int err; + struct count_info cntinfo; + assert(oss->mmapped); =20 + err =3D ioctl (oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo); + if (err < 0) { + oss_logerr(errno, "SNDCTL_DSP_GETOPTR failed\n"); + return 0; + } + + return audio_ring_dist(cntinfo.ptr, oss->hw.pos_emul, oss->hw.size_emu= l); +} + +static void *oss_get_buffer_out(HWVoiceOut *hw, size_t *size) +{ + OSSVoiceOut *oss =3D (OSSVoiceOut *) hw; + if (oss->mmapped) { + *size =3D MIN(oss_get_available_bytes(oss), hw->size_emul - hw->po= s_emul); + return hw->buf_emul + hw->pos_emul; + } else { + return audio_generic_get_buffer_out(hw, size); + } +} + +static size_t oss_put_buffer_out(HWVoiceOut *hw, void *buf, size_t size) +{ + OSSVoiceOut *oss =3D (OSSVoiceOut *) hw; if (oss->mmapped) { - return; + assert(buf =3D=3D hw->buf_emul + hw->pos_emul && size < hw->size_e= mul); + + hw->pos_emul =3D (hw->pos_emul + size) % hw->size_emul; + return size; + } else { + return audio_generic_put_buffer_out(hw, buf, size); + } +} + +static size_t oss_write(HWVoiceOut *hw, void *buf, size_t len) +{ + OSSVoiceOut *oss =3D (OSSVoiceOut *) hw; + size_t pos; + + if (oss->mmapped) { + size_t total_len; + len =3D MIN(len, oss_get_available_bytes(oss)); + + total_len =3D len; + while (len) { + size_t to_copy =3D MIN(len, hw->size_emul - hw->pos_emul); + memcpy(hw->buf_emul + hw->pos_emul, buf, to_copy); + + hw->pos_emul =3D (hw->pos_emul + to_copy) % hw->pos_emul; + buf +=3D to_copy; + len -=3D to_copy; + } + return total_len; } =20 - while (oss->pending) { - int samples_written; + pos =3D 0; + while (len) { ssize_t bytes_written; - int samples_till_end =3D hw->samples - oss->wpos; - int samples_to_write =3D MIN (oss->pending, samples_till_end); - int bytes_to_write =3D samples_to_write << hw->info.shift; - void *pcm =3D advance (oss->pcm_buf, oss->wpos << hw->info.shift); + void *pcm =3D advance(buf, pos); =20 - bytes_written =3D write (oss->fd, pcm, bytes_to_write); + bytes_written =3D write(oss->fd, pcm, len); if (bytes_written < 0) { if (errno !=3D EAGAIN) { - oss_logerr (errno, "failed to write %d bytes\n", - bytes_to_write); + oss_logerr(errno, "failed to write %zu bytes\n", + len); } - break; - } - - if (bytes_written & hw->info.align) { - dolog ("misaligned write asked for %d, but got %zd\n", - bytes_to_write, bytes_written); - return; + return pos; } =20 - samples_written =3D bytes_written >> hw->info.shift; - oss->pending -=3D samples_written; - oss->wpos =3D (oss->wpos + samples_written) % hw->samples; - if (bytes_written - bytes_to_write) { + pos +=3D bytes_written; + if (bytes_written < len) { break; } + len -=3D bytes_written; } -} - -static size_t oss_run_out(HWVoiceOut *hw, size_t live) -{ - OSSVoiceOut *oss =3D (OSSVoiceOut *) hw; - int err; - size_t decr; - struct audio_buf_info abinfo; - struct count_info cntinfo; - size_t bufsize; - - bufsize =3D hw->samples << hw->info.shift; - - if (oss->mmapped) { - int bytes, pos; - - err =3D ioctl (oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo); - if (err < 0) { - oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n"); - return 0; - } - - pos =3D hw->rpos << hw->info.shift; - bytes =3D audio_ring_dist (cntinfo.ptr, pos, bufsize); - decr =3D MIN (bytes >> hw->info.shift, live); - } - else { - err =3D ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &abinfo); - if (err < 0) { - oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n"); - return 0; - } - - if (abinfo.bytes > bufsize) { - trace_oss_invalid_available_size(abinfo.bytes, bufsize); - abinfo.bytes =3D bufsize; - } - - if (abinfo.bytes < 0) { - trace_oss_invalid_available_size(abinfo.bytes, bufsize); - return 0; - } - - decr =3D MIN (abinfo.bytes >> hw->info.shift, live); - if (!decr) { - return 0; - } - } - - decr =3D audio_pcm_hw_clip_out (hw, oss->pcm_buf, decr, oss->pending); - oss->pending +=3D decr; - oss_write_pending (oss); - - return decr; + return pos; } =20 static void oss_fini_out (HWVoiceOut *hw) @@ -472,18 +457,13 @@ static void oss_fini_out (HWVoiceOut *hw) ldebug ("oss_fini\n"); oss_anal_close (&oss->fd); =20 - if (oss->pcm_buf) { - if (oss->mmapped) { - err =3D munmap (oss->pcm_buf, hw->samples << hw->info.shift); - if (err) { - oss_logerr(errno, "Failed to unmap buffer %p, size %zu\n", - oss->pcm_buf, hw->samples << hw->info.shift); - } + if (oss->mmapped && hw->buf_emul) { + err =3D munmap(hw->buf_emul, hw->size_emul); + if (err) { + oss_logerr(errno, "Failed to unmap buffer %p, size %zu\n", + hw->buf_emul, hw->size_emul); } - else { - g_free (oss->pcm_buf); - } - oss->pcm_buf =3D NULL; + hw->buf_emul =3D NULL; } } =20 @@ -534,19 +514,20 @@ static int oss_init_out(HWVoiceOut *hw, struct audset= tings *as, =20 oss->mmapped =3D 0; if (oopts->has_try_mmap && oopts->try_mmap) { - oss->pcm_buf =3D mmap ( + hw->size_emul =3D hw->samples << hw->info.shift; + hw->buf_emul =3D mmap ( NULL, - hw->samples << hw->info.shift, + hw->size_emul, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 ); - if (oss->pcm_buf =3D=3D MAP_FAILED) { + if (hw->buf_emul =3D=3D MAP_FAILED) { oss_logerr(errno, "Failed to map %zu bytes of DAC\n", - hw->samples << hw->info.shift); - } - else { + hw->size_emul); + hw->buf_emul =3D NULL; + } else { int err; int trig =3D 0; if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) { @@ -566,30 +547,16 @@ static int oss_init_out(HWVoiceOut *hw, struct audset= tings *as, } =20 if (!oss->mmapped) { - err =3D munmap (oss->pcm_buf, hw->samples << hw->info.shif= t); + err =3D munmap(hw->buf_emul, hw->size_emul); if (err) { oss_logerr(errno, "Failed to unmap buffer %p size %zu\= n", - oss->pcm_buf, hw->samples << hw->info.shift= ); + hw->buf_emul, hw->size_emul); } + hw->buf_emul =3D NULL; } } } =20 - if (!oss->mmapped) { - oss->pcm_buf =3D audio_calloc(__func__, - hw->samples, - 1 << hw->info.shift); - if (!oss->pcm_buf) { - dolog ( - "Could not allocate DAC buffer (%zu samples, each %d bytes= )\n", - hw->samples, - 1 << hw->info.shift - ); - oss_anal_close (&fd); - return -1; - } - } - oss->fd =3D fd; oss->dev =3D dev; return 0; @@ -617,7 +584,7 @@ static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...) return 0; } =20 - audio_pcm_info_clear_buf (&hw->info, oss->pcm_buf, hw->samples= ); + audio_pcm_info_clear_buf(&hw->info, hw->buf_emul, hw->samples); trig =3D PCM_ENABLE_OUTPUT; if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) { oss_logerr ( @@ -691,13 +658,6 @@ static int oss_init_in(HWVoiceIn *hw, struct audsettin= gs *as, void *drv_opaque) } =20 hw->samples =3D (obt.nfrags * obt.fragsize) >> hw->info.shift; - oss->pcm_buf =3D audio_calloc(__func__, hw->samples, 1 << hw->info.shi= ft); - if (!oss->pcm_buf) { - dolog("Could not allocate ADC buffer (%zu samples, each %d bytes)\= n", - hw->samples, 1 << hw->info.shift); - oss_anal_close (&fd); - return -1; - } =20 oss->fd =3D fd; oss->dev =3D dev; @@ -709,78 +669,36 @@ static void oss_fini_in (HWVoiceIn *hw) OSSVoiceIn *oss =3D (OSSVoiceIn *) hw; =20 oss_anal_close (&oss->fd); - - g_free(oss->pcm_buf); - oss->pcm_buf =3D NULL; } =20 -static size_t oss_run_in(HWVoiceIn *hw) +static size_t oss_read(HWVoiceIn *hw, void *buf, size_t len) { OSSVoiceIn *oss =3D (OSSVoiceIn *) hw; - int hwshift =3D hw->info.shift; - int i; - size_t live =3D audio_pcm_hw_get_live_in (hw); - size_t dead =3D hw->samples - live; - size_t read_samples =3D 0; - struct { - size_t add; - size_t len; - } bufs[2] =3D { - { .add =3D hw->wpos, .len =3D 0 }, - { .add =3D 0, .len =3D 0 } - }; + size_t pos =3D 0; =20 - if (!dead) { - return 0; - } - - if (hw->wpos + dead > hw->samples) { - bufs[0].len =3D (hw->samples - hw->wpos) << hwshift; - bufs[1].len =3D (dead - (hw->samples - hw->wpos)) << hwshift; - } - else { - bufs[0].len =3D dead << hwshift; - } - - for (i =3D 0; i < 2; ++i) { + while (len) { ssize_t nread; =20 - if (bufs[i].len) { - void *p =3D advance (oss->pcm_buf, bufs[i].add << hwshift); - nread =3D read (oss->fd, p, bufs[i].len); + void *dst =3D advance(buf, pos); + nread =3D read(oss->fd, dst, len); =20 - if (nread > 0) { - if (nread & hw->info.align) { - dolog("warning: Misaligned read %zd (requested %zu), " - "alignment %d\n", nread, bufs[i].add << hwshift, - hw->info.align + 1); - } - read_samples +=3D nread >> hwshift; - hw->conv (hw->conv_buf + bufs[i].add, p, nread >> hwshift); - } - - if (bufs[i].len - nread) { - if (nread =3D=3D -1) { - switch (errno) { - case EINTR: - case EAGAIN: - break; - default: - oss_logerr( - errno, - "Failed to read %zu bytes of audio (to %p)\n", - bufs[i].len, p - ); - break; - } - } + if (nread =3D=3D -1) { + switch (errno) { + case EINTR: + case EAGAIN: + break; + default: + oss_logerr(errno, "Failed to read %zu bytes of audio (to %= p)\n", + len, dst); break; } } + + pos +=3D nread; + len -=3D nread; } =20 - hw->wpos =3D (hw->wpos + read_samples) % hw->samples; - return read_samples; + return pos; } =20 static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...) @@ -850,12 +768,14 @@ static void oss_audio_fini (void *opaque) static struct audio_pcm_ops oss_pcm_ops =3D { .init_out =3D oss_init_out, .fini_out =3D oss_fini_out, - .run_out =3D oss_run_out, + .write =3D oss_write, + .get_buffer_out =3D oss_get_buffer_out, + .put_buffer_out =3D oss_put_buffer_out, .ctl_out =3D oss_ctl_out, =20 .init_in =3D oss_init_in, .fini_in =3D oss_fini_in, - .run_in =3D oss_run_in, + .read =3D oss_read, .ctl_in =3D oss_ctl_in }; =20 --=20 2.20.1