changeset 34373:7a4dbec9415b

Flush cache and sync stream position/eof after seeking STREAM_CTRLs. This avoid some strange differences in behaviour between -cache and -nocache.
author reimar
date Fri, 23 Dec 2011 21:50:32 +0000
parents 622b7e95695a
children 09f2662d11cb
files stream/cache2.c
diffstat 1 files changed, 24 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/stream/cache2.c	Fri Dec 23 19:00:31 2011 +0000
+++ b/stream/cache2.c	Fri Dec 23 21:50:32 2011 +0000
@@ -94,7 +94,6 @@
   volatile unsigned control_uint_arg;
   volatile double control_double_arg;
   volatile int control_res;
-  volatile off_t control_new_pos;
   volatile double stream_time_length;
   volatile double stream_time_pos;
 } cache_vars_t;
@@ -109,6 +108,12 @@
 #endif
 }
 
+static void cache_flush(cache_vars_t *s)
+{
+  s->offset= // FIXME!?
+  s->min_filepos=s->max_filepos=s->read_filepos; // drop cache content :(
+}
+
 static int cache_read(cache_vars_t *s, unsigned char *buf, int size)
 {
   int total=0;
@@ -184,8 +189,7 @@
       // issues with e.g. mov or badly interleaved files
       if(read<s->min_filepos || read>=s->max_filepos+s->seek_limit)
       {
-        s->offset= // FIXME!?
-        s->min_filepos=s->max_filepos=read; // drop cache content :(
+        cache_flush(s);
         if(s->stream->eof) stream_reset(s->stream);
         stream_seek_internal(s->stream,read);
         mp_msg(MSGT_CACHE,MSGL_DBG2,"Seek done. new pos: 0x%"PRIX64"  \n",(int64_t)stream_tell(s->stream));
@@ -262,12 +266,12 @@
 static int cache_execute_control(cache_vars_t *s) {
   double double_res;
   unsigned uint_res;
+  int needs_flush = 0;
   static unsigned last;
   int quit = s->control == -2;
   if (quit || !s->stream->control) {
     s->stream_time_length = 0;
     s->stream_time_pos = MP_NOPTS_VALUE;
-    s->control_new_pos = 0;
     s->control_res = STREAM_UNSUPPORTED;
     s->control = -1;
     return !quit;
@@ -294,6 +298,7 @@
   if (s->control == -1) return 1;
   switch (s->control) {
     case STREAM_CTRL_SEEK_TO_TIME:
+      needs_flush = 1;
       double_res = s->control_double_arg;
     case STREAM_CTRL_GET_CURRENT_TIME:
     case STREAM_CTRL_GET_ASPECT_RATIO:
@@ -302,6 +307,7 @@
       break;
     case STREAM_CTRL_SEEK_TO_CHAPTER:
     case STREAM_CTRL_SET_ANGLE:
+      needs_flush = 1;
       uint_res = s->control_uint_arg;
     case STREAM_CTRL_GET_NUM_CHAPTERS:
     case STREAM_CTRL_GET_CURRENT_CHAPTER:
@@ -314,7 +320,11 @@
       s->control_res = STREAM_UNSUPPORTED;
       break;
   }
-  s->control_new_pos = s->stream->pos;
+  if (needs_flush) {
+    s->read_filepos = s->stream->pos;
+    s->eof = s->stream->eof;
+    cache_flush(s);
+  }
   s->control = -1;
   return 1;
 }
@@ -593,16 +603,19 @@
 
 int cache_do_control(stream_t *stream, int cmd, void *arg) {
   int sleep_count = 0;
+  int pos_change = 0;
   cache_vars_t* s = stream->cache_data;
   switch (cmd) {
     case STREAM_CTRL_SEEK_TO_TIME:
       s->control_double_arg = *(double *)arg;
       s->control = cmd;
+      pos_change = 1;
       break;
     case STREAM_CTRL_SEEK_TO_CHAPTER:
     case STREAM_CTRL_SET_ANGLE:
       s->control_uint_arg = *(unsigned *)arg;
       s->control = cmd;
+      pos_change = 1;
       break;
     // the core might call these every frame, so cache them...
     case STREAM_CTRL_GET_TIME_LENGTH:
@@ -631,6 +644,12 @@
       return STREAM_UNSUPPORTED;
     }
   }
+  // to avoid unnecessary differences with non-cache behaviour,
+  // do this also on failure.
+  if (pos_change) {
+    stream->pos = s->read_filepos;
+    stream->eof = s->eof;
+  }
   if (s->control_res != STREAM_OK)
     return s->control_res;
   switch (cmd) {
@@ -645,11 +664,6 @@
     case STREAM_CTRL_GET_ANGLE:
       *(unsigned *)arg = s->control_uint_arg;
       break;
-    case STREAM_CTRL_SEEK_TO_CHAPTER:
-    case STREAM_CTRL_SEEK_TO_TIME:
-    case STREAM_CTRL_SET_ANGLE:
-      stream->pos = s->read_filepos = s->control_new_pos;
-      break;
   }
   return s->control_res;
 }