~ fix audio thread not stopping when a signal for stopping is received

should fix #107

Signed-off-by: Alexis Maiquez <almamu@almamu.com>
This commit is contained in:
Alexis Maiquez 2022-06-10 14:19:46 +02:00
parent e5ef7bd53e
commit ec6164c4cf
2 changed files with 22 additions and 9 deletions

View File

@ -409,14 +409,21 @@ int main (int argc, char* argv[])
usleep ((minimumTime - (endTime - startTime)) * CLOCKS_PER_SEC); usleep ((minimumTime - (endTime - startTime)) * CLOCKS_PER_SEC);
} }
// free context // ensure this is updated as sometimes it might not come from a signal
delete context; g_KeepRunning = false;
std::cout << "Stop requested" << std::endl;
// terminate gl // terminate gl
glfwTerminate (); glfwTerminate ();
// terminate SDL
SDL_Quit ();
// terminate free image // terminate free image
FreeImage_DeInitialise (); FreeImage_DeInitialise ();
// terminate SDL
SDL_QuitSubSystem(SDL_INIT_AUDIO);
SDL_Quit ();
// free context
delete context;
return 0; return 0;
} }

View File

@ -4,6 +4,7 @@
#include <math.h> #include <math.h>
extern int g_AudioVolume; extern int g_AudioVolume;
extern bool g_KeepRunning;
using namespace WallpaperEngine::Audio; using namespace WallpaperEngine::Audio;
@ -17,7 +18,7 @@ void audio_callback (void* userdata, uint8_t* stream, int length)
static unsigned int audio_buf_size = 0; static unsigned int audio_buf_size = 0;
static unsigned int audio_buf_index = 0; static unsigned int audio_buf_index = 0;
while (length > 0) while (length > 0 && g_KeepRunning)
{ {
if (audio_buf_index >= audio_buf_size) if (audio_buf_index >= audio_buf_size)
{ {
@ -61,7 +62,7 @@ int audio_read_thread (void* arg)
AVPacket* packet = av_packet_alloc (); AVPacket* packet = av_packet_alloc ();
int ret = 0; int ret = 0;
while (ret >= 0) while (ret >= 0 && g_KeepRunning == true)
{ {
ret = av_read_frame (stream->getFormatContext (), packet); ret = av_read_frame (stream->getFormatContext (), packet);
@ -83,6 +84,9 @@ int audio_read_thread (void* arg)
break; break;
} }
// stop the audio too just in case
stream->stop ();
return 0; return 0;
} }
@ -306,7 +310,7 @@ void CAudioStream::dequeuePacket (AVPacket* output)
SDL_LockMutex (this->m_queue->mutex); SDL_LockMutex (this->m_queue->mutex);
while (true) while (g_KeepRunning)
{ {
// enough data available, read it // enough data available, read it
if (av_fifo_size (this->m_queue->packetList) >= sizeof (entry)) if (av_fifo_size (this->m_queue->packetList) >= sizeof (entry))
@ -323,7 +327,7 @@ void CAudioStream::dequeuePacket (AVPacket* output)
} }
// make the thread wait if nothing was available // make the thread wait if nothing was available
SDL_CondWait (this->m_queue->cond, this->m_queue->mutex); SDL_CondWaitTimeout (this->m_queue->cond, this->m_queue->mutex, 1000);
} }
SDL_UnlockMutex (this->m_queue->mutex); SDL_UnlockMutex (this->m_queue->mutex);
@ -664,7 +668,7 @@ int CAudioStream::decodeFrame (uint8_t* audioBuffer, int bufferSize)
return -1; return -1;
} }
for (;;) { for (; g_KeepRunning;) {
while (audio_pkt_size > 0) { while (audio_pkt_size > 0) {
int got_frame = 0; int got_frame = 0;
int ret = avcodec_receive_frame(this->getContext (), avFrame); int ret = avcodec_receive_frame(this->getContext (), avFrame);
@ -731,4 +735,6 @@ int CAudioStream::decodeFrame (uint8_t* audioBuffer, int bufferSize)
audio_pkt_data = pkt->data; audio_pkt_data = pkt->data;
audio_pkt_size = pkt->size; audio_pkt_size = pkt->size;
} }
return 0;
} }