diff stream/stream.c @ 32472:5d1d67cf8718

Add internal read and seek function to avoid a useless memcpy when using the cache.
author reimar
date Wed, 27 Oct 2010 19:04:04 +0000
parents faefba58f413
children b39155e98ac3
line wrap: on
line diff
--- a/stream/stream.c	Wed Oct 27 18:38:39 2010 +0000
+++ b/stream/stream.c	Wed Oct 27 19:04:04 2010 +0000
@@ -280,36 +280,43 @@
   }
 }
 
-int stream_fill_buffer(stream_t *s){
-  int len;
+int stream_read_internal(stream_t *s, void *buf, int len)
+{
   // we will retry even if we already reached EOF previously.
   switch(s->type){
   case STREAMTYPE_STREAM:
 #ifdef CONFIG_NETWORKING
     if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_read ) {
-	    len=s->streaming_ctrl->streaming_read(s->fd,s->buffer,STREAM_BUFFER_SIZE, s->streaming_ctrl);
+      len=s->streaming_ctrl->streaming_read(s->fd, buf, len, s->streaming_ctrl);
     } else
 #endif
     if (s->fill_buffer)
-      len = s->fill_buffer(s, s->buffer, STREAM_BUFFER_SIZE);
+      len = s->fill_buffer(s, buf, len);
     else
-      len=read(s->fd,s->buffer,STREAM_BUFFER_SIZE);
+      len = read(s->fd, buf, len);
     break;
   case STREAMTYPE_DS:
-    len = demux_read_data((demux_stream_t*)s->priv,s->buffer,STREAM_BUFFER_SIZE);
+    len = demux_read_data((demux_stream_t*)s->priv, buf, len);
     break;
 
 
   default:
-    len= s->fill_buffer ? s->fill_buffer(s,s->buffer,STREAM_BUFFER_SIZE) : 0;
+    len= s->fill_buffer ? s->fill_buffer(s, buf, len) : 0;
   }
   if(len<=0){ s->eof=1; return 0; }
   // When reading succeeded we are obviously not at eof.
   // This e.g. avoids issues with eof getting stuck when lavf seeks in MPEG-TS
   s->eof=0;
+  s->pos+=len;
+  return len;
+}
+
+int stream_fill_buffer(stream_t *s){
+  int len = stream_read_internal(s, s->buffer, STREAM_BUFFER_SIZE);
+  if (len <= 0)
+    return 0;
   s->buf_pos=0;
   s->buf_len=len;
-  s->pos+=len;
 //  printf("[%d]",len);fflush(stdout);
   if (s->capture_file)
     stream_capture_do(s);
@@ -327,7 +334,56 @@
   return rd;
 }
 
+int stream_seek_internal(stream_t *s, off_t newpos)
+{
+if(newpos==0 || newpos!=s->pos){
+  switch(s->type){
+  case STREAMTYPE_STREAM:
+    //s->pos=newpos; // real seek
+    // Some streaming protocol allow to seek backward and forward
+    // A function call that return -1 can tell that the protocol
+    // doesn't support seeking.
+#ifdef CONFIG_NETWORKING
+    if(s->seek) { // new stream seek is much cleaner than streaming_ctrl one
+      if(!s->seek(s,newpos)) {
+      	mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
+      	return 0;
+      }
+      break;
+    }
+
+    if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_seek ) {
+      if( s->streaming_ctrl->streaming_seek( s->fd, newpos, s->streaming_ctrl )<0 ) {
+        mp_msg(MSGT_STREAM,MSGL_INFO,"Stream not seekable!\n");
+        return 1;
+      }
+      break;
+    }
+#endif
+    if(newpos<s->pos){
+      mp_msg(MSGT_STREAM,MSGL_INFO,"Cannot seek backward in linear streams!\n");
+      return 1;
+    }
+    break;
+  default:
+    // This should at the beginning as soon as all streams are converted
+    if(!s->seek)
+      return 0;
+    // Now seek
+    if(!s->seek(s,newpos)) {
+      mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
+      return 0;
+    }
+  }
+//   putchar('.');fflush(stdout);
+//} else {
+//   putchar('%');fflush(stdout);
+}
+  return -1;
+}
+
 int stream_seek_long(stream_t *s,off_t pos){
+  int res;
 off_t newpos=0;
 
 //  if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ) printf("seek_long to 0x%X\n",(unsigned int)pos);
@@ -351,52 +407,13 @@
 }
   pos-=newpos;
 
-if(newpos==0 || newpos!=s->pos){
-  switch(s->type){
-  case STREAMTYPE_STREAM:
-    //s->pos=newpos; // real seek
-    // Some streaming protocol allow to seek backward and forward
-    // A function call that return -1 can tell that the protocol
-    // doesn't support seeking.
-#ifdef CONFIG_NETWORKING
-    if(s->seek) { // new stream seek is much cleaner than streaming_ctrl one
-      if(!s->seek(s,newpos)) {
-      	mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
-      	return 0;
-      }
-      break;
-    }
+  res = stream_seek_internal(s, newpos);
+  if (res >= 0)
+    return res;
 
-    if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_seek ) {
-      if( s->streaming_ctrl->streaming_seek( s->fd, pos, s->streaming_ctrl )<0 ) {
-        mp_msg(MSGT_STREAM,MSGL_INFO,"Stream not seekable!\n");
-        return 1;
-      }
-      break;
-    }
-#endif
-    if(newpos<s->pos){
-      mp_msg(MSGT_STREAM,MSGL_INFO,"Cannot seek backward in linear streams!\n");
-      return 1;
-    }
-    while(s->pos<newpos){
-      if(stream_fill_buffer(s)<=0) break; // EOF
-    }
-    break;
-  default:
-    // This should at the beginning as soon as all streams are converted
-    if(!s->seek)
-      return 0;
-    // Now seek
-    if(!s->seek(s,newpos)) {
-      mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
-      return 0;
-    }
+  while(s->pos<newpos){
+    if(stream_fill_buffer(s)<=0) break; // EOF
   }
-//   putchar('.');fflush(stdout);
-//} else {
-//   putchar('%');fflush(stdout);
-}
 
 while(stream_fill_buffer(s) > 0 && pos >= 0) {
   if(pos<=s->buf_len){