core: Clean up move-to-next-file logic
[mplayer.git] / stream / cache2.c
blob59d6e0aacb45080abb2db099c2ae104fec57a09f
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 "cache2.h"
20 #include "osdep/timer.h"
21 #ifdef WIN32
22 #include <windows.h>
23 static DWORD WINAPI ThreadProc(void* s);
24 #elif defined(__OS2__)
25 #define INCL_DOS
26 #include <os2.h>
27 static void ThreadProc( void *s );
28 #else
29 #include <sys/wait.h>
30 #include "osdep/shmem.h"
31 #endif
33 #include "mp_msg.h"
34 #include "help_mp.h"
36 #include "stream.h"
37 extern int use_gui;
39 typedef struct {
40 // constats:
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
47 // filler's pointers:
48 int eof;
49 off_t min_filepos; // buffer contain only a part of the file, from min-max pos
50 off_t max_filepos;
51 off_t offset; // filepos <-> bufferpos offset value (filepos of the buffer's first byte)
52 // reader's pointers:
53 off_t read_filepos;
54 // commands/locking:
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.
57 // callback
58 stream_t* stream;
59 volatile int control;
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;
65 } cache_vars_t;
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){
78 int total=0;
79 while(size>0){
80 int pos,newb,len;
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){
85 // eof?
86 if(s->eof) break;
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;
104 // check:
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);
110 buf+=newb;
111 len=newb;
112 // ...
114 s->read_filepos+=len;
115 size-=len;
116 total+=len;
119 cache_fill_status=(s->max_filepos-s->read_filepos)/(s->buffer_size / 100);
120 return total;
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){
128 // seek...
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);
154 // calc bufferpos:
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
173 #if 1
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;
177 #else
178 s->min_filepos=read-back; // avoid seeking-back to temp area...
179 #endif
181 // ....
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!
185 // ....
186 len=stream_read(s->stream,&s->buffer[pos],space);
187 if(!len) s->eof=1;
189 s->max_filepos+=len;
190 if(pos+len>=s->buffer_size){
191 // wrap...
192 s->offset+=s->buffer_size;
195 return len;
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;
205 s->control = -1;
206 return;
208 if (GetTimerMS() - last > 99) {
209 double len;
210 if (s->stream->control(s->stream, STREAM_CTRL_GET_TIME_LENGTH, &len) == STREAM_OK)
211 s->stream_time_length = len;
212 else
213 s->stream_time_length = 0;
214 last = GetTimerMS();
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);
222 break;
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);
230 break;
231 default:
232 s->control_res = STREAM_UNSUPPORTED;
233 break;
235 s->control_new_pos = s->stream->pos;
236 s->control = -1;
239 static cache_vars_t* cache_init(int size,int sector){
240 int num;
241 #if !defined(WIN32) && !defined(__OS2__)
242 cache_vars_t* s=shmem_alloc(sizeof(cache_vars_t));
243 #else
244 cache_vars_t* s=malloc(sizeof(cache_vars_t));
245 #endif
246 if(s==NULL) return NULL;
248 memset(s,0,sizeof(cache_vars_t));
249 num=size/sector;
250 if(num < 16){
251 num = 16;
252 }//32kb min_size
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);
257 #else
258 s->buffer=malloc(s->buffer_size);
259 #endif
261 if(s->buffer == NULL){
262 #if !defined(WIN32) && !defined(__OS2__)
263 shmem_free(s,sizeof(cache_vars_t));
264 #else
265 free(s);
266 #endif
267 return NULL;
270 s->fill_limit=8*sector;
271 s->back_size=s->buffer_size/2;
272 return s;
275 void cache_uninit(stream_t *s) {
276 cache_vars_t* c = s->cache_data;
277 if(!s->cache_pid) return;
278 #ifdef WIN32
279 TerminateThread((HANDLE)s->cache_pid,0);
280 #elif defined(__OS2__)
281 DosKillThread( s->cache_pid );
282 DosWaitThread( &s->cache_pid, DCWW_WAIT );
283 #else
284 kill(s->cache_pid,SIGKILL);
285 waitpid(s->cache_pid,NULL,0);
286 #endif
287 if(!c) return;
288 #if defined(WIN32) || defined(__OS2__)
289 free(c->stream);
290 free(c->buffer);
291 free(s->cache_data);
292 #else
293 shmem_free(c->buffer,c->buffer_size);
294 shmem_free(s->cache_data,sizeof(cache_vars_t));
295 #endif
298 static void exit_sighandler(int x){
299 // close stream
300 exit(0);
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;
305 cache_vars_t* s;
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");
310 return 1;
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())){
331 #else
333 #ifdef WIN32
334 DWORD threadId;
335 #endif
336 stream_t* stream2=malloc(sizeof(stream_t));
337 memcpy(stream2,s->stream,sizeof(stream_t));
338 s->stream=stream2;
339 #ifdef WIN32
340 stream->cache_pid = CreateThread(NULL,0,ThreadProc,s,0,&threadId);
341 #else // OS2
342 stream->cache_pid = _beginthread( ThreadProc, NULL, 256 * 1024, s );
343 #endif
344 #endif
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))
355 return 0;
357 mp_msg(MSGT_CACHE,MSGL_STATUS,"\n");
358 return 1; // parent exits
361 #if defined(WIN32) || defined(__OS2__)
363 #ifdef WIN32
364 static DWORD WINAPI ThreadProc(void*s){
365 #else // OS2
366 static void ThreadProc( void *s ){
367 #endif
368 #endif
370 #ifdef CONFIG_GUI
371 use_gui = 0; // mp_msg may not use gui stuff in forked code
372 #endif
373 // cache thread mainloop:
374 signal(SIGTERM,exit_sighandler); // kill
375 while(1){
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){
385 int len;
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; }
397 s->buf_pos=0;
398 s->buf_len=len;
399 s->pos+=len;
400 // printf("[%d]",len);fflush(stdout);
401 return len;
405 int cache_stream_seek_long(stream_t *stream,off_t pos){
406 cache_vars_t* s;
407 off_t newpos;
408 if(!stream->cache_pid) return stream_seek_long(stream,pos);
410 s=stream->cache_data;
411 // s->seek_lock=1;
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;
417 s->eof=0; // !!!!!!!
419 cache_stream_fill_buffer(stream);
421 pos-=newpos;
422 if(pos>=0 && pos<=stream->buf_len){
423 stream->buf_pos=pos; // byte position in sector
424 return 1;
427 // stream->buf_pos=stream->buf_len=0;
428 // return 1;
430 mp_msg(MSGT_CACHE,MSGL_V,"cache_stream_seek: WARNING! Can't seek to 0x%"PRIX64" !\n",(int64_t)(pos+newpos));
431 return 0;
434 int cache_do_control(stream_t *stream, int cmd, void *arg) {
435 cache_vars_t* s = stream->cache_data;
436 switch (cmd) {
437 case STREAM_CTRL_SEEK_TO_TIME:
438 s->control_double_arg = *(double *)arg;
439 s->control = cmd;
440 break;
441 case STREAM_CTRL_SEEK_TO_CHAPTER:
442 case STREAM_CTRL_SET_ANGLE:
443 s->control_uint_arg = *(unsigned *)arg;
444 s->control = cmd;
445 break;
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:
456 s->control = cmd;
457 break;
458 default:
459 return STREAM_UNSUPPORTED;
461 while (s->control != -1)
462 usec_sleep(CONTROL_SLEEP_TIME);
463 switch (cmd) {
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;
468 break;
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;
474 break;
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;
479 break;
481 return s->control_res;