Skip to content

Commit

Permalink
system/nxrecorder: add amr offload record support
Browse files Browse the repository at this point in the history
Signed-off-by: jinxiuxu <jinxiuxu@xiaomi.com>
  • Loading branch information
jinxiuxu authored and xiaoxiang781216 committed Oct 27, 2023
1 parent 4439b24 commit 518b1ae
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 4 deletions.
45 changes: 45 additions & 0 deletions include/system/nxrecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
* Public Type Declarations
****************************************************************************/

struct nxrecorder_enc_ops_s
{
int format;
CODE int (*pre_write)(int fd, uint32_t samplerate,
uint8_t chans, uint8_t bps);
CODE int (*write_data)(int fd, struct ap_buffer_s *apb);
};

/* This structure describes the internal state of the NxRecorder */

struct nxrecorder_s
Expand All @@ -54,6 +62,8 @@ struct nxrecorder_s
#ifdef CONFIG_AUDIO_MULTI_SESSION
FAR void *session; /* Session assignment from device */
#endif

FAR const struct nxrecorder_enc_ops_s *ops;
};

typedef int (*nxrecorder_func)(FAR struct nxrecorder_s *precorder,
Expand Down Expand Up @@ -229,6 +239,41 @@ int nxrecorder_pause(FAR struct nxrecorder_s *precorder);
int nxrecorder_resume(FAR struct nxrecorder_s *precorder);
#endif

/****************************************************************************
* Name: nxrecorder_write_amr
*
* Performs pre-process when record amr file.
*
* Input Parameters:
* fd - recording file descriptor
* samplerate - sample rate
* chans - channels num
* bps - bit width
*
* Returned Value:
* OK if file writed successfully.
*
****************************************************************************/

int nxrecorder_write_amr(int fd, uint32_t samplerate,
uint8_t chans, uint8_t bps);

/****************************************************************************
* Name: nxrecorder_write_common
*
* Performs common function to write apb buffer to FILE
*
* Input Parameters:
* fd - recording file descriptor
* apb - apb buffer
*
* Returned Value:
* OK if apb buffer write successfully.
*
****************************************************************************/

int nxrecorder_write_common(int fd, FAR struct ap_buffer_s *apb);

#undef EXTERN
#ifdef __cplusplus
}
Expand Down
4 changes: 4 additions & 0 deletions system/nxrecorder/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ if(CONFIG_SYSTEM_NXRECORDER)
${CONFIG_NXRECORDER_MAINTHREAD_STACKSIZE})
endif()
target_sources(apps PRIVATE nxrecorder.c)

set(CSRCS nxrecorder.c nxrecorder_common.c nxrecorder_amr.c)

target_sources(apps PRIVATE ${CSRCS})
endif()
5 changes: 3 additions & 2 deletions system/nxrecorder/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ include $(APPDIR)/Make.defs

# NxRecorder Library

CSRCS = nxrecorder.c

CSRCS = nxrecorder.c
CSRCS += nxrecorder_amr.c
CSRCS += nxrecorder_common.c

ifneq ($(CONFIG_NXRECORDER_COMMAND_LINE),)
PROGNAME = nxrecorder
Expand Down
44 changes: 42 additions & 2 deletions system/nxrecorder/nxrecorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,29 @@ static const struct nxrecorder_ext_fmt_s g_known_ext[] =
{ "midi", AUDIO_FMT_MIDI, NULL },
#endif
#ifdef CONFIG_AUDIO_FORMAT_OGG_VORBIS
{ "ogg", AUDIO_FMT_OGG_VORBIS, NULL }
{ "ogg", AUDIO_FMT_OGG_VORBIS, NULL },
#endif
{ "amr", AUDIO_FMT_AMR, NULL }
};

static const int g_known_ext_count = sizeof(g_known_ext) /
sizeof(struct nxrecorder_ext_fmt_s);
#endif

static const struct nxrecorder_enc_ops_s g_enc_ops[] =
{
{
AUDIO_FMT_AMR,
nxrecorder_write_amr,
nxrecorder_write_common,
},
{
AUDIO_FMT_PCM,
NULL,
nxrecorder_write_common,
}
};

/****************************************************************************
* Private Functions
****************************************************************************/
Expand Down Expand Up @@ -353,7 +368,7 @@ static int nxrecorder_writebuffer(FAR struct nxrecorder_s *precorder,

/* Write data to the file. */

ret = write(precorder->fd, apb->samp, apb->nbytes);
ret = precorder->ops->write_data(precorder->fd, apb);
if (ret < 0)
{
return ret;
Expand Down Expand Up @@ -1019,6 +1034,7 @@ int nxrecorder_recordinternal(FAR struct nxrecorder_s *precorder,
int min_channels;
int ret;
int subfmt = AUDIO_FMT_UNDEF;
int index;

DEBUGASSERT(precorder != NULL);
DEBUGASSERT(pfilename != NULL);
Expand Down Expand Up @@ -1070,6 +1086,30 @@ int nxrecorder_recordinternal(FAR struct nxrecorder_s *precorder,
goto err_out_nodev;
}

for (index = 0; index < sizeof(g_enc_ops) / sizeof(g_enc_ops[0]); index++)
{
if (g_enc_ops[index].format == filefmt)
{
precorder->ops = &g_enc_ops[index];
break;
}
}

if (!precorder->ops)
{
goto err_out;
}

if (precorder->ops->pre_write)
{
ret = precorder->ops->pre_write(precorder->fd,
samprate, nchannels, bpsamp);
if (ret < 0)
{
goto err_out;
}
}

/* Try to reserve the device */

#ifdef CONFIG_AUDIO_MULTI_SESSION
Expand Down
67 changes: 67 additions & 0 deletions system/nxrecorder/nxrecorder_amr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/****************************************************************************
* apps/system/nxrecorder/nxrecorder_amr.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <sys/types.h>

#include <stdio.h>
#include <unistd.h>

#include <nuttx/audio/audio.h>

#include "system/nxrecorder.h"

/****************************************************************************
* Private Type Declarations
****************************************************************************/

static const uint8_t AMR_NB[6] = "#!AMR\n";
static const uint8_t AMR_WB[9] = "#!AMR-WB\n";

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: nxrecorder_record_amr
*
* nxrecorder_record_amr() add amr file header
*
****************************************************************************/

int nxrecorder_write_amr(int fd, uint32_t samplerate,
uint8_t chans, uint8_t bps)
{
const uint8_t *data;
int size;

if (samplerate != 8000 && samplerate != 16000)
{
return -EINVAL;
}

data = samplerate == 16000 ? &AMR_WB[0] : &AMR_NB[0];
size = samplerate == 16000 ? sizeof(AMR_WB) : sizeof(AMR_NB);

return write(fd, data, size);
}
67 changes: 67 additions & 0 deletions system/nxrecorder/nxrecorder_common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/****************************************************************************
* apps/system/nxrecorder/nxrecorder_common.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <sys/types.h>

#include <assert.h>
#include <debug.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <nuttx/audio/audio.h>

#include "system/nxrecorder.h"

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: nxrecorder_write_common
*
* Performs common function to write data to apb buffer
*
* Input Parameters:
* fd - recording file descriptor
* apb - ap buffer
*
* Returned Value:
* OK if apb buffer write successfully.
*
****************************************************************************/

int nxrecorder_write_common(int fd, FAR struct ap_buffer_s *apb)
{
int ret = 0;

ret = write(fd, apb->samp, apb->nbytes);
if (ret < 0)
{
auderr("ERROR: precorder write failed: %d\n", ret);
}

return ret;
}

0 comments on commit 518b1ae

Please sign in to comment.