Fix vf_tcdump's compilation
[mplayer/kovensky.git] / stream / cache2.c
blobe0f67dd9d4cb3a9aebaa33c9e88c471bfb320dfc
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
21 // Initial draft of my new cache system...
22 // Note it runs in 2 processes (using fork()), but doesn't requires locking!!
23 // TODO: seeking, data consistency checking
25 #define READ_USLEEP_TIME 10000
26 #define FILL_USLEEP_TIME 50000
27 #define PREFILL_SLEEP_TIME 200
28 #define CONTROL_SLEEP_TIME 0
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <signal.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36 #include <errno.h>
38 #include "osdep/shmem.h"
39 #include "osdep/timer.h"
40 #if defined(__MINGW32__)
41 #include <windows.h>
42 static void ThreadProc( void *s );
43 #elif defined(__OS2__)
44 #define INCL_DOS
45 #include <os2.h>
46 static void ThreadProc( void *s );
47 #elif defined(PTHREAD_CACHE)
48 #include <pthread.h>
49 static void *ThreadProc(void *s);
50 #else
51 #include <sys/wait.h>
52 #endif
54 #include "mp_msg.h"
56 #include "stream.h"
57 #include "cache2.h"
59 typedef struct {
60 // constats:
61 unsigned char *buffer; // base pointer of the alllocated buffer memory
62 int buffer_size; // size of the alllocated buffer memory
63 int sector_size; // size of a single sector (2048/2324)
64 int back_size; // we should keep back_size amount of old bytes for backward seek
65 int fill_limit; // we should fill buffer only if space>=fill_limit
66 int seek_limit; // keep filling cache if distanse is less that seek limit
67 // filler's pointers:
68 int eof;
69 off_t min_filepos; // buffer contain only a part of the file, from min-max pos
70 off_t max_filepos;
71 off_t offset; // filepos <-> bufferpos offset value (filepos of the buffer's first byte)
72 // reader's pointers:
73 off_t read_filepos;
74 // commands/locking:
75 // int seek_lock; // 1 if we will seek/reset buffer, 2 if we are ready for cmd
76 // int fifo_flag; // 1 if we should use FIFO to notice cache about buffer reads.
77 // callback
78 stream_t* stream;
79 volatile int control;
80 volatile unsigned control_uint_arg;
81 volatile double control_double_arg;
82 volatile int control_res;
83 volatile off_t control_new_pos;
84 volatile double stream_time_length;
85 } cache_vars_t;
87 static int min_fill=0;
89 int cache_fill_status=0;
91 static void cache_stats(cache_vars_t* s){
92 int newb=s->max_filepos-s->read_filepos; // new bytes in the buffer
93 mp_msg(MSGT_CACHE,MSGL_INFO,"0x%06X [0x%06X] 0x%06X ",(int)s->min_filepos,(int)s->read_filepos,(int)s->max_filepos);
94 mp_msg(MSGT_CACHE,MSGL_INFO,"%3d %% (%3d%%)\n",100*newb/s->buffer_size,100*min_fill/s->buffer_size);
97 static int cache_read(cache_vars_t* s,unsigned char* buf,int size){
98 int total=0;
99 while(size>0){
100 int pos,newb,len;
102 //printf("CACHE2_READ: 0x%X <= 0x%X <= 0x%X \n",s->min_filepos,s->read_filepos,s->max_filepos);
104 if(s->read_filepos>=s->max_filepos || s->read_filepos<s->min_filepos){
105 // eof?
106 if(s->eof) break;
107 // waiting for buffer fill...
108 usec_sleep(READ_USLEEP_TIME); // 10ms
109 continue; // try again...
112 newb=s->max_filepos-s->read_filepos; // new bytes in the buffer
113 if(newb<min_fill) min_fill=newb; // statistics...
115 // printf("*** newb: %d bytes ***\n",newb);
117 pos=s->read_filepos - s->offset;
118 if(pos<0) pos+=s->buffer_size; else
119 if(pos>=s->buffer_size) pos-=s->buffer_size;
121 if(newb>s->buffer_size-pos) newb=s->buffer_size-pos; // handle wrap...
122 if(newb>size) newb=size;
124 // check:
125 if(s->read_filepos<s->min_filepos) mp_msg(MSGT_CACHE,MSGL_ERR,"Ehh. s->read_filepos<s->min_filepos !!! Report bug...\n");
127 // len=write(mem,newb)
128 //printf("Buffer read: %d bytes\n",newb);
129 memcpy(buf,&s->buffer[pos],newb);
130 buf+=newb;
131 len=newb;
132 // ...
134 s->read_filepos+=len;
135 size-=len;
136 total+=len;
139 cache_fill_status=(s->max_filepos-s->read_filepos)/(s->buffer_size / 100);
140 return total;
143 static int cache_fill(cache_vars_t* s){
144 int back,back2,newb,space,len,pos;
145 off_t read=s->read_filepos;
147 if(read<s->min_filepos || read>s->max_filepos){
148 // seek...
149 mp_msg(MSGT_CACHE,MSGL_DBG2,"Out of boundaries... seeking to 0x%"PRIX64" \n",(int64_t)read);
150 // streaming: drop cache contents only if seeking backward or too much fwd:
151 if(s->stream->type!=STREAMTYPE_STREAM ||
152 read<s->min_filepos || read>=s->max_filepos+s->seek_limit)
154 s->offset= // FIXME!?
155 s->min_filepos=s->max_filepos=read; // drop cache content :(
156 if(s->stream->eof) stream_reset(s->stream);
157 stream_seek(s->stream,read);
158 mp_msg(MSGT_CACHE,MSGL_DBG2,"Seek done. new pos: 0x%"PRIX64" \n",(int64_t)stream_tell(s->stream));
162 // calc number of back-bytes:
163 back=read - s->min_filepos;
164 if(back<0) back=0; // strange...
165 if(back>s->back_size) back=s->back_size;
167 // calc number of new bytes:
168 newb=s->max_filepos - read;
169 if(newb<0) newb=0; // strange...
171 // calc free buffer space:
172 space=s->buffer_size - (newb+back);
174 // calc bufferpos:
175 pos=s->max_filepos - s->offset;
176 if(pos>=s->buffer_size) pos-=s->buffer_size; // wrap-around
178 if(space<s->fill_limit){
179 // printf("Buffer is full (%d bytes free, limit: %d)\n",space,s->fill_limit);
180 return 0; // no fill...
183 // printf("### read=0x%X back=%d newb=%d space=%d pos=%d\n",read,back,newb,space,pos);
185 // reduce space if needed:
186 if(space>s->buffer_size-pos) space=s->buffer_size-pos;
188 // if(space>32768) space=32768; // limit one-time block size
189 if(space>4*s->sector_size) space=4*s->sector_size;
191 // if(s->seek_lock) return 0; // FIXME
193 #if 1
194 // back+newb+space <= buffer_size
195 back2=s->buffer_size-(space+newb); // max back size
196 if(s->min_filepos<(read-back2)) s->min_filepos=read-back2;
197 #else
198 s->min_filepos=read-back; // avoid seeking-back to temp area...
199 #endif
201 // ....
202 //printf("Buffer fill: %d bytes of %d\n",space,s->buffer_size);
203 //len=stream_fill_buffer(s->stream);
204 //memcpy(&s->buffer[pos],s->stream->buffer,len); // avoid this extra copy!
205 // ....
206 len=stream_read(s->stream,&s->buffer[pos],space);
207 if(!len) s->eof=1;
209 s->max_filepos+=len;
210 if(pos+len>=s->buffer_size){
211 // wrap...
212 s->offset+=s->buffer_size;
215 return len;
219 static int cache_execute_control(cache_vars_t *s) {
220 static unsigned last;
221 int quit = s->control == -2;
222 if (quit || !s->stream->control) {
223 s->stream_time_length = 0;
224 s->control_new_pos = 0;
225 s->control_res = STREAM_UNSUPPORTED;
226 s->control = -1;
227 return !quit;
229 if (GetTimerMS() - last > 99) {
230 double len;
231 if (s->stream->control(s->stream, STREAM_CTRL_GET_TIME_LENGTH, &len) == STREAM_OK)
232 s->stream_time_length = len;
233 else
234 s->stream_time_length = 0;
235 last = GetTimerMS();
237 if (s->control == -1) return 1;
238 switch (s->control) {
239 case STREAM_CTRL_GET_CURRENT_TIME:
240 case STREAM_CTRL_SEEK_TO_TIME:
241 case STREAM_CTRL_GET_ASPECT_RATIO:
242 s->control_res = s->stream->control(s->stream, s->control, &s->control_double_arg);
243 break;
244 case STREAM_CTRL_SEEK_TO_CHAPTER:
245 case STREAM_CTRL_GET_NUM_CHAPTERS:
246 case STREAM_CTRL_GET_CURRENT_CHAPTER:
247 case STREAM_CTRL_GET_NUM_ANGLES:
248 case STREAM_CTRL_GET_ANGLE:
249 case STREAM_CTRL_SET_ANGLE:
250 s->control_res = s->stream->control(s->stream, s->control, &s->control_uint_arg);
251 break;
252 default:
253 s->control_res = STREAM_UNSUPPORTED;
254 break;
256 s->control_new_pos = s->stream->pos;
257 s->control = -1;
258 return 1;
261 static cache_vars_t* cache_init(int size,int sector){
262 int num;
263 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
264 cache_vars_t* s=shmem_alloc(sizeof(cache_vars_t));
265 #else
266 cache_vars_t* s=malloc(sizeof(cache_vars_t));
267 #endif
268 if(s==NULL) return NULL;
270 memset(s,0,sizeof(cache_vars_t));
271 num=size/sector;
272 if(num < 16){
273 num = 16;
274 }//32kb min_size
275 s->buffer_size=num*sector;
276 s->sector_size=sector;
277 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
278 s->buffer=shmem_alloc(s->buffer_size);
279 #else
280 s->buffer=malloc(s->buffer_size);
281 #endif
283 if(s->buffer == NULL){
284 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
285 shmem_free(s,sizeof(cache_vars_t));
286 #else
287 free(s);
288 #endif
289 return NULL;
292 s->fill_limit=8*sector;
293 s->back_size=s->buffer_size/2;
294 return s;
297 void cache_uninit(stream_t *s) {
298 cache_vars_t* c = s->cache_data;
299 if(s->cache_pid) {
300 #if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
301 cache_do_control(s, -2, NULL);
302 #else
303 kill(s->cache_pid,SIGKILL);
304 waitpid(s->cache_pid,NULL,0);
305 #endif
306 s->cache_pid = 0;
308 if(!c) return;
309 #if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
310 free(c->buffer);
311 c->buffer = NULL;
312 c->stream = NULL;
313 free(s->cache_data);
314 #else
315 shmem_free(c->buffer,c->buffer_size);
316 c->buffer = NULL;
317 shmem_free(s->cache_data,sizeof(cache_vars_t));
318 #endif
319 s->cache_data = NULL;
322 static void exit_sighandler(int x){
323 // close stream
324 exit(0);
328 * \return 1 on success, 0 if the function was interrupted and -1 on error
330 int stream_enable_cache(stream_t *stream,int size,int min,int seek_limit){
331 int ss = stream->sector_size ? stream->sector_size : STREAM_BUFFER_SIZE;
332 int res = -1;
333 cache_vars_t* s;
335 if (stream->flags & STREAM_NON_CACHEABLE) {
336 mp_msg(MSGT_CACHE,MSGL_STATUS,"\rThis stream is non-cacheable\n");
337 return 1;
340 s=cache_init(size,ss);
341 if(s == NULL) return -1;
342 stream->cache_data=s;
343 s->stream=stream; // callback
344 s->seek_limit=seek_limit;
347 //make sure that we won't wait from cache_fill
348 //more data than it is alowed to fill
349 if (s->seek_limit > s->buffer_size - s->fill_limit ){
350 s->seek_limit = s->buffer_size - s->fill_limit;
352 if (min > s->buffer_size - s->fill_limit) {
353 min = s->buffer_size - s->fill_limit;
356 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
357 if((stream->cache_pid=fork())){
358 if ((pid_t)stream->cache_pid == -1)
359 stream->cache_pid = 0;
360 #else
362 stream_t* stream2=malloc(sizeof(stream_t));
363 memcpy(stream2,s->stream,sizeof(stream_t));
364 s->stream=stream2;
365 #if defined(__MINGW32__)
366 stream->cache_pid = _beginthread( ThreadProc, 0, s );
367 #elif defined(__OS2__)
368 stream->cache_pid = _beginthread( ThreadProc, NULL, 256 * 1024, s );
369 #else
371 pthread_t tid;
372 pthread_create(&tid, NULL, ThreadProc, s);
373 stream->cache_pid = 1;
375 #endif
376 #endif
377 if (!stream->cache_pid) {
378 mp_msg(MSGT_CACHE, MSGL_ERR,
379 "Starting cache process/thread failed: %s.\n", strerror(errno));
380 goto err_out;
382 // wait until cache is filled at least prefill_init %
383 mp_msg(MSGT_CACHE,MSGL_V,"CACHE_PRE_INIT: %"PRId64" [%"PRId64"] %"PRId64" pre:%d eof:%d \n",
384 (int64_t)s->min_filepos,(int64_t)s->read_filepos,(int64_t)s->max_filepos,min,s->eof);
385 while(s->read_filepos<s->min_filepos || s->max_filepos-s->read_filepos<min){
386 mp_tmsg(MSGT_CACHE,MSGL_STATUS,"\rCache fill: %5.2f%% (%"PRId64" bytes) ",
387 100.0*(float)(s->max_filepos-s->read_filepos)/(float)(s->buffer_size),
388 (int64_t)s->max_filepos-s->read_filepos
390 if(s->eof) break; // file is smaller than prefill size
391 if(stream_check_interrupt(PREFILL_SLEEP_TIME)) {
392 res = 0;
393 goto err_out;
396 mp_msg(MSGT_CACHE,MSGL_STATUS,"\n");
397 return 1; // parent exits
399 err_out:
400 cache_uninit(stream);
401 return res;
404 #if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
406 #ifdef PTHREAD_CACHE
407 static void *ThreadProc( void *s ){
408 #else
409 static void ThreadProc( void *s ){
410 #endif
411 #endif
413 // cache thread mainloop:
414 signal(SIGTERM,exit_sighandler); // kill
415 do {
416 if(!cache_fill(s)){
417 usec_sleep(FILL_USLEEP_TIME); // idle
419 // cache_stats(s->cache_data);
420 } while (cache_execute_control(s));
421 #if defined(__MINGW32__) || defined(__OS2__)
422 _endthread();
423 #elif defined(PTHREAD_CACHE)
424 return NULL;
425 #else
426 // make sure forked code never leaves this function
427 exit(0);
428 #endif
431 int cache_stream_fill_buffer(stream_t *s){
432 int len;
433 if(s->eof){ s->buf_pos=s->buf_len=0; return 0; }
434 if(!s->cache_pid) return stream_fill_buffer(s);
436 // cache_stats(s->cache_data);
438 if(s->pos!=((cache_vars_t*)s->cache_data)->read_filepos) mp_msg(MSGT_CACHE,MSGL_ERR,"!!! read_filepos differs!!! report this bug...\n");
440 len=cache_read(s->cache_data,s->buffer, ((cache_vars_t*)s->cache_data)->sector_size);
441 //printf("cache_stream_fill_buffer->read -> %d\n",len);
443 if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; return 0; }
444 s->buf_pos=0;
445 s->buf_len=len;
446 s->pos+=len;
447 // printf("[%d]",len);fflush(stdout);
448 return len;
452 int cache_stream_seek_long(stream_t *stream,off_t pos){
453 cache_vars_t* s;
454 off_t newpos;
455 if(!stream->cache_pid) return stream_seek_long(stream,pos);
457 s=stream->cache_data;
458 // s->seek_lock=1;
460 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);
462 newpos=pos/s->sector_size; newpos*=s->sector_size; // align
463 stream->pos=s->read_filepos=newpos;
464 s->eof=0; // !!!!!!!
466 cache_stream_fill_buffer(stream);
468 pos-=newpos;
469 if(pos>=0 && pos<=stream->buf_len){
470 stream->buf_pos=pos; // byte position in sector
471 return 1;
474 // stream->buf_pos=stream->buf_len=0;
475 // return 1;
477 mp_msg(MSGT_CACHE,MSGL_V,"cache_stream_seek: WARNING! Can't seek to 0x%"PRIX64" !\n",(int64_t)(pos+newpos));
478 return 0;
481 int cache_do_control(stream_t *stream, int cmd, void *arg) {
482 cache_vars_t* s = stream->cache_data;
483 switch (cmd) {
484 case STREAM_CTRL_SEEK_TO_TIME:
485 s->control_double_arg = *(double *)arg;
486 s->control = cmd;
487 break;
488 case STREAM_CTRL_SEEK_TO_CHAPTER:
489 case STREAM_CTRL_SET_ANGLE:
490 s->control_uint_arg = *(unsigned *)arg;
491 s->control = cmd;
492 break;
493 // the core might call these every frame, they are too slow for this...
494 case STREAM_CTRL_GET_TIME_LENGTH:
495 // case STREAM_CTRL_GET_CURRENT_TIME:
496 *(double *)arg = s->stream_time_length;
497 return s->stream_time_length ? STREAM_OK : STREAM_UNSUPPORTED;
498 case STREAM_CTRL_GET_NUM_CHAPTERS:
499 case STREAM_CTRL_GET_CURRENT_CHAPTER:
500 case STREAM_CTRL_GET_ASPECT_RATIO:
501 case STREAM_CTRL_GET_NUM_ANGLES:
502 case STREAM_CTRL_GET_ANGLE:
503 case -2:
504 s->control = cmd;
505 break;
506 default:
507 return STREAM_UNSUPPORTED;
509 while (s->control != -1)
510 usec_sleep(CONTROL_SLEEP_TIME);
511 switch (cmd) {
512 case STREAM_CTRL_GET_TIME_LENGTH:
513 case STREAM_CTRL_GET_CURRENT_TIME:
514 case STREAM_CTRL_GET_ASPECT_RATIO:
515 *(double *)arg = s->control_double_arg;
516 break;
517 case STREAM_CTRL_GET_NUM_CHAPTERS:
518 case STREAM_CTRL_GET_CURRENT_CHAPTER:
519 case STREAM_CTRL_GET_NUM_ANGLES:
520 case STREAM_CTRL_GET_ANGLE:
521 *(unsigned *)arg = s->control_uint_arg;
522 break;
523 case STREAM_CTRL_SEEK_TO_CHAPTER:
524 case STREAM_CTRL_SEEK_TO_TIME:
525 case STREAM_CTRL_SET_ANGLE:
526 stream->pos = s->read_filepos = s->control_new_pos;
527 break;
529 return s->control_res;