2001-10-20 23:57:55 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
// Initial draft of my new cache system...
|
2001-10-21 22:13:12 +00:00
|
|
|
// Note it runs in 2 processes (using fork()), but doesn't requires locking!!
|
2001-10-20 23:51:02 +00:00
|
|
|
// TODO: seeking, data consistency checking
|
|
|
|
|
2001-10-21 22:13:12 +00:00
|
|
|
#define READ_USLEEP_TIME 10000
|
|
|
|
#define FILL_USLEEP_TIME 50000
|
2002-02-23 21:22:55 +00:00
|
|
|
#define PREFILL_SLEEP_TIME 200
|
2008-05-24 07:48:35 +00:00
|
|
|
#define CONTROL_SLEEP_TIME 0
|
2001-10-20 23:51:02 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
2001-12-25 11:20:58 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2001-10-20 23:51:02 +00:00
|
|
|
|
2008-10-13 15:13:40 +00:00
|
|
|
#include "osdep/shmem.h"
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "osdep/timer.h"
|
2008-11-15 19:08:50 +00:00
|
|
|
#if defined(__MINGW32__)
|
2003-05-26 18:35:42 +00:00
|
|
|
#include <windows.h>
|
2008-10-19 18:05:45 +00:00
|
|
|
static void ThreadProc( void *s );
|
2008-02-28 16:35:47 +00:00
|
|
|
#elif defined(__OS2__)
|
|
|
|
#define INCL_DOS
|
|
|
|
#include <os2.h>
|
|
|
|
static void ThreadProc( void *s );
|
2008-11-15 19:08:50 +00:00
|
|
|
#elif defined(PTHREAD_CACHE)
|
|
|
|
#include <pthread.h>
|
|
|
|
static void *ThreadProc(void *s);
|
2008-02-28 16:35:47 +00:00
|
|
|
#else
|
|
|
|
#include <sys/wait.h>
|
2003-05-26 18:35:42 +00:00
|
|
|
#endif
|
2001-10-20 23:51:02 +00:00
|
|
|
|
2001-10-22 16:09:34 +00:00
|
|
|
#include "mp_msg.h"
|
2005-10-18 11:11:25 +00:00
|
|
|
#include "help_mp.h"
|
2001-10-22 16:09:34 +00:00
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
#include "stream.h"
|
2008-11-14 19:29:04 +00:00
|
|
|
#include "cache2.h"
|
2001-10-20 23:51:02 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
// constats:
|
|
|
|
unsigned char *buffer; // base pointer of the alllocated buffer memory
|
2001-10-21 22:13:12 +00:00
|
|
|
int buffer_size; // size of the alllocated buffer memory
|
|
|
|
int sector_size; // size of a single sector (2048/2324)
|
|
|
|
int back_size; // we should keep back_size amount of old bytes for backward seek
|
|
|
|
int fill_limit; // we should fill buffer only if space>=fill_limit
|
2005-07-31 00:26:07 +00:00
|
|
|
int seek_limit; // keep filling cache if distanse is less that seek limit
|
2001-10-20 23:51:02 +00:00
|
|
|
// filler's pointers:
|
2001-10-21 18:14:24 +00:00
|
|
|
int eof;
|
2001-10-22 17:08:24 +00:00
|
|
|
off_t min_filepos; // buffer contain only a part of the file, from min-max pos
|
|
|
|
off_t max_filepos;
|
|
|
|
off_t offset; // filepos <-> bufferpos offset value (filepos of the buffer's first byte)
|
|
|
|
// reader's pointers:
|
|
|
|
off_t read_filepos;
|
2001-10-20 23:51:02 +00:00
|
|
|
// commands/locking:
|
2001-10-21 22:13:12 +00:00
|
|
|
// 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.
|
2001-10-20 23:51:02 +00:00
|
|
|
// callback
|
|
|
|
stream_t* stream;
|
2008-05-24 07:48:35 +00:00
|
|
|
volatile int control;
|
|
|
|
volatile unsigned control_uint_arg;
|
|
|
|
volatile double control_double_arg;
|
|
|
|
volatile int control_res;
|
|
|
|
volatile off_t control_new_pos;
|
2008-05-26 18:46:13 +00:00
|
|
|
volatile double stream_time_length;
|
2001-10-20 23:51:02 +00:00
|
|
|
} cache_vars_t;
|
|
|
|
|
2001-10-21 22:13:12 +00:00
|
|
|
static int min_fill=0;
|
|
|
|
|
|
|
|
int cache_fill_status=0;
|
2001-10-20 23:51:02 +00:00
|
|
|
|
2008-04-23 03:14:53 +00:00
|
|
|
static void cache_stats(cache_vars_t* s){
|
2001-10-20 23:51:02 +00:00
|
|
|
int newb=s->max_filepos-s->read_filepos; // new bytes in the buffer
|
2006-04-22 05:12:10 +00:00
|
|
|
mp_msg(MSGT_CACHE,MSGL_INFO,"0x%06X [0x%06X] 0x%06X ",(int)s->min_filepos,(int)s->read_filepos,(int)s->max_filepos);
|
|
|
|
mp_msg(MSGT_CACHE,MSGL_INFO,"%3d %% (%3d%%)\n",100*newb/s->buffer_size,100*min_fill/s->buffer_size);
|
2001-10-20 23:51:02 +00:00
|
|
|
}
|
|
|
|
|
2008-04-23 03:14:53 +00:00
|
|
|
static int cache_read(cache_vars_t* s,unsigned char* buf,int size){
|
2001-10-20 23:51:02 +00:00
|
|
|
int total=0;
|
|
|
|
while(size>0){
|
|
|
|
int pos,newb,len;
|
2001-10-21 22:13:12 +00:00
|
|
|
|
|
|
|
//printf("CACHE2_READ: 0x%X <= 0x%X <= 0x%X \n",s->min_filepos,s->read_filepos,s->max_filepos);
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-10-22 17:08:24 +00:00
|
|
|
if(s->read_filepos>=s->max_filepos || s->read_filepos<s->min_filepos){
|
2001-10-21 18:14:24 +00:00
|
|
|
// eof?
|
|
|
|
if(s->eof) break;
|
2001-10-20 23:51:02 +00:00
|
|
|
// waiting for buffer fill...
|
2003-05-26 18:35:42 +00:00
|
|
|
usec_sleep(READ_USLEEP_TIME); // 10ms
|
2001-10-21 22:13:12 +00:00
|
|
|
continue; // try again...
|
2001-10-20 23:51:02 +00:00
|
|
|
}
|
2001-10-21 22:13:12 +00:00
|
|
|
|
2001-10-22 17:08:24 +00:00
|
|
|
newb=s->max_filepos-s->read_filepos; // new bytes in the buffer
|
2001-10-21 22:13:12 +00:00
|
|
|
if(newb<min_fill) min_fill=newb; // statistics...
|
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
// printf("*** newb: %d bytes ***\n",newb);
|
2001-10-21 22:13:12 +00:00
|
|
|
|
|
|
|
pos=s->read_filepos - s->offset;
|
|
|
|
if(pos<0) pos+=s->buffer_size; else
|
|
|
|
if(pos>=s->buffer_size) pos-=s->buffer_size;
|
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
if(newb>s->buffer_size-pos) newb=s->buffer_size-pos; // handle wrap...
|
|
|
|
if(newb>size) newb=size;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-10-21 22:13:12 +00:00
|
|
|
// check:
|
2001-10-22 16:09:34 +00:00
|
|
|
if(s->read_filepos<s->min_filepos) mp_msg(MSGT_CACHE,MSGL_ERR,"Ehh. s->read_filepos<s->min_filepos !!! Report bug...\n");
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
// len=write(mem,newb)
|
|
|
|
//printf("Buffer read: %d bytes\n",newb);
|
|
|
|
memcpy(buf,&s->buffer[pos],newb);
|
|
|
|
buf+=newb;
|
2001-10-21 22:13:12 +00:00
|
|
|
len=newb;
|
2001-10-20 23:51:02 +00:00
|
|
|
// ...
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
s->read_filepos+=len;
|
|
|
|
size-=len;
|
|
|
|
total+=len;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
}
|
2006-04-09 18:16:29 +00:00
|
|
|
cache_fill_status=(s->max_filepos-s->read_filepos)/(s->buffer_size / 100);
|
2001-10-20 23:51:02 +00:00
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2008-04-23 03:14:53 +00:00
|
|
|
static int cache_fill(cache_vars_t* s){
|
2002-09-22 02:33:28 +00:00
|
|
|
int back,back2,newb,space,len,pos;
|
2001-10-22 17:08:24 +00:00
|
|
|
off_t read=s->read_filepos;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-10-21 22:13:12 +00:00
|
|
|
if(read<s->min_filepos || read>s->max_filepos){
|
|
|
|
// seek...
|
2006-01-12 20:04:36 +00:00
|
|
|
mp_msg(MSGT_CACHE,MSGL_DBG2,"Out of boundaries... seeking to 0x%"PRIX64" \n",(int64_t)read);
|
2003-01-13 03:08:56 +00:00
|
|
|
// streaming: drop cache contents only if seeking backward or too much fwd:
|
|
|
|
if(s->stream->type!=STREAMTYPE_STREAM ||
|
2005-07-31 00:26:07 +00:00
|
|
|
read<s->min_filepos || read>=s->max_filepos+s->seek_limit)
|
2003-01-13 03:08:56 +00:00
|
|
|
{
|
|
|
|
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);
|
2006-01-12 20:04:36 +00:00
|
|
|
mp_msg(MSGT_CACHE,MSGL_DBG2,"Seek done. new pos: 0x%"PRIX64" \n",(int64_t)stream_tell(s->stream));
|
2003-01-13 03:08:56 +00:00
|
|
|
}
|
2001-10-21 22:13:12 +00:00
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
// calc number of back-bytes:
|
|
|
|
back=read - s->min_filepos;
|
|
|
|
if(back<0) back=0; // strange...
|
|
|
|
if(back>s->back_size) back=s->back_size;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
// calc number of new bytes:
|
|
|
|
newb=s->max_filepos - read;
|
|
|
|
if(newb<0) newb=0; // strange...
|
|
|
|
|
|
|
|
// calc free buffer space:
|
|
|
|
space=s->buffer_size - (newb+back);
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
// calc bufferpos:
|
|
|
|
pos=s->max_filepos - s->offset;
|
|
|
|
if(pos>=s->buffer_size) pos-=s->buffer_size; // wrap-around
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
if(space<s->fill_limit){
|
|
|
|
// printf("Buffer is full (%d bytes free, limit: %d)\n",space,s->fill_limit);
|
|
|
|
return 0; // no fill...
|
|
|
|
}
|
|
|
|
|
|
|
|
// printf("### read=0x%X back=%d newb=%d space=%d pos=%d\n",read,back,newb,space,pos);
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
// reduce space if needed:
|
|
|
|
if(space>s->buffer_size-pos) space=s->buffer_size-pos;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
// if(space>32768) space=32768; // limit one-time block size
|
|
|
|
if(space>4*s->sector_size) space=4*s->sector_size;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-10-21 22:13:12 +00:00
|
|
|
// 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
|
2001-10-20 23:51:02 +00:00
|
|
|
s->min_filepos=read-back; // avoid seeking-back to temp area...
|
2001-10-21 22:13:12 +00:00
|
|
|
#endif
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
// ....
|
|
|
|
//printf("Buffer fill: %d bytes of %d\n",space,s->buffer_size);
|
|
|
|
//len=stream_fill_buffer(s->stream);
|
|
|
|
//memcpy(&s->buffer[pos],s->stream->buffer,len); // avoid this extra copy!
|
|
|
|
// ....
|
2001-10-21 18:14:24 +00:00
|
|
|
len=stream_read(s->stream,&s->buffer[pos],space);
|
|
|
|
if(!len) s->eof=1;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
s->max_filepos+=len;
|
|
|
|
if(pos+len>=s->buffer_size){
|
|
|
|
// wrap...
|
|
|
|
s->offset+=s->buffer_size;
|
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
return len;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
}
|
|
|
|
|
2008-10-19 18:05:45 +00:00
|
|
|
static int cache_execute_control(cache_vars_t *s) {
|
|
|
|
int res = 1;
|
2008-05-26 18:46:13 +00:00
|
|
|
static unsigned last;
|
2008-05-30 15:20:42 +00:00
|
|
|
if (!s->stream->control) {
|
|
|
|
s->stream_time_length = 0;
|
|
|
|
s->control_new_pos = 0;
|
|
|
|
s->control_res = STREAM_UNSUPPORTED;
|
|
|
|
s->control = -1;
|
2008-10-19 18:05:45 +00:00
|
|
|
return res;
|
2008-05-30 15:20:42 +00:00
|
|
|
}
|
2008-05-26 18:46:13 +00:00
|
|
|
if (GetTimerMS() - last > 99) {
|
|
|
|
double len;
|
|
|
|
if (s->stream->control(s->stream, STREAM_CTRL_GET_TIME_LENGTH, &len) == STREAM_OK)
|
|
|
|
s->stream_time_length = len;
|
|
|
|
else
|
|
|
|
s->stream_time_length = 0;
|
|
|
|
last = GetTimerMS();
|
|
|
|
}
|
2008-10-19 18:05:45 +00:00
|
|
|
if (s->control == -1) return res;
|
2008-05-24 07:48:35 +00:00
|
|
|
switch (s->control) {
|
|
|
|
case STREAM_CTRL_GET_CURRENT_TIME:
|
|
|
|
case STREAM_CTRL_SEEK_TO_TIME:
|
|
|
|
case STREAM_CTRL_GET_ASPECT_RATIO:
|
|
|
|
s->control_res = s->stream->control(s->stream, s->control, &s->control_double_arg);
|
|
|
|
break;
|
|
|
|
case STREAM_CTRL_SEEK_TO_CHAPTER:
|
|
|
|
case STREAM_CTRL_GET_NUM_CHAPTERS:
|
|
|
|
case STREAM_CTRL_GET_CURRENT_CHAPTER:
|
|
|
|
case STREAM_CTRL_GET_NUM_ANGLES:
|
|
|
|
case STREAM_CTRL_GET_ANGLE:
|
|
|
|
case STREAM_CTRL_SET_ANGLE:
|
|
|
|
s->control_res = s->stream->control(s->stream, s->control, &s->control_uint_arg);
|
|
|
|
break;
|
2008-10-19 18:05:45 +00:00
|
|
|
case -2:
|
|
|
|
res = 0;
|
2008-05-24 07:48:35 +00:00
|
|
|
default:
|
|
|
|
s->control_res = STREAM_UNSUPPORTED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s->control_new_pos = s->stream->pos;
|
|
|
|
s->control = -1;
|
2008-10-19 18:05:45 +00:00
|
|
|
return res;
|
2008-05-24 07:48:35 +00:00
|
|
|
}
|
|
|
|
|
2008-04-23 03:14:53 +00:00
|
|
|
static cache_vars_t* cache_init(int size,int sector){
|
2001-10-20 23:51:02 +00:00
|
|
|
int num;
|
2008-11-15 19:08:50 +00:00
|
|
|
#if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
|
2001-10-20 23:51:02 +00:00
|
|
|
cache_vars_t* s=shmem_alloc(sizeof(cache_vars_t));
|
2003-05-26 18:35:42 +00:00
|
|
|
#else
|
|
|
|
cache_vars_t* s=malloc(sizeof(cache_vars_t));
|
|
|
|
#endif
|
2004-07-26 22:06:30 +00:00
|
|
|
if(s==NULL) return NULL;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
memset(s,0,sizeof(cache_vars_t));
|
|
|
|
num=size/sector;
|
2004-07-16 20:31:17 +00:00
|
|
|
if(num < 16){
|
|
|
|
num = 16;
|
|
|
|
}//32kb min_size
|
2001-10-20 23:51:02 +00:00
|
|
|
s->buffer_size=num*sector;
|
|
|
|
s->sector_size=sector;
|
2008-11-15 19:08:50 +00:00
|
|
|
#if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
|
2001-10-20 23:51:02 +00:00
|
|
|
s->buffer=shmem_alloc(s->buffer_size);
|
2003-05-26 18:35:42 +00:00
|
|
|
#else
|
|
|
|
s->buffer=malloc(s->buffer_size);
|
|
|
|
#endif
|
2004-07-26 22:06:30 +00:00
|
|
|
|
|
|
|
if(s->buffer == NULL){
|
2008-11-15 19:08:50 +00:00
|
|
|
#if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
|
2004-07-26 22:06:30 +00:00
|
|
|
shmem_free(s,sizeof(cache_vars_t));
|
|
|
|
#else
|
|
|
|
free(s);
|
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
s->fill_limit=8*sector;
|
2004-07-16 20:31:17 +00:00
|
|
|
s->back_size=s->buffer_size/2;
|
2001-10-20 23:51:02 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2003-04-12 13:53:33 +00:00
|
|
|
void cache_uninit(stream_t *s) {
|
|
|
|
cache_vars_t* c = s->cache_data;
|
|
|
|
if(!s->cache_pid) return;
|
2008-11-15 19:08:50 +00:00
|
|
|
#if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
|
2008-10-19 18:05:45 +00:00
|
|
|
cache_do_control(s, -2, NULL);
|
2008-02-28 16:35:47 +00:00
|
|
|
#else
|
2003-04-12 13:53:33 +00:00
|
|
|
kill(s->cache_pid,SIGKILL);
|
|
|
|
waitpid(s->cache_pid,NULL,0);
|
2003-05-26 18:35:42 +00:00
|
|
|
#endif
|
2003-04-12 13:53:33 +00:00
|
|
|
if(!c) return;
|
2008-11-15 19:08:50 +00:00
|
|
|
#if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
|
2008-02-28 16:35:47 +00:00
|
|
|
free(c->stream);
|
2003-05-26 18:35:42 +00:00
|
|
|
free(c->buffer);
|
|
|
|
free(s->cache_data);
|
2008-02-28 16:35:47 +00:00
|
|
|
#else
|
|
|
|
shmem_free(c->buffer,c->buffer_size);
|
|
|
|
shmem_free(s->cache_data,sizeof(cache_vars_t));
|
2003-05-26 18:35:42 +00:00
|
|
|
#endif
|
2003-04-12 13:53:33 +00:00
|
|
|
}
|
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
static void exit_sighandler(int x){
|
|
|
|
// close stream
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2005-07-31 00:26:07 +00:00
|
|
|
int stream_enable_cache(stream_t *stream,int size,int min,int seek_limit){
|
2007-12-20 11:36:53 +00:00
|
|
|
int ss = stream->sector_size ? stream->sector_size : STREAM_BUFFER_SIZE;
|
2002-05-05 21:43:16 +00:00
|
|
|
cache_vars_t* s;
|
2002-08-14 21:46:48 +00:00
|
|
|
|
2002-08-31 15:59:10 +00:00
|
|
|
if (stream->type==STREAMTYPE_STREAM && stream->fd < 0) {
|
|
|
|
// The stream has no 'fd' behind it, so is non-cacheable
|
|
|
|
mp_msg(MSGT_CACHE,MSGL_STATUS,"\rThis stream is non-cacheable\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2002-08-14 21:46:48 +00:00
|
|
|
|
2002-05-05 21:43:16 +00:00
|
|
|
s=cache_init(size,ss);
|
2004-07-26 22:06:30 +00:00
|
|
|
if(s == NULL) return 0;
|
2001-12-17 16:55:40 +00:00
|
|
|
stream->cache_data=s;
|
|
|
|
s->stream=stream; // callback
|
2005-07-31 00:26:07 +00:00
|
|
|
s->seek_limit=seek_limit;
|
2004-07-16 20:31:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
//make sure that we won't wait from cache_fill
|
|
|
|
//more data than it is alowed to fill
|
2005-07-31 00:26:07 +00:00
|
|
|
if (s->seek_limit > s->buffer_size - s->fill_limit ){
|
|
|
|
s->seek_limit = s->buffer_size - s->fill_limit;
|
2004-07-16 20:31:17 +00:00
|
|
|
}
|
|
|
|
if (min > s->buffer_size - s->fill_limit) {
|
|
|
|
min = s->buffer_size - s->fill_limit;
|
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2008-11-15 19:08:50 +00:00
|
|
|
#if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
|
2001-12-17 16:55:40 +00:00
|
|
|
if((stream->cache_pid=fork())){
|
2003-05-26 18:35:42 +00:00
|
|
|
#else
|
|
|
|
{
|
|
|
|
stream_t* stream2=malloc(sizeof(stream_t));
|
|
|
|
memcpy(stream2,s->stream,sizeof(stream_t));
|
|
|
|
s->stream=stream2;
|
2008-11-15 19:08:50 +00:00
|
|
|
#if defined(__MINGW32__)
|
2008-10-19 18:05:45 +00:00
|
|
|
stream->cache_pid = _beginthread( ThreadProc, 0, s );
|
2008-11-15 19:08:50 +00:00
|
|
|
#elif defined(__OS2__)
|
2008-02-28 16:35:47 +00:00
|
|
|
stream->cache_pid = _beginthread( ThreadProc, NULL, 256 * 1024, s );
|
2008-11-15 19:08:50 +00:00
|
|
|
#else
|
2008-11-15 19:43:39 +00:00
|
|
|
{
|
|
|
|
pthread_t tid;
|
|
|
|
pthread_create(&tid, NULL, ThreadProc, s);
|
|
|
|
stream->cache_pid = 1;
|
|
|
|
}
|
2008-02-28 16:35:47 +00:00
|
|
|
#endif
|
2003-05-26 18:35:42 +00:00
|
|
|
#endif
|
2001-12-17 16:55:40 +00:00
|
|
|
// wait until cache is filled at least prefill_init %
|
2006-01-12 20:04:36 +00:00
|
|
|
mp_msg(MSGT_CACHE,MSGL_V,"CACHE_PRE_INIT: %"PRId64" [%"PRId64"] %"PRId64" pre:%d eof:%d \n",
|
2006-01-14 04:09:48 +00:00
|
|
|
(int64_t)s->min_filepos,(int64_t)s->read_filepos,(int64_t)s->max_filepos,min,s->eof);
|
2001-12-17 16:55:40 +00:00
|
|
|
while(s->read_filepos<s->min_filepos || s->max_filepos-s->read_filepos<min){
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_CACHE,MSGL_STATUS,"\rCache fill: %5.2f%% (%"PRId64" bytes) ",
|
2001-12-19 02:16:43 +00:00
|
|
|
100.0*(float)(s->max_filepos-s->read_filepos)/(float)(s->buffer_size),
|
2006-01-12 20:04:36 +00:00
|
|
|
(int64_t)s->max_filepos-s->read_filepos
|
2001-12-17 16:55:40 +00:00
|
|
|
);
|
|
|
|
if(s->eof) break; // file is smaller than prefill size
|
2008-04-09 00:36:28 +00:00
|
|
|
if(stream_check_interrupt(PREFILL_SLEEP_TIME))
|
2002-02-23 21:22:55 +00:00
|
|
|
return 0;
|
2001-12-17 16:55:40 +00:00
|
|
|
}
|
2005-10-27 06:55:05 +00:00
|
|
|
mp_msg(MSGT_CACHE,MSGL_STATUS,"\n");
|
2002-02-23 21:22:55 +00:00
|
|
|
return 1; // parent exits
|
2001-12-17 16:55:40 +00:00
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2008-11-15 19:08:50 +00:00
|
|
|
#if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
|
2003-05-26 18:35:42 +00:00
|
|
|
}
|
2008-11-15 19:08:50 +00:00
|
|
|
#ifdef PTHREAD_CACHE
|
|
|
|
static void *ThreadProc( void *s ){
|
|
|
|
#else
|
2008-02-28 16:35:47 +00:00
|
|
|
static void ThreadProc( void *s ){
|
|
|
|
#endif
|
2008-11-15 19:08:50 +00:00
|
|
|
#endif
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
// cache thread mainloop:
|
|
|
|
signal(SIGTERM,exit_sighandler); // kill
|
2008-10-19 18:05:45 +00:00
|
|
|
do {
|
2008-10-19 17:28:23 +00:00
|
|
|
if(!cache_fill(s)){
|
2003-05-26 18:35:42 +00:00
|
|
|
usec_sleep(FILL_USLEEP_TIME); // idle
|
2001-10-20 23:51:02 +00:00
|
|
|
}
|
2001-10-21 00:32:47 +00:00
|
|
|
// cache_stats(s->cache_data);
|
2008-10-19 18:05:45 +00:00
|
|
|
} while (cache_execute_control(s));
|
2008-11-15 19:08:50 +00:00
|
|
|
#if defined(__MINGW32__) || defined(__OS2__)
|
2008-10-19 18:05:45 +00:00
|
|
|
_endthread();
|
|
|
|
#endif
|
2008-11-15 19:08:50 +00:00
|
|
|
#ifdef PTHREAD_CACHE
|
|
|
|
return NULL;
|
|
|
|
#endif
|
2001-10-20 23:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int cache_stream_fill_buffer(stream_t *s){
|
|
|
|
int len;
|
|
|
|
if(s->eof){ s->buf_pos=s->buf_len=0; return 0; }
|
|
|
|
if(!s->cache_pid) return stream_fill_buffer(s);
|
|
|
|
|
2001-10-21 22:13:12 +00:00
|
|
|
// cache_stats(s->cache_data);
|
|
|
|
|
2001-10-22 16:09:34 +00:00
|
|
|
if(s->pos!=((cache_vars_t*)s->cache_data)->read_filepos) mp_msg(MSGT_CACHE,MSGL_ERR,"!!! read_filepos differs!!! report this bug...\n");
|
2001-10-21 00:32:47 +00:00
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
len=cache_read(s->cache_data,s->buffer, ((cache_vars_t*)s->cache_data)->sector_size);
|
2001-10-21 22:13:12 +00:00
|
|
|
//printf("cache_stream_fill_buffer->read -> %d\n",len);
|
2001-10-21 00:32:47 +00:00
|
|
|
|
2001-10-20 23:51:02 +00:00
|
|
|
if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; return 0; }
|
|
|
|
s->buf_pos=0;
|
|
|
|
s->buf_len=len;
|
|
|
|
s->pos+=len;
|
|
|
|
// printf("[%d]",len);fflush(stdout);
|
|
|
|
return len;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-10-21 22:13:12 +00:00
|
|
|
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);
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-10-21 22:13:12 +00:00
|
|
|
s=stream->cache_data;
|
|
|
|
// s->seek_lock=1;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2006-01-12 20:04:36 +00:00
|
|
|
mp_msg(MSGT_CACHE,MSGL_DBG2,"CACHE2_SEEK: 0x%"PRIX64" <= 0x%"PRIX64" (0x%"PRIX64") <= 0x%"PRIX64" \n",s->min_filepos,pos,s->read_filepos,s->max_filepos);
|
2001-10-21 22:13:12 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2001-10-20 23:51:02 +00:00
|
|
|
|
2001-10-21 22:13:12 +00:00
|
|
|
// stream->buf_pos=stream->buf_len=0;
|
|
|
|
// return 1;
|
2001-10-20 23:51:02 +00:00
|
|
|
|
2005-10-13 18:33:56 +00:00
|
|
|
mp_msg(MSGT_CACHE,MSGL_V,"cache_stream_seek: WARNING! Can't seek to 0x%"PRIX64" !\n",(int64_t)(pos+newpos));
|
2001-10-20 23:51:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2008-05-24 07:48:35 +00:00
|
|
|
|
|
|
|
int cache_do_control(stream_t *stream, int cmd, void *arg) {
|
|
|
|
cache_vars_t* s = stream->cache_data;
|
|
|
|
switch (cmd) {
|
|
|
|
case STREAM_CTRL_SEEK_TO_TIME:
|
|
|
|
s->control_double_arg = *(double *)arg;
|
|
|
|
s->control = cmd;
|
|
|
|
break;
|
|
|
|
case STREAM_CTRL_SEEK_TO_CHAPTER:
|
|
|
|
case STREAM_CTRL_SET_ANGLE:
|
|
|
|
s->control_uint_arg = *(unsigned *)arg;
|
|
|
|
s->control = cmd;
|
|
|
|
break;
|
|
|
|
// the core might call these every frame, they are too slow for this...
|
2008-05-26 18:46:13 +00:00
|
|
|
case STREAM_CTRL_GET_TIME_LENGTH:
|
2008-06-01 18:14:56 +00:00
|
|
|
// case STREAM_CTRL_GET_CURRENT_TIME:
|
2008-05-26 18:46:13 +00:00
|
|
|
*(double *)arg = s->stream_time_length;
|
|
|
|
return s->stream_time_length ? STREAM_OK : STREAM_UNSUPPORTED;
|
2008-06-01 18:14:56 +00:00
|
|
|
case STREAM_CTRL_GET_NUM_CHAPTERS:
|
|
|
|
case STREAM_CTRL_GET_CURRENT_CHAPTER:
|
2008-05-24 07:48:35 +00:00
|
|
|
case STREAM_CTRL_GET_ASPECT_RATIO:
|
|
|
|
case STREAM_CTRL_GET_NUM_ANGLES:
|
|
|
|
case STREAM_CTRL_GET_ANGLE:
|
2008-10-19 18:05:45 +00:00
|
|
|
case -2:
|
2008-05-24 07:48:35 +00:00
|
|
|
s->control = cmd;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return STREAM_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
while (s->control != -1)
|
|
|
|
usec_sleep(CONTROL_SLEEP_TIME);
|
|
|
|
switch (cmd) {
|
|
|
|
case STREAM_CTRL_GET_TIME_LENGTH:
|
|
|
|
case STREAM_CTRL_GET_CURRENT_TIME:
|
|
|
|
case STREAM_CTRL_GET_ASPECT_RATIO:
|
|
|
|
*(double *)arg = s->control_double_arg;
|
|
|
|
break;
|
|
|
|
case STREAM_CTRL_GET_NUM_CHAPTERS:
|
|
|
|
case STREAM_CTRL_GET_CURRENT_CHAPTER:
|
|
|
|
case STREAM_CTRL_GET_NUM_ANGLES:
|
|
|
|
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;
|
|
|
|
}
|