Mercurial > mplayer.hg
annotate stream/cache2.c @ 27981:53912f852a86
Move setting of frame_buffer variable out of 'if', as preferred by Reimar.
author | diego |
---|---|
date | Mon, 24 Nov 2008 08:40:11 +0000 |
parents | 4c2232462353 |
children | e54712304c3e |
rev | line source |
---|---|
2324 | 1 #include "config.h" |
2 | |
2322 | 3 // Initial draft of my new cache system... |
2352 | 4 // Note it runs in 2 processes (using fork()), but doesn't requires locking!! |
2322 | 5 // TODO: seeking, data consistency checking |
6 | |
2352 | 7 #define READ_USLEEP_TIME 10000 |
8 #define FILL_USLEEP_TIME 50000 | |
4825
41d2da3bd082
Make blocking call in libmpdemux interuptable (only with new input,
albeu
parents:
3726
diff
changeset
|
9 #define PREFILL_SLEEP_TIME 200 |
26833
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
10 #define CONTROL_SLEEP_TIME 0 |
2322 | 11 |
12 #include <stdio.h> | |
13 #include <stdlib.h> | |
14 #include <string.h> | |
15 #include <signal.h> | |
3726 | 16 #include <sys/types.h> |
17 #include <unistd.h> | |
2322 | 18 |
27894
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
19 #ifdef __CYGWIN__ |
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
20 #define PTHREAD_CACHE 1 |
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
21 #endif |
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
22 |
27723
fb67a8f56bfc
Unconditionally #include osdep/shem.h, fixes the warnings on Cygwin:
diego
parents:
27343
diff
changeset
|
23 #include "osdep/shmem.h" |
17012 | 24 #include "osdep/timer.h" |
27894
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
25 #if defined(__MINGW32__) |
26077 | 26 #include <windows.h> |
27770
c8d4cace053d
Avoid CreateThread and especially TerminateThread since they cause a memleak.
reimar
parents:
27769
diff
changeset
|
27 static void ThreadProc( void *s ); |
26077 | 28 #elif defined(__OS2__) |
29 #define INCL_DOS | |
30 #include <os2.h> | |
27756
1266470a5651
Revert declaring ThreadProc as void, it breaks the WINAPI.
diego
parents:
27727
diff
changeset
|
31 static void ThreadProc( void *s ); |
27894
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
32 #elif defined(PTHREAD_CACHE) |
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
33 #include <pthread.h> |
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
34 static void *ThreadProc(void *s); |
26077 | 35 #else |
10242 | 36 #include <sys/wait.h> |
10197 | 37 #endif |
2322 | 38 |
2371 | 39 #include "mp_msg.h" |
16793
8d4fb5469efb
Make a few more messages translatable by moving them into help_mp-en.h.
diego
parents:
16750
diff
changeset
|
40 #include "help_mp.h" |
2371 | 41 |
2322 | 42 #include "stream.h" |
27876
298a3cd86bbb
Include cache2.h in cache2.c, fixes an implicit declaration warning for cache_do_control
reimar
parents:
27770
diff
changeset
|
43 #include "cache2.h" |
24697
8f2154e066cf
Make sure forked code does not try to display a GTK message box (and thus crashes)
reimar
parents:
22142
diff
changeset
|
44 extern int use_gui; |
2322 | 45 |
46 int stream_fill_buffer(stream_t *s); | |
47 int stream_seek_long(stream_t *s,off_t pos); | |
48 | |
49 typedef struct { | |
50 // constats: | |
51 unsigned char *buffer; // base pointer of the alllocated buffer memory | |
2352 | 52 int buffer_size; // size of the alllocated buffer memory |
53 int sector_size; // size of a single sector (2048/2324) | |
54 int back_size; // we should keep back_size amount of old bytes for backward seek | |
55 int fill_limit; // we should fill buffer only if space>=fill_limit | |
16152
10a69a812eff
remove unused cache-prefill and create cache-seek-min that controls when seek_long is prefered over waiting for cache to fill
iive
parents:
12899
diff
changeset
|
56 int seek_limit; // keep filling cache if distanse is less that seek limit |
2374 | 57 // filler's pointers: |
58 int eof; | |
59 off_t min_filepos; // buffer contain only a part of the file, from min-max pos | |
60 off_t max_filepos; | |
61 off_t offset; // filepos <-> bufferpos offset value (filepos of the buffer's first byte) | |
2322 | 62 // reader's pointers: |
2374 | 63 off_t read_filepos; |
2322 | 64 // commands/locking: |
2352 | 65 // int seek_lock; // 1 if we will seek/reset buffer, 2 if we are ready for cmd |
66 // int fifo_flag; // 1 if we should use FIFO to notice cache about buffer reads. | |
2322 | 67 // callback |
68 stream_t* stream; | |
26833
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
69 volatile int control; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
70 volatile unsigned control_uint_arg; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
71 volatile double control_double_arg; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
72 volatile int control_res; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
73 volatile off_t control_new_pos; |
26847
4f875ae5d538
Emulate STREAM_CTRL_GET_TIME_LENGTH if cache is used.
reimar
parents:
26833
diff
changeset
|
74 volatile double stream_time_length; |
2322 | 75 } cache_vars_t; |
76 | |
2352 | 77 static int min_fill=0; |
78 | |
79 int cache_fill_status=0; | |
2322 | 80 |
81 void cache_stats(cache_vars_t* s){ | |
82 int newb=s->max_filepos-s->read_filepos; // new bytes in the buffer | |
18176
f72bc5754209
Part3 of Otvos Attila's oattila AT chello-hu mp_msg changes, with lots of modifications as usual
reynaldo
parents:
18067
diff
changeset
|
83 mp_msg(MSGT_CACHE,MSGL_INFO,"0x%06X [0x%06X] 0x%06X ",(int)s->min_filepos,(int)s->read_filepos,(int)s->max_filepos); |
f72bc5754209
Part3 of Otvos Attila's oattila AT chello-hu mp_msg changes, with lots of modifications as usual
reynaldo
parents:
18067
diff
changeset
|
84 mp_msg(MSGT_CACHE,MSGL_INFO,"%3d %% (%3d%%)\n",100*newb/s->buffer_size,100*min_fill/s->buffer_size); |
2322 | 85 } |
86 | |
87 int cache_read(cache_vars_t* s,unsigned char* buf,int size){ | |
88 int total=0; | |
89 while(size>0){ | |
90 int pos,newb,len; | |
2352 | 91 |
92 //printf("CACHE2_READ: 0x%X <= 0x%X <= 0x%X \n",s->min_filepos,s->read_filepos,s->max_filepos); | |
2322 | 93 |
2374 | 94 if(s->read_filepos>=s->max_filepos || s->read_filepos<s->min_filepos){ |
2352 | 95 // eof? |
96 if(s->eof) break; | |
97 // waiting for buffer fill... | |
10197 | 98 usec_sleep(READ_USLEEP_TIME); // 10ms |
2352 | 99 continue; // try again... |
100 } | |
101 | |
2374 | 102 newb=s->max_filepos-s->read_filepos; // new bytes in the buffer |
2322 | 103 if(newb<min_fill) min_fill=newb; // statistics... |
104 | |
105 // printf("*** newb: %d bytes ***\n",newb); | |
2352 | 106 |
107 pos=s->read_filepos - s->offset; | |
108 if(pos<0) pos+=s->buffer_size; else | |
109 if(pos>=s->buffer_size) pos-=s->buffer_size; | |
110 | |
2322 | 111 if(newb>s->buffer_size-pos) newb=s->buffer_size-pos; // handle wrap... |
112 if(newb>size) newb=size; | |
113 | |
2352 | 114 // check: |
2371 | 115 if(s->read_filepos<s->min_filepos) mp_msg(MSGT_CACHE,MSGL_ERR,"Ehh. s->read_filepos<s->min_filepos !!! Report bug...\n"); |
2352 | 116 |
2322 | 117 // len=write(mem,newb) |
118 //printf("Buffer read: %d bytes\n",newb); | |
119 memcpy(buf,&s->buffer[pos],newb); | |
120 buf+=newb; | |
2352 | 121 len=newb; |
2322 | 122 // ... |
123 | |
124 s->read_filepos+=len; | |
125 size-=len; | |
126 total+=len; | |
127 | |
128 } | |
18067
ac7eaa0313c2
avoid cache fill status overflow with caches > ca. 20 MB
reimar
parents:
17384
diff
changeset
|
129 cache_fill_status=(s->max_filepos-s->read_filepos)/(s->buffer_size / 100); |
2322 | 130 return total; |
131 } | |
132 | |
133 int cache_fill(cache_vars_t* s){ | |
7472
c4434bdf6e51
tons of warning fixes, also some 10l bugfixes, including Dominik's PVA bug
arpi
parents:
7204
diff
changeset
|
134 int back,back2,newb,space,len,pos; |
2374 | 135 off_t read=s->read_filepos; |
2322 | 136 |
2352 | 137 if(read<s->min_filepos || read>s->max_filepos){ |
138 // seek... | |
17366 | 139 mp_msg(MSGT_CACHE,MSGL_DBG2,"Out of boundaries... seeking to 0x%"PRIX64" \n",(int64_t)read); |
8938
fc21a94f98c6
do not discard cache content at seeking type=STREAMTYPE_STREAM
arpi
parents:
7862
diff
changeset
|
140 // streaming: drop cache contents only if seeking backward or too much fwd: |
fc21a94f98c6
do not discard cache content at seeking type=STREAMTYPE_STREAM
arpi
parents:
7862
diff
changeset
|
141 if(s->stream->type!=STREAMTYPE_STREAM || |
16152
10a69a812eff
remove unused cache-prefill and create cache-seek-min that controls when seek_long is prefered over waiting for cache to fill
iive
parents:
12899
diff
changeset
|
142 read<s->min_filepos || read>=s->max_filepos+s->seek_limit) |
8938
fc21a94f98c6
do not discard cache content at seeking type=STREAMTYPE_STREAM
arpi
parents:
7862
diff
changeset
|
143 { |
fc21a94f98c6
do not discard cache content at seeking type=STREAMTYPE_STREAM
arpi
parents:
7862
diff
changeset
|
144 s->offset= // FIXME!? |
fc21a94f98c6
do not discard cache content at seeking type=STREAMTYPE_STREAM
arpi
parents:
7862
diff
changeset
|
145 s->min_filepos=s->max_filepos=read; // drop cache content :( |
fc21a94f98c6
do not discard cache content at seeking type=STREAMTYPE_STREAM
arpi
parents:
7862
diff
changeset
|
146 if(s->stream->eof) stream_reset(s->stream); |
fc21a94f98c6
do not discard cache content at seeking type=STREAMTYPE_STREAM
arpi
parents:
7862
diff
changeset
|
147 stream_seek(s->stream,read); |
17366 | 148 mp_msg(MSGT_CACHE,MSGL_DBG2,"Seek done. new pos: 0x%"PRIX64" \n",(int64_t)stream_tell(s->stream)); |
8938
fc21a94f98c6
do not discard cache content at seeking type=STREAMTYPE_STREAM
arpi
parents:
7862
diff
changeset
|
149 } |
2352 | 150 } |
151 | |
2322 | 152 // calc number of back-bytes: |
153 back=read - s->min_filepos; | |
154 if(back<0) back=0; // strange... | |
155 if(back>s->back_size) back=s->back_size; | |
156 | |
157 // calc number of new bytes: | |
158 newb=s->max_filepos - read; | |
159 if(newb<0) newb=0; // strange... | |
160 | |
161 // calc free buffer space: | |
162 space=s->buffer_size - (newb+back); | |
163 | |
164 // calc bufferpos: | |
165 pos=s->max_filepos - s->offset; | |
166 if(pos>=s->buffer_size) pos-=s->buffer_size; // wrap-around | |
167 | |
168 if(space<s->fill_limit){ | |
169 // printf("Buffer is full (%d bytes free, limit: %d)\n",space,s->fill_limit); | |
170 return 0; // no fill... | |
171 } | |
172 | |
173 // printf("### read=0x%X back=%d newb=%d space=%d pos=%d\n",read,back,newb,space,pos); | |
174 | |
175 // reduce space if needed: | |
176 if(space>s->buffer_size-pos) space=s->buffer_size-pos; | |
177 | |
178 // if(space>32768) space=32768; // limit one-time block size | |
179 if(space>4*s->sector_size) space=4*s->sector_size; | |
180 | |
2352 | 181 // if(s->seek_lock) return 0; // FIXME |
182 | |
183 #if 1 | |
184 // back+newb+space <= buffer_size | |
185 back2=s->buffer_size-(space+newb); // max back size | |
186 if(s->min_filepos<(read-back2)) s->min_filepos=read-back2; | |
187 #else | |
2322 | 188 s->min_filepos=read-back; // avoid seeking-back to temp area... |
2352 | 189 #endif |
2322 | 190 |
191 // .... | |
192 //printf("Buffer fill: %d bytes of %d\n",space,s->buffer_size); | |
193 //len=stream_fill_buffer(s->stream); | |
194 //memcpy(&s->buffer[pos],s->stream->buffer,len); // avoid this extra copy! | |
195 // .... | |
2348 | 196 len=stream_read(s->stream,&s->buffer[pos],space); |
197 if(!len) s->eof=1; | |
2322 | 198 |
199 s->max_filepos+=len; | |
200 if(pos+len>=s->buffer_size){ | |
201 // wrap... | |
202 s->offset+=s->buffer_size; | |
203 } | |
204 | |
205 return len; | |
206 | |
207 } | |
208 | |
27770
c8d4cace053d
Avoid CreateThread and especially TerminateThread since they cause a memleak.
reimar
parents:
27769
diff
changeset
|
209 static int cache_execute_control(cache_vars_t *s) { |
c8d4cace053d
Avoid CreateThread and especially TerminateThread since they cause a memleak.
reimar
parents:
27769
diff
changeset
|
210 int res = 1; |
26847
4f875ae5d538
Emulate STREAM_CTRL_GET_TIME_LENGTH if cache is used.
reimar
parents:
26833
diff
changeset
|
211 static unsigned last; |
26897
23c3741dc490
Handle NULL control function in cache_execute_control, fixes crash with http urls.
reimar
parents:
26847
diff
changeset
|
212 if (!s->stream->control) { |
23c3741dc490
Handle NULL control function in cache_execute_control, fixes crash with http urls.
reimar
parents:
26847
diff
changeset
|
213 s->stream_time_length = 0; |
23c3741dc490
Handle NULL control function in cache_execute_control, fixes crash with http urls.
reimar
parents:
26847
diff
changeset
|
214 s->control_new_pos = 0; |
23c3741dc490
Handle NULL control function in cache_execute_control, fixes crash with http urls.
reimar
parents:
26847
diff
changeset
|
215 s->control_res = STREAM_UNSUPPORTED; |
23c3741dc490
Handle NULL control function in cache_execute_control, fixes crash with http urls.
reimar
parents:
26847
diff
changeset
|
216 s->control = -1; |
27770
c8d4cace053d
Avoid CreateThread and especially TerminateThread since they cause a memleak.
reimar
parents:
27769
diff
changeset
|
217 return res; |
26897
23c3741dc490
Handle NULL control function in cache_execute_control, fixes crash with http urls.
reimar
parents:
26847
diff
changeset
|
218 } |
26847
4f875ae5d538
Emulate STREAM_CTRL_GET_TIME_LENGTH if cache is used.
reimar
parents:
26833
diff
changeset
|
219 if (GetTimerMS() - last > 99) { |
4f875ae5d538
Emulate STREAM_CTRL_GET_TIME_LENGTH if cache is used.
reimar
parents:
26833
diff
changeset
|
220 double len; |
4f875ae5d538
Emulate STREAM_CTRL_GET_TIME_LENGTH if cache is used.
reimar
parents:
26833
diff
changeset
|
221 if (s->stream->control(s->stream, STREAM_CTRL_GET_TIME_LENGTH, &len) == STREAM_OK) |
4f875ae5d538
Emulate STREAM_CTRL_GET_TIME_LENGTH if cache is used.
reimar
parents:
26833
diff
changeset
|
222 s->stream_time_length = len; |
4f875ae5d538
Emulate STREAM_CTRL_GET_TIME_LENGTH if cache is used.
reimar
parents:
26833
diff
changeset
|
223 else |
4f875ae5d538
Emulate STREAM_CTRL_GET_TIME_LENGTH if cache is used.
reimar
parents:
26833
diff
changeset
|
224 s->stream_time_length = 0; |
4f875ae5d538
Emulate STREAM_CTRL_GET_TIME_LENGTH if cache is used.
reimar
parents:
26833
diff
changeset
|
225 last = GetTimerMS(); |
4f875ae5d538
Emulate STREAM_CTRL_GET_TIME_LENGTH if cache is used.
reimar
parents:
26833
diff
changeset
|
226 } |
27770
c8d4cace053d
Avoid CreateThread and especially TerminateThread since they cause a memleak.
reimar
parents:
27769
diff
changeset
|
227 if (s->control == -1) return res; |
26833
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
228 switch (s->control) { |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
229 case STREAM_CTRL_GET_CURRENT_TIME: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
230 case STREAM_CTRL_SEEK_TO_TIME: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
231 case STREAM_CTRL_GET_ASPECT_RATIO: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
232 s->control_res = s->stream->control(s->stream, s->control, &s->control_double_arg); |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
233 break; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
234 case STREAM_CTRL_SEEK_TO_CHAPTER: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
235 case STREAM_CTRL_GET_NUM_CHAPTERS: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
236 case STREAM_CTRL_GET_CURRENT_CHAPTER: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
237 case STREAM_CTRL_GET_NUM_ANGLES: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
238 case STREAM_CTRL_GET_ANGLE: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
239 case STREAM_CTRL_SET_ANGLE: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
240 s->control_res = s->stream->control(s->stream, s->control, &s->control_uint_arg); |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
241 break; |
27770
c8d4cace053d
Avoid CreateThread and especially TerminateThread since they cause a memleak.
reimar
parents:
27769
diff
changeset
|
242 case -2: |
c8d4cace053d
Avoid CreateThread and especially TerminateThread since they cause a memleak.
reimar
parents:
27769
diff
changeset
|
243 res = 0; |
26833
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
244 default: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
245 s->control_res = STREAM_UNSUPPORTED; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
246 break; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
247 } |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
248 s->control_new_pos = s->stream->pos; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
249 s->control = -1; |
27770
c8d4cace053d
Avoid CreateThread and especially TerminateThread since they cause a memleak.
reimar
parents:
27769
diff
changeset
|
250 return res; |
26833
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
251 } |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
252 |
2322 | 253 cache_vars_t* cache_init(int size,int sector){ |
254 int num; | |
27894
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
255 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__) |
2322 | 256 cache_vars_t* s=shmem_alloc(sizeof(cache_vars_t)); |
10197 | 257 #else |
258 cache_vars_t* s=malloc(sizeof(cache_vars_t)); | |
259 #endif | |
12899 | 260 if(s==NULL) return NULL; |
261 | |
2322 | 262 memset(s,0,sizeof(cache_vars_t)); |
263 num=size/sector; | |
12835
4235ae5a2d60
cache min fill adjustment, based on patch by Jeremy Huddleston
iive
parents:
10272
diff
changeset
|
264 if(num < 16){ |
4235ae5a2d60
cache min fill adjustment, based on patch by Jeremy Huddleston
iive
parents:
10272
diff
changeset
|
265 num = 16; |
4235ae5a2d60
cache min fill adjustment, based on patch by Jeremy Huddleston
iive
parents:
10272
diff
changeset
|
266 }//32kb min_size |
2322 | 267 s->buffer_size=num*sector; |
268 s->sector_size=sector; | |
27894
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
269 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__) |
2322 | 270 s->buffer=shmem_alloc(s->buffer_size); |
10197 | 271 #else |
272 s->buffer=malloc(s->buffer_size); | |
273 #endif | |
12899 | 274 |
275 if(s->buffer == NULL){ | |
27894
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
276 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__) |
12899 | 277 shmem_free(s,sizeof(cache_vars_t)); |
278 #else | |
279 free(s); | |
280 #endif | |
281 return NULL; | |
282 } | |
283 | |
2322 | 284 s->fill_limit=8*sector; |
12835
4235ae5a2d60
cache min fill adjustment, based on patch by Jeremy Huddleston
iive
parents:
10272
diff
changeset
|
285 s->back_size=s->buffer_size/2; |
2322 | 286 return s; |
287 } | |
288 | |
9915 | 289 void cache_uninit(stream_t *s) { |
290 cache_vars_t* c = s->cache_data; | |
291 if(!s->cache_pid) return; | |
27894
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
292 #if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__) |
27770
c8d4cace053d
Avoid CreateThread and especially TerminateThread since they cause a memleak.
reimar
parents:
27769
diff
changeset
|
293 cache_do_control(s, -2, NULL); |
26077 | 294 #else |
9915 | 295 kill(s->cache_pid,SIGKILL); |
296 waitpid(s->cache_pid,NULL,0); | |
10197 | 297 #endif |
9915 | 298 if(!c) return; |
27894
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
299 #if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__) |
26077 | 300 free(c->stream); |
301 free(c->buffer); | |
302 free(s->cache_data); | |
303 #else | |
9915 | 304 shmem_free(c->buffer,c->buffer_size); |
305 shmem_free(s->cache_data,sizeof(cache_vars_t)); | |
10197 | 306 #endif |
9915 | 307 } |
308 | |
2322 | 309 static void exit_sighandler(int x){ |
310 // close stream | |
311 exit(0); | |
312 } | |
313 | |
16152
10a69a812eff
remove unused cache-prefill and create cache-seek-min that controls when seek_long is prefered over waiting for cache to fill
iive
parents:
12899
diff
changeset
|
314 int stream_enable_cache(stream_t *stream,int size,int min,int seek_limit){ |
25445
959fca775f43
Fix stream_cache to use sector_size set in stream_t.
ulion
parents:
24697
diff
changeset
|
315 int ss = stream->sector_size ? stream->sector_size : STREAM_BUFFER_SIZE; |
5991 | 316 cache_vars_t* s; |
7006
c0b490505298
disable cache if stream->fd<0 (no regular file/pipe but some special thing)
arpi
parents:
5991
diff
changeset
|
317 |
7204 | 318 if (stream->type==STREAMTYPE_STREAM && stream->fd < 0) { |
319 // The stream has no 'fd' behind it, so is non-cacheable | |
320 mp_msg(MSGT_CACHE,MSGL_STATUS,"\rThis stream is non-cacheable\n"); | |
321 return 1; | |
322 } | |
7006
c0b490505298
disable cache if stream->fd<0 (no regular file/pipe but some special thing)
arpi
parents:
5991
diff
changeset
|
323 |
5991 | 324 s=cache_init(size,ss); |
12899 | 325 if(s == NULL) return 0; |
3562 | 326 stream->cache_data=s; |
327 s->stream=stream; // callback | |
16152
10a69a812eff
remove unused cache-prefill and create cache-seek-min that controls when seek_long is prefered over waiting for cache to fill
iive
parents:
12899
diff
changeset
|
328 s->seek_limit=seek_limit; |
12835
4235ae5a2d60
cache min fill adjustment, based on patch by Jeremy Huddleston
iive
parents:
10272
diff
changeset
|
329 |
4235ae5a2d60
cache min fill adjustment, based on patch by Jeremy Huddleston
iive
parents:
10272
diff
changeset
|
330 |
4235ae5a2d60
cache min fill adjustment, based on patch by Jeremy Huddleston
iive
parents:
10272
diff
changeset
|
331 //make sure that we won't wait from cache_fill |
4235ae5a2d60
cache min fill adjustment, based on patch by Jeremy Huddleston
iive
parents:
10272
diff
changeset
|
332 //more data than it is alowed to fill |
16152
10a69a812eff
remove unused cache-prefill and create cache-seek-min that controls when seek_long is prefered over waiting for cache to fill
iive
parents:
12899
diff
changeset
|
333 if (s->seek_limit > s->buffer_size - s->fill_limit ){ |
10a69a812eff
remove unused cache-prefill and create cache-seek-min that controls when seek_long is prefered over waiting for cache to fill
iive
parents:
12899
diff
changeset
|
334 s->seek_limit = s->buffer_size - s->fill_limit; |
12835
4235ae5a2d60
cache min fill adjustment, based on patch by Jeremy Huddleston
iive
parents:
10272
diff
changeset
|
335 } |
4235ae5a2d60
cache min fill adjustment, based on patch by Jeremy Huddleston
iive
parents:
10272
diff
changeset
|
336 if (min > s->buffer_size - s->fill_limit) { |
4235ae5a2d60
cache min fill adjustment, based on patch by Jeremy Huddleston
iive
parents:
10272
diff
changeset
|
337 min = s->buffer_size - s->fill_limit; |
4235ae5a2d60
cache min fill adjustment, based on patch by Jeremy Huddleston
iive
parents:
10272
diff
changeset
|
338 } |
3562 | 339 |
27894
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
340 #if !defined(__MINGW32__) && !defined(PTHREAD_CACHE) && !defined(__OS2__) |
3562 | 341 if((stream->cache_pid=fork())){ |
10197 | 342 #else |
343 { | |
344 stream_t* stream2=malloc(sizeof(stream_t)); | |
345 memcpy(stream2,s->stream,sizeof(stream_t)); | |
346 s->stream=stream2; | |
27894
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
347 #if defined(__MINGW32__) |
27770
c8d4cace053d
Avoid CreateThread and especially TerminateThread since they cause a memleak.
reimar
parents:
27769
diff
changeset
|
348 stream->cache_pid = _beginthread( ThreadProc, 0, s ); |
27894
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
349 #elif defined(__OS2__) |
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
350 stream->cache_pid = _beginthread( ThreadProc, NULL, 256 * 1024, s ); |
27770
c8d4cace053d
Avoid CreateThread and especially TerminateThread since they cause a memleak.
reimar
parents:
27769
diff
changeset
|
351 #else |
27896
4c2232462353
100l, stream->cache_pid can not be used directly in pthread_create,
reimar
parents:
27894
diff
changeset
|
352 { |
4c2232462353
100l, stream->cache_pid can not be used directly in pthread_create,
reimar
parents:
27894
diff
changeset
|
353 pthread_t tid; |
4c2232462353
100l, stream->cache_pid can not be used directly in pthread_create,
reimar
parents:
27894
diff
changeset
|
354 pthread_create(&tid, NULL, ThreadProc, s); |
4c2232462353
100l, stream->cache_pid can not be used directly in pthread_create,
reimar
parents:
27894
diff
changeset
|
355 stream->cache_pid = 1; |
4c2232462353
100l, stream->cache_pid can not be used directly in pthread_create,
reimar
parents:
27894
diff
changeset
|
356 } |
26077 | 357 #endif |
10197 | 358 #endif |
3562 | 359 // wait until cache is filled at least prefill_init % |
17366 | 360 mp_msg(MSGT_CACHE,MSGL_V,"CACHE_PRE_INIT: %"PRId64" [%"PRId64"] %"PRId64" pre:%d eof:%d \n", |
17384 | 361 (int64_t)s->min_filepos,(int64_t)s->read_filepos,(int64_t)s->max_filepos,min,s->eof); |
3562 | 362 while(s->read_filepos<s->min_filepos || s->max_filepos-s->read_filepos<min){ |
16793
8d4fb5469efb
Make a few more messages translatable by moving them into help_mp-en.h.
diego
parents:
16750
diff
changeset
|
363 mp_msg(MSGT_CACHE,MSGL_STATUS,MSGTR_CacheFill, |
3600 | 364 100.0*(float)(s->max_filepos-s->read_filepos)/(float)(s->buffer_size), |
17366 | 365 (int64_t)s->max_filepos-s->read_filepos |
3562 | 366 ); |
367 if(s->eof) break; // file is smaller than prefill size | |
26326
5bfc1d8bece9
Remove the need for code using stream to export an mp_input_check_interrupt()
albeu
parents:
26077
diff
changeset
|
368 if(stream_check_interrupt(PREFILL_SLEEP_TIME)) |
4825
41d2da3bd082
Make blocking call in libmpdemux interuptable (only with new input,
albeu
parents:
3726
diff
changeset
|
369 return 0; |
3562 | 370 } |
16870 | 371 mp_msg(MSGT_CACHE,MSGL_STATUS,"\n"); |
4825
41d2da3bd082
Make blocking call in libmpdemux interuptable (only with new input,
albeu
parents:
3726
diff
changeset
|
372 return 1; // parent exits |
3562 | 373 } |
374 | |
27894
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
375 #if defined(__MINGW32__) || defined(PTHREAD_CACHE) || defined(__OS2__) |
10197 | 376 } |
27894
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
377 #ifdef PTHREAD_CACHE |
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
378 static void *ThreadProc( void *s ){ |
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
379 #else |
26077 | 380 static void ThreadProc( void *s ){ |
381 #endif | |
27894
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
382 #endif |
10197 | 383 |
27343 | 384 #ifdef CONFIG_GUI |
24697
8f2154e066cf
Make sure forked code does not try to display a GTK message box (and thus crashes)
reimar
parents:
22142
diff
changeset
|
385 use_gui = 0; // mp_msg may not use gui stuff in forked code |
8f2154e066cf
Make sure forked code does not try to display a GTK message box (and thus crashes)
reimar
parents:
22142
diff
changeset
|
386 #endif |
2322 | 387 // cache thread mainloop: |
388 signal(SIGTERM,exit_sighandler); // kill | |
27770
c8d4cace053d
Avoid CreateThread and especially TerminateThread since they cause a memleak.
reimar
parents:
27769
diff
changeset
|
389 do { |
27769 | 390 if(!cache_fill(s)){ |
10197 | 391 usec_sleep(FILL_USLEEP_TIME); // idle |
2322 | 392 } |
2327 | 393 // cache_stats(s->cache_data); |
27770
c8d4cace053d
Avoid CreateThread and especially TerminateThread since they cause a memleak.
reimar
parents:
27769
diff
changeset
|
394 } while (cache_execute_control(s)); |
27894
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
395 #if defined(__MINGW32__) || defined(__OS2__) |
27770
c8d4cace053d
Avoid CreateThread and especially TerminateThread since they cause a memleak.
reimar
parents:
27769
diff
changeset
|
396 _endthread(); |
c8d4cace053d
Avoid CreateThread and especially TerminateThread since they cause a memleak.
reimar
parents:
27769
diff
changeset
|
397 #endif |
27894
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
398 #ifdef PTHREAD_CACHE |
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
399 return NULL; |
d06d8e459ae1
Use pthreads for the cache on Cygwin, since _beginthread is not available
reimar
parents:
27876
diff
changeset
|
400 #endif |
2322 | 401 } |
402 | |
403 int cache_stream_fill_buffer(stream_t *s){ | |
404 int len; | |
405 if(s->eof){ s->buf_pos=s->buf_len=0; return 0; } | |
406 if(!s->cache_pid) return stream_fill_buffer(s); | |
407 | |
2352 | 408 // cache_stats(s->cache_data); |
409 | |
2371 | 410 if(s->pos!=((cache_vars_t*)s->cache_data)->read_filepos) mp_msg(MSGT_CACHE,MSGL_ERR,"!!! read_filepos differs!!! report this bug...\n"); |
2327 | 411 |
2322 | 412 len=cache_read(s->cache_data,s->buffer, ((cache_vars_t*)s->cache_data)->sector_size); |
2352 | 413 //printf("cache_stream_fill_buffer->read -> %d\n",len); |
2327 | 414 |
2322 | 415 if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; return 0; } |
416 s->buf_pos=0; | |
417 s->buf_len=len; | |
418 s->pos+=len; | |
419 // printf("[%d]",len);fflush(stdout); | |
420 return len; | |
421 | |
422 } | |
423 | |
2352 | 424 int cache_stream_seek_long(stream_t *stream,off_t pos){ |
425 cache_vars_t* s; | |
426 off_t newpos; | |
427 if(!stream->cache_pid) return stream_seek_long(stream,pos); | |
428 | |
429 s=stream->cache_data; | |
430 // s->seek_lock=1; | |
431 | |
17366 | 432 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); |
2322 | 433 |
2352 | 434 newpos=pos/s->sector_size; newpos*=s->sector_size; // align |
435 stream->pos=s->read_filepos=newpos; | |
436 s->eof=0; // !!!!!!! | |
437 | |
438 cache_stream_fill_buffer(stream); | |
2322 | 439 |
2352 | 440 pos-=newpos; |
441 if(pos>=0 && pos<=stream->buf_len){ | |
442 stream->buf_pos=pos; // byte position in sector | |
443 return 1; | |
444 } | |
445 | |
446 // stream->buf_pos=stream->buf_len=0; | |
447 // return 1; | |
448 | |
16750
0a31740dd5e6
Use PRI?64 defines as format strings for 64 bit variables.
reimar
parents:
16152
diff
changeset
|
449 mp_msg(MSGT_CACHE,MSGL_V,"cache_stream_seek: WARNING! Can't seek to 0x%"PRIX64" !\n",(int64_t)(pos+newpos)); |
2322 | 450 return 0; |
451 } | |
26833
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
452 |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
453 int cache_do_control(stream_t *stream, int cmd, void *arg) { |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
454 cache_vars_t* s = stream->cache_data; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
455 switch (cmd) { |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
456 case STREAM_CTRL_SEEK_TO_TIME: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
457 s->control_double_arg = *(double *)arg; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
458 s->control = cmd; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
459 break; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
460 case STREAM_CTRL_SEEK_TO_CHAPTER: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
461 case STREAM_CTRL_SET_ANGLE: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
462 s->control_uint_arg = *(unsigned *)arg; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
463 s->control = cmd; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
464 break; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
465 // the core might call these every frame, they are too slow for this... |
26847
4f875ae5d538
Emulate STREAM_CTRL_GET_TIME_LENGTH if cache is used.
reimar
parents:
26833
diff
changeset
|
466 case STREAM_CTRL_GET_TIME_LENGTH: |
26924
ca50c4a72f68
100l, fix wrong order of cases in cache_do_control
reimar
parents:
26897
diff
changeset
|
467 // case STREAM_CTRL_GET_CURRENT_TIME: |
26847
4f875ae5d538
Emulate STREAM_CTRL_GET_TIME_LENGTH if cache is used.
reimar
parents:
26833
diff
changeset
|
468 *(double *)arg = s->stream_time_length; |
4f875ae5d538
Emulate STREAM_CTRL_GET_TIME_LENGTH if cache is used.
reimar
parents:
26833
diff
changeset
|
469 return s->stream_time_length ? STREAM_OK : STREAM_UNSUPPORTED; |
26924
ca50c4a72f68
100l, fix wrong order of cases in cache_do_control
reimar
parents:
26897
diff
changeset
|
470 case STREAM_CTRL_GET_NUM_CHAPTERS: |
ca50c4a72f68
100l, fix wrong order of cases in cache_do_control
reimar
parents:
26897
diff
changeset
|
471 case STREAM_CTRL_GET_CURRENT_CHAPTER: |
26833
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
472 case STREAM_CTRL_GET_ASPECT_RATIO: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
473 case STREAM_CTRL_GET_NUM_ANGLES: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
474 case STREAM_CTRL_GET_ANGLE: |
27770
c8d4cace053d
Avoid CreateThread and especially TerminateThread since they cause a memleak.
reimar
parents:
27769
diff
changeset
|
475 case -2: |
26833
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
476 s->control = cmd; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
477 break; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
478 default: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
479 return STREAM_UNSUPPORTED; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
480 } |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
481 while (s->control != -1) |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
482 usec_sleep(CONTROL_SLEEP_TIME); |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
483 switch (cmd) { |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
484 case STREAM_CTRL_GET_TIME_LENGTH: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
485 case STREAM_CTRL_GET_CURRENT_TIME: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
486 case STREAM_CTRL_GET_ASPECT_RATIO: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
487 *(double *)arg = s->control_double_arg; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
488 break; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
489 case STREAM_CTRL_GET_NUM_CHAPTERS: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
490 case STREAM_CTRL_GET_CURRENT_CHAPTER: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
491 case STREAM_CTRL_GET_NUM_ANGLES: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
492 case STREAM_CTRL_GET_ANGLE: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
493 *(unsigned *)arg = s->control_uint_arg; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
494 break; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
495 case STREAM_CTRL_SEEK_TO_CHAPTER: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
496 case STREAM_CTRL_SEEK_TO_TIME: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
497 case STREAM_CTRL_SET_ANGLE: |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
498 stream->pos = s->read_filepos = s->control_new_pos; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
499 break; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
500 } |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
501 return s->control_res; |
77003eb2d9a8
Add basic support for stream controls with cache enabled.
reimar
parents:
26326
diff
changeset
|
502 } |