From 09a3bc44b462246d49aa1f1afeaa767aa64a90f1 Mon Sep 17 00:00:00 2001 From: ZhongAn Date: Mon, 27 Aug 2018 08:04:48 -0600 Subject: [PATCH] system/nxplayer/nxplayer.c: Add playraw command --- include/system/nxplayer.h | 24 +++++++ system/nxplayer/nxplayer.c | 114 +++++++++++++++++++++++++++++--- system/nxplayer/nxplayer_main.c | 62 +++++++++++++++++ 3 files changed, 192 insertions(+), 8 deletions(-) diff --git a/include/system/nxplayer.h b/include/system/nxplayer.h index c4d1f5b8e..9425c3850 100644 --- a/include/system/nxplayer.h +++ b/include/system/nxplayer.h @@ -207,6 +207,30 @@ int nxplayer_setdevice(FAR struct nxplayer_s *pPlayer, int nxplayer_playfile(FAR struct nxplayer_s *pPlayer, FAR const char *filename, int filefmt, int subfmt); +/**************************************************************************** + * Name: nxplayer_playraw + * + * Plays the specified media file (from the filesystem) using the + * Audio system. If a preferred device has been set, that device + * will be used for the playback, otherwise the first suitable device + * found in the /dev/audio directory will be used. + * + * Input Parameters: + * pPlayer - Pointer to the context to initialize + * filename - Pointer to pathname of the file to play + * nchannels channel num + * bpsampe bit width + * samprate sample rate + * + * Returned Value: + * OK if file found, device found, and playback started. + * + ****************************************************************************/ + +int nxplayer_playraw(FAR struct nxplayer_s *pPlayer, + FAR const char *filename, uint8_t nchannels, + uint8_t bpsamp, uint32_t samprate); + /**************************************************************************** * Name: nxplayer_stop * diff --git a/system/nxplayer/nxplayer.c b/system/nxplayer/nxplayer.c index ef1d0de1a..d98b73939 100644 --- a/system/nxplayer/nxplayer.c +++ b/system/nxplayer/nxplayer.c @@ -1775,18 +1775,21 @@ int nxplayer_stop(FAR struct nxplayer_s *pPlayer) #endif /* CONFIG_AUDIO_EXCLUDE_STOP */ /**************************************************************************** - * Name: nxplayer_playfile + * Name: nxplayer_playinternal * - * nxplayer_playfile() tries to play the specified file using the Audio - * system. If a preferred device is specified, it will try to use that - * device otherwise it will perform a search of the Audio device files - * to find a suitable device. + * nxplayer_playinternal() tries to play the specified file/raw data + * using the Audio system. If a preferred device is specified, it will + * try to use that device otherwise it will perform a search of the Audio + * device files to find a suitable device. * * Input: * pPlayer Pointer to the initialized MPlayer context * pFilename Pointer to the filename to play * filefmt Format of the file or AUD_FMT_UNDEF if unknown / to be * determined by nxplayer_playfile() + * nchannels channels num (raw data playback needed) + * bpsamp bits pre sample (raw data playback needed) + * samplrate samplre rate (raw data playback needed) * * Returns: * OK File is being played @@ -1795,15 +1798,19 @@ int nxplayer_stop(FAR struct nxplayer_s *pPlayer) * -ENODEV No audio device suitable to play the media type * -ENOENT The media file was not found * + * ****************************************************************************/ -int nxplayer_playfile(FAR struct nxplayer_s *pPlayer, - FAR const char *pFilename, int filefmt, int subfmt) +static int nxplayer_playinternal(FAR struct nxplayer_s *pPlayer, + FAR const char *pFilename, int filefmt, + int subfmt, uint8_t nchannels, + uint8_t bpsamp, uint32_t samprate) { struct mq_attr attr; struct sched_param sparam; pthread_attr_t tattr; - void *value; + FAR void *value; + struct audio_caps_desc_s cap_desc; #ifdef CONFIG_NXPLAYER_INCLUDE_MEDIADIR char path[128]; #endif @@ -1922,6 +1929,21 @@ int nxplayer_playfile(FAR struct nxplayer_s *pPlayer, goto err_out; } + if (nchannels && samprate && bpsamp) + { +#ifdef CONFIG_AUDIO_MULTI_SESSION + cap_desc.session = pPlayer->session; +#endif + cap_desc.caps.ac_len = sizeof(struct audio_caps_s); + cap_desc.caps.ac_type = AUDIO_TYPE_OUTPUT; + cap_desc.caps.ac_channels = nchannels; + cap_desc.caps.ac_controls.hw[0] = samprate; + cap_desc.caps.ac_controls.b[3] = samprate >> 16; + cap_desc.caps.ac_controls.b[2] = bpsamp; + + ioctl(pPlayer->devFd, AUDIOIOC_CONFIGURE, (unsigned long)&cap_desc); + } + /* Create a message queue for the playthread */ attr.mq_maxmsg = 16; @@ -1997,6 +2019,82 @@ err_out_nodev: return ret; } +/**************************************************************************** + * Name: nxplayer_playfile + * + * nxplayer_playfile() tries to play the specified file using the Audio + * system. If a preferred device is specified, it will try to use that + * device otherwise it will perform a search of the Audio device files + * to find a suitable device. + * + * Input: + * pPlayer Pointer to the initialized MPlayer context + * pFilename Pointer to the filename to play + * filefmt Format of the file or AUD_FMT_UNDEF if unknown / to be + * determined by nxplayer_playfile() + * + * Returns: + * OK File is being played + * -EBUSY The media device is busy + * -ENOSYS The media file is an unsupported type + * -ENODEV No audio device suitable to play the media type + * -ENOENT The media file was not found + * + ****************************************************************************/ + +int nxplayer_playfile(FAR struct nxplayer_s *pPlayer, + FAR const char *pFilename, int filefmt, int subfmt) +{ + return nxplayer_playinternal(pPlayer, pFilename, filefmt, subfmt, 0, 0, 0); +} + +/**************************************************************************** + * Name: nxplayer_playraw + * + * nxplayer_playraw() tries to play the raw data file using the Audio + * system. If a preferred device is specified, it will try to use that + * device otherwise it will perform a search of the Audio device files + * to find a suitable device. + * + * Input: + * pPlayer Pointer to the initialized MPlayer context + * pFilename Pointer to the filename to play + * nchannels channel num + * bpsampe bit width + * samprate sample rate + * + * Returns: + * OK File is being played + * -EBUSY The media device is busy + * -ENOSYS The media file is an unsupported type + * -ENODEV No audio device suitable to play the media type + * -ENOENT The media file was not found + * + ****************************************************************************/ + +int nxplayer_playraw(FAR struct nxplayer_s *pPlayer, + FAR const char *pFilename, uint8_t nchannels, + uint8_t bpsamp, uint32_t samprate) +{ + if (nchannels == 0) + { + nchannels = 2; + } + + if (bpsamp == 0) + { + bpsamp = 16; + } + + if (samprate == 0) + { + samprate = 48000; + } + + return nxplayer_playinternal(pPlayer, pFilename, AUDIO_FMT_PCM, 0, + nchannels, bpsamp, samprate); +} + /**************************************************************************** * Name: nxplayer_setmediadir * diff --git a/system/nxplayer/nxplayer_main.c b/system/nxplayer/nxplayer_main.c index 30c85fff3..ad6a1765c 100644 --- a/system/nxplayer/nxplayer_main.c +++ b/system/nxplayer/nxplayer_main.c @@ -81,6 +81,7 @@ struct mp_cmd_s { static int nxplayer_cmd_quit(FAR struct nxplayer_s *pPlayer, char *parg); static int nxplayer_cmd_play(FAR struct nxplayer_s *pPlayer, char *parg); +static int nxplayer_cmd_playraw(FAR struct nxplayer_s *pPlayer, char *parg); #ifdef CONFIG_NXPLAYER_INCLUDE_SYSTEM_RESET static int nxplayer_cmd_reset(FAR struct nxplayer_s *pPlayer, char *parg); @@ -144,6 +145,7 @@ static struct mp_cmd_s g_nxplayer_cmds[] = { "mediadir", "path", nxplayer_cmd_mediadir, NXPLAYER_HELP_TEXT(Change the media directory) }, #endif { "play", "filename", nxplayer_cmd_play, NXPLAYER_HELP_TEXT(Play a media file) }, + { "playraw", "filename", nxplayer_cmd_playraw, NXPLAYER_HELP_TEXT(Play a raw data file) }, #ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME { "pause", "", nxplayer_cmd_pause, NXPLAYER_HELP_TEXT(Pause playback) }, #endif @@ -227,6 +229,66 @@ static int nxplayer_cmd_play(FAR struct nxplayer_s *pPlayer, char *parg) return ret; } +/**************************************************************************** + * Name: nxplayer_cmd_playraw + * + * nxplayer_cmd_play() plays the raw data file using the nxplayer + * context. + * + ****************************************************************************/ + +static int nxplayer_cmd_playraw(FAR struct nxplayer_s *pPlayer, char *parg) +{ + int ret; + int channels = 0; + int bpsamp = 0; + int samprate = 0; + char filename[128]; + + sscanf(parg, "%s %d %d %d", filename, &channels, &bpsamp, &samprate); + + /* Try to play the file specified */ + + ret = nxplayer_playraw(pPlayer, filename, channels, bpsamp, samprate); + + /* nxplayer_playfile returned values: + * + * OK File is being played + * -EBUSY The media device is busy + * -ENOSYS The media file is an unsupported type + * -ENODEV No audio device suitable to play the media type + * -ENOENT The media file was not found + */ + + switch (-ret) + { + case OK: + break; + + case ENODEV: + printf("No suitable Audio Device found\n"); + break; + + case EBUSY: + printf("Audio device busy\n"); + break; + + case ENOENT: + printf("File %s not found\n", filename); + break; + + case ENOSYS: + printf("Unknown audio format\n"); + break; + + default: + printf("Error playing file: %d\n", -ret); + break; + } + + return ret; +} + /**************************************************************************** * Name: nxplayer_cmd_volume *