summaryrefslogtreecommitdiffstats
path: root/stream
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2013-06-05 01:58:36 +0200
committerwm4 <wm4@nowhere>2013-06-09 22:04:56 +0200
commit40567fb1011f266cf04dfd92bf4943d93f890329 (patch)
treee4cfe6f1909961f56180ab33cfe19acbaed0bdc7 /stream
parent4af59abbb416a365a98efca8875c0f0c1b0c872f (diff)
downloadmpv-40567fb1011f266cf04dfd92bf4943d93f890329.tar.bz2
mpv-40567fb1011f266cf04dfd92bf4943d93f890329.tar.xz
cache2: uncrustify
Diffstat (limited to 'stream')
-rw-r--r--stream/cache2.c973
1 files changed, 521 insertions, 452 deletions
diff --git a/stream/cache2.c b/stream/cache2.c
index 302bf6ba17..a0ad2559ae 100644
--- a/stream/cache2.c
+++ b/stream/cache2.c
@@ -45,7 +45,7 @@
#include "osdep/timer.h"
#if defined(__MINGW32__)
#include <windows.h>
-static void ThreadProc( void *s );
+static void ThreadProc(void *s);
#elif defined(PTHREAD_CACHE)
#include <pthread.h>
static void *ThreadProc(void *s);
@@ -64,291 +64,316 @@ static void *ThreadProc(void *s);
#include "core/mp_common.h"
typedef struct {
- // constats:
- unsigned char *buffer; // base pointer of the allocated buffer memory
- int64_t buffer_size; // size of the allocated buffer memory
- int sector_size; // size of a single sector (2048/2324)
- int64_t back_size; // we should keep back_size amount of old bytes for backward seek
- int64_t fill_limit; // we should fill buffer only if space>=fill_limit
- int64_t seek_limit; // keep filling cache if distance is less that seek limit
+ // constats:
+ unsigned char *buffer; // base pointer of the allocated buffer memory
+ int64_t buffer_size; // size of the allocated buffer memory
+ int sector_size; // size of a single sector (2048/2324)
+ int64_t back_size; // we should keep back_size amount of old bytes for backward seek
+ int64_t fill_limit; // we should fill buffer only if space>=fill_limit
+ int64_t seek_limit; // keep filling cache if distance is less that seek limit
#if FORKED_CACHE
- pid_t ppid; // parent PID to detect killed parent
+ pid_t ppid; // parent PID to detect killed parent
#endif
- // filler's pointers:
- int eof;
- int64_t min_filepos; // buffer contain only a part of the file, from min-max pos
- int64_t max_filepos;
- int64_t offset; // filepos <-> bufferpos offset value (filepos of the buffer's first byte)
- // reader's pointers:
- int64_t read_filepos;
- // commands/locking:
+ // filler's pointers:
+ int eof;
+ int64_t min_filepos; // buffer contain only a part of the file, from min-max pos
+ int64_t max_filepos;
+ int64_t offset; // filepos <-> bufferpos offset value (filepos of the buffer's first byte)
+ // reader's pointers:
+ int64_t read_filepos;
+ // commands/locking:
// int seek_lock; // 1 if we will seek/reset buffer, 2 if we are ready for cmd
// int fifo_flag; // 1 if we should use FIFO to notice cache about buffer reads.
- // callback
- stream_t* stream;
- volatile int control;
- volatile uint64_t control_uint_arg;
- volatile double control_double_arg;
- volatile struct stream_lang_req control_lang_arg;
- volatile int control_res;
- volatile double stream_time_length;
- volatile double stream_time_pos;
- volatile double stream_start_time;
- volatile int idle;
+// callback
+ stream_t *stream;
+ volatile int control;
+ volatile uint64_t control_uint_arg;
+ volatile double control_double_arg;
+ volatile struct stream_lang_req control_lang_arg;
+ volatile int control_res;
+ volatile double stream_time_length;
+ volatile double stream_time_pos;
+ volatile double stream_start_time;
+ volatile int idle;
} cache_vars_t;
static void cache_wakeup(stream_t *s)
{
#if FORKED_CACHE
- // signal process to wake up immediately
- kill(s->cache_pid, SIGUSR1);
+ // signal process to wake up immediately
+ kill(s->cache_pid, SIGUSR1);
#endif
}
static void cache_flush(cache_vars_t *s)
{
- s->offset= // FIXME!?
- s->min_filepos=s->max_filepos=s->read_filepos; // drop cache content :(
+ s->offset = s->min_filepos = s->max_filepos = s->read_filepos; // drop cache content :(
}
static int cache_read(cache_vars_t *s, unsigned char *buf, int size)
{
- int total=0;
- int sleep_count = 0;
- int64_t last_max = s->max_filepos;
- while(size>0){
- int64_t pos,newb,len;
-
- //printf("CACHE2_READ: 0x%X <= 0x%X <= 0x%X \n",s->min_filepos,s->read_filepos,s->max_filepos);
-
- if(s->read_filepos>=s->max_filepos || s->read_filepos<s->min_filepos){
- // eof?
- if(s->eof) break;
- if (s->max_filepos == last_max) {
- if (sleep_count++ == 10)
- mp_msg(MSGT_CACHE, MSGL_WARN, "Cache empty, consider increasing -cache and/or -cache-min. [performance issue]\n");
- } else {
- last_max = s->max_filepos;
- sleep_count = 0;
- }
- // waiting for buffer fill...
- if (stream_check_interrupt(READ_SLEEP_TIME)) {
- s->eof = 1;
- break;
- }
- continue; // try again...
- }
- sleep_count = 0;
+ int total = 0;
+ int sleep_count = 0;
+ int64_t last_max = s->max_filepos;
+ while (size > 0) {
+ int64_t pos, newb, len;
+
+ //printf("CACHE2_READ: 0x%X <= 0x%X <= 0x%X \n",s->min_filepos,s->read_filepos,s->max_filepos);
+
+ if (s->read_filepos >= s->max_filepos || s->read_filepos <
+ s->min_filepos) {
+ // eof?
+ if (s->eof)
+ break;
+ if (s->max_filepos == last_max) {
+ if (sleep_count++ == 10)
+ mp_msg(MSGT_CACHE, MSGL_WARN, "Cache empty, consider "
+ "increasing -cache and/or -cache-min. "
+ "[performance issue]\n");
+ } else {
+ last_max = s->max_filepos;
+ sleep_count = 0;
+ }
+ // waiting for buffer fill...
+ if (stream_check_interrupt(READ_SLEEP_TIME)) {
+ s->eof = 1;
+ break;
+ }
+ continue; // try again...
+ }
+ sleep_count = 0;
- newb=s->max_filepos-s->read_filepos; // new bytes in the buffer
+ newb = s->max_filepos - s->read_filepos; // new bytes in the buffer
// printf("*** newb: %d bytes ***\n",newb);
- pos=s->read_filepos - s->offset;
- if(pos<0) pos+=s->buffer_size; else
- if(pos>=s->buffer_size) pos-=s->buffer_size;
-
- if(newb>s->buffer_size-pos) newb=s->buffer_size-pos; // handle wrap...
- if(newb>size) newb=size;
-
- // check:
- if(s->read_filepos<s->min_filepos) mp_msg(MSGT_CACHE,MSGL_ERR,"Ehh. s->read_filepos<s->min_filepos !!! Report bug...\n");
+ pos = s->read_filepos - s->offset;
+ if (pos < 0)
+ pos += s->buffer_size;
+ else if (pos >= s->buffer_size)
+ pos -= s->buffer_size;
+
+ if (newb > s->buffer_size - pos)
+ newb = s->buffer_size - pos; // handle wrap...
+ if (newb > size)
+ newb = size;
+
+ // check:
+ if (s->read_filepos < s->min_filepos)
+ mp_msg(MSGT_CACHE, MSGL_ERR,
+ "Ehh. s->read_filepos<s->min_filepos !!! Report bug...\n");
+
+ // len=write(mem,newb)
+ //printf("Buffer read: %d bytes\n",newb);
+ memcpy(buf, &s->buffer[pos], newb);
+ buf += newb;
+ len = newb;
+ // ...
+
+ s->read_filepos += len;
+ size -= len;
+ total += len;
- // len=write(mem,newb)
- //printf("Buffer read: %d bytes\n",newb);
- memcpy(buf,&s->buffer[pos],newb);
- buf+=newb;
- len=newb;
- // ...
-
- s->read_filepos+=len;
- size-=len;
- total+=len;
-
- }
- return total;
+ }
+ return total;
}
static int cache_fill(cache_vars_t *s)
{
- int64_t back,back2,newb,space,len,pos;
- int64_t read=s->read_filepos;
- int read_chunk;
- int wraparound_copy = 0;
-
- if(read<s->min_filepos || read>s->max_filepos){
- // seek...
- mp_msg(MSGT_CACHE,MSGL_DBG2,"Out of boundaries... seeking to 0x%"PRIX64" \n",read);
- // drop cache contents only if seeking backward or too much fwd.
- // This is also done for on-disk files, since it loses the backseek cache.
- // That in turn can cause major bandwidth increase and performance
- // issues with e.g. mov or badly interleaved files
- if(read<s->min_filepos || read>=s->max_filepos+s->seek_limit)
- {
- cache_flush(s);
- if(s->stream->eof) stream_reset(s->stream);
- stream_seek_internal(s->stream,read);
- mp_msg(MSGT_CACHE,MSGL_DBG2,"Seek done. new pos: 0x%"PRIX64" \n",(int64_t)stream_tell(s->stream));
- }
- }
+ int64_t back, back2, newb, space, len, pos;
+ int64_t read = s->read_filepos;
+ int read_chunk;
+ int wraparound_copy = 0;
+
+ if (read < s->min_filepos || read > s->max_filepos) {
+ // seek...
+ mp_msg(MSGT_CACHE, MSGL_DBG2,
+ "Out of boundaries... seeking to 0x%" PRIX64 " \n", read);
+ // drop cache contents only if seeking backward or too much fwd.
+ // This is also done for on-disk files, since it loses the backseek cache.
+ // That in turn can cause major bandwidth increase and performance
+ // issues with e.g. mov or badly interleaved files
+ if (read < s->min_filepos || read >= s->max_filepos + s->seek_limit) {
+ cache_flush(s);
+ if (s->stream->eof)
+ stream_reset(s->stream);
+ stream_seek_internal(s->stream, read);
+ mp_msg(MSGT_CACHE, MSGL_DBG2, "Seek done. new pos: 0x%" PRIX64 " \n",
+ (int64_t)stream_tell(s->stream));
+ }
+ }
- // calc number of back-bytes:
- back=read - s->min_filepos;
- if(back<0) back=0; // strange...
- if(back>s->back_size) back=s->back_size;
+ // calc number of back-bytes:
+ back = read - s->min_filepos;
+ if (back < 0)
+ back = 0; // strange...
+ if (back > s->back_size)
+ back = s->back_size;
- // calc number of new bytes:
- newb=s->max_filepos - read;
- if(newb<0) newb=0; // strange...
+ // calc number of new bytes:
+ newb = s->max_filepos - read;
+ if (newb < 0)
+ newb = 0; // strange...
- // calc free buffer space:
- space=s->buffer_size - (newb+back);
+ // calc free buffer space:
+ space = s->buffer_size - (newb + back);
- // calc bufferpos:
- pos=s->max_filepos - s->offset;
- if(pos>=s->buffer_size) pos-=s->buffer_size; // wrap-around
+ // calc bufferpos:
+ pos = s->max_filepos - s->offset;
+ if (pos >= s->buffer_size)
+ pos -= s->buffer_size; // wrap-around
- if(space<s->fill_limit){
+ if (space < s->fill_limit) {
// printf("Buffer is full (%d bytes free, limit: %d)\n",space,s->fill_limit);
- return 0; // no fill...
- }
+ return 0; // no fill...
+ }
// printf("### read=0x%X back=%d newb=%d space=%d pos=%d\n",read,back,newb,space,pos);
- // try to avoid wrap-around. If not possible due to sector size
- // do an extra copy.
- if(space>s->buffer_size-pos) {
- if (s->buffer_size-pos >= s->sector_size) {
- space=s->buffer_size-pos;
- } else {
- space = s->sector_size;
- wraparound_copy = 1;
+ // try to avoid wrap-around. If not possible due to sector size
+ // do an extra copy.
+ if (space > s->buffer_size - pos) {
+ if (s->buffer_size - pos >= s->sector_size)
+ space = s->buffer_size - pos;
+ else {
+ space = s->sector_size;
+ wraparound_copy = 1;
+ }
}
- }
- // limit one-time block size
- read_chunk = s->stream->read_chunk;
- if (!read_chunk) read_chunk = 4*s->sector_size;
- space = FFMIN(space, read_chunk);
+ // limit one-time block size
+ read_chunk = s->stream->read_chunk;
+ if (!read_chunk)
+ read_chunk = 4 * s->sector_size;
+ space = FFMIN(space, read_chunk);
#if 1
- // back+newb+space <= buffer_size
- back2=s->buffer_size-(space+newb); // max back size
- if(s->min_filepos<(read-back2)) s->min_filepos=read-back2;
+ // back+newb+space <= buffer_size
+ back2 = s->buffer_size - (space + newb); // max back size
+ if (s->min_filepos < (read - back2))
+ s->min_filepos = read - back2;
#else
- s->min_filepos=read-back; // avoid seeking-back to temp area...
+ s->min_filepos = read - back; // avoid seeking-back to temp area...
#endif
- if (wraparound_copy) {
- int to_copy;
- len = stream_read_internal(s->stream, s->stream->buffer, space);
- to_copy = FFMIN(len, s->buffer_size-pos);
- memcpy(s->buffer + pos, s->stream->buffer, to_copy);
- memcpy(s->buffer, s->stream->buffer + to_copy, len - to_copy);
- } else
- len = stream_read_internal(s->stream, &s->buffer[pos], space);
- s->eof= !len;
+ if (wraparound_copy) {
+ int to_copy;
+ len = stream_read_internal(s->stream, s->stream->buffer, space);
+ to_copy = FFMIN(len, s->buffer_size - pos);
+ memcpy(s->buffer + pos, s->stream->buffer, to_copy);
+ memcpy(s->buffer, s->stream->buffer + to_copy, len - to_copy);
+ } else
+ len = stream_read_internal(s->stream, &s->buffer[pos], space);
+ s->eof = !len;
- s->max_filepos+=len;
- if(pos+len>=s->buffer_size){
- // wrap...
- s->offset+=s->buffer_size;
- }
+ s->max_filepos += len;
+ if (pos + len >= s->buffer_size)
+ s->offset += s->buffer_size; // wrap...
- return len;
+ return len;
}
-static int cache_execute_control(cache_vars_t *s) {
- double double_res;
- unsigned uint_res;
- uint64_t uint64_res;
- int needs_flush = 0;
- static double last;
- int quit = s->control == -2;
- uint64_t old_pos = s->stream->pos;
- int old_eof = s->stream->eof;
- if (quit || !s->stream->control) {
- s->stream_time_length = 0;
- s->stream_time_pos = MP_NOPTS_VALUE;
- s->control_res = STREAM_UNSUPPORTED;
- s->control = -1;
- return !quit;
- }
- if (mp_time_sec() - last > 0.099) {
- double len, pos;
- if (s->stream->control(s->stream, STREAM_CTRL_GET_TIME_LENGTH, &len) == STREAM_OK)
- s->stream_time_length = len;
- else
- s->stream_time_length = 0;
- if (s->stream->control(s->stream, STREAM_CTRL_GET_CURRENT_TIME, &pos) == STREAM_OK)
- s->stream_time_pos = pos;
- else
- s->stream_time_pos = MP_NOPTS_VALUE;
- if (s->stream->control(s->stream, STREAM_CTRL_GET_START_TIME, &pos) == STREAM_OK)
- s->stream_start_time = pos;
- else
- s->stream_start_time = MP_NOPTS_VALUE;
-#if FORKED_CACHE
- // if parent PID changed, main process was killed -> exit
- if (s->ppid != getppid()) {
- mp_msg(MSGT_CACHE, MSGL_WARN, "Parent process disappeared, exiting cache process.\n");
- return 0;
+static int cache_execute_control(cache_vars_t *s)
+{
+ double double_res;
+ unsigned uint_res;
+ uint64_t uint64_res;
+ int needs_flush = 0;
+ static double last;
+ int quit = s->control == -2;
+ uint64_t old_pos = s->stream->pos;
+ int old_eof = s->stream->eof;
+ if (quit || !s->stream->control) {
+ s->stream_time_length = 0;
+ s->stream_time_pos = MP_NOPTS_VALUE;
+ s->control_res = STREAM_UNSUPPORTED;
+ s->control = -1;
+ return !quit;
}
+ if (mp_time_sec() - last > 0.099) {
+ double len, pos;
+ if (s->stream->control(s->stream, STREAM_CTRL_GET_TIME_LENGTH,
+ &len) == STREAM_OK)
+ s->stream_time_length = len;
+ else
+ s->stream_time_length = 0;
+ if (s->stream->control(s->stream, STREAM_CTRL_GET_CURRENT_TIME,
+ &pos) == STREAM_OK)
+ s->stream_time_pos = pos;
+ else
+ s->stream_time_pos = MP_NOPTS_VALUE;
+ if (s->stream->control(s->stream, STREAM_CTRL_GET_START_TIME,
+ &pos) == STREAM_OK)
+ s->stream_start_time = pos;
+ else
+ s->stream_start_time = MP_NOPTS_VALUE;
+#if FORKED_CACHE
+ // if parent PID changed, main process was killed -> exit
+ if (s->ppid != getppid()) {
+ mp_msg(MSGT_CACHE, MSGL_WARN,
+ "Parent process disappeared, exiting cache process.\n");
+ return 0;
+ }
#endif
- last = mp_time_sec();
- }
- if (s->control == -1) return 1;
- switch (s->control) {
+ last = mp_time_sec();
+ }
+ if (s->control == -1)
+ return 1;
+ switch (s->control) {
case STREAM_CTRL_SEEK_TO_TIME:
- needs_flush = 1;
+ needs_flush = 1;
case STREAM_CTRL_GET_CURRENT_TIME:
case STREAM_CTRL_GET_ASPECT_RATIO:
case STREAM_CTRL_GET_START_TIME:
case STREAM_CTRL_GET_CHAPTER_TIME:
- double_res = s->control_double_arg;
- s->control_res = s->stream->control(s->stream, s->control, &double_res);
- s->control_double_arg = double_res;
- break;
+ double_res = s->control_double_arg;
+ s->control_res = s->stream->control(s->stream, s->control, &double_res);
+ s->control_double_arg = double_res;
+ break;
case STREAM_CTRL_SEEK_TO_CHAPTER:
case STREAM_CTRL_SET_ANGLE:
- needs_flush = 1;
- uint_res = s->control_uint_arg;
+ needs_flush = 1;
+ uint_res = s->control_uint_arg;
case STREAM_CTRL_GET_NUM_TITLES:
case STREAM_CTRL_GET_NUM_CHAPTERS:
case STREAM_CTRL_GET_CURRENT_TITLE:
case STREAM_CTRL_GET_CURRENT_CHAPTER:
case STREAM_CTRL_GET_NUM_ANGLES:
case STREAM_CTRL_GET_ANGLE:
- s->control_res = s->stream->control(s->stream, s->control, &uint_res);
- s->control_uint_arg = uint_res;
- break;
+ s->control_res = s->stream->control(s->stream, s->control, &uint_res);
+ s->control_uint_arg = uint_res;
+ break;
case STREAM_CTRL_GET_SIZE:
- s->control_res = s->stream->control(s->stream, s->control, &uint64_res);
- s->control_uint_arg = uint64_res;
- break;
+ s->control_res = s->stream->control(s->stream, s->control, &uint64_res);
+ s->control_uint_arg = uint64_res;
+ break;
case STREAM_CTRL_GET_LANG:
- s->control_res = s->stream->control(s->stream, s->control, (void *)&s->control_lang_arg);
- break;
+ s->control_res = s->stream->control(s->stream, s->control,
+ (void *)&s->control_lang_arg);
+ break;
case STREAM_CTRL_MANAGES_TIMELINE:
- s->control_res = s->stream->control(s->stream, s->control, NULL);
- break;
+ s->control_res = s->stream->control(s->stream, s->control, NULL);
+ break;
default:
- s->control_res = STREAM_UNSUPPORTED;
- break;
- }
- if (s->control_res == STREAM_OK && needs_flush) {
- s->read_filepos = s->stream->pos;
- s->eof = s->stream->eof;
- cache_flush(s);
- } else if (needs_flush &&
- (old_pos != s->stream->pos || old_eof != s->stream->eof))
- mp_msg(MSGT_STREAM, MSGL_ERR, "STREAM_CTRL changed stream pos but returned error, this is not allowed!\n");
- s->control = -1;
- return 1;
+ s->control_res = STREAM_UNSUPPORTED;
+ break;
+ }
+ if (s->control_res == STREAM_OK && needs_flush) {
+ s->read_filepos = s->stream->pos;
+ s->eof = s->stream->eof;
+ cache_flush(s);
+ } else if (needs_flush &&
+ (old_pos != s->stream->pos || old_eof != s->stream->eof))
+ mp_msg(
+ MSGT_STREAM, MSGL_ERR,
+ "STREAM_CTRL changed stream pos but returned error, this is not allowed!\n");
+ s->control = -1;
+ return 1;
}
-static void *shared_alloc(int64_t size) {
+static void *shared_alloc(int64_t size)
+{
#if FORKED_CACHE
return shmem_alloc(size);
#else
@@ -356,7 +381,8 @@ static void *shared_alloc(int64_t size) {
#endif
}
-static void shared_free(void *ptr, int64_t size) {
+static void shared_free(void *ptr, int64_t size)
+{
#if FORKED_CACHE
shmem_free(ptr, size);
#else
@@ -364,69 +390,78 @@ static void shared_free(void *ptr, int64_t size) {
#endif
}
-static cache_vars_t* cache_init(int64_t size,int sector){
- int64_t num;
- cache_vars_t* s=shared_alloc(sizeof(cache_vars_t));
- if(s==NULL) return NULL;
-
- memset(s,0,sizeof(cache_vars_t));
- num=size/sector;
- if(num < 32){
- num = 32;
- }//64kb min_size
- s->buffer_size=num*sector;
- s->sector_size=sector;
- s->buffer=shared_alloc(s->buffer_size);
-
- if(s->buffer == NULL){
- shared_free(s, sizeof(cache_vars_t));
- return NULL;
- }
+static cache_vars_t *cache_init(int64_t size, int sector)
+{
+ int64_t num;
+ cache_vars_t *s = shared_alloc(sizeof(cache_vars_t));
+ if (s == NULL)
+ return NULL;
+
+ memset(s, 0, sizeof(cache_vars_t));
+ num = size / sector;
+ if (num < 32)
+ num = 32;
+ //64kb min_size
+ s->buffer_size = num * sector;
+ s->sector_size = sector;
+ s->buffer = shared_alloc(s->buffer_size);
+
+ if (s->buffer == NULL) {
+ shared_free(s, sizeof(cache_vars_t));
+ return NULL;
+ }
- s->fill_limit=8*sector;
- s->back_size=s->buffer_size/2;
+ s->fill_limit = 8 * sector;
+ s->back_size = s->buffer_size / 2;
#if FORKED_CACHE
- s->ppid = getpid();
+ s->ppid = getpid();
#endif
- return s;
+ return s;
}
-void cache_uninit(stream_t *s) {
- cache_vars_t* c = s->cache_data;
- if(s->cache_pid) {
+void cache_uninit(stream_t *s)
+{
+ cache_vars_t *c = s->cache_data;
+ if (s->cache_pid) {
#if !FORKED_CACHE
- cache_do_control(s, -2, NULL);
+ cache_do_control(s, -2, NULL);
#else
- kill(s->cache_pid,SIGKILL);
- waitpid(s->cache_pid,NULL,0);
+ kill(s->cache_pid, SIGKILL);
+ waitpid(s->cache_pid, NULL, 0);
#endif
- s->cache_pid = 0;
- }
- if(!c) return;
- shared_free(c->buffer, c->buffer_size);
- c->buffer = NULL;
- c->stream = NULL;
- shared_free(s->cache_data, sizeof(cache_vars_t));
- s->cache_data = NULL;
+ s->cache_pid = 0;
+ }
+ if (!c)
+ return;
+ shared_free(c->buffer, c->buffer_size);
+ c->buffer = NULL;
+ c->stream = NULL;
+ shared_free(s->cache_data, sizeof(cache_vars_t));
+ s->cache_data = NULL;
}
#if FORKED_CACHE
-static void exit_sighandler(int x){
- // close stream
- exit(0);
+static void exit_sighandler(int x)
+{
+ // close stream
+ exit(0);
}
-static void dummy_sighandler(int x) {
+static void dummy_sighandler(int x)
+{
}
#endif
/**
* Main loop of the cache process or thread.
*/
-static void cache_mainloop(cache_vars_t *s) {
+static void cache_mainloop(cache_vars_t *s)
+{
int sleep_count = 0;
#if FORKED_CACHE
- struct sigaction sa = { .sa_handler = SIG_IGN };
+ struct sigaction sa = {
+ .sa_handler = SIG_IGN
+ };
sigaction(SIGUSR1, &sa, NULL);
#endif
do {
@@ -443,7 +478,7 @@ static void cache_mainloop(cache_vars_t *s) {
sleep_count++;
mp_sleep_us(INITIAL_FILL_USLEEP_TIME);
} else
- mp_sleep_us(FILL_USLEEP_TIME); // idle
+ mp_sleep_us(FILL_USLEEP_TIME); // idle
#if FORKED_CACHE
sa.sa_handler = SIG_IGN;
sigaction(SIGUSR1, &sa, NULL);
@@ -456,219 +491,252 @@ static void cache_mainloop(cache_vars_t *s) {
}
int stream_enable_cache_percent(stream_t *stream, int64_t stream_cache_size,
- float stream_cache_min_percent, float stream_cache_seek_min_percent)
+ float stream_cache_min_percent,
+ float stream_cache_seek_min_percent)
{
return stream_enable_cache(stream, stream_cache_size * 1024,
- stream_cache_size * 1024 * (stream_cache_min_percent / 100.0),
- stream_cache_size * 1024 * (stream_cache_seek_min_percent / 100.0));
+ stream_cache_size * 1024 *
+ (stream_cache_min_percent / 100.0),
+ stream_cache_size * 1024 *
+ (stream_cache_seek_min_percent / 100.0));
}
/**
* \return 1 on success, 0 if the function was interrupted and -1 on error
*/
-int stream_enable_cache(stream_t *stream,int64_t size,int64_t min,int64_t seek_limit)
+int stream_enable_cache(stream_t *stream, int64_t size, int64_t min,
+ int64_t seek_limit)
{
- if (size < 0)
- size = stream->cache_size * 1024;
- if (!size)
- return 1;
- mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Cache size set to %"PRId64" KiB\n", size / 1024);
-
- int ss = stream->sector_size ? stream->sector_size : STREAM_BUFFER_SIZE;
- int res = -1;
- cache_vars_t* s;
-
- if (size > SIZE_MAX) {
- mp_msg(MSGT_CACHE, MSGL_FATAL, "Cache size larger than max. allocation size\n");
- return -1;
- }
-
- s=cache_init(size,ss);
- if(s == NULL) return -1;
- stream->cache_data=s;
- s->stream=stream; // callback
- s->seek_limit=seek_limit;
-
-
- //make sure that we won't wait from cache_fill
- //more data than it is allowed to fill
- if (s->seek_limit > s->buffer_size - s->fill_limit ){
- s->seek_limit = s->buffer_size - s->fill_limit;
- }
- if (min > s->buffer_size - s->fill_limit) {
- min = s->buffer_size - s->fill_limit;
- }
- // to make sure we wait for the cache process/thread to be active
- // before continuing
- if (min <= 0)
- min = 1;
+ if (size < 0)
+ size = stream->cache_size * 1024;
+ if (!size)
+ return 1;
+ mp_tmsg(MSGT_NETWORK, MSGL_INFO, "Cache size set to %" PRId64 " KiB\n",
+ size / 1024);
+
+ int ss = stream->sector_size ? stream->sector_size : STREAM_BUFFER_SIZE;
+ int res = -1;
+ cache_vars_t *s;
+
+ if (size > SIZE_MAX) {
+ mp_msg(MSGT_CACHE, MSGL_FATAL,
+ "Cache size larger than max. allocation size\n");
+ return -1;
+ }
+
+ s = cache_init(size, ss);
+ if (s == NULL)
+ return -1;
+ stream->cache_data = s;
+ s->stream = stream; // callback
+ s->seek_limit = seek_limit;
+
+
+ //make sure that we won't wait from cache_fill
+ //more data than it is allowed to fill
+ if (s->seek_limit > s->buffer_size - s->fill_limit)
+ s->seek_limit = s->buffer_size - s->fill_limit;
+ if (min > s->buffer_size - s->fill_limit)
+ min = s->buffer_size - s->fill_limit;
+ // to make sure we wait for the cache process/thread to be active
+ // before continuing
+ if (min <= 0)
+ min = 1;
#if FORKED_CACHE
- if((stream->cache_pid=fork())){
- if ((pid_t)stream->cache_pid == -1)
- stream->cache_pid = 0;
+ if ((stream->cache_pid = fork())) {
+ if ((pid_t)stream->cache_pid == -1)
+ stream->cache_pid = 0;
#else
- {
- stream_t* stream2=malloc(sizeof(stream_t));
- memcpy(stream2,s->stream,sizeof(stream_t));
- s->stream=stream2;
+ {
+ stream_t *stream2 = malloc(sizeof(stream_t));
+ memcpy(stream2, s->stream, sizeof(stream_t));
+ s->stream = stream2;
#if defined(__MINGW32__)
- stream->cache_pid = _beginthread( ThreadProc, 0, s );
+ stream->cache_pid = _beginthread(ThreadProc, 0, s);
#else
- {
- pthread_t tid;
- pthread_create(&tid, NULL, ThreadProc, s);
- stream->cache_pid = 1;
- }
+ {
+ pthread_t tid;
+ pthread_create(&tid, NULL, ThreadProc, s);
+ stream->cache_pid = 1;
+ }
#endif
#endif
- if (!stream->cache_pid) {
- mp_msg(MSGT_CACHE, MSGL_ERR,
- "Starting cache process/thread failed: %s.\n", strerror(errno));
- goto err_out;
- }
- // wait until cache is filled at least prefill_init %
- mp_msg(MSGT_CACHE,MSGL_V,"CACHE_PRE_INIT: %"PRId64" [%"PRId64"] %"PRId64" pre:%"PRId64" eof:%d \n",
- s->min_filepos,s->read_filepos,s->max_filepos,min,s->eof);
- while(s->read_filepos<s->min_filepos || s->max_filepos-s->read_filepos<min){
- mp_tmsg(MSGT_CACHE,MSGL_STATUS,"\rCache fill: %5.2f%% (%"PRId64" bytes) ",
- 100.0*(float)(s->max_filepos-s->read_filepos)/(float)(s->buffer_size),
- s->max_filepos-s->read_filepos
- );
- if(s->eof) break; // file is smaller than prefill size
- if(stream_check_interrupt(PREFILL_SLEEP_TIME)) {
- res = 0;
- goto err_out;
+ if (!stream->cache_pid) {
+ mp_msg(MSGT_CACHE, MSGL_ERR,
+ "Starting cache process/thread failed: %s.\n",
+ strerror(errno));
+ goto err_out;
}
- }
- mp_msg(MSGT_CACHE,MSGL_STATUS,"\n");
- stream->cached = true;
- return 1; // parent exits
+ // wait until cache is filled at least prefill_init %
+ mp_msg(MSGT_CACHE, MSGL_V, "CACHE_PRE_INIT: %" PRId64 " [%" PRId64 "] "
+ "%" PRId64 " pre:%" PRId64 " eof:%d \n",
+ s->min_filepos, s->read_filepos, s->max_filepos, min, s->eof);
+ while (s->read_filepos < s->min_filepos ||
+ s->max_filepos - s->read_filepos < min)
+ {
+ mp_tmsg(MSGT_CACHE, MSGL_STATUS, "\rCache fill: %5.2f%% "
+ "(%" PRId64 " bytes) ",
+ 100.0 * (float)(s->max_filepos - s->read_filepos) /
+ (float)(s->buffer_size),
+ s->max_filepos - s->read_filepos);
+ if (s->eof)
+ break; // file is smaller than prefill size
+ if (stream_check_interrupt(PREFILL_SLEEP_TIME)) {
+ res = 0;
+ goto err_out;
+ }
+ }
+ mp_msg(MSGT_CACHE, MSGL_STATUS, "\n");
+ stream->cached = true;
+ return 1; // parent exits
err_out:
- cache_uninit(stream);
- return res;
- }
+ cache_uninit(stream);
+ return res;
+ }
#if FORKED_CACHE
- signal(SIGTERM,exit_sighandler); // kill
- cache_mainloop(s);
- // make sure forked code never leaves this function
- exit(0);
+ signal(SIGTERM, exit_sighandler); // kill
+ cache_mainloop(s);
+ // make sure forked code never leaves this function
+ exit(0);
#endif
}
#if !FORKED_CACHE
#if defined(__MINGW32__)
-static void ThreadProc( void *s ){
- cache_mainloop(s);
- _endthread();
+static void ThreadProc(void *s)
+{
+ cache_mainloop(s);
+ _endthread();
}
#else
-static void *ThreadProc( void *s ){
- cache_mainloop(s);
- return NULL;
+static void *ThreadProc(void *s)
+{
+ cache_mainloop(s);
+ return NULL;
}
#endif
#endif
-int cache_stream_fill_buffer(stream_t *s){
- int len;
- int sector_size;
- if(!s->cache_pid) return stream_fill_buffer(s);
-
- if(s->pos!=((cache_vars_t*)s->cache_data)->read_filepos) mp_msg(MSGT_CACHE,MSGL_ERR,"!!! read_filepos differs!!! report this bug...\n");
- sector_size = ((cache_vars_t*)s->cache_data)->sector_size;
- if (sector_size > STREAM_MAX_SECTOR_SIZE) {
- mp_msg(MSGT_CACHE, MSGL_ERR, "Sector size %i larger than maximum %i\n", sector_size, STREAM_MAX_SECTOR_SIZE);
- sector_size = STREAM_MAX_SECTOR_SIZE;
- }
-
- len=cache_read(s->cache_data,s->buffer, sector_size);
- //printf("cache_stream_fill_buffer->read -> %d\n",len);
-
- if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; return 0; }
- s->eof=0;
- s->buf_pos=0;
- s->buf_len=len;
- s->pos+=len;
+int cache_stream_fill_buffer(stream_t *s)
+{
+ int len;
+ int sector_size;
+ if (!s->cache_pid)
+ return stream_fill_buffer(s);
+
+ if (s->pos != ((cache_vars_t *)s->cache_data)->read_filepos)
+ mp_msg(MSGT_CACHE, MSGL_ERR,
+ "!!! read_filepos differs!!! report this bug...\n");
+ sector_size = ((cache_vars_t *)s->cache_data)->sector_size;
+ if (sector_size > STREAM_MAX_SECTOR_SIZE) {
+ mp_msg(MSGT_CACHE, MSGL_ERR, "Sector size %i larger than maximum %i\n",
+ sector_size,
+ STREAM_MAX_SECTOR_SIZE);
+ sector_size = STREAM_MAX_SECTOR_SIZE;
+ }
+
+ len = cache_read(s->cache_data, s->buffer, sector_size);
+ //printf("cache_stream_fill_buffer->read -> %d\n",len);
+
+ if (len <= 0) {
+ s->eof = 1;
+ s->buf_pos = s->buf_len = 0;
+ return 0;
+ }
+ s->eof = 0;
+ s->buf_pos = 0;
+ s->buf_len = len;
+ s->pos += len;
// printf("[%d]",len);fflush(stdout);
- stream_capture_write(s);
- return len;
+ stream_capture_write(s);
+ return len;
}
-int cache_stream_seek_long(stream_t *stream,int64_t pos){
- cache_vars_t* s;
- int64_t newpos;
- if(!stream->cache_pid) return stream_seek_long(stream,pos);
+int cache_stream_seek_long(stream_t *stream, int64_t pos)
+{
+ cache_vars_t *s;
+ int64_t newpos;
+ if (!stream->cache_pid)
+ return stream_seek_long(stream, pos);
- s=stream->cache_data;
+ s = stream->cache_data;
// s->seek_lock=1;
- 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);
+ 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);
- newpos=pos/s->sector_size; newpos*=s->sector_size; // align
- stream->pos=s->read_filepos=newpos;
- s->eof=0; // !!!!!!!
- cache_wakeup(stream);
+ newpos = pos / s->sector_size;
+ newpos *= s->sector_size; // align
+ stream->pos = s->read_filepos = newpos;
+ s->eof = 0; // !!!!!!!
+ cache_wakeup(stream);
- cache_stream_fill_buffer(stream);
+ cache_stream_fill_buffer(stream);
- pos-=newpos;
- if(pos>=0 && pos<=stream->buf_len){
- stream->buf_pos=pos; // byte position in sector
- return 1;
- }
+ pos -= newpos;
+ if (pos