3 // Initial draft of my new cache system...
4 // Note it runs in 2 processes (using fork()), but doesn't requires locking!!
5 // TODO: seeking, data consistency checking
7 #define READ_USLEEP_TIME 10000
8 #define FILL_USLEEP_TIME 50000
9 #define PREFILL_SLEEP_TIME 200
10 #define CONTROL_SLEEP_TIME 0
16 #include <sys/types.h>
20 #include "osdep/timer.h"
23 static DWORD WINAPI
ThreadProc(void* s
);
24 #elif defined(__OS2__)
27 static void ThreadProc( void *s
);
30 #include "osdep/shmem.h"
41 unsigned char *buffer
; // base pointer of the alllocated buffer memory
42 int buffer_size
; // size of the alllocated buffer memory
43 int sector_size
; // size of a single sector (2048/2324)
44 int back_size
; // we should keep back_size amount of old bytes for backward seek
45 int fill_limit
; // we should fill buffer only if space>=fill_limit
46 int seek_limit
; // keep filling cache if distanse is less that seek limit
49 off_t min_filepos
; // buffer contain only a part of the file, from min-max pos
51 off_t offset
; // filepos <-> bufferpos offset value (filepos of the buffer's first byte)
55 // int seek_lock; // 1 if we will seek/reset buffer, 2 if we are ready for cmd
56 // int fifo_flag; // 1 if we should use FIFO to notice cache about buffer reads.
60 volatile unsigned control_uint_arg
;
61 volatile double control_double_arg
;
62 volatile int control_res
;
63 volatile off_t control_new_pos
;
64 volatile double stream_time_length
;
67 static int min_fill
=0;
69 int cache_fill_status
=0;
71 static void cache_stats(cache_vars_t
* s
){
72 int newb
=s
->max_filepos
-s
->read_filepos
; // new bytes in the buffer
73 mp_msg(MSGT_CACHE
,MSGL_INFO
,"0x%06X [0x%06X] 0x%06X ",(int)s
->min_filepos
,(int)s
->read_filepos
,(int)s
->max_filepos
);
74 mp_msg(MSGT_CACHE
,MSGL_INFO
,"%3d %% (%3d%%)\n",100*newb
/s
->buffer_size
,100*min_fill
/s
->buffer_size
);
77 static int cache_read(cache_vars_t
* s
,unsigned char* buf
,int size
){
82 //printf("CACHE2_READ: 0x%X <= 0x%X <= 0x%X \n",s->min_filepos,s->read_filepos,s->max_filepos);
84 if(s
->read_filepos
>=s
->max_filepos
|| s
->read_filepos
<s
->min_filepos
){
87 // waiting for buffer fill...
88 usec_sleep(READ_USLEEP_TIME
); // 10ms
89 continue; // try again...
92 newb
=s
->max_filepos
-s
->read_filepos
; // new bytes in the buffer
93 if(newb
<min_fill
) min_fill
=newb
; // statistics...
95 // printf("*** newb: %d bytes ***\n",newb);
97 pos
=s
->read_filepos
- s
->offset
;
98 if(pos
<0) pos
+=s
->buffer_size
; else
99 if(pos
>=s
->buffer_size
) pos
-=s
->buffer_size
;
101 if(newb
>s
->buffer_size
-pos
) newb
=s
->buffer_size
-pos
; // handle wrap...
102 if(newb
>size
) newb
=size
;
105 if(s
->read_filepos
<s
->min_filepos
) mp_msg(MSGT_CACHE
,MSGL_ERR
,"Ehh. s->read_filepos<s->min_filepos !!! Report bug...\n");
107 // len=write(mem,newb)
108 //printf("Buffer read: %d bytes\n",newb);
109 memcpy(buf
,&s
->buffer
[pos
],newb
);
114 s
->read_filepos
+=len
;
119 cache_fill_status
=(s
->max_filepos
-s
->read_filepos
)/(s
->buffer_size
/ 100);
123 static int cache_fill(cache_vars_t
* s
){
124 int back
,back2
,newb
,space
,len
,pos
;
125 off_t read
=s
->read_filepos
;
127 if(read
<s
->min_filepos
|| read
>s
->max_filepos
){
129 mp_msg(MSGT_CACHE
,MSGL_DBG2
,"Out of boundaries... seeking to 0x%"PRIX64
" \n",(int64_t)read
);
130 // streaming: drop cache contents only if seeking backward or too much fwd:
131 if(s
->stream
->type
!=STREAMTYPE_STREAM
||
132 read
<s
->min_filepos
|| read
>=s
->max_filepos
+s
->seek_limit
)
134 s
->offset
= // FIXME!?
135 s
->min_filepos
=s
->max_filepos
=read
; // drop cache content :(
136 if(s
->stream
->eof
) stream_reset(s
->stream
);
137 stream_seek(s
->stream
,read
);
138 mp_msg(MSGT_CACHE
,MSGL_DBG2
,"Seek done. new pos: 0x%"PRIX64
" \n",(int64_t)stream_tell(s
->stream
));
142 // calc number of back-bytes:
143 back
=read
- s
->min_filepos
;
144 if(back
<0) back
=0; // strange...
145 if(back
>s
->back_size
) back
=s
->back_size
;
147 // calc number of new bytes:
148 newb
=s
->max_filepos
- read
;
149 if(newb
<0) newb
=0; // strange...
151 // calc free buffer space:
152 space
=s
->buffer_size
- (newb
+back
);
155 pos
=s
->max_filepos
- s
->offset
;
156 if(pos
>=s
->buffer_size
) pos
-=s
->buffer_size
; // wrap-around
158 if(space
<s
->fill_limit
){
159 // printf("Buffer is full (%d bytes free, limit: %d)\n",space,s->fill_limit);
160 return 0; // no fill...
163 // printf("### read=0x%X back=%d newb=%d space=%d pos=%d\n",read,back,newb,space,pos);
165 // reduce space if needed:
166 if(space
>s
->buffer_size
-pos
) space
=s
->buffer_size
-pos
;
168 // if(space>32768) space=32768; // limit one-time block size
169 if(space
>4*s
->sector_size
) space
=4*s
->sector_size
;
171 // if(s->seek_lock) return 0; // FIXME
174 // back+newb+space <= buffer_size
175 back2
=s
->buffer_size
-(space
+newb
); // max back size
176 if(s
->min_filepos
<(read
-back2
)) s
->min_filepos
=read
-back2
;
178 s
->min_filepos
=read
-back
; // avoid seeking-back to temp area...
182 //printf("Buffer fill: %d bytes of %d\n",space,s->buffer_size);
183 //len=stream_fill_buffer(s->stream);
184 //memcpy(&s->buffer[pos],s->stream->buffer,len); // avoid this extra copy!
186 len
=stream_read(s
->stream
,&s
->buffer
[pos
],space
);
190 if(pos
+len
>=s
->buffer_size
){
192 s
->offset
+=s
->buffer_size
;
199 static void cache_execute_control(cache_vars_t
*s
) {
200 static unsigned last
;
201 if (!s
->stream
->control
) {
202 s
->stream_time_length
= 0;
203 s
->control_new_pos
= 0;
204 s
->control_res
= STREAM_UNSUPPORTED
;
208 if (GetTimerMS() - last
> 99) {
210 if (s
->stream
->control(s
->stream
, STREAM_CTRL_GET_TIME_LENGTH
, &len
) == STREAM_OK
)
211 s
->stream_time_length
= len
;
213 s
->stream_time_length
= 0;
216 if (s
->control
== -1) return;
217 switch (s
->control
) {
218 case STREAM_CTRL_GET_CURRENT_TIME
:
219 case STREAM_CTRL_SEEK_TO_TIME
:
220 case STREAM_CTRL_GET_ASPECT_RATIO
:
221 s
->control_res
= s
->stream
->control(s
->stream
, s
->control
, &s
->control_double_arg
);
223 case STREAM_CTRL_SEEK_TO_CHAPTER
:
224 case STREAM_CTRL_GET_NUM_CHAPTERS
:
225 case STREAM_CTRL_GET_CURRENT_CHAPTER
:
226 case STREAM_CTRL_GET_NUM_ANGLES
:
227 case STREAM_CTRL_GET_ANGLE
:
228 case STREAM_CTRL_SET_ANGLE
:
229 s
->control_res
= s
->stream
->control(s
->stream
, s
->control
, &s
->control_uint_arg
);
232 s
->control_res
= STREAM_UNSUPPORTED
;
235 s
->control_new_pos
= s
->stream
->pos
;
239 static cache_vars_t
* cache_init(int size
,int sector
){
241 #if !defined(WIN32) && !defined(__OS2__)
242 cache_vars_t
* s
=shmem_alloc(sizeof(cache_vars_t
));
244 cache_vars_t
* s
=malloc(sizeof(cache_vars_t
));
246 if(s
==NULL
) return NULL
;
248 memset(s
,0,sizeof(cache_vars_t
));
253 s
->buffer_size
=num
*sector
;
254 s
->sector_size
=sector
;
255 #if !defined(WIN32) && !defined(__OS2__)
256 s
->buffer
=shmem_alloc(s
->buffer_size
);
258 s
->buffer
=malloc(s
->buffer_size
);
261 if(s
->buffer
== NULL
){
262 #if !defined(WIN32) && !defined(__OS2__)
263 shmem_free(s
,sizeof(cache_vars_t
));
270 s
->fill_limit
=8*sector
;
271 s
->back_size
=s
->buffer_size
/2;
275 void cache_uninit(stream_t
*s
) {
276 cache_vars_t
* c
= s
->cache_data
;
277 if(!s
->cache_pid
) return;
279 TerminateThread((HANDLE
)s
->cache_pid
,0);
280 #elif defined(__OS2__)
281 DosKillThread( s
->cache_pid
);
282 DosWaitThread( &s
->cache_pid
, DCWW_WAIT
);
284 kill(s
->cache_pid
,SIGKILL
);
285 waitpid(s
->cache_pid
,NULL
,0);
288 #if defined(WIN32) || defined(__OS2__)
293 shmem_free(c
->buffer
,c
->buffer_size
);
294 shmem_free(s
->cache_data
,sizeof(cache_vars_t
));
298 static void exit_sighandler(int x
){
303 int stream_enable_cache(stream_t
*stream
,int size
,int min
,int seek_limit
){
304 int ss
= stream
->sector_size
? stream
->sector_size
: STREAM_BUFFER_SIZE
;
307 if (stream
->type
==STREAMTYPE_STREAM
&& stream
->fd
< 0) {
308 // The stream has no 'fd' behind it, so is non-cacheable
309 mp_msg(MSGT_CACHE
,MSGL_STATUS
,"\rThis stream is non-cacheable\n");
313 s
=cache_init(size
,ss
);
314 if(s
== NULL
) return 0;
315 stream
->cache_data
=s
;
316 s
->stream
=stream
; // callback
317 s
->seek_limit
=seek_limit
;
320 //make sure that we won't wait from cache_fill
321 //more data than it is alowed to fill
322 if (s
->seek_limit
> s
->buffer_size
- s
->fill_limit
){
323 s
->seek_limit
= s
->buffer_size
- s
->fill_limit
;
325 if (min
> s
->buffer_size
- s
->fill_limit
) {
326 min
= s
->buffer_size
- s
->fill_limit
;
329 #if !defined(WIN32) && !defined(__OS2__)
330 if((stream
->cache_pid
=fork())){
336 stream_t
* stream2
=malloc(sizeof(stream_t
));
337 memcpy(stream2
,s
->stream
,sizeof(stream_t
));
340 stream
->cache_pid
= CreateThread(NULL
,0,ThreadProc
,s
,0,&threadId
);
342 stream
->cache_pid
= _beginthread( ThreadProc
, NULL
, 256 * 1024, s
);
345 // wait until cache is filled at least prefill_init %
346 mp_msg(MSGT_CACHE
,MSGL_V
,"CACHE_PRE_INIT: %"PRId64
" [%"PRId64
"] %"PRId64
" pre:%d eof:%d \n",
347 (int64_t)s
->min_filepos
,(int64_t)s
->read_filepos
,(int64_t)s
->max_filepos
,min
,s
->eof
);
348 while(s
->read_filepos
<s
->min_filepos
|| s
->max_filepos
-s
->read_filepos
<min
){
349 mp_msg(MSGT_CACHE
,MSGL_STATUS
,MSGTR_CacheFill
,
350 100.0*(float)(s
->max_filepos
-s
->read_filepos
)/(float)(s
->buffer_size
),
351 (int64_t)s
->max_filepos
-s
->read_filepos
353 if(s
->eof
) break; // file is smaller than prefill size
354 if(stream_check_interrupt(PREFILL_SLEEP_TIME
))
357 mp_msg(MSGT_CACHE
,MSGL_STATUS
,"\n");
358 return 1; // parent exits
361 #if defined(WIN32) || defined(__OS2__)
364 static DWORD WINAPI
ThreadProc(void*s
){
366 static void ThreadProc( void *s
){
371 use_gui
= 0; // mp_msg may not use gui stuff in forked code
373 // cache thread mainloop:
374 signal(SIGTERM
,exit_sighandler
); // kill
376 if(!cache_fill((cache_vars_t
*)s
)){
377 usec_sleep(FILL_USLEEP_TIME
); // idle
379 cache_execute_control((cache_vars_t
*)s
);
380 // cache_stats(s->cache_data);
384 int cache_stream_fill_buffer(stream_t
*s
){
386 if(s
->eof
){ s
->buf_pos
=s
->buf_len
=0; return 0; }
387 if(!s
->cache_pid
) return stream_fill_buffer(s
);
389 // cache_stats(s->cache_data);
391 if(s
->pos
!=((cache_vars_t
*)s
->cache_data
)->read_filepos
) mp_msg(MSGT_CACHE
,MSGL_ERR
,"!!! read_filepos differs!!! report this bug...\n");
393 len
=cache_read(s
->cache_data
,s
->buffer
, ((cache_vars_t
*)s
->cache_data
)->sector_size
);
394 //printf("cache_stream_fill_buffer->read -> %d\n",len);
396 if(len
<=0){ s
->eof
=1; s
->buf_pos
=s
->buf_len
=0; return 0; }
400 // printf("[%d]",len);fflush(stdout);
405 int cache_stream_seek_long(stream_t
*stream
,off_t pos
){
408 if(!stream
->cache_pid
) return stream_seek_long(stream
,pos
);
410 s
=stream
->cache_data
;
413 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
);
415 newpos
=pos
/s
->sector_size
; newpos
*=s
->sector_size
; // align
416 stream
->pos
=s
->read_filepos
=newpos
;
419 cache_stream_fill_buffer(stream
);
422 if(pos
>=0 && pos
<=stream
->buf_len
){
423 stream
->buf_pos
=pos
; // byte position in sector
427 // stream->buf_pos=stream->buf_len=0;
430 mp_msg(MSGT_CACHE
,MSGL_V
,"cache_stream_seek: WARNING! Can't seek to 0x%"PRIX64
" !\n",(int64_t)(pos
+newpos
));
434 int cache_do_control(stream_t
*stream
, int cmd
, void *arg
) {
435 cache_vars_t
* s
= stream
->cache_data
;
437 case STREAM_CTRL_SEEK_TO_TIME
:
438 s
->control_double_arg
= *(double *)arg
;
441 case STREAM_CTRL_SEEK_TO_CHAPTER
:
442 case STREAM_CTRL_SET_ANGLE
:
443 s
->control_uint_arg
= *(unsigned *)arg
;
446 // the core might call these every frame, they are too slow for this...
447 case STREAM_CTRL_GET_TIME_LENGTH
:
448 // case STREAM_CTRL_GET_CURRENT_TIME:
449 *(double *)arg
= s
->stream_time_length
;
450 return s
->stream_time_length
? STREAM_OK
: STREAM_UNSUPPORTED
;
451 case STREAM_CTRL_GET_NUM_CHAPTERS
:
452 case STREAM_CTRL_GET_CURRENT_CHAPTER
:
453 case STREAM_CTRL_GET_ASPECT_RATIO
:
454 case STREAM_CTRL_GET_NUM_ANGLES
:
455 case STREAM_CTRL_GET_ANGLE
:
459 return STREAM_UNSUPPORTED
;
461 while (s
->control
!= -1)
462 usec_sleep(CONTROL_SLEEP_TIME
);
464 case STREAM_CTRL_GET_TIME_LENGTH
:
465 case STREAM_CTRL_GET_CURRENT_TIME
:
466 case STREAM_CTRL_GET_ASPECT_RATIO
:
467 *(double *)arg
= s
->control_double_arg
;
469 case STREAM_CTRL_GET_NUM_CHAPTERS
:
470 case STREAM_CTRL_GET_CURRENT_CHAPTER
:
471 case STREAM_CTRL_GET_NUM_ANGLES
:
472 case STREAM_CTRL_GET_ANGLE
:
473 *(unsigned *)arg
= s
->control_uint_arg
;
475 case STREAM_CTRL_SEEK_TO_CHAPTER
:
476 case STREAM_CTRL_SEEK_TO_TIME
:
477 case STREAM_CTRL_SET_ANGLE
:
478 stream
->pos
= s
->read_filepos
= s
->control_new_pos
;
481 return s
->control_res
;