From nobody Thu Dec 18 07:58:10 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1686239855; cv=none; d=zohomail.com; s=zohoarc; b=GB5vt8See8iadVI/2v2f9Qo1qGLvMrqYtmpHQfstYMKIf/CvGVmyWDSh0v6stc/1hX70vKdTYXGvZBVHzq2H7u/nE/XkK/sMhjQrDIAyeNi5Lx1F9ETD7X4Mre998/hfAM2jfU91lc7WBg/Ow5RUoP7cEcyUJbYhhRs5AZWyJ1o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686239855; h=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; bh=8+St/cNjhfZOYAVPddWdTAJq1qrsE4cJ+BFd3Ppf/lg=; b=b8RL4LVrHNE4Yv+o8xUesoCCGN/NpXZxRb7h0o95SmWzs7VD2o+v0UOO2/QbC0N7/QvF29cR2OMgStHPoWRP2aDOJqqKMyM8LV3FnQ1bTqYty9UBmBTYGyEBte8yVOajH7+tmESP4YAdUZoHh7xeVJPnU1SgSIcaDZlIMfwwl5c= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686239855207579.7108769614616; Thu, 8 Jun 2023 08:57:35 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q7I0O-0005bk-Ey; Thu, 08 Jun 2023 11:56:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q7I0L-0005a0-1q for qemu-devel@nongnu.org; Thu, 08 Jun 2023 11:56:54 -0400 Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1q7I0I-0000Wm-0O for qemu-devel@nongnu.org; Thu, 08 Jun 2023 11:56:52 -0400 Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-3f7f6341bf9so7269785e9.1 for ; Thu, 08 Jun 2023 08:56:49 -0700 (PDT) Received: from localhost.localdomain (adsl-175.37.6.3.tellas.gr. [37.6.3.175]) by smtp.gmail.com with ESMTPSA id c17-20020adffb51000000b00307bc4e39e5sm1927105wrs.117.2023.06.08.08.56.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 08:56:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1686239808; x=1688831808; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8+St/cNjhfZOYAVPddWdTAJq1qrsE4cJ+BFd3Ppf/lg=; b=elbwwQ2f3lf+gNIDfwt3iSe1c5TXFIAXi66Oxqry9mmAautH4PyuS4mNbC9QwYPxbq g5Nxxz6SpHx+aDRp5yz3MYTH2vlGk8cWji2El1arDsGdnGepfd3WQNLxmNylMAv1Ps81 PmA1mS79ohFAEwq4v1B9BainJ+MuGpQGeW6ITl16XPCqV6mK55tOLL+7wFzfcwyxV7yN 79QCy9fO3T7uUTQfUmSL0hvM1InB7q2PA/4B62wr+lmH8C5mzRy6Op/NugvPR0fRN7Ll 95gJfzljAQX3uC6TuQ95iuFwN5cJtbAhSx5Sk+W+gA3RPUj8a8AGT64nnhutOykiI61f 62Hg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686239808; x=1688831808; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8+St/cNjhfZOYAVPddWdTAJq1qrsE4cJ+BFd3Ppf/lg=; b=BEK7VpYtChObopOTbYDh8rLxU48k9P349aa8FHQSZ3l/EFQzSkrTWHz9eqlthkXdYV 8r3z6VjjGYD8mPixc5bwQh+MG5yu01KDp6NIZnuABZADSO0HNbTQWV73j2MFMdeeE7ew uR2PpYG2L/P3y8ITVyDzX3UaLIiT1q41YfjeeHFx5X8PirVdrohn3IWo4tOy3GL+XdDX n7Ja9kobID/jQwRSxXg3+IpjwJzynfYlKvAOr5AMTqyBtXMNkx2okSIKL8eljsDmSOwv 0hFJOqmYjep6pl5jGMCcXnxQUVoA56NA0AY1ItgbnWjEtdSAA41mVlahqKFsdr8YsFWA /DrQ== X-Gm-Message-State: AC+VfDz3nLd45Dz+/hZBwq+/BPXuj84uhM1ezVfRMT9UzO/st4CBFwFT WQ1a8akL3crhT8c9vxKElRvVv/PeJJ6Ypcjquqc= X-Google-Smtp-Source: ACHHUZ4PZDkAAKmAizdC2u7bMzYBAaacmmkl2Q5dZe03Q19K2cYQ1fVW4JM5nPZjcfWmEp7VLiw9pw== X-Received: by 2002:a05:600c:364b:b0:3f1:93c2:4df7 with SMTP id y11-20020a05600c364b00b003f193c24df7mr1656483wmq.5.1686239808244; Thu, 08 Jun 2023 08:56:48 -0700 (PDT) From: Manos Pitsidianakis To: qemu-devel@nongnu.org Cc: Manos Pitsidianakis , Igor Skalkin , Anton Yakovlev Subject: [PATCH v2 3/3] Implement audio capture in virtio-snd device Date: Thu, 8 Jun 2023 18:56:19 +0300 Message-Id: <4c130f066d87fd9dce82dfdae7c4bb3b2fbc5ffd.1686238728.git.manos.pitsidianakis@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.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; Received-SPF: pass client-ip=2a00:1450:4864:20::333; envelope-from=manos.pitsidianakis@linaro.org; helo=mail-wm1-x333.google.com X-Spam_score_int: -5 X-Spam_score: -0.6 X-Spam_bar: / X-Spam_report: (-0.6 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_SORBS_WEB=1.5, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1686239856673100003 Content-Type: text/plain; charset="utf-8" Signed-off-by: Emmanouil Pitsidianakis --- hw/virtio/trace-events | 3 +- hw/virtio/virtio-snd.c | 239 +++++++++++++++++++++++++++++++++++------ 2 files changed, 206 insertions(+), 36 deletions(-) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index b77d78abdc..b58c007297 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -162,7 +162,8 @@ virtio_snd_handle_pcm_set_params(int stream) "VIRTIO_SN= D_PCM_SET_PARAMS called f virtio_snd_handle_pcm_start_stop(const char *code, int stream) "%s called = for stream %d" virtio_snd_handle_pcm_release(int stream) "VIRTIO_SND_PCM_RELEASE called f= or stream %d" virtio_snd_handle_chmap_info(void) "VIRTIO_SND_CHMAP_INFO called" -virtio_snd_handle_xfer(void) "tx/rx queue callback called" +virtio_snd_handle_tx_xfer(void) "tx queue callback called" +virtio_snd_handle_rx_xfer(void) "rx queue callback called" virtio_snd_handle_event(void) "event queue callback called" virtio_snd_realize(void *snd) "snd %p: realize" virtio_snd_unrealize(void *snd) "snd %p: unrealize" diff --git a/hw/virtio/virtio-snd.c b/hw/virtio/virtio-snd.c index e3b6c353ad..fe7d70ff06 100644 --- a/hw/virtio/virtio-snd.c +++ b/hw/virtio/virtio-snd.c @@ -25,7 +25,7 @@ #define VIRTIO_SOUND_VM_VERSION 1 =20 #define VIRTIO_SOUND_JACK_DEFAULT 0 -#define VIRTIO_SOUND_STREAM_DEFAULT 1 +#define VIRTIO_SOUND_STREAM_DEFAULT 2 #define VIRTIO_SOUND_CHMAP_DEFAULT 0 =20 #define VIRTIO_SOUND_HDA_FN_NID 0 @@ -89,12 +89,16 @@ virtio_snd_set_config(VirtIODevice *vdev, const uint8_t= *config) } =20 static void virtio_snd_process_cmdq(VirtIOSound *s); -static void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream); +static void virtio_snd_pcm_out_flush(VirtIOSoundPCMStream *stream); +static void virtio_snd_pcm_in_flush(VirtIOSoundPCMStream *stream); static void virtio_snd_pcm_out_cb(void *data, int available); -static uint32_t virtio_snd_pcm_read_write(VirtIOSoundPCMStream *stream, +static void virtio_snd_pcm_in_cb(void *data, int available); +static uint32_t virtio_snd_pcm_write(VirtIOSoundPCMStream *stream, VirtQueue *vq, - VirtQueueElement *element, - bool read); + VirtQueueElement *element); +static uint32_t virtio_snd_pcm_read(VirtIOSoundPCMStream *stream, + VirtQueue *vq, + VirtQueueElement *element); =20 /* * Get a specific stream from the virtio sound card device. @@ -450,15 +454,12 @@ static uint32_t virtio_snd_pcm_prepare_impl(VirtIOSou= nd *s, uint32_t stream_id) &as); =20 } else { - qemu_log_mask(LOG_UNIMP, "virtio_snd: input/capture is unimplement= ed."); - /* - * stream->voice.in =3D AUD_open_in(&s->card, - * stream->voice.in, - * "virtio_snd_card", - * stream, - * virtio_snd_input_cb, - * &as); - */ + stream->voice.in =3D AUD_open_in(&s->card, + stream->voice.in, + "virtio_snd_card", + stream, + virtio_snd_pcm_in_cb, + &as); } =20 stream->as =3D as; @@ -522,6 +523,8 @@ static void virtio_snd_handle_pcm_start_stop(VirtIOSoun= d *s, if (stream) { if (stream->direction =3D=3D VIRTIO_SND_D_OUTPUT) { AUD_set_active_out(stream->voice.out, start); + } else { + AUD_set_active_in(stream->voice.in, start); } } else { cmd->resp.code =3D VIRTIO_SND_S_BAD_MSG; @@ -573,7 +576,11 @@ static uint32_t virtio_snd_pcm_release_impl(VirtIOSoun= dPCMStream *stream, */ virtio_snd_process_cmdq(stream->s); trace_virtio_snd_pcm_stream_flush(stream_id); - virtio_snd_pcm_flush(stream); + if (stream->direction =3D=3D VIRTIO_SND_D_OUTPUT) { + virtio_snd_pcm_out_flush(stream); + } else { + virtio_snd_pcm_in_flush(stream); + } } =20 return VIRTIO_SND_S_OK; @@ -754,7 +761,7 @@ static void virtio_snd_handle_event(VirtIODevice *vdev,= VirtQueue *vq) * @vdev: VirtIOSound device * @vq: tx virtqueue */ -static void virtio_snd_handle_xfer(VirtIODevice *vdev, VirtQueue *vq) +static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq) { VirtIOSound *s =3D VIRTIO_SND(vdev); VirtIOSoundPCMStream *stream =3D NULL; @@ -763,7 +770,7 @@ static void virtio_snd_handle_xfer(VirtIODevice *vdev, = VirtQueue *vq) virtio_snd_pcm_xfer hdr; virtio_snd_pcm_status resp =3D { 0 }; =20 - trace_virtio_snd_handle_xfer(); + trace_virtio_snd_handle_tx_xfer(); =20 for (;;) { elem =3D virtqueue_pop(vq, sizeof(VirtQueueElement)); @@ -780,11 +787,11 @@ static void virtio_snd_handle_xfer(VirtIODevice *vdev= , VirtQueue *vq) stream =3D s->pcm->streams[hdr.stream_id]; assert(stream); =20 + assert(hdr.stream_id !=3D VIRTIO_SND_D_INPUT); WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { - virtio_snd_pcm_read_write(stream, + virtio_snd_pcm_write(stream, vq, - elem, - hdr.stream_id =3D=3D VIRTIO_SND_D_INPUT); + elem); =20 resp.status =3D VIRTIO_SND_S_OK; sz =3D iov_from_buf(elem->in_sg, @@ -803,6 +810,53 @@ static void virtio_snd_handle_xfer(VirtIODevice *vdev,= VirtQueue *vq) virtio_notify(VIRTIO_DEVICE(s), vq); } =20 +/* + * The rx virtqueue handler. Makes the buffers available to their respecti= ve + * streams for consumption. + * + * @vdev: VirtIOSound device + * @vq: tx virtqueue + */ +static void virtio_snd_handle_rx_xfer(VirtIODevice *vdev, VirtQueue *vq) +{ + VirtIOSound *s =3D VIRTIO_SND(vdev); + VirtIOSoundPCMStream *stream =3D NULL; + VirtQueueElement *elem; + size_t sz; + virtio_snd_pcm_xfer hdr; + + trace_virtio_snd_handle_rx_xfer(); + + for (;;) { + elem =3D virtqueue_pop(vq, sizeof(VirtQueueElement)); + if (!elem) { + break; + } + /* get the message hdr object */ + sz =3D iov_to_buf(elem->out_sg, + elem->out_num, + 0, + &hdr, + sizeof(hdr)); + assert(sz =3D=3D sizeof(hdr)); + stream =3D s->pcm->streams[hdr.stream_id]; + assert(stream); + + assert(hdr.stream_id =3D=3D VIRTIO_SND_D_INPUT); + WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { + virtio_snd_pcm_read(stream, vq, elem); + + } + } + + /* + * Notify vq about virtio_snd_pcm_status responses. + * Buffer responses must be notified separately later. + */ + virtio_notify(VIRTIO_DEVICE(s), vq); +} + + static uint64_t get_features(VirtIODevice *vdev, uint64_t features, Error **errp) { @@ -928,8 +982,8 @@ static void virtio_snd_realize(DeviceState *dev, Error = **errp) virtio_snd_common_realize(dev, virtio_snd_handle_ctrl, virtio_snd_handle_event, - virtio_snd_handle_xfer, - virtio_snd_handle_xfer, + virtio_snd_handle_tx_xfer, + virtio_snd_handle_rx_xfer, &err); if (err !=3D NULL) { error_propagate(errp, err); @@ -984,26 +1038,124 @@ static void virtio_snd_pcm_out_cb(void *data, int a= vailable) } =20 /* - * Flush all buffer data from this stream's queue into the driver's virtual - * queue. + * AUD_* input callback. * - * @stream: VirtIOSoundPCMStream *stream + * @data: VirtIOSoundPCMStream stream + * @available: number of bytes that can be read with AUD_read() */ -static void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream) +static void virtio_snd_pcm_in_cb(void *data, int available) { + VirtIOSoundPCMStream *stream =3D data; VirtIOSoundPCMBlock *block; - VirtIOSoundPCMBlock *next; + uint32_t sz; + virtio_snd_pcm_status resp =3D { 0 }; + int size; =20 WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { - QSIMPLEQ_FOREACH_SAFE(block, &stream->queue, entry, next) { - AUD_write(stream->voice.out, block->data, block->size); - virtqueue_push(block->vq, block->elem, sizeof(block->elem)); - virtio_notify(VIRTIO_DEVICE(stream->s), block->vq); - QSIMPLEQ_REMOVE(&stream->queue, block, VirtIOSoundPCMBlock, en= try); + while (!QSIMPLEQ_EMPTY(&stream->queue)) { + block =3D QSIMPLEQ_FIRST(&stream->queue); + assert(block !=3D NULL); + + for (;;) { + size =3D AUD_read(stream->voice.in, + block->data + block->offset, + MIN(stream->period_bytes - block->offset, availabl= e)); + block->offset +=3D size; + block->size +=3D size; + if (size =3D=3D 0 || block->size =3D=3D stream->period_byt= es) { + resp.status =3D VIRTIO_SND_S_OK; + sz =3D iov_from_buf(block->elem->in_sg, + block->elem->in_num, + 0, + &resp, + sizeof(resp)); + assert(sz =3D=3D sizeof(virtio_snd_pcm_status)); + + /* Copy data -if any- to guest */ + if (block->size) { + sz =3D iov_from_buf(block->elem->in_sg, + block->elem->in_num, + sz, + &block->data, + block->size); + assert(sz <=3D stream->period_bytes); + } + virtqueue_push(block->vq, + block->elem, + sizeof(block->elem)); + virtio_notify(VIRTIO_DEVICE(stream->s), + block->vq); + QSIMPLEQ_REMOVE_HEAD(&stream->queue, entry); + g_free(block); + available -=3D size; + break; + } + + available -=3D size; + if (!available) { + break; + } + } + if (!available) { + break; + } } } } =20 +#define virtio_snd_pcm_flush(AUD_CB) \ + VirtIOSoundPCMBlock *block; \ + VirtIOSoundPCMBlock *next; \ + WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { \ + QSIMPLEQ_FOREACH_SAFE(block, &stream->queue, entry, next) { \ + do { \ + AUD_CB; \ + } while (0) \ + ; \ + virtqueue_push(block->vq, block->elem, sizeof(block->elem));\ + virtio_notify(VIRTIO_DEVICE(stream->s), block->vq); \ + QSIMPLEQ_REMOVE(&stream->queue, \ + block, \ + VirtIOSoundPCMBlock, \ + entry); \ + } \ + } \ + + +/* + * Flush all buffer data from this output stream's queue into the driver's + * virtual queue. + * + * @stream: VirtIOSoundPCMStream *stream + */ +static void virtio_snd_pcm_out_flush(VirtIOSoundPCMStream *stream) +{ + virtio_snd_pcm_flush( + AUD_write(stream->voice.out, + block->data, + block->size); + ); +} + +/* + * Flush all buffer data from this input stream's queue into the driver's + * virtual queue. + * + * @stream: VirtIOSoundPCMStream *stream + */ +static void virtio_snd_pcm_in_flush(VirtIOSoundPCMStream *stream) +{ + uint32_t sz; + virtio_snd_pcm_flush( + sz =3D iov_from_buf(block->elem->in_sg, + block->elem->in_num, + sizeof(virtio_snd_pcm_info), + block->data, + block->offset); + assert(sz <=3D block->size); + ); +} + /* * Close the sound card. * @@ -1046,10 +1198,9 @@ static void virtio_snd_unrealize(DeviceState *dev) =20 =20 static uint32_t -virtio_snd_pcm_read_write(VirtIOSoundPCMStream *stream, +virtio_snd_pcm_write(VirtIOSoundPCMStream *stream, VirtQueue *vq, - VirtQueueElement *element, - bool read) + VirtQueueElement *element) { VirtIOSoundPCMBlock *fragment; size_t size =3D iov_size(element->out_sg, element->out_num) - @@ -1072,6 +1223,24 @@ virtio_snd_pcm_read_write(VirtIOSoundPCMStream *stre= am, return fragment->size; } =20 +static uint32_t +virtio_snd_pcm_read(VirtIOSoundPCMStream *stream, + VirtQueue *vq, + VirtQueueElement *element) +{ + VirtIOSoundPCMBlock *fragment; + + fragment =3D g_malloc0(sizeof(VirtIOSoundPCMBlock) + stream->period_by= tes); + fragment->elem =3D element; + fragment->vq =3D vq; + fragment->size =3D 0; + fragment->offset =3D 0; + + QSIMPLEQ_INSERT_TAIL(&stream->queue, fragment, entry); + + return fragment->size; +} + static void virtio_snd_reset(VirtIODevice *vdev) { VirtIOSound *s =3D VIRTIO_SND(vdev); --=20 2.39.2