From 686082b34dd80f6544a570fe62ed955bebc30118 Mon Sep 17 00:00:00 2001 From: Mike Brady Date: Tue, 2 Feb 2016 14:43:37 +0000 Subject: [PATCH] Take a little more care of access to the alsa system; wait 100 ms after finishing with it to release data strucutres --- audio_alsa.c | 8 +++++++- player.c | 13 +++++++------ rtsp.c | 8 ++++---- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/audio_alsa.c b/audio_alsa.c index a389ee28..4e2677b1 100644 --- a/audio_alsa.c +++ b/audio_alsa.c @@ -437,8 +437,10 @@ static uint32_t delay() { static void play(short buf[], int samples) { int ret = 0; if (alsa_handle == NULL) { + pthread_mutex_lock(&alsa_mutex); ret = open_alsa_device(); open_mixer(); + pthread_mutex_unlock(&alsa_mutex); if ((ret == 0) && (audio_alsa.volume)) audio_alsa.volume(set_volume); } @@ -467,6 +469,7 @@ static void play(short buf[], int samples) { } static void flush(void) { + pthread_mutex_lock(&alsa_mutex); int derr; if (alsa_mix_handle) { snd_mixer_close(alsa_mix_handle); @@ -495,6 +498,7 @@ static void flush(void) { snd_pcm_close(alsa_handle); alsa_handle = NULL; } + pthread_mutex_unlock(&alsa_mutex); } static void stop(void) { @@ -513,7 +517,8 @@ static void parameters(audio_parameters *info) { } static void volume(double vol) { - debug(2, "Setting volume db to %f.", vol); + pthread_mutex_lock(&alsa_mutex); + debug(2, "Setting volume db to %f.", vol); set_volume = vol; if (alsa_mix_handle) { if (snd_mixer_selem_set_playback_dB_all(alsa_mix_elem, vol, 0) != 0) { @@ -523,6 +528,7 @@ static void volume(double vol) { die("Failed to set playback dB volume"); } } + pthread_mutex_unlock(&alsa_mutex); } static void linear_volume(double vol) { diff --git a/player.c b/player.c index a6b1f915..348eac63 100644 --- a/player.c +++ b/player.c @@ -902,12 +902,13 @@ typedef struct stats { // statistics for running averages } stats_t; static void *player_thread_func(void *arg) { - int threads_stop = 0; + struct inter_threads_record itr; + itr.please_stop = 0; // create and start the timing, control and audio receiver threads pthread_t rtp_audio_thread, rtp_control_thread, rtp_timing_thread; - pthread_create(&rtp_audio_thread, NULL, &rtp_audio_receiver, (void *)&threads_stop); - pthread_create(&rtp_control_thread, NULL, &rtp_control_receiver, (void *)&threads_stop); - pthread_create(&rtp_timing_thread, NULL, &rtp_timing_receiver, (void *)&threads_stop); + pthread_create(&rtp_audio_thread, NULL, &rtp_audio_receiver, (void *)&itr); + pthread_create(&rtp_control_thread, NULL, &rtp_control_receiver, (void *)&itr); + pthread_create(&rtp_timing_thread, NULL, &rtp_timing_receiver, (void *)&itr); session_corrections = 0; play_segment_reference_frame = 0; // zero signals that we are not in a play segment @@ -1237,11 +1238,11 @@ static void *player_thread_func(void *arg) { } if (config.output->stop) config.output->stop(); + usleep(100000); // allow this time to (?) allow the alsa subsystem to finish cleaning up after itself. 50 ms seems too short free(outbuf); free(silence); debug(1,"Shut down audio, control and timing threads"); - // usleep(1000000); - threads_stop = 1; + itr.please_stop = 1; pthread_kill(rtp_audio_thread, SIGUSR1); pthread_kill(rtp_control_thread, SIGUSR1); pthread_kill(rtp_timing_thread, SIGUSR1); diff --git a/rtsp.c b/rtsp.c index 7a768c3a..055fc6cd 100644 --- a/rtsp.c +++ b/rtsp.c @@ -1694,10 +1694,10 @@ static void *rtsp_conversation_thread_func(void *pconn) { rtp_shutdown(); pthread_mutex_unlock(&play_lock); pthread_mutex_unlock(&playing_mutex); - } else { - debug(1, "This RTSP conversation thread doesn't think it's playing for a " - "close RTSP connection."); - } + } // else { + //debug(1, "This RTSP conversation thread doesn't think it's playing for a " + // "close RTSP connection."); + // } if (auth_nonce) free(auth_nonce); conn->running = 0; -- 2.47.3