From 69088303c00f71f908227abc2baa7d4a2fc794f2 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 16 Sep 2024 23:28:27 +0200 Subject: Fix mixed audio messed up because of sleep in mutex lock --- src/main.cpp | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 45a994e..ddbf95b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3287,23 +3287,25 @@ int main(int argc, char **argv) { amix_thread = std::thread([&]() { AVFrame *aframe = av_frame_alloc(); while(running) { - std::lock_guard lock(audio_filter_mutex); - for(AudioTrack &audio_track : audio_tracks) { - if(!audio_track.sink) - continue; - - int err = 0; - while ((err = av_buffersink_get_frame(audio_track.sink, aframe)) >= 0) { - aframe->pts = audio_track.pts; - err = avcodec_send_frame(audio_track.codec_context, aframe); - if(err >= 0){ - // TODO: Move to separate thread because this could write to network (for example when livestreaming) - receive_frames(audio_track.codec_context, audio_track.stream_index, audio_track.stream, aframe->pts, av_format_context, record_start_time, frame_data_queue, replay_buffer_size_secs, frames_erased, write_output_mutex, paused_time_offset); - } else { - fprintf(stderr, "Failed to encode audio!\n"); + { + std::lock_guard lock(audio_filter_mutex); + for(AudioTrack &audio_track : audio_tracks) { + if(!audio_track.sink) + continue; + + int err = 0; + while ((err = av_buffersink_get_frame(audio_track.sink, aframe)) >= 0) { + aframe->pts = audio_track.pts; + err = avcodec_send_frame(audio_track.codec_context, aframe); + if(err >= 0){ + // TODO: Move to separate thread because this could write to network (for example when livestreaming) + receive_frames(audio_track.codec_context, audio_track.stream_index, audio_track.stream, aframe->pts, av_format_context, record_start_time, frame_data_queue, replay_buffer_size_secs, frames_erased, write_output_mutex, paused_time_offset); + } else { + fprintf(stderr, "Failed to encode audio!\n"); + } + av_frame_unref(aframe); + audio_track.pts += audio_track.codec_context->frame_size; } - av_frame_unref(aframe); - audio_track.pts += audio_track.codec_context->frame_size; } } av_usleep(5 * 1000); // 5 milliseconds -- cgit v1.2.3