This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit d2dfa5867b769385ea5a4c9005b61f2354f37514
Author: fangyibo <[email protected]>
AuthorDate: Wed Nov 5 11:17:46 2025 +0800

    audio: add AUDIOIOC_DUMP to record the data of a device
    
    AUDIOIOC_DUMP takes a file path, and the upper half then writes every
    buffer the lower half returns into that file: the samples a playback
    buffer carried, before the buffer is cleared, or the samples a capture
    buffer received.  A NULL path stops the recording.  It shows whether a
    glitch is already in the data or comes from the driver.
    
    The write happens in audio_dequeuebuffer() before the spinlock is
    taken, and is skipped when a buffer is returned in interrupt context,
    since writing a file may block.
    
    Signed-off-by: fangyibo <[email protected]>
---
 audio/audio.c               | 55 +++++++++++++++++++++++++++++++++++++++++++++
 include/nuttx/audio/audio.h |  1 +
 2 files changed, 56 insertions(+)

diff --git a/audio/audio.c b/audio/audio.c
index 6ca3720516e..cd9ef5ce8af 100644
--- a/audio/audio.c
+++ b/audio/audio.c
@@ -95,6 +95,8 @@ struct audio_upperhalf_s
   FAR struct audio_lowerhalf_s *dev;     /* lower-half state */
   FAR struct audio_openpriv_s  *head;    /* Appl private info list */
   struct audio_status_s        *status;  /* lowerhalf driver status */
+  struct file                  file;     /* dump file */
+  bool                         dump;     /* dump started */
 };
 
 /****************************************************************************
@@ -866,6 +868,48 @@ static int audio_enqueuebuffer(FAR struct file *filep,
   return ret;
 }
 
+static int audio_dump(FAR struct audio_upperhalf_s *upper, FAR char *path)
+{
+  int ret = OK;
+
+  if (path == NULL)
+    {
+      if (upper->dump)
+        {
+          ret = file_close(&upper->file);
+          if (ret < 0)
+            {
+              return ret;
+            }
+
+          upper->dump = false;
+        }
+
+      return ret;
+    }
+
+  if (upper->dump)
+    {
+      ret = file_close(&upper->file);
+      if (ret < 0)
+        {
+          return ret;
+        }
+
+      upper->dump = false;
+    }
+
+  ret = file_open(&upper->file, path,
+                  O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, 0666);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  upper->dump = true;
+  return ret;
+}
+
 /****************************************************************************
  * Name: audio_ioctl
  *
@@ -1232,6 +1276,12 @@ static int audio_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
         }
         break;
 
+      case AUDIOIOC_DUMP:
+        {
+          ret = audio_dump(upper, (FAR char *)arg);
+        }
+        break;
+
       /* Any unrecognized IOCTL commands might be
        * platform-specific ioctl commands
        */
@@ -1437,6 +1487,11 @@ static inline void audio_dequeuebuffer(FAR struct 
audio_upperhalf_s *upper,
 
   audinfo("Entry\n");
 
+  if (upper->dump && !up_interrupt_context())
+    {
+      file_write(&upper->file, apb->samp, apb->nbytes);
+    }
+
   if (upper->info.type == AUDIO_TYPE_OUTPUT)
     {
       apb->nbytes = 0;
diff --git a/include/nuttx/audio/audio.h b/include/nuttx/audio/audio.h
index a199de999ad..1d44baa7c63 100644
--- a/include/nuttx/audio/audio.h
+++ b/include/nuttx/audio/audio.h
@@ -120,6 +120,7 @@
 #define AUDIOIOC_GETAUDIOINFO       _AUDIOIOC(22)
 #define AUDIOIOC_GETSTATUS          _AUDIOIOC(23)
 #define AUDIOIOC_RESETSTATUS        _AUDIOIOC(24)
+#define AUDIOIOC_DUMP               _AUDIOIOC(25)
 
 /* Audio Device Types *******************************************************/
 

Reply via email to