Get rid of nonsensical limits on -geometry x, y,w and h values, they only
[mplayer/glamo.git] / stream / cache2.c
blobd211ba483b851eb202aa0ae89b060c1d17623532
1 #include "config.h"
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
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <signal.h>
16 #include <sys/types.h>
17 #include <unistd.h>
19 #include "osdep/shmem.h"
20 #include "osdep/timer.h"
21 #if defined(__MINGW32__)
22 #include <windows.h>
23 static void ThreadProc( void *s );
24 #elif defined(__OS2__)
25 #define INCL_DOS
26 #include <os2.h>
27 static void ThreadProc( void *s );
28 #elif defined(PTHREAD_CACHE)
29 #include <pthread.h>
30 static void *ThreadProc(void *s);
31 #else
32 #include <sys/wait.h>
33 #endif
35 #include "mp_msg.h"
36 #include "help_mp.h"
38 #include "stream.h"
39 #include "cache2.h"
40 extern int use_gui;
42 int stream_fill_buffer(stream_t *s);
43 int stream_seek_long(stream_t *s,off_t pos);
45 typedef struct {
46 // constats:
47 unsigned char *buffer; // base pointer of the alllocated buffer memory
48 int buffer_size; // size of the alllocated buffer memory
49 int sector_size; // size of a single sector (2048/2324)
50 int back_size; // we should keep back_size amount of old bytes for backward seek
51 int fill_limit; // we should fill buffer only if space>=fill_limit
52 int seek_limit; // keep filling cache if distanse is less that seek limit
53 // filler's pointers:
54 int eof;
55 off_t min_filepos; // buffer contain only a part of the file, from min-max pos
56 off_t max_filepos;
57 off_t offset; // filepos <-> bufferpos offset value (filepos of the buffer's first byte)
58 // reader's pointers:
59 off_t read_filepos;
60 // commands/locking:
61 // int seek_lock; // 1 if we will seek/reset buffer, 2 if we are ready for cmd
62 // int fifo_flag; // 1 if we should use FIFO to notice cache about buffer reads.
63 // callback
64 stream_t* stream;
65 volatile int control;
66 volatile unsigned control_uint_arg;
67 volatile double control_double_arg;
68 volatile int control_res;
69 volatile off_t control_new_pos;
70 volatile double stream_time_length;
71 } cache_vars_t;
73 static int min_fill=0;
75 int cache_fill_status=0;
77 void cache_stats(cache_vars_t* s){
78 int newb=s->max_filepos-s->read_filepos; // new bytes in the buffer
79 mp_msg(MSGT_CACHE,MSGL_INFO,"0x%06X [0x%06X] 0x%06X ",(int)s->min_filepos,(int)s->read_filepos,(int)s->max_filepos);
80 mp_msg(MSGT_CACHE,MSGL_INFO,"%3d %% (%3d%%)\n",100*newb/s->buffer_size,100*min_fill/s->buffer_size);
83 int cache_read(cache_vars_t* s,unsigned char* buf,int size){
84 int total=0;
85 while(size>0){
86 int pos,newb,len;
88 //printf("CACHE2_READ: 0x%X <= 0x%X <= 0x%X \n",s->min_filepos,s->read_filepos,s->max_filepos);
90 if(s->read_filepos>=s->max_filepos || s->read_filepos<s->min_filepos){
91 // eof?
92 if(s->eof) break;
93 // waiting for buffer fill...
94 usec_sleep(READ_USLEEP_TIME); // 10ms
95 continue; // try again...
98 newb=s->max_filepos-s->read_filepos; // new bytes in the buffer
99 if(newb<min_fill) min_fill=newb; // statistics...
101 // printf("*** newb: %d bytes ***\n",newb);
103 pos=s->read_filepos - s->offset;
104 if(pos<0) pos+=s->buffer_size; else
105 if(pos>=s->buffer_size) pos-=s->buffer_size;
107 if(newb>s->buffer_size-pos) newb=s->buffer_size-pos; // handle wrap...
108 if(newb>size) newb=size;
110 // check:
111 if(s->read_filepos<s->min_filepos) mp_msg(MSGT_CACHE,MSGL_ERR,"Ehh. s->read_filepos<s->min_filepos !!! Report bug...\n");
113 // len=write(mem,newb)
114 //printf("Buffer read: %d bytes\n",newb);
115 memcpy(buf,&s->buffer[pos],newb);
116 buf+=newb;
117 len=newb;
118 // ...
120 s->read_filepos+=len;
121 size-=len;
122 total+=len;
125 cache_fill_status=(s->max_filepos-s->read_filepos)/(s->buffer_size / 100);
126 return total;
129 int cache_fill(cache_vars_t* s){
130 int back,back2,newb,space,len,pos;
131 off_t read=s->read_filepos;
133 if(read<s->min_filepos || read>s->max_filepos){
134 // seek...
135 mp_msg(MSGT_CACHE,MSGL_DBG2,"Out of boundaries... seeking to 0x%"PRIX64" \n",(int64_t)read);
136 // streaming: drop cache contents only if seeking backward or too much fwd:
137 if(s->stream->type!=STREAMTYPE_STREAM ||
138 read<s->min_filepos || read>=s->max_filepos+s->seek_limit)
140 s->offset= // FIXME!?
141 s->min_filepos=s->max_filepos=read; // drop cache content :(
142 if(s->stream->eof) stream_reset(s->stream);
143 stream_seek(s->stream,read);
144 mp_msg(MSGT_CACHE,MSGL_DBG2,"Seek done. new pos: 0x%"PRIX64" \n",(int64_t)stream_tell(s->stream));
148 // calc number of back-bytes:
149 back=read - s->min_filepos;
150 if(back<0) back=0; // strange...
151 if(back>s->back_size) back=s->back_size;
153 // calc number of new bytes:
154 newb=s->max_filepos - read;
155 if(newb<0) newb=0; // strange...
157 // calc free buffer space:
158 space=s->buffer_size - (newb+back);
160 // calc bufferpos:
161 pos=s->max_filepos - s->offset;
162 if(pos>=s->buffer_size) pos-=s->buffer_size; // wrap-around
164 if(space<s->fill_limit){
165 // printf("Buffer is full (%d bytes free, limit: %d)\n",space,s->fill_limit);
166 return 0; // no fill...
169 // printf("### read=0x%X back=%d newb=%d space=%d pos=%d\n",read,back,newb,space,pos);
171 // reduce space if needed:
172 if(space>s->buffer_size-pos) space=s->buffer_size-pos;
174 // if(space>32768) space=32768; // limit one-time block size
175 if(space>4*s->sector_size) space=4*s->sector_size;
177 // if(s->seek_lock) return 0; // FIXME
179 #if 1
180 // back+newb+space <= buffer_size
181 back2=s->buffer_size-(space+newb); // max back size
182 if(s->min_filepos<(read-back2)) s->min_filepos=read-back2;
183 #else
184 s->min_filepos=read-back; // avoid seeking-back to temp area...
185 #endif
187 // ....
188 //printf("Buffer fill: %d bytes of %d\n",space,s->buffer_size);
189 //len=stream_fill_buffer(s->stream);
190 //memcpy(&s->buffer[pos],s->stream->buffer,len); // avoid this extra copy!
191 // ....
192 len=stream_read(s->stream,&s->buffer[pos],space);
193 if(!len) s->eof=1;
195 s->max_filepos+=len;
196 if(pos+len>=s->buffer_size){
197 // wrap...
198 s->offset+=s->buffer_size;
201 return len;
205 static int cache_execute_control(cache_vars_t *s) {
206 int res = 1;
207 static unsigned last;
208 if (!s->stream->control) {
209 s->stream_time_length = 0;
210 s->control_new_pos = 0;
211 s->control_res = STREAM_UNSUPPORTED;
212 s->control = -1;
213 return res;
215 if (GetTimerMS() - last > 99) {
216 double len;
217 if (s->stream->control(s->stream, STREAM_CTRL_GET_TIME_LENGTH, &len) == STREAM_OK)
218 s->stream_time_length = len;
219 else
220 s->stream_time_length = 0;
221 last = GetTimerMS();
223 if (s->control == -1) return res;
224 switch (s->control) {
225 case STREAM_CTRL_GET_CURRENT_TIME:
226 case STREAM_CTRL_SEEK_TO_TIME:
227 case STREAM_CTRL_GET_ASPECT_RATIO:
228 s->control_res = s->stream->control(s->stream, s->control, &s->control_double_arg);
229 break;
230 case STREAM_CTRL_SEEK_TO_CHAPTER:
231 case STREAM_CTRL_GET_NUM_CHAPTERS:
232 case STREAM_CTRL_GET_CURRENT_CHAPTER:
233 case STREAM_CTRL_GET_NUM_ANGLES:
234 case STREAM_CTRL_GET_ANGLE:
235 case STREAM_CTRL_SET_ANGLE:
236 s->control_res = s->stream->control(s->stream, s->control, &s->control_uint_arg);
237 break;
238 case -2:
239 res = 0;
240 default:
241 s->control_res = STREAM_UNSUPPORTED;
242 break;
244 s->control_new_pos = s->stream->pos;
245 s->control = -1;
246 return res;
249 cache_vars_t* cache_init(int size,int sector){
250 int num;
251 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
252 cache_vars_t* s=shmem_alloc(sizeof(cache_vars_t));
253 #else
254 cache_vars_t* s=malloc(sizeof(cache_vars_t));
255 #endif
256 if(s==NULL) return NULL;
258 memset(s,0,sizeof(cache_vars_t));
259 num=size/sector;
260 if(num < 16){
261 num = 16;
262 }//32kb min_size
263 s->buffer_size=num*sector;
264 s->sector_size=sector;
265 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
266 s->buffer=shmem_alloc(s->buffer_size);
267 #else
268 s->buffer=malloc(s->buffer_size);
269 #endif
271 if(s->buffer == NULL){
272 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
273 shmem_free(s,sizeof(cache_vars_t));
274 #else
275 free(s);
276 #endif
277 return NULL;
280 s->fill_limit=8*sector;
281 s->back_size=s->buffer_size/2;
282 return s;
285 void cache_uninit(stream_t *s) {
286 cache_vars_t* c = s->cache_data;
287 if(!s->cache_pid) return;
288 #if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
289 cache_do_control(s, -2, NULL);
290 #else
291 kill(s->cache_pid,SIGKILL);
292 waitpid(s->cache_pid,NULL,0);
293 #endif
294 if(!c) return;
295 #if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
296 free(c->stream);
297 free(c->buffer);
298 free(s->cache_data);
299 #else
300 shmem_free(c->buffer,c->buffer_size);
301 shmem_free(s->cache_data,sizeof(cache_vars_t));
302 #endif
305 static void exit_sighandler(int x){
306 // close stream
307 exit(0);
310 int stream_enable_cache(stream_t *stream,int size,int min,int seek_limit){
311 int ss = stream->sector_size ? stream->sector_size : STREAM_BUFFER_SIZE;
312 cache_vars_t* s;
314 if (stream->type==STREAMTYPE_STREAM && stream->fd < 0) {
315 // The stream has no 'fd' behind it, so is non-cacheable
316 mp_msg(MSGT_CACHE,MSGL_STATUS,"\rThis stream is non-cacheable\n");
317 return 1;
320 s=cache_init(size,ss);
321 if(s == NULL) return 0;
322 stream->cache_data=s;
323 s->stream=stream; // callback
324 s->seek_limit=seek_limit;
327 //make sure that we won't wait from cache_fill
328 //more data than it is alowed to fill
329 if (s->seek_limit > s->buffer_size - s->fill_limit ){
330 s->seek_limit = s->buffer_size - s->fill_limit;
332 if (min > s->buffer_size - s->fill_limit) {
333 min = s->buffer_size - s->fill_limit;
336 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
337 if((stream->cache_pid=fork())){
338 #else
340 stream_t* stream2=malloc(sizeof(stream_t));
341 memcpy(stream2,s->stream,sizeof(stream_t));
342 s->stream=stream2;
343 #if defined(__MINGW32__)
344 stream->cache_pid = _beginthread( ThreadProc, 0, s );
345 #elif defined(__OS2__)
346 stream->cache_pid = _beginthread( ThreadProc, NULL, 256 * 1024, s );
347 #else
349 pthread_t tid;
350 pthread_create(&tid, NULL, ThreadProc, s);
351 stream->cache_pid = 1;
353 #endif
354 #endif
355 // wait until cache is filled at least prefill_init %
356 mp_msg(MSGT_CACHE,MSGL_V,"CACHE_PRE_INIT: %"PRId64" [%"PRId64"] %"PRId64" pre:%d eof:%d \n",
357 (int64_t)s->min_filepos,(int64_t)s->read_filepos,(int64_t)s->max_filepos,min,s->eof);
358 while(s->read_filepos<s->min_filepos || s->max_filepos-s->read_filepos<min){
359 mp_msg(MSGT_CACHE,MSGL_STATUS,MSGTR_CacheFill,
360 100.0*(float)(s->max_filepos-s->read_filepos)/(float)(s->buffer_size),
361 (int64_t)s->max_filepos-s->read_filepos
363 if(s->eof) break; // file is smaller than prefill size
364 if(stream_check_interrupt(PREFILL_SLEEP_TIME))
365 return 0;
367 mp_msg(MSGT_CACHE,MSGL_STATUS,"\n");
368 return 1; // parent exits
371 #if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
373 #ifdef PTHREAD_CACHE
374 static void *ThreadProc( void *s ){
375 #else
376 static void ThreadProc( void *s ){
377 #endif
378 #endif
380 #ifdef CONFIG_GUI
381 use_gui = 0; // mp_msg may not use gui stuff in forked code
382 #endif
383 // cache thread mainloop:
384 signal(SIGTERM,exit_sighandler); // kill
385 do {
386 if(!cache_fill(s)){
387 usec_sleep(FILL_USLEEP_TIME); // idle
389 // cache_stats(s->cache_data);
390 } while (cache_execute_control(s));
391 #if defined(__MINGW32__) || defined(__OS2__)
392 _endthread();
393 #endif
394 #ifdef PTHREAD_CACHE
395 return NULL;
396 #endif
399 int cache_stream_fill_buffer(stream_t *s){
400 int len;
401 if(s->eof){ s->buf_pos=s->buf_len=0; return 0; }
402 if(!s->cache_pid) return stream_fill_buffer(s);
404 // cache_stats(s->cache_data);
406 if(s->pos!=((cache_vars_t*)s->cache_data)->read_filepos) mp_msg(MSGT_CACHE,MSGL_ERR,"!!! read_filepos differs!!! report this bug...\n");
408 len=cache_read(s->cache_data,s->buffer, ((cache_vars_t*)s->cache_data)->sector_size);
409 //printf("cache_stream_fill_buffer->read -> %d\n",len);
411 if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; return 0; }
412 s->buf_pos=0;
413 s->buf_len=len;
414 s->pos+=len;
415 // printf("[%d]",len);fflush(stdout);
416 return len;
420 int cache_stream_seek_long(stream_t *stream,off_t pos){
421 cache_vars_t* s;
422 off_t newpos;
423 if(!stream->cache_pid) return stream_seek_long(stream,pos);
425 s=stream->cache_data;
426 // s->seek_lock=1;
428 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);
430 newpos=pos/s->sector_size; newpos*=s->sector_size; // align
431 stream->pos=s->read_filepos=newpos;
432 s->eof=0; // !!!!!!!
434 cache_stream_fill_buffer(stream);
436 pos-=newpos;
437 if(pos>=0 && pos<=stream->buf_len){
438 stream->buf_pos=pos; // byte position in sector
439 return 1;
442 // stream->buf_pos=stream->buf_len=0;
443 // return 1;
445 mp_msg(MSGT_CACHE,MSGL_V,"cache_stream_seek: WARNING! Can't seek to 0x%"PRIX64" !\n",(int64_t)(pos+newpos));
446 return 0;
449 int cache_do_control(stream_t *stream, int cmd, void *arg) {
450 cache_vars_t* s = stream->cache_data;
451 switch (cmd) {
452 case STREAM_CTRL_SEEK_TO_TIME:
453 s->control_double_arg = *(double *)arg;
454 s->control = cmd;
455 break;
456 case STREAM_CTRL_SEEK_TO_CHAPTER:
457 case STREAM_CTRL_SET_ANGLE:
458 s->control_uint_arg = *(unsigned *)arg;
459 s->control = cmd;
460 break;
461 // the core might call these every frame, they are too slow for this...
462 case STREAM_CTRL_GET_TIME_LENGTH:
463 // case STREAM_CTRL_GET_CURRENT_TIME:
464 *(double *)arg = s->stream_time_length;
465 return s->stream_time_length ? STREAM_OK : STREAM_UNSUPPORTED;
466 case STREAM_CTRL_GET_NUM_CHAPTERS:
467 case STREAM_CTRL_GET_CURRENT_CHAPTER:
468 case STREAM_CTRL_GET_ASPECT_RATIO:
469 case STREAM_CTRL_GET_NUM_ANGLES:
470 case STREAM_CTRL_GET_ANGLE:
471 case -2:
472 s->control = cmd;
473 break;
474 default:
475 return STREAM_UNSUPPORTED;
477 while (s->control != -1)
478 usec_sleep(CONTROL_SLEEP_TIME);
479 switch (cmd) {
480 case STREAM_CTRL_GET_TIME_LENGTH:
481 case STREAM_CTRL_GET_CURRENT_TIME:
482 case STREAM_CTRL_GET_ASPECT_RATIO:
483 *(double *)arg = s->control_double_arg;
484 break;
485 case STREAM_CTRL_GET_NUM_CHAPTERS:
486 case STREAM_CTRL_GET_CURRENT_CHAPTER:
487 case STREAM_CTRL_GET_NUM_ANGLES:
488 case STREAM_CTRL_GET_ANGLE:
489 *(unsigned *)arg = s->control_uint_arg;
490 break;
491 case STREAM_CTRL_SEEK_TO_CHAPTER:
492 case STREAM_CTRL_SEEK_TO_TIME:
493 case STREAM_CTRL_SET_ANGLE:
494 stream->pos = s->read_filepos = s->control_new_pos;
495 break;
497 return s->control_res;