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.
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
34 #include <sys/types.h>
38 #include "osdep/shmem.h"
39 #include "osdep/timer.h"
40 #if defined(__MINGW32__)
42 static void ThreadProc( void *s
);
43 #elif defined(__OS2__)
46 static void ThreadProc( void *s
);
47 #elif defined(PTHREAD_CACHE)
49 static void *ThreadProc(void *s
);
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
69 off_t min_filepos
; // buffer contain only a part of the file, from min-max pos
71 off_t offset
; // filepos <-> bufferpos offset value (filepos of the buffer's first byte)
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.
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
;
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
){
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
){
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
;
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
);
134 s
->read_filepos
+=len
;
139 cache_fill_status
=(s
->max_filepos
-s
->read_filepos
)/(s
->buffer_size
/ 100);
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
){
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
);
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
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
;
198 s
->min_filepos
=read
-back
; // avoid seeking-back to temp area...
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!
206 len
=stream_read(s
->stream
,&s
->buffer
[pos
],space
);
210 if(pos
+len
>=s
->buffer_size
){
212 s
->offset
+=s
->buffer_size
;
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
;
229 if (GetTimerMS() - last
> 99) {
231 if (s
->stream
->control(s
->stream
, STREAM_CTRL_GET_TIME_LENGTH
, &len
) == STREAM_OK
)
232 s
->stream_time_length
= len
;
234 s
->stream_time_length
= 0;
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
);
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
);
253 s
->control_res
= STREAM_UNSUPPORTED
;
256 s
->control_new_pos
= s
->stream
->pos
;
261 static cache_vars_t
* cache_init(int size
,int sector
){
263 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
264 cache_vars_t
* s
=shmem_alloc(sizeof(cache_vars_t
));
266 cache_vars_t
* s
=malloc(sizeof(cache_vars_t
));
268 if(s
==NULL
) return NULL
;
270 memset(s
,0,sizeof(cache_vars_t
));
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
);
280 s
->buffer
=malloc(s
->buffer_size
);
283 if(s
->buffer
== NULL
){
284 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__)
285 shmem_free(s
,sizeof(cache_vars_t
));
292 s
->fill_limit
=8*sector
;
293 s
->back_size
=s
->buffer_size
/2;
297 void cache_uninit(stream_t
*s
) {
298 cache_vars_t
* c
= s
->cache_data
;
300 #if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
301 cache_do_control(s
, -2, NULL
);
303 kill(s
->cache_pid
,SIGKILL
);
304 waitpid(s
->cache_pid
,NULL
,0);
309 #if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
315 shmem_free(c
->buffer
,c
->buffer_size
);
317 shmem_free(s
->cache_data
,sizeof(cache_vars_t
));
319 s
->cache_data
= NULL
;
322 static void exit_sighandler(int x
){
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
;
335 if (stream
->flags
& STREAM_NON_CACHEABLE
) {
336 mp_msg(MSGT_CACHE
,MSGL_STATUS
,"\rThis stream is non-cacheable\n");
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;
362 stream_t
* stream2
=malloc(sizeof(stream_t
));
363 memcpy(stream2
,s
->stream
,sizeof(stream_t
));
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
);
372 pthread_create(&tid
, NULL
, ThreadProc
, s
);
373 stream
->cache_pid
= 1;
377 if (!stream
->cache_pid
) {
378 mp_msg(MSGT_CACHE
, MSGL_ERR
,
379 "Starting cache process/thread failed: %s.\n", strerror(errno
));
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
)) {
396 mp_msg(MSGT_CACHE
,MSGL_STATUS
,"\n");
397 return 1; // parent exits
400 cache_uninit(stream
);
404 #if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__)
407 static void *ThreadProc( void *s
){
409 static void ThreadProc( void *s
){
413 // cache thread mainloop:
414 signal(SIGTERM
,exit_sighandler
); // kill
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__)
423 #elif defined(PTHREAD_CACHE)
426 // make sure forked code never leaves this function
431 int cache_stream_fill_buffer(stream_t
*s
){
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; }
447 // printf("[%d]",len);fflush(stdout);
452 int cache_stream_seek_long(stream_t
*stream
,off_t pos
){
455 if(!stream
->cache_pid
) return stream_seek_long(stream
,pos
);
457 s
=stream
->cache_data
;
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
;
466 cache_stream_fill_buffer(stream
);
469 if(pos
>=0 && pos
<=stream
->buf_len
){
470 stream
->buf_pos
=pos
; // byte position in sector
474 // stream->buf_pos=stream->buf_len=0;
477 mp_msg(MSGT_CACHE
,MSGL_V
,"cache_stream_seek: WARNING! Can't seek to 0x%"PRIX64
" !\n",(int64_t)(pos
+newpos
));
481 int cache_do_control(stream_t
*stream
, int cmd
, void *arg
) {
482 cache_vars_t
* s
= stream
->cache_data
;
484 case STREAM_CTRL_SEEK_TO_TIME
:
485 s
->control_double_arg
= *(double *)arg
;
488 case STREAM_CTRL_SEEK_TO_CHAPTER
:
489 case STREAM_CTRL_SET_ANGLE
:
490 s
->control_uint_arg
= *(unsigned *)arg
;
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
:
507 return STREAM_UNSUPPORTED
;
509 while (s
->control
!= -1)
510 usec_sleep(CONTROL_SLEEP_TIME
);
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
;
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
;
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
;
529 return s
->control_res
;