[FFmpeg-devel] [PATCH 2/3] ffplay: replace SDL_delay in read thread with SDL_CondWait

Marton Balint cus at passwd.hu
Thu Aug 30 00:10:06 CEST 2012


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: Marton Balint <cus at passwd.hu>
---
 ffplay.c |   14 +++++++++++++-
 1 files changed, 13 insertions(+), 1 deletions(-)

diff --git a/ffplay.c b/ffplay.c
index 747f2f9..8188b27 100644
--- a/ffplay.c
+++ b/ffplay.c
@@ -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) {
-- 
1.7.3.4



More information about the ffmpeg-devel mailing list