mirror of https://github.com/mpv-player/mpv
cache seeking fixed(?)
git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@2353 b3059339-0415-0410-9bf9-f77b7e298cf2
This commit is contained in:
parent
d87d77aadb
commit
d44dc01224
|
@ -1,14 +1,13 @@
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#ifdef USE_STREAM_CACHE
|
#ifdef USE_STREAM_CACHE
|
||||||
// gcc cache2.c ../linux/shmem.o -o cache2
|
|
||||||
|
|
||||||
// Initial draft of my new cache system...
|
// Initial draft of my new cache system...
|
||||||
// includes some simulation code, using usleep() to emulate limited bandwith
|
// Note it runs in 2 processes (using fork()), but doesn't requires locking!!
|
||||||
// TODO: seeking, data consistency checking
|
// TODO: seeking, data consistency checking
|
||||||
|
|
||||||
#define READ_SPEED 20
|
#define READ_USLEEP_TIME 10000
|
||||||
#define FILL_SPEED 10
|
#define FILL_USLEEP_TIME 50000
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
@ -28,9 +27,8 @@ typedef struct {
|
||||||
unsigned char *buffer; // base pointer of the alllocated buffer memory
|
unsigned char *buffer; // base pointer of the alllocated buffer memory
|
||||||
int buffer_size; // size of the alllocated buffer memory
|
int buffer_size; // size of the alllocated buffer memory
|
||||||
int sector_size; // size of a single sector (2048/2324)
|
int sector_size; // size of a single sector (2048/2324)
|
||||||
// Note: buffer_size should be N*sector_size, where N is integer...
|
|
||||||
int back_size; // we should keep back_size amount of old bytes for backward seek
|
int back_size; // we should keep back_size amount of old bytes for backward seek
|
||||||
int fill_limit; // we should fill buffer if space>fill_limit
|
int fill_limit; // we should fill buffer only if space>=fill_limit
|
||||||
// reader's pointers:
|
// reader's pointers:
|
||||||
int read_filepos;
|
int read_filepos;
|
||||||
// filler's pointers:
|
// filler's pointers:
|
||||||
|
@ -39,14 +37,16 @@ typedef struct {
|
||||||
int offset; // filepos <-> bufferpos offset value (filepos of the buffer's first byte)
|
int offset; // filepos <-> bufferpos offset value (filepos of the buffer's first byte)
|
||||||
int eof;
|
int eof;
|
||||||
// commands/locking:
|
// commands/locking:
|
||||||
int cmd_lock; // 1 if we will seek/reset buffer, 2 if we are ready for cmd
|
// int seek_lock; // 1 if we will seek/reset buffer, 2 if we are ready for cmd
|
||||||
int fifo_flag; // 1 if we should use FIFO to notice cache about buffer reads.
|
// int fifo_flag; // 1 if we should use FIFO to notice cache about buffer reads.
|
||||||
// callback
|
// callback
|
||||||
stream_t* stream;
|
stream_t* stream;
|
||||||
} cache_vars_t;
|
} cache_vars_t;
|
||||||
|
|
||||||
int min_fill=0;
|
static int min_fill=0;
|
||||||
int sleep_flag=0;
|
static int sleep_flag=0;
|
||||||
|
|
||||||
|
int cache_fill_status=0;
|
||||||
|
|
||||||
void cache_stats(cache_vars_t* s){
|
void cache_stats(cache_vars_t* s){
|
||||||
int newb=s->max_filepos-s->read_filepos; // new bytes in the buffer
|
int newb=s->max_filepos-s->read_filepos; // new bytes in the buffer
|
||||||
|
@ -59,32 +59,37 @@ int cache_read(cache_vars_t* s,unsigned char* buf,int size){
|
||||||
while(size>0){
|
while(size>0){
|
||||||
int pos,newb,len;
|
int pos,newb,len;
|
||||||
|
|
||||||
|
//printf("CACHE2_READ: 0x%X <= 0x%X <= 0x%X \n",s->min_filepos,s->read_filepos,s->max_filepos);
|
||||||
|
|
||||||
|
newb=s->max_filepos-s->read_filepos; // new bytes in the buffer
|
||||||
|
|
||||||
|
if(newb<=0 || s->read_filepos<s->min_filepos){
|
||||||
|
// eof?
|
||||||
|
if(s->eof) break;
|
||||||
|
// waiting for buffer fill...
|
||||||
|
usleep(READ_USLEEP_TIME); // 10ms
|
||||||
|
continue; // try again...
|
||||||
|
}
|
||||||
|
|
||||||
|
if(newb<min_fill) min_fill=newb; // statistics...
|
||||||
|
|
||||||
|
// printf("*** newb: %d bytes ***\n",newb);
|
||||||
|
|
||||||
pos=s->read_filepos - s->offset;
|
pos=s->read_filepos - s->offset;
|
||||||
if(pos<0) pos+=s->buffer_size; else
|
if(pos<0) pos+=s->buffer_size; else
|
||||||
if(pos>=s->buffer_size) pos-=s->buffer_size;
|
if(pos>=s->buffer_size) pos-=s->buffer_size;
|
||||||
|
|
||||||
newb=s->max_filepos-s->read_filepos; // new bytes in the buffer
|
|
||||||
|
|
||||||
if(newb<min_fill) min_fill=newb; // statistics...
|
|
||||||
|
|
||||||
if(newb<=0){
|
|
||||||
// eof?
|
|
||||||
if(s->eof) break;
|
|
||||||
// waiting for buffer fill...
|
|
||||||
usleep(10000); // 10ms
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// printf("*** newb: %d bytes ***\n",newb);
|
|
||||||
|
|
||||||
if(newb>s->buffer_size-pos) newb=s->buffer_size-pos; // handle wrap...
|
if(newb>s->buffer_size-pos) newb=s->buffer_size-pos; // handle wrap...
|
||||||
if(newb>size) newb=size;
|
if(newb>size) newb=size;
|
||||||
|
|
||||||
|
// check:
|
||||||
|
if(s->read_filepos<s->min_filepos) printf("Ehh. s->read_filepos<s->min_filepos !!! Report bug...\n");
|
||||||
|
|
||||||
// len=write(mem,newb)
|
// len=write(mem,newb)
|
||||||
//printf("Buffer read: %d bytes\n",newb);
|
//printf("Buffer read: %d bytes\n",newb);
|
||||||
memcpy(buf,&s->buffer[pos],newb);
|
memcpy(buf,&s->buffer[pos],newb);
|
||||||
buf+=newb;
|
buf+=newb;
|
||||||
len=newb; //usleep(len*READ_SPEED*sleep_flag);
|
len=newb;
|
||||||
// ...
|
// ...
|
||||||
|
|
||||||
s->read_filepos+=len;
|
s->read_filepos+=len;
|
||||||
|
@ -92,14 +97,25 @@ int cache_read(cache_vars_t* s,unsigned char* buf,int size){
|
||||||
total+=len;
|
total+=len;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
cache_fill_status=100*(s->max_filepos-s->read_filepos)/s->buffer_size;
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cache_fill(cache_vars_t* s){
|
int cache_fill(cache_vars_t* s){
|
||||||
int read,back,newb,space,len,pos,endpos;
|
int read,back,back2,newb,space,len,pos,endpos;
|
||||||
|
|
||||||
read=s->read_filepos;
|
read=s->read_filepos;
|
||||||
|
|
||||||
|
if(read<s->min_filepos || read>s->max_filepos){
|
||||||
|
// seek...
|
||||||
|
printf("Out of boundaries... seeking to 0x%X \n",read);
|
||||||
|
s->offset= // FIXME!?
|
||||||
|
s->min_filepos=s->max_filepos=read; // drop cache content :(
|
||||||
|
if(s->stream->eof) stream_reset(s->stream);
|
||||||
|
stream_seek(s->stream,read);
|
||||||
|
printf("Seek done. new pos: 0x%X \n",(int)stream_tell(s->stream));
|
||||||
|
}
|
||||||
|
|
||||||
// calc number of back-bytes:
|
// calc number of back-bytes:
|
||||||
back=read - s->min_filepos;
|
back=read - s->min_filepos;
|
||||||
if(back<0) back=0; // strange...
|
if(back<0) back=0; // strange...
|
||||||
|
@ -129,11 +145,18 @@ int cache_fill(cache_vars_t* s){
|
||||||
// if(space>32768) space=32768; // limit one-time block size
|
// if(space>32768) space=32768; // limit one-time block size
|
||||||
if(space>4*s->sector_size) space=4*s->sector_size;
|
if(space>4*s->sector_size) space=4*s->sector_size;
|
||||||
|
|
||||||
|
// if(s->seek_lock) return 0; // FIXME
|
||||||
|
|
||||||
|
#if 1
|
||||||
|
// back+newb+space <= buffer_size
|
||||||
|
back2=s->buffer_size-(space+newb); // max back size
|
||||||
|
if(s->min_filepos<(read-back2)) s->min_filepos=read-back2;
|
||||||
|
#else
|
||||||
s->min_filepos=read-back; // avoid seeking-back to temp area...
|
s->min_filepos=read-back; // avoid seeking-back to temp area...
|
||||||
|
#endif
|
||||||
|
|
||||||
// ....
|
// ....
|
||||||
//printf("Buffer fill: %d bytes of %d\n",space,s->buffer_size);
|
//printf("Buffer fill: %d bytes of %d\n",space,s->buffer_size);
|
||||||
//len=space; usleep(len*FILL_SPEED*sleep_flag);
|
|
||||||
//len=stream_fill_buffer(s->stream);
|
//len=stream_fill_buffer(s->stream);
|
||||||
//memcpy(&s->buffer[pos],s->stream->buffer,len); // avoid this extra copy!
|
//memcpy(&s->buffer[pos],s->stream->buffer,len); // avoid this extra copy!
|
||||||
// ....
|
// ....
|
||||||
|
@ -177,7 +200,7 @@ void stream_enable_cache(stream_t *s,int size){
|
||||||
signal(SIGTERM,exit_sighandler); // kill
|
signal(SIGTERM,exit_sighandler); // kill
|
||||||
while(1){
|
while(1){
|
||||||
if(!cache_fill(s->cache_data)){
|
if(!cache_fill(s->cache_data)){
|
||||||
usleep(50000); // idle
|
usleep(FILL_USLEEP_TIME); // idle
|
||||||
}
|
}
|
||||||
// cache_stats(s->cache_data);
|
// cache_stats(s->cache_data);
|
||||||
}
|
}
|
||||||
|
@ -188,9 +211,12 @@ int cache_stream_fill_buffer(stream_t *s){
|
||||||
if(s->eof){ s->buf_pos=s->buf_len=0; return 0; }
|
if(s->eof){ s->buf_pos=s->buf_len=0; return 0; }
|
||||||
if(!s->cache_pid) return stream_fill_buffer(s);
|
if(!s->cache_pid) return stream_fill_buffer(s);
|
||||||
|
|
||||||
cache_stats(s->cache_data);
|
// cache_stats(s->cache_data);
|
||||||
|
|
||||||
|
if(s->pos!=((cache_vars_t*)s->cache_data)->read_filepos) printf("!!! read_filepos differs!!! report this bug...\n");
|
||||||
|
|
||||||
len=cache_read(s->cache_data,s->buffer, ((cache_vars_t*)s->cache_data)->sector_size);
|
len=cache_read(s->cache_data,s->buffer, ((cache_vars_t*)s->cache_data)->sector_size);
|
||||||
|
//printf("cache_stream_fill_buffer->read -> %d\n",len);
|
||||||
|
|
||||||
if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; return 0; }
|
if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; return 0; }
|
||||||
s->buf_pos=0;
|
s->buf_pos=0;
|
||||||
|
@ -201,11 +227,32 @@ int cache_stream_fill_buffer(stream_t *s){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int cache_stream_seek_long(stream_t *s,off_t pos){
|
int cache_stream_seek_long(stream_t *stream,off_t pos){
|
||||||
|
cache_vars_t* s;
|
||||||
|
off_t newpos;
|
||||||
|
if(!stream->cache_pid) return stream_seek_long(stream,pos);
|
||||||
|
|
||||||
if(!s->cache_pid) return stream_seek_long(s,pos);
|
s=stream->cache_data;
|
||||||
|
// s->seek_lock=1;
|
||||||
|
|
||||||
printf("cache2 seek not implemented!!!!!!!!\n");
|
printf("CACHE2_SEEK: 0x%X <= 0x%X (0x%X) <= 0x%X \n",s->min_filepos,(int)pos,s->read_filepos,s->max_filepos);
|
||||||
|
|
||||||
|
newpos=pos/s->sector_size; newpos*=s->sector_size; // align
|
||||||
|
stream->pos=s->read_filepos=newpos;
|
||||||
|
s->eof=0; // !!!!!!!
|
||||||
|
|
||||||
|
cache_stream_fill_buffer(stream);
|
||||||
|
|
||||||
|
pos-=newpos;
|
||||||
|
if(pos>=0 && pos<=stream->buf_len){
|
||||||
|
stream->buf_pos=pos; // byte position in sector
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// stream->buf_pos=stream->buf_len=0;
|
||||||
|
// return 1;
|
||||||
|
|
||||||
|
printf("cache_stream_seek: WARNING! Can't seek to 0x%X !\n",(int)(pos+newpos));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue