Commit 0e5042be authored by Marton Balint's avatar Marton Balint

ffplay: replace SDL_delay in read thread with SDL_CondWait

When the audio queue was empty, it was not filled until the 10ms delay expired
in the read thread. This patch changes the delay method with a condition wait,
which reacts to an empty queue a lot faster, therefore the audio buffer
underruns become less common especially after seeking.
Signed-off-by: 's avatarMarton Balint <cus@passwd.hu>
parent 4a45e722
......@@ -237,6 +237,8 @@ typedef struct VideoState {
int refresh;
int last_video_stream, last_audio_stream, last_subtitle_stream;
SDL_cond *continue_read_thread;
} VideoState;
typedef struct AllocEventProps {
......@@ -919,6 +921,7 @@ static void stream_close(VideoState *is)
SDL_DestroyCond(is->pictq_cond);
SDL_DestroyMutex(is->subpq_mutex);
SDL_DestroyCond(is->subpq_cond);
SDL_DestroyCond(is->continue_read_thread);
#if !CONFIG_AVFILTER
if (is->img_convert_ctx)
sws_freeContext(is->img_convert_ctx);
......@@ -2055,6 +2058,9 @@ static int audio_decode_frame(VideoState *is, double *pts_ptr)
return -1;
}
if (is->audioq.nb_packets == 0)
SDL_CondSignal(is->continue_read_thread);
/* read next packet */
if ((new_packet = packet_queue_get(&is->audioq, pkt, 1)) < 0)
return -1;
......@@ -2371,6 +2377,7 @@ static int read_thread(void *arg)
AVDictionaryEntry *t;
AVDictionary **opts;
int orig_nb_streams;
SDL_mutex *wait_mutex = SDL_CreateMutex();
memset(st_index, -1, sizeof(st_index));
is->last_video_stream = is->video_stream = -1;
......@@ -2538,7 +2545,9 @@ static int read_thread(void *arg)
&& (is->videoq .nb_packets > MIN_FRAMES || is->video_stream < 0 || is->videoq.abort_request)
&& (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0 || is->subtitleq.abort_request)))) {
/* wait 10 ms */
SDL_Delay(10);
SDL_LockMutex(wait_mutex);
SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
SDL_UnlockMutex(wait_mutex);
continue;
}
if (eof) {
......@@ -2619,6 +2628,7 @@ static int read_thread(void *arg)
event.user.data1 = is;
SDL_PushEvent(&event);
}
SDL_DestroyMutex(wait_mutex);
return 0;
}
......@@ -2645,6 +2655,8 @@ static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
packet_queue_init(&is->audioq);
packet_queue_init(&is->subtitleq);
is->continue_read_thread = SDL_CreateCond();
is->av_sync_type = av_sync_type;
is->read_tid = SDL_CreateThread(read_thread, is);
if (!is->read_tid) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment