From aaa7a7d2936e2f65eca6299f2338db14b7a3dd81 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Wed, 23 Jul 2014 07:46:49 -0600 Subject: NxPlayer: Check for read errors and end-of-file with nothing read --- apps/system/nxplayer/nxplayer.c | 79 ++++++++++++++++++++++++++++------------- 1 file changed, 55 insertions(+), 24 deletions(-) (limited to 'apps/system/nxplayer') diff --git a/apps/system/nxplayer/nxplayer.c b/apps/system/nxplayer/nxplayer.c index 514d6fdd8..fba3a4e68 100644 --- a/apps/system/nxplayer/nxplayer.c +++ b/apps/system/nxplayer/nxplayer.c @@ -480,54 +480,81 @@ static int nxplayer_mediasearch(FAR struct nxplayer_s *pPlayer, * ****************************************************************************/ -static int nxplayer_enqueuebuffer(struct nxplayer_s *pPlayer, - struct ap_buffer_s* pBuf) +static int nxplayer_enqueuebuffer(FAR struct nxplayer_s *pPlayer, + FAR struct ap_buffer_s* pBuf) { struct audio_buf_desc_s bufdesc; - int ret; + int ret; //auddbg("Entry: %p\n", pBuf); /* Validate the file is still open */ if (pPlayer->fileFd == NULL) - return OK; + { + return OK; + } /* Read data into the buffer. */ pBuf->nbytes = fread(&pBuf->samp, 1, pBuf->nmaxbytes, pPlayer->fileFd); if (pBuf->nbytes < pBuf->nmaxbytes) { + /* End of file or read error.. We are finished with this file in any + * event. + */ + fclose(pPlayer->fileFd); pPlayer->fileFd = NULL; + + /* Was this a file read error */ + + if (ferror(pPlayer->fileFd)) + { + int errcode = errno; + DEBUGASSERT(errcode > 0); + + auddbg("ERROR: fread failed: %d\n", errcode); + return errcode; + } } - /* Now enqueue the buffer with the audio device. If the number of bytes - * in the file happens to be an exact multiple of the audio buffer size, - * then we will receive the last buffer size = 0. We encode this buffer - * also so the audio system knows its the end of the file and can do - * proper cleanup. - */ + /* Do nothing more if this was the end-of-file with nothing read */ + + if (pBuf->nbytes > 0) + { + /* Now enqueue the buffer with the audio device. If the number of + * bytes in the file happens to be an exact multiple of the audio + * buffer size, then we will receive the last buffer size = 0. We + * encode this buffer also so the audio system knows its the end of + * the file and can do proper clean-up. + */ #ifdef CONFIG_AUDIO_MULTI_SESSION - bufdesc.session = pPlayer->session; + bufdesc.session = pPlayer->session; #endif - bufdesc.numbytes = pBuf->nbytes; - bufdesc.u.pBuffer = pBuf; - ret = ioctl(pPlayer->devFd, AUDIOIOC_ENQUEUEBUFFER, (unsigned long) - &bufdesc); - if (ret >= 0) - ret = OK; - else - ret = errno; + bufdesc.numbytes = pBuf->nbytes; + bufdesc.u.pBuffer = pBuf; - return ret; + ret = ioctl(pPlayer->devFd, AUDIOIOC_ENQUEUEBUFFER, + (unsigned long)&bufdesc); + if (ret < 0) + { + int errcode = errno; + DEBUGASSERT(errcode > 0); + + auddbg("ERROR: AUDIOIOC_ENQUEUEBUFFER ioctl failed: %d\n", errcode); + return errcode; + } + } + + return OK; } /**************************************************************************** * Name: nxplayer_thread_playthread * - * This is the thread that reads the audio file file and enqueue's / + * This is the thread that reads the audio file file and enqueues / * dequeues buffers to the selected and opened audio device. * ****************************************************************************/ @@ -627,9 +654,13 @@ static void *nxplayer_playthread(pthread_addr_t pvarg) /* Error encoding initial buffers or file is small */ if (x == 0) - running = false; + { + running = false; + } else - playing = false; + { + playing = false; + } break; } @@ -639,7 +670,7 @@ static void *nxplayer_playthread(pthread_addr_t pvarg) #ifdef CONFIG_AUDIO_MULTI_SESSION ret = ioctl(pPlayer->devFd, AUDIOIOC_START, - (unsigned long) pPlayer->session); + (unsigned long) pPlayer->session); #else ret = ioctl(pPlayer->devFd, AUDIOIOC_START, 0); #endif -- cgit v1.2.3