From nobody Wed Nov 12 06:49:36 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 ARC-Seal: i=1; a=rsa-sha256; t=1568576057; cv=none; d=zoho.com; s=zohoarc; b=blBnLpfpOQB2BYBP5TA+q/PBMzkSHryOXy9pPey+l0ns8g+IjyAmBvmTJJsrm8IXxAtM2Ea3oQIaFn8Arm+KCKkef527u0T0ycnYpfGSu4UfqpxPOXAniiv/1WvgMMD1wbgl3KJYZiZ5/KcwQggt6D5FN30kOBF0lEulzXdK0d0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1568576057; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=y7iHNMJr/K6vEM5IKPVSAJe6YZjvyzTOTmHMWROPW74=; b=anjWimjX6QFNhTr+i6ymBByoP9INv3nswCxKhwbWeB3Bv3QZrOpTAoLCmcdKIPG5dvvZYIcuCYeXHVG1tb1r1LqIH9OlbmQNMf6Yef59ew8wMWAOFl+8nKbYY4H4bjhvqON1nVChdmNryBZ2m9h3VeOZw4x8cNDxSJSRbiRvxpk= ARC-Authentication-Results: i=1; mx.zoho.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 header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1568576057726723.27473962613; Sun, 15 Sep 2019 12:34:17 -0700 (PDT) Received: from localhost ([::1]:56624 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i9aHo-000778-5B for importer@patchew.org; Sun, 15 Sep 2019 15:34:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39980) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i9a7f-00060S-UK for qemu-devel@nongnu.org; Sun, 15 Sep 2019 15:23:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i9a7e-0001yO-8P for qemu-devel@nongnu.org; Sun, 15 Sep 2019 15:23:47 -0400 Received: from mail-ed1-x542.google.com ([2a00:1450:4864:20::542]:35755) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1i9a7d-0001y8-VZ for qemu-devel@nongnu.org; Sun, 15 Sep 2019 15:23:46 -0400 Received: by mail-ed1-x542.google.com with SMTP id f24so13626266edv.2 for ; Sun, 15 Sep 2019 12:23:45 -0700 (PDT) Received: from nullptr.home.dirty-ice.org (2a01-036c-0113-74ef-0000-0000-0000-0005.pool6.digikabel.hu. [2a01:36c:113:74ef::5]) by smtp.gmail.com with ESMTPSA id j20sm6480562edy.95.2019.09.15.12.23.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Sep 2019 12:23:44 -0700 (PDT) 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=y7iHNMJr/K6vEM5IKPVSAJe6YZjvyzTOTmHMWROPW74=; b=IZSaHi+W3pD1RNOLTWo9ZJRs51HNYSf2kZ9YStSAdlvFXa6RzqwNJ5qz99SwUvthph 59AqL5EO6KlgclZe3nBBlntILRv++aVgarXyABRskjjvw7AX6dm+eJG2ESq15GdriApj H3iRv/DW9EVPFmMbGvs0OrXC+N98cg0wjaEbegj6xqujIlhdG2pxXXOMh/FRUotyCSV7 FdZ4QOg3Zu8YECURgNn862cSYAb9gII5LGOEAnbvG5MlqVtm9myU8LPfVEk6FsefhPG1 +ILyOFpSGWyvRIH+owVngp6Sk5cg8oTL50OmPJzgXvEEIjG3nl5m7upn5cr0auR2IR2w JfEA== 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=y7iHNMJr/K6vEM5IKPVSAJe6YZjvyzTOTmHMWROPW74=; b=qMQOBVw5Sj85mdvo6n5wOiFmM28jtA8DVMJR/bhaV2lC7fDFAJHuxlGS16D0V8JYGC tY5ZiPR6LaJ3/CifBv/HIeEFIIap8L1SnRgd9NissgGRneeMnkI6HK1FqEAhaF0pDMS6 TLLyCDk6Lyq0NvCiS48NwLCoCJPZ1Rds90dXl03AQIAN1hHSwYGh/BheBAj5Cd/4B56z mBq+s41k8W6VU9YM9UnNlHe3iJaATrr251SyMLPveZdjn62HHGXaX21GK5GMefJldSoS Gpdi2vq+Y6MIHCdDi3FlcGjfNFIDIT1ZLBvtoRTPW/sNkbZXo1EN7bkahHZRc54Izw0z S2Ng== X-Gm-Message-State: APjAAAUj0Pt4u9CkQoQrdQKUkoqpG2tBgzCHNp+SmVOJcO4rJ6KWoxeV AvIG2cRYpib5iVC2ejbPbwz/d5mY X-Google-Smtp-Source: APXvYqwUW5h2ZLxdUUxWGSPAZfp0VlzLp6+pVxDAbJG7N2dXCbQdNcX/R9BJHNENI8y7CryyfeTW4w== X-Received: by 2002:a17:906:c72d:: with SMTP id fj13mr49485918ejb.36.1568575424752; Sun, 15 Sep 2019 12:23:44 -0700 (PDT) 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: Sun, 15 Sep 2019 21:23:22 +0200 Message-Id: <44f4e888975c1d94f5d89e945df9782c0f541582.1568574965.git.DirtY.iCE.hu@gmail.com> X-Mailer: git-send-email 2.23.0 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::542 Subject: [Qemu-devel] [PATCH v3 06/24] ossaudio: port to the new audio backend api X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 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 | 272 +++++++++++++++++------------------------------ 1 file changed, 96 insertions(+), 176 deletions(-) diff --git a/audio/ossaudio.c b/audio/ossaudio.c index 1696933688..2782512706 100644 --- a/audio/ossaudio.c +++ b/audio/ossaudio.c @@ -40,19 +40,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; @@ -371,98 +367,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 - if (oss->mmapped) { - return; + err =3D ioctl(oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo); + if (err < 0) { + oss_logerr(errno, "SNDCTL_DSP_GETOPTR failed\n"); + return 0; } =20 - while (oss->pending) { - int samples_written; - 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); + return audio_ring_dist(cntinfo.ptr, oss->hw.pos_emul, oss->hw.size_emu= l); +} =20 - bytes_written =3D write (oss->fd, pcm, bytes_to_write); - if (bytes_written < 0) { - if (errno !=3D EAGAIN) { - oss_logerr (errno, "failed to write %d bytes\n", - bytes_to_write); - } - break; - } +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); + } +} =20 - if (bytes_written & hw->info.align) { - dolog ("misaligned write asked for %d, but got %zd\n", - bytes_to_write, bytes_written); - return; - } +static size_t oss_put_buffer_out(HWVoiceOut *hw, void *buf, size_t size) +{ + OSSVoiceOut *oss =3D (OSSVoiceOut *) hw; + if (oss->mmapped) { + assert(buf =3D=3D hw->buf_emul + hw->pos_emul && size < hw->size_e= mul); =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) { - break; - } + hw->pos_emul =3D (hw->pos_emul + size) % hw->size_emul; + return size; + } else { + return audio_generic_put_buffer_out(hw, buf, size); } } =20 -static size_t oss_run_out(HWVoiceOut *hw, size_t live) +static size_t oss_write(HWVoiceOut *hw, void *buf, size_t len) { 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; + size_t pos; =20 if (oss->mmapped) { - int bytes, pos; + size_t total_len; + len =3D MIN(len, oss_get_available_bytes(oss)); =20 - err =3D ioctl (oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo); - if (err < 0) { - oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n"); - return 0; - } + 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); =20 - 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; + hw->pos_emul =3D (hw->pos_emul + to_copy) % hw->pos_emul; + buf +=3D to_copy; + len -=3D to_copy; } + return total_len; + } =20 - if (abinfo.bytes > bufsize) { - trace_oss_invalid_available_size(abinfo.bytes, bufsize); - abinfo.bytes =3D bufsize; - } + pos =3D 0; + while (len) { + ssize_t bytes_written; + void *pcm =3D advance(buf, pos); =20 - if (abinfo.bytes < 0) { - trace_oss_invalid_available_size(abinfo.bytes, bufsize); - return 0; + bytes_written =3D write(oss->fd, pcm, len); + if (bytes_written < 0) { + if (errno !=3D EAGAIN) { + oss_logerr(errno, "failed to write %zu bytes\n", + len); + } + return pos; } =20 - decr =3D MIN (abinfo.bytes >> hw->info.shift, live); - if (!decr) { - return 0; + pos +=3D bytes_written; + if (bytes_written < len) { + break; } + len -=3D bytes_written; } - - 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) @@ -473,18 +458,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); - } - } - else { - g_free (oss->pcm_buf); + 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); } - oss->pcm_buf =3D NULL; + hw->buf_emul =3D NULL; } } =20 @@ -535,19 +515,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) { @@ -567,30 +548,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; @@ -618,7 +585,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 ( @@ -692,13 +659,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; @@ -710,78 +670,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 } - }; - - 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; - } + size_t pos =3D 0; =20 - 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); - - 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); - } + void *dst =3D advance(buf, pos); + nread =3D read(oss->fd, dst, len); =20 - 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, ...) @@ -845,12 +763,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.23.0