sync with en/mplayer.1 rev. 30611
[mplayer/glamo.git] / stream / cache2.c
blob00415ad9fe6ba7f101d5c9328acdd28c47d4896a
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"
55 #include "help_mp.h"
57 #include "stream.h"
58 #include "cache2.h"
59 extern int use_gui;
61 int stream_fill_buffer(stream_t *s);
62 int stream_seek_long(stream_t *s,off_t pos);
64 typedef struct {
65 // constats:
66 unsigned char *buffer; // base pointer of the alllocated buffer memory
67 int buffer_size; // size of the alllocated buffer memory
68 int sector_size; // size of a single sector (2048/2324)
69 int back_size; // we should keep back_size amount of old bytes for backward seek
70 int fill_limit; // we should fill buffer only if space>=fill_limit
71 int seek_limit; // keep filling cache if distanse is less that seek limit
72 // filler's pointers:
73 int eof;
74 off_t min_filepos; // buffer contain only a part of the file, from min-max pos
75 off_t max_filepos;
76 off_t offset; // filepos <-> bufferpos offset value (filepos of the buffer's first byte)
77 // reader's pointers:
78 off_t read_filepos;
79 // commands/locking:
80 // int seek_lock; // 1 if we will seek/reset buffer, 2 if we are ready for cmd
81 // int fifo_flag; // 1 if we should use FIFO to notice cache about buffer reads.
82 // callback
83 stream_t* stream;
84 volatile int control;
85 volatile unsigned control_uint_arg;
86 volatile double control_double_arg;
87 volatile int control_res;
88 volatile off_t control_new_pos;
89 volatile double stream_time_length;
90 } cache_vars_t;
92 static int min_fill=0;
94 int cache_fill_status=0;
96 static void cache_stats(cache_vars_t *s)
98 int newb=s->max_filepos-s->read_filepos; // new bytes in the buffer
99 mp_msg(MSGT_CACHE,MSGL_INFO,"0x%06X [0x%06X] 0x%06X ",(int)s->min_filepos,(int)s->read_filepos,(int)s->max_filepos);
100 mp_msg(MSGT_CACHE,MSGL_INFO,"%3d %% (%3d%%)\n",100*newb/s->buffer_size,100*min_fill/s->buffer_size);
103 static int cache_read(cache_vars_t *s, unsigned char *buf, int size)
105 int total=0;
106 while(size>0){
107 int pos,newb,len;
109 //printf("CACHE2_READ: 0x%X <= 0x%X <= 0x%X \n",s->min_filepos,s->read_filepos,s->max_filepos);
111 if(s->read_filepos>=s->max_filepos || s->read_filepos<s->min_filepos){
112 // eof?
113 if(s->eof) break;
114 // waiting for buffer fill...
115 usec_sleep(READ_USLEEP_TIME); // 10ms
116 continue; // try again...
119 newb=s->max_filepos-s->read_filepos; // new bytes in the buffer
120 if(newb<min_fill) min_fill=newb; // statistics...
122 // printf("*** newb: %d bytes ***\n",newb);
124 pos=s->read_filepos - s->offset;
125 if(pos<0) pos+=s->buffer_size; else
126 if(pos>=s->buffer_size) pos-=s->buffer_size;
128 if(newb>s->buffer_size-pos) newb=s->buffer_size-pos; // handle wrap...
129 if(newb>size) newb=size;
131 // check:
132 if(s->read_filepos<s->min_filepos) mp_msg(MSGT_CACHE,MSGL_ERR,"Ehh. s->read_filepos<s->min_filepos !!! Report bug...\n");
134 // len=write(mem,newb)
135 //printf("Buffer read: %d bytes\n",newb);
136 memcpy(buf,&s->buffer[pos],newb);
137 buf+=newb;
138 len=newb;
139 // ...
141 s->read_filepos+=len;
142 size-=len;
143 total+=len;
146 cache_fill_status=(s->max_filepos-s->read_filepos)/(s->buffer_size / 100);
147 return total;
150 static int cache_fill(cache_vars_t *s)
152 int back,back2,newb,space,len,pos;
153 off_t read=s->read_filepos;
155 if(read<s->min_filepos || read>s->max_filepos){
156 // seek...
157 mp_msg(MSGT_CACHE,MSGL_DBG2,"Out of boundaries... seeking to 0x%"PRIX64" \n",(int64_t)read);
158 // streaming: drop cache contents only if seeking backward or too much fwd:
159 if(s->stream->type!=STREAMTYPE_STREAM ||
160 read<s->min_filepos || read>=s->max_filepos+s->seek_limit)
162 s->offset= // FIXME!?
163 s->min_filepos=s->max_filepos=read; // drop cache content :(
164 if(s->stream->eof) stream_reset(s->stream);
165 stream_seek(s->stream,read);
166 mp_msg(MSGT_CACHE,MSGL_DBG2,"Seek done. new pos: 0x%"PRIX64" \n",(int64_t)stream_tell(s->stream));
170 // calc number of back-bytes:
171 back=read - s->min_filepos;
172 if(back<0) back=0; // strange...
173 if(back>s->back_size) back=s->back_size;
175 // calc number of new bytes:
176 newb=s->max_filepos - read;
177 if(newb<0) newb=0; // strange...
179 // calc free buffer space:
180 space=s->buffer_size - (newb+back);
182 // calc bufferpos:
183 pos=s->max_filepos - s->offset;
184 if(pos>=s->buffer_size) pos-=s->buffer_size; // wrap-around
186 if(space<s->fill_limit){
187 // printf("Buffer is full (%d bytes free, limit: %d)\n",space,s->fill_limit);
188 return 0; // no fill...
191 // printf("### read=0x%X back=%d newb=%d space=%d pos=%d\n",read,back,newb,space,pos);
193 // reduce space if needed:
194 if(space>s->buffer_size-pos) space=s->buffer_size-pos;
196 // if(space>32768) space=32768; // limit one-time block size
197 if(space>4*s->sector_size) space=4*s->sector_size;
199 // if(s->seek_lock) return 0; // FIXME
201 #if 1
202 // back+newb+space <= buffer_size
203 back2=s->buffer_size-(space+newb); // max back size
204 if(s->min_filepos<(read-back2)) s->min_filepos=read-back2;
205 #else
206 s->min_filepos=read-back; // avoid seeking-back to temp area...
207 #endif
209 // ....
210 //printf("Buffer fill: %d bytes of %d\n",space,s->buffer_size);
211 //len=stream_fill_buffer(s->stream);
212 //memcpy(&s->buffer[pos],s->stream->buffer,len); // avoid this extra copy!
213 // ....
214 len=stream_read(s->stream,&s->buffer[pos],space);
215 if(!len) s->eof=1;
217 s->max_filepos+=len;
218 if(pos+len>=s->buffer_size){
219 // wrap...
220 s->offset+=s->buffer_size;
223 return len;
227 static int cache_execute_control(cache_vars_t *s) {
228 int res = 1;
229 static unsigned last;
230 if (!s->stream->control) {
231 s->stream_time_length = 0;
232 s->control_new_pos = 0;
233 s->control_res = STREAM_UNSUPPORTED;
234 s->control = -1;
235 return res;
237 if (GetTimerMS() - last > 99) {
238 double len;
239 if (s->stream->control(s->stream, STREAM_CTRL_GET_TIME_LENGTH, &len) == STREAM_OK)
240 s->stream_time_length = len;
241 else
242 s->stream_time_length = 0;
243 last = GetTimerMS();
245 if (s->control == -1) return res;
246 switch (s->control) {
247 case STREAM_CTRL_GET_CURRENT_TIME:
248 case STREAM_CTRL_SEEK_TO_TIME:
249 case STREAM_CTRL_GET_ASPECT_RATIO:
250 s->control_res = s->stream->control(s->stream, s->control, &s->control_double_arg);
251 break;
252 case STREAM_CTRL_SEEK_TO_CHAPTER:
253 case STREAM_CTRL_GET_NUM_CHAPTERS:
254 case STREAM_CTRL_GET_CURRENT_CHAPTER:
255 case STREAM_CTRL_GET_NUM_ANGLES:
256 case STREAM_CTRL_GET_ANGLE:
257 case STREAM_CTRL_SET_ANGLE:
258 s->control_res = s->stream->control(s->stream, s->control, &s->control_uint_arg);
259 break;
260 case -2:
261 res = 0;
262 default:
263 s->control_res = STREAM_UNSUPPORTED;
264 break;
266 s->control_new_pos = s->stream->pos;
267 s->control = -1;
268 return res;
271 static cache_vars_t* cache_init(int size,int sector){
272 int num;
273 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
274 cache_vars_t* s=shmem_alloc(sizeof(cache_vars_t));
275 #else
276 cache_vars_t* s=malloc(sizeof(cache_vars_t));
277 #endif
278 if(s==NULL) return NULL;
280 memset(s,0,sizeof(cache_vars_t));
281 num=size/sector;
282 if(num < 16){
283 num = 16;
284 }//32kb min_size
285 s->buffer_size=num*sector;
286 s->sector_size=sector;
287 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
288 s->buffer=shmem_alloc(s->buffer_size);
289 #else
290 s->buffer=malloc(s->buffer_size);
291 #endif
293 if(s->buffer == NULL){
294 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
295 shmem_free(s,sizeof(cache_vars_t));
296 #else
297 free(s);
298 #endif
299 return NULL;
302 s->fill_limit=8*sector;
303 s->back_size=s->buffer_size/2;
304 return s;
307 void cache_uninit(stream_t *s) {
308 cache_vars_t* c = s->cache_data;
309 if(s->cache_pid) {
310 #if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
311 cache_do_control(s, -2, NULL);
312 #else
313 kill(s->cache_pid,SIGKILL);
314 waitpid(s->cache_pid,NULL,0);
315 #endif
316 s->cache_pid = 0;
318 if(!c) return;
319 #if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
320 free(c->stream);
321 free(c->buffer);
322 c->buffer = NULL;
323 free(s->cache_data);
324 #else
325 shmem_free(c->buffer,c->buffer_size);
326 c->buffer = NULL;
327 shmem_free(s->cache_data,sizeof(cache_vars_t));
328 #endif
329 s->cache_data = NULL;
332 static void exit_sighandler(int x){
333 // close stream
334 exit(0);
338 * \return 1 on success, 0 if the function was interrupted and -1 on error
340 int stream_enable_cache(stream_t *stream,int size,int min,int seek_limit){
341 int ss = stream->sector_size ? stream->sector_size : STREAM_BUFFER_SIZE;
342 int res = -1;
343 cache_vars_t* s;
345 if (stream->flags & STREAM_NON_CACHEABLE) {
346 mp_msg(MSGT_CACHE,MSGL_STATUS,"\rThis stream is non-cacheable\n");
347 return 1;
350 s=cache_init(size,ss);
351 if(s == NULL) return -1;
352 stream->cache_data=s;
353 s->stream=stream; // callback
354 s->seek_limit=seek_limit;
357 //make sure that we won't wait from cache_fill
358 //more data than it is alowed to fill
359 if (s->seek_limit > s->buffer_size - s->fill_limit ){
360 s->seek_limit = s->buffer_size - s->fill_limit;
362 if (min > s->buffer_size - s->fill_limit) {
363 min = s->buffer_size - s->fill_limit;
366 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
367 if((stream->cache_pid=fork())){
368 if ((pid_t)stream->cache_pid == -1)
369 stream->cache_pid = 0;
370 #else
372 stream_t* stream2=malloc(sizeof(stream_t));
373 memcpy(stream2,s->stream,sizeof(stream_t));
374 s->stream=stream2;
375 #if defined(__MINGW32__)
376 stream->cache_pid = _beginthread( ThreadProc, 0, s );
377 #elif defined(__OS2__)
378 stream->cache_pid = _beginthread( ThreadProc, NULL, 256 * 1024, s );
379 #else
381 pthread_t tid;
382 pthread_create(&tid, NULL, ThreadProc, s);
383 stream->cache_pid = 1;
385 #endif
386 #endif
387 if (!stream->cache_pid) {
388 mp_msg(MSGT_CACHE, MSGL_ERR,
389 "Starting cache process/thread failed: %s.\n", strerror(errno));
390 goto err_out;
392 // wait until cache is filled at least prefill_init %
393 mp_msg(MSGT_CACHE,MSGL_V,"CACHE_PRE_INIT: %"PRId64" [%"PRId64"] %"PRId64" pre:%d eof:%d \n",
394 (int64_t)s->min_filepos,(int64_t)s->read_filepos,(int64_t)s->max_filepos,min,s->eof);
395 while(s->read_filepos<s->min_filepos || s->max_filepos-s->read_filepos<min){
396 mp_msg(MSGT_CACHE,MSGL_STATUS,MSGTR_CacheFill,
397 100.0*(float)(s->max_filepos-s->read_filepos)/(float)(s->buffer_size),
398 (int64_t)s->max_filepos-s->read_filepos
400 if(s->eof) break; // file is smaller than prefill size
401 if(stream_check_interrupt(PREFILL_SLEEP_TIME)) {
402 res = 0;
403 goto err_out;
406 mp_msg(MSGT_CACHE,MSGL_STATUS,"\n");
407 return 1; // parent exits
409 err_out:
410 cache_uninit(stream);
411 return res;
414 #if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
416 #ifdef PTHREAD_CACHE
417 static void *ThreadProc( void *s ){
418 #else
419 static void ThreadProc( void *s ){
420 #endif
421 #endif
423 #ifdef CONFIG_GUI
424 use_gui = 0; // mp_msg may not use gui stuff in forked code
425 #endif
426 // cache thread mainloop:
427 signal(SIGTERM,exit_sighandler); // kill
428 do {
429 if(!cache_fill(s)){
430 usec_sleep(FILL_USLEEP_TIME); // idle
432 // cache_stats(s->cache_data);
433 } while (cache_execute_control(s));
434 #if defined(__MINGW32__) || defined(__OS2__)
435 _endthread();
436 #endif
437 #ifdef PTHREAD_CACHE
438 return NULL;
439 #endif
440 // make sure forked code never leaves this function
441 exit(0);
444 int cache_stream_fill_buffer(stream_t *s){
445 int len;
446 if(s->eof){ s->buf_pos=s->buf_len=0; return 0; }
447 if(!s->cache_pid) return stream_fill_buffer(s);
449 // cache_stats(s->cache_data);
451 if(s->pos!=((cache_vars_t*)s->cache_data)->read_filepos) mp_msg(MSGT_CACHE,MSGL_ERR,"!!! read_filepos differs!!! report this bug...\n");
453 len=cache_read(s->cache_data,s->buffer, ((cache_vars_t*)s->cache_data)->sector_size);
454 //printf("cache_stream_fill_buffer->read -> %d\n",len);
456 if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; return 0; }
457 s->buf_pos=0;
458 s->buf_len=len;
459 s->pos+=len;
460 // printf("[%d]",len);fflush(stdout);
461 return len;
465 int cache_stream_seek_long(stream_t *stream,off_t pos){
466 cache_vars_t* s;
467 off_t newpos;
468 if(!stream->cache_pid) return stream_seek_long(stream,pos);
470 s=stream->cache_data;
471 // s->seek_lock=1;
473 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);
475 newpos=pos/s->sector_size; newpos*=s->sector_size; // align
476 stream->pos=s->read_filepos=newpos;
477 s->eof=0; // !!!!!!!
479 cache_stream_fill_buffer(stream);
481 pos-=newpos;
482 if(pos>=0 && pos<=stream->buf_len){
483 stream->buf_pos=pos; // byte position in sector
484 return 1;
487 // stream->buf_pos=stream->buf_len=0;
488 // return 1;
490 mp_msg(MSGT_CACHE,MSGL_V,"cache_stream_seek: WARNING! Can't seek to 0x%"PRIX64" !\n",(int64_t)(pos+newpos));
491 return 0;
494 int cache_do_control(stream_t *stream, int cmd, void *arg) {
495 cache_vars_t* s = stream->cache_data;
496 switch (cmd) {
497 case STREAM_CTRL_SEEK_TO_TIME:
498 s->control_double_arg = *(double *)arg;
499 s->control = cmd;
500 break;
501 case STREAM_CTRL_SEEK_TO_CHAPTER:
502 case STREAM_CTRL_SET_ANGLE:
503 s->control_uint_arg = *(unsigned *)arg;
504 s->control = cmd;
505 break;
506 // the core might call these every frame, they are too slow for this...
507 case STREAM_CTRL_GET_TIME_LENGTH:
508 // case STREAM_CTRL_GET_CURRENT_TIME:
509 *(double *)arg = s->stream_time_length;
510 return s->stream_time_length ? STREAM_OK : STREAM_UNSUPPORTED;
511 case STREAM_CTRL_GET_NUM_CHAPTERS:
512 case STREAM_CTRL_GET_CURRENT_CHAPTER:
513 case STREAM_CTRL_GET_ASPECT_RATIO:
514 case STREAM_CTRL_GET_NUM_ANGLES:
515 case STREAM_CTRL_GET_ANGLE:
516 case -2:
517 s->control = cmd;
518 break;
519 default:
520 return STREAM_UNSUPPORTED;
522 while (s->control != -1)
523 usec_sleep(CONTROL_SLEEP_TIME);
524 switch (cmd) {
525 case STREAM_CTRL_GET_TIME_LENGTH:
526 case STREAM_CTRL_GET_CURRENT_TIME:
527 case STREAM_CTRL_GET_ASPECT_RATIO:
528 *(double *)arg = s->control_double_arg;
529 break;
530 case STREAM_CTRL_GET_NUM_CHAPTERS:
531 case STREAM_CTRL_GET_CURRENT_CHAPTER:
532 case STREAM_CTRL_GET_NUM_ANGLES:
533 case STREAM_CTRL_GET_ANGLE:
534 *(unsigned *)arg = s->control_uint_arg;
535 break;
536 case STREAM_CTRL_SEEK_TO_CHAPTER:
537 case STREAM_CTRL_SEEK_TO_TIME:
538 case STREAM_CTRL_SET_ANGLE:
539 stream->pos = s->read_filepos = s->control_new_pos;
540 break;
542 return s->control_res;