summaryrefslogtreecommitdiffstats
path: root/stream/stream.c
diff options
context:
space:
mode:
Diffstat (limited to 'stream/stream.c')
-rw-r--r--stream/stream.c89
1 files changed, 53 insertions, 36 deletions
diff --git a/stream/stream.c b/stream/stream.c
index ca45f511e5..041b3a8c67 100644
--- a/stream/stream.c
+++ b/stream/stream.c
@@ -275,36 +275,43 @@ void stream_capture_do(stream_t *s)
}
}
-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);
@@ -322,30 +329,8 @@ int stream_write_buffer(stream_t *s, unsigned char *buf, int len) {
return rd;
}
-int stream_seek_long(stream_t *s,off_t pos){
-off_t newpos=0;
-
-// if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ) printf("seek_long to 0x%X\n",(unsigned int)pos);
-
- s->buf_pos=s->buf_len=0;
-
- if(s->mode == STREAM_WRITE) {
- if(!s->seek || !s->seek(s,pos))
- return 0;
- return 1;
- }
-
- if(s->sector_size)
- newpos = (pos/s->sector_size)*s->sector_size;
- else
- newpos = pos&(~((off_t)STREAM_BUFFER_SIZE-1));
-
-if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ){
- mp_msg(MSGT_STREAM,MSGL_DBG3, "s->pos=%"PRIX64" newpos=%"PRIX64" new_bufpos=%"PRIX64" buflen=%X \n",
- (int64_t)s->pos,(int64_t)newpos,(int64_t)pos,s->buf_len);
-}
- pos-=newpos;
-
+int stream_seek_internal(stream_t *s, off_t newpos)
+{
if(newpos==0 || newpos!=s->pos){
switch(s->type){
case STREAMTYPE_STREAM:
@@ -363,7 +348,7 @@ if(newpos==0 || newpos!=s->pos){
}
if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_seek ) {
- if( s->streaming_ctrl->streaming_seek( s->fd, pos, s->streaming_ctrl )<0 ) {
+ 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;
}
@@ -374,9 +359,6 @@ if(newpos==0 || 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
@@ -392,6 +374,41 @@ if(newpos==0 || newpos!=s->pos){
//} 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);
+
+ s->buf_pos=s->buf_len=0;
+
+ if(s->mode == STREAM_WRITE) {
+ if(!s->seek || !s->seek(s,pos))
+ return 0;
+ return 1;
+ }
+
+ if(s->sector_size)
+ newpos = (pos/s->sector_size)*s->sector_size;
+ else
+ newpos = pos&(~((off_t)STREAM_BUFFER_SIZE-1));
+
+if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ){
+ mp_msg(MSGT_STREAM,MSGL_DBG3, "s->pos=%"PRIX64" newpos=%"PRIX64" new_bufpos=%"PRIX64" buflen=%X \n",
+ (int64_t)s->pos,(int64_t)newpos,(int64_t)pos,s->buf_len);
+}
+ pos-=newpos;
+
+ res = stream_seek_internal(s, newpos);
+ if (res >= 0)
+ return res;
+
+ while(s->pos<newpos){
+ if(stream_fill_buffer(s)<=0) break; // EOF
+ }
s->eof = 0; // EOF reset when seek succeeds.
while (stream_fill_buffer(s) > 0) {