From f3b36db81d36c287e1b0522a9673b54ade7d6df2 Mon Sep 17 00:00:00 2001 From: John Janus Date: Sat, 23 Sep 2017 16:47:40 +0200 Subject: [PATCH] cleanup decode thread --- mp3player.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/mp3player.c b/mp3player.c index d7392de..c12ee3e 100644 --- a/mp3player.c +++ b/mp3player.c @@ -6,17 +6,18 @@ #include "fifo.h" #define BITS 8 -struct playermem { +typedef struct { ao_device* aodev; mpg123_handle* mh; unsigned char* buffer; fifo* queue; -}; + pthread_t decode_thread; +} playermem; -struct playersettings { +typedef struct { const char* file; bool loop; -}; +} playersettings; typedef struct { fifo* queue; @@ -26,8 +27,10 @@ typedef struct { static void cleanupThread(void* arg) { - struct playermem* mem = (struct playermem*) arg; + playermem* mem = (playermem*) arg; free(mem->buffer); + pthread_cancel(mem->decode_thread); + pthread_join(mem->decode_thread, NULL); ao_close(mem->aodev); mpg123_close(mem->mh); mpg123_delete(mem->mh); @@ -54,10 +57,10 @@ static void* decodeFunc(void* arg) { static void* playFunc(void* arg) { pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL); - //pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); bool running = true; - struct playermem* mem = (struct playermem*) malloc(sizeof(struct playermem)); - struct playersettings* settings = (struct playersettings*) arg; + playermem* mem = (playermem*) malloc(sizeof(playermem)); + playersettings* settings = (playersettings*) arg; //mpg123_handle* mh; //unsigned char* buffer; int err; @@ -85,12 +88,12 @@ static void* playFunc(void* arg) ao_option options = {"debug","",NULL}; mem->aodev = ao_open_live(driver, &format, &options); mem->queue = create_fifo(5); - pthread_t dt; decodeData dd; dd.file = settings->file; dd.queue = mem->queue; dd.mh = mem->mh; - pthread_create(&dt, NULL, decodeFunc, &dd); + pthread_create(&mem->decode_thread, NULL, decodeFunc, &dd); + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); // do { while (running) { @@ -120,7 +123,7 @@ static void* playFunc(void* arg) pthread_t startPlayThread(const char* file, bool loop) { pthread_t thread; - struct playersettings* settings = malloc(sizeof(struct playersettings)); + playersettings* settings = malloc(sizeof(playersettings)); settings->file=file; settings->loop=loop; pthread_create(&thread, NULL, playFunc, (void*) settings); -- 2.47.0