1 #include "allegro5/allegro.h"
2 #include "allegro5/allegro_audio.h"
3 #include "allegro5/internal/aintern.h"
4 #include "allegro5/internal/aintern_audio.h"
5 #include "allegro5/internal/aintern_system.h"
6 #include "helper.h"
7 
_al_acodec_start_feed_thread(ALLEGRO_AUDIO_STREAM * stream)8 void _al_acodec_start_feed_thread(ALLEGRO_AUDIO_STREAM *stream)
9 {
10    stream->feed_thread = al_create_thread(_al_kcm_feed_stream, stream);
11    stream->feed_thread_started_cond = al_create_cond();
12    stream->feed_thread_started_mutex = al_create_mutex();
13    al_start_thread(stream->feed_thread);
14 }
15 
_al_acodec_stop_feed_thread(ALLEGRO_AUDIO_STREAM * stream)16 void _al_acodec_stop_feed_thread(ALLEGRO_AUDIO_STREAM *stream)
17 {
18    ALLEGRO_EVENT quit_event;
19 
20    /* Need to wait for the thread to start, otherwise the quit event may be
21     * sent before the event source is registered with the queue. */
22    al_lock_mutex(stream->feed_thread_started_mutex);
23    while (!stream->feed_thread_started) {
24       al_wait_cond(stream->feed_thread_started_cond, stream->feed_thread_started_mutex);
25    }
26    al_unlock_mutex(stream->feed_thread_started_mutex);
27 
28    quit_event.type = _KCM_STREAM_FEEDER_QUIT_EVENT_TYPE;
29    al_emit_user_event(al_get_audio_stream_event_source(stream), &quit_event, NULL);
30    al_join_thread(stream->feed_thread, NULL);
31    al_destroy_thread(stream->feed_thread);
32    al_destroy_cond(stream->feed_thread_started_cond);
33    al_destroy_mutex(stream->feed_thread_started_mutex);
34 
35    stream->feed_thread = NULL;
36 }
37