cache: refactor how cache enabling is done

Code enabling the cache by default for network streams did that by
modifying the value of the "cache" option. This wasn't sane, as
multiple streams may be created and all share the same options. Change
the code to not modify options but store data in the stream instance
instead.

Conflicts:
	core/mplayer.c
	demux/demux.c
	stream/cache2.c
	stream/network.c
	stream/network.h
	stream/pnm.c
	stream/stream.c
	stream/stream_rtp.c

Merged from mplayer2 commit e26070. Note that this doesn't solve any
actual bug, as the playlist crashing bug has been fixed before.

Since the global cache size option value is not overwritten anymore, the
option doesn't need to be restored on end of playback (M_OPT_LOCAL).
This commit is contained in:
Uoti Urpala 2012-11-17 19:12:13 +02:00 committed by wm4
parent c082240c62
commit 2d58234c86
11 changed files with 41 additions and 55 deletions

View File

@ -324,8 +324,7 @@ const m_option_t common_opts[] = {
// ------------------------- stream options --------------------
#ifdef CONFIG_STREAM_CACHE
OPT_INTRANGE("cache", stream_cache_size, M_OPT_LOCAL, 32, 0x7fffffff,
OPTDEF_INT(-1)),
OPT_INTRANGE("cache", stream_cache_size, 0, 32, 0x7fffffff, OPTDEF_INT(-1)),
OPT_FLAG_CONSTANTS("no-cache", stream_cache_size, 0, -1, 0),
OPT_FLOATRANGE("cache-min", stream_cache_min_percent, 0, 0, 99),

View File

@ -1183,7 +1183,7 @@ static void print_status(struct MPContext *mpctx)
#ifdef CONFIG_STREAM_CACHE
// cache stats
if (opts->stream_cache_size > 0)
if (mpctx->stream->cached)
saddf(line, width, " C: %d%%", cache_fill_status(mpctx->stream));
#endif
@ -3301,7 +3301,7 @@ static void run_playloop(struct MPContext *mpctx)
#ifdef CONFIG_STREAM_CACHE
// The cache status is part of the status line. Possibly update it.
if (mpctx->paused && opts->stream_cache_size > 0)
if (mpctx->paused && mpctx->stream && mpctx->stream->cached)
print_status(mpctx);
#endif
@ -3633,25 +3633,16 @@ static void open_external_file(struct MPContext *mpctx, char *filename,
char *demuxer_name, int stream_cache,
enum stream_type filter)
{
struct MPOpts *opts = &mpctx->opts;
if (!filename)
return;
int format = 0;
struct stream *stream = open_stream(filename, &mpctx->opts, &format);
if (!stream)
goto err_out;
if (stream_cache) {
if (!stream_enable_cache(stream, stream_cache * 1024,
stream_cache * 1024 *
(mpctx->opts.stream_cache_min_percent / 100.0),
stream_cache * 1024 *
(mpctx->opts.stream_cache_seek_min_percent / 100.0)))
{
free_stream(stream);
mp_msg(MSGT_CPLAYER, MSGL_ERR,
"Can't enable external file stream cache\n");
return;
}
}
stream_enable_cache_percent(stream, stream_cache,
opts->stream_cache_min_percent,
opts->stream_cache_seek_min_percent);
// deal with broken demuxers: preselect streams
int vs = -2, as = -2, ss = -2;
switch (filter) {
@ -3893,17 +3884,15 @@ static void play_current_file(struct MPContext *mpctx)
// CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
#ifdef CONFIG_DVBIN
goto_enable_cache:
goto_enable_cache: ;
#endif
if (opts->stream_cache_size > 0) {
int res = stream_enable_cache_percent(mpctx->stream,
opts->stream_cache_size,
opts->stream_cache_min_percent,
opts->stream_cache_seek_min_percent);
if (res == 0)
if (demux_was_interrupted(mpctx))
goto terminate_playback;
}
int res = stream_enable_cache_percent(mpctx->stream,
opts->stream_cache_size,
opts->stream_cache_min_percent,
opts->stream_cache_seek_min_percent);
if (res == 0)
if (demux_was_interrupted(mpctx))
goto terminate_playback;
//============ Open DEMUXERS --- DETECT file type =======================

View File

@ -127,7 +127,8 @@ static int enable_cache(struct MPContext *mpctx, struct stream **stream,
{
struct MPOpts *opts = &mpctx->opts;
if (opts->stream_cache_size <= 0)
if (!(opts->stream_cache_size > 0 ||
opts->stream_cache_size < 0 && (*stream)->cache_size))
return 0;
char *filename = talloc_strdup(NULL, (*demuxer)->filename);

View File

@ -839,7 +839,6 @@ static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
if (*file_format != DEMUXER_TYPE_PLAYLIST)
*file_format = DEMUXER_TYPE_ASF;
stream->type = STREAMTYPE_STREAM;
fixup_network_stream_cache(stream);
return STREAM_OK;
}

View File

@ -452,7 +452,14 @@ int stream_enable_cache_percent(stream_t *stream, int64_t stream_cache_size,
/**
* \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;
@ -521,6 +528,7 @@ int stream_enable_cache(stream_t *stream,int64_t size,int64_t min,int64_t seek_l
}
}
mp_msg(MSGT_CACHE,MSGL_STATUS,"\n");
stream->cached = true;
return 1; // parent exits
err_out:

View File

@ -880,7 +880,6 @@ static int fixup_open(stream_t *stream,int seekable) {
return STREAM_UNSUPPORTED;
}
fixup_network_stream_cache(stream);
return STREAM_OK;
}

View File

@ -461,19 +461,3 @@ int
nop_streaming_seek( int fd, int64_t pos, streaming_ctrl_t *stream_ctrl ) {
return -1;
}
void fixup_network_stream_cache(stream_t *stream) {
struct MPOpts *opts = stream->opts;
if(!opts)
return;
if(stream->streaming_ctrl->buffering) {
if(opts->stream_cache_size<0) {
// cache option not set, will use our computed value.
// buffer in KBytes, *5 because the prefill is 20% of the buffer.
opts->stream_cache_size = (stream->streaming_ctrl->prebuffer_size/1024)*5;
if( opts->stream_cache_size<64 ) opts->stream_cache_size = 64; // 16KBytes min buffer
}
mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Cache size set to %d KBytes\n", opts->stream_cache_size);
}
}

View File

@ -77,7 +77,6 @@ int http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry);
URL_t* check4proxies(const URL_t *url);
URL_t *url_new_with_proxy(const char *urlstr);
void fixup_network_stream_cache(stream_t *stream);
int http_seek(stream_t *stream, int64_t pos);
#endif /* MPLAYER_NETWORK_H */

View File

@ -182,6 +182,16 @@ static stream_t *open_stream_plugin(const stream_info_t *sinfo,
free(s);
return NULL;
}
s->cache_size = 0;
if (s->streaming_ctrl && s->streaming_ctrl->buffering) {
// Set default cache size to use if user does not specify it.
// buffer in KBytes, *5 assuming the prefill is 20% of the buffer.
s->cache_size = s->streaming_ctrl->prebuffer_size / 1024 * 5;
if (s->cache_size < 64)
s->cache_size = 64;
}
if(s->type <= -2)
mp_msg(MSGT_OPEN,MSGL_WARN, "Warning streams need a type !!!!\n");
if(s->flags & MP_STREAM_SEEK && !s->seek)
@ -200,8 +210,8 @@ static stream_t *open_stream_plugin(const stream_info_t *sinfo,
}
stream_t *open_stream_full(const char *filename, int mode,
struct MPOpts *options, int *file_format)
static stream_t *open_stream_full(const char *filename, int mode,
struct MPOpts *options, int *file_format)
{
int i,j,l,r;
const stream_info_t* sinfo;

View File

@ -22,6 +22,7 @@
#include "config.h"
#include "core/mp_msg.h"
#include "url.h"
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
@ -166,15 +167,15 @@ typedef struct stream {
int64_t pos,start_pos,end_pos;
int eof;
int mode; //STREAM_READ or STREAM_WRITE
int cache_size; // cache size to use if enabled
bool cached;
unsigned int cache_pid;
void* cache_data;
void* priv; // used for DVD, TV, RTSP etc
char* url; // strdup() of filename/url
char *lavf_type; // name of expected demuxer type for lavf
struct MPOpts *opts;
#ifdef CONFIG_NETWORKING
streaming_ctrl_t *streaming_ctrl;
#endif
unsigned char buffer[STREAM_BUFFER_SIZE>STREAM_MAX_SECTOR_SIZE?STREAM_BUFFER_SIZE:STREAM_MAX_SECTOR_SIZE];
} stream_t;
@ -356,8 +357,6 @@ void free_stream(stream_t *s);
stream_t* new_memory_stream(unsigned char* data,int len);
stream_t *open_stream(const char *filename, struct MPOpts *options,
int *file_format);
stream_t *open_stream_full(const char *filename,int mode,
struct MPOpts *options, int *file_format);
stream_t *open_output_stream(const char *filename, struct MPOpts *options);
struct demux_stream;
struct stream *new_ds_stream(struct demux_stream *ds);

View File

@ -91,7 +91,6 @@ udp_stream_open (stream_t *stream, int mode, void *opts, int *file_format)
}
stream->type = STREAMTYPE_STREAM;
fixup_network_stream_cache (stream);
return STREAM_OK;
}