Skip to content

Commit

Permalink
modules/audiolite: Fix not stop after decode done
Browse files Browse the repository at this point in the history
NuttX audio driver stops when it receives audio frame buffer with
EOF(end of frame) flag. MP3 decoder should set the flag when the
final decoded PCM data is sent, but it didn't.
This causes that no stop the playing after mp3 decode done.
  • Loading branch information
SPRESENSE committed Nov 13, 2024
1 parent 9c080bb commit cdb1dc1
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 14 deletions.
10 changes: 8 additions & 2 deletions sdk/modules/audiolite/src/base/al_stream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,21 @@ int audiolite_filestream::receive_data(audiolite_mem *mem,
int sz = mem->get_fullsize() - ofst;
int ret = 0;

mem->set_storedsize(0);
if (_fp && sz > 0)
{
ret = fread(&data[ofst], 1, sz, _fp);
if (sz != ret)
if (feof(_fp))
{
mem->set_eof();
fclose(_fp);
_fp = NULL;
}

mem->set_storedsize(ret);
if (ret >= 0)
{
mem->set_storedsize(ret);
}
}

return ret;
Expand Down
2 changes: 1 addition & 1 deletion sdk/modules/audiolite/src/base/al_workercmd.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,6 @@ int alworker_inject_omem(al_wtask_t *wtask, audiolite_mem *mem)
int alworker_inject_imem(al_wtask_t *wtask, audiolite_mem *mem)
{
return send_iframe(wtask, mem->get_data(),
mem->get_fullsize(),
mem->get_storedsize(),
mem->is_eof());
}
6 changes: 6 additions & 0 deletions sdk/modules/audiolite/src/components/al_mp3dec.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ int audiolite_mp3dec::handle_mesage(al_comm_msghdr_t hdr,
if (thiz->_worker_booted)
{
mem->set_storedsize(opt->size);
mem->clear_eof();
if (opt->eof != 0)
{
mem->set_eof();
}

thiz->_outs[0]->push_data(mem);
}
mem->release();
Expand Down
1 change: 0 additions & 1 deletion sdk/modules/audiolite/worker/ext_libs/minimp3/minimp3.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
See <http://creativecommons.org/publicdomain/zero/1.0/>.
*/

#include <stdlib.h>
#include <string.h>

#include "minimp3.h"
Expand Down
11 changes: 6 additions & 5 deletions sdk/modules/audiolite/worker/mp3dec/mp3dec_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static struct state_proc_s state_procs[] =
[SPRMP3_STATE_FILLUPREMAIN] = {exec_fillremstate, 1},
[SPRMP3_STATE_WAITIN] = {exec_waitinstate, 1},
[SPRMP3_STATE_WAITINREMAIN] = {exec_winremstate, 1},
[SPRMP3_STATE_ENDING] = {exec_endingstate, 1}
[SPRMP3_STATE_ENDING] = {exec_endingstate, 2}
};

static sprmp3_sys_t g_sys;
Expand Down Expand Up @@ -205,7 +205,7 @@ static int exec_endingstate(sprmp3_t *inst, sprmp3_outmemqueue_t *outq)

if (!(outq->done & mask))
{
if (is_decode_done(inst))
if (is_decode_done(inst) && outq->filled_size == 0)
{
send_framedone(inst->id);
reset_instance(inst);
Expand Down Expand Up @@ -1104,7 +1104,7 @@ static bool all_player_done(sprmp3_t *insts, unsigned int done)
for (i = 0; i < SPRMP3_MAX_INSTANCE; i++)
{
mask = 1 << insts->id;
if (state_procs[insts->state].playing && !(mask & done))
if (state_procs[insts->state].playing == 1 && !(mask & done))
{
return false;
}
Expand Down Expand Up @@ -1167,7 +1167,7 @@ static int deliver_decodedpcm(sprmp3_t *insts, sprmp3_outmemqueue_t *outq,
break;
}

return deliver_outpcm(outq);
return deliver_outpcm(outq, is_decode_done(insts) ? 1 : 0);
}
}

Expand Down Expand Up @@ -1215,7 +1215,8 @@ int mp3dec_main(void)
!exist_playing(g_sys.insts))
{
sprmp3_dprintf("Just Deliver PCM..\n");
deliver_outpcm(&g_sys.outqueue);
deliver_outpcm(&g_sys.outqueue,
is_decode_done(g_sys.insts) ? 1 : 0);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion sdk/modules/audiolite/worker/mp3dec/sprmp3_msghandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static void release_all_mem(sprmp3_sys_t *sys)

while (sq_peek(&sys->outqueue.queued) != NULL)
{
deliver_outpcm(&sys->outqueue);
deliver_outpcm(&sys->outqueue, 0);
}

for (i = 0; i < SPRMP3_MAX_INSTANCE; i++)
Expand Down
4 changes: 2 additions & 2 deletions sdk/modules/audiolite/worker/mp3dec/sprmp3_sendback.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ int release_framemem(int id, sprmp3_fmemqueue_t *queue)

/*** name: deliver_outpcm */

int deliver_outpcm(sprmp3_outmemqueue_t *outq)
int deliver_outpcm(sprmp3_outmemqueue_t *outq, int eof)
{
al_comm_msghdr_t hdr;
al_comm_msgopt_t opt;
Expand All @@ -197,7 +197,7 @@ int deliver_outpcm(sprmp3_outmemqueue_t *outq)
opt.addr = mem->addr;
opt.size = (outq->mode == SPRMP3_MODE_JUSTDECODE) ?
outq->filled_size : mem->size;
opt.eof = 0;
opt.eof = eof;

sq_addlast((sq_entry_t *)mem, &outq->free);
outq->done = 0;
Expand Down
2 changes: 1 addition & 1 deletion sdk/modules/audiolite/worker/mp3dec/sprmp3_sendback.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ int send_errormsg(int id, int errcode);
int send_bootmsg(void *d);
int send_debug(unsigned char opt);
int release_framemem(int id, sprmp3_fmemqueue_t *queue);
int deliver_outpcm(sprmp3_outmemqueue_t *outq);
int deliver_outpcm(sprmp3_outmemqueue_t *outq, int eof);

#ifdef __cplusplus
}
Expand Down
3 changes: 2 additions & 1 deletion sdk/modules/include/audiolite/alworker_comm.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@

#define AL_WORKER_VERSION_0 (0)
#define AL_WORKER_VERSION_1 (1)
#define AL_WORKER_VERSION_2 (2)

#define AL_MP3DECWORKER_VERSION AL_WORKER_VERSION_1
#define AL_MP3DECWORKER_VERSION AL_WORKER_VERSION_2

#define AL_COMM_NO_MSG (0xffffffff)

Expand Down

0 comments on commit cdb1dc1

Please sign in to comment.