summaryrefslogtreecommitdiffstats
path: root/demux
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2014-08-27 01:13:20 +0200
committerwm4 <wm4@nowhere>2014-08-27 03:39:04 +0200
commit0b428e44829abc4387f051e3c618c1c878b3a838 (patch)
tree994e0580c831642fde1f2ff6918814404c61deae /demux
parenta8513f8b37343c27a4abea380ae0aa6bbae3894c (diff)
downloadmpv-0b428e44829abc4387f051e3c618c1c878b3a838.tar.bz2
mpv-0b428e44829abc4387f051e3c618c1c878b3a838.tar.xz
player: redo how stream caching and pausing on low cache works
Add the --cache-secs option, which literally overrides the value of --demuxer-readahead-secs if the stream cache is active. The default value is very high (10 seconds), which means it can act as network cache. Remove the old behavior of trying to pause once the byte cache runs low. Instead, do something similar wit the demuxer cache. The nice thing is that we can guess how many seconds of video it has cached, and we can make better decisions. But for now, apply a relatively naive heuristic: if the cache is below 0.5 secs, pause, and wait until at least 2 secs are available. Note that due to timestamp reordering, the estimated cached duration of video might be inaccurate, depending on the file format. If the file format has DTS, it's easy, otherwise the duration will seemingly jump back and forth.
Diffstat (limited to 'demux')
-rw-r--r--demux/demux.c28
-rw-r--r--demux/demux.h6
2 files changed, 33 insertions, 1 deletions
diff --git a/demux/demux.c b/demux/demux.c
index e54027cc9d..2560a0736d 100644
--- a/demux/demux.c
+++ b/demux/demux.c
@@ -109,6 +109,7 @@ struct demux_internal {
bool warned_queue_overflow;
bool last_eof; // last actual global EOF status
bool eof; // whether we're in EOF state (reset for retry)
+ bool idle;
bool autoselect;
double min_secs;
int min_packs;
@@ -144,6 +145,10 @@ struct demux_stream {
struct demux_packet *tail;
};
+// If one of the values is NOPTS, always pick the other one.
+#define MP_PTS_MIN(a, b) ((a) == MP_NOPTS_VALUE || ((a) > (b)) ? (b) : (a))
+#define MP_PTS_MAX(a, b) ((a) == MP_NOPTS_VALUE || ((a) < (b)) ? (b) : (a))
+
static void demuxer_sort_chapters(demuxer_t *demuxer);
static void *demux_thread(void *pctx);
static void update_cache(struct demux_internal *in);
@@ -329,6 +334,7 @@ int demux_add_packet(struct sh_stream *stream, demux_packet_t *dp)
static bool read_packet(struct demux_internal *in)
{
in->eof = false;
+ in->idle = true;
// Check if we need to read a new packet. We do this if all queues are below
// the minimum, or if a stream explicitly needs new packets. Also includes
@@ -373,6 +379,7 @@ static bool read_packet(struct demux_internal *in)
// Actually read a packet. Drop the lock while doing so, because waiting
// for disk or network I/O can take time.
+ in->idle = false;
pthread_mutex_unlock(&in->lock);
struct demuxer *demux = in->d_thread;
bool eof = !demux->desc->fill_buffer || demux->desc->fill_buffer(demux) <= 0;
@@ -808,7 +815,8 @@ static struct demuxer *open_given_type(struct mpv_global *global,
.d_thread = talloc(demuxer, struct demuxer),
.d_buffer = talloc(demuxer, struct demuxer),
.d_user = demuxer,
- .min_secs = demuxer->opts->demuxer_min_secs,
+ .min_secs = stream->uncached_stream ? demuxer->opts->demuxer_min_secs_cache
+ : demuxer->opts->demuxer_min_secs,
.min_packs = demuxer->opts->demuxer_min_packs,
.min_bytes = demuxer->opts->demuxer_min_bytes,
};
@@ -1162,6 +1170,24 @@ static int cached_demux_control(struct demux_internal *in, int cmd, void *arg)
in->tracks_switched = true;
pthread_cond_signal(&in->wakeup);
return DEMUXER_CTRL_OK;
+ case DEMUXER_CTRL_GET_READER_STATE: {
+ struct demux_ctrl_reader_state *r = arg;
+ *r = (struct demux_ctrl_reader_state){
+ .eof = in->last_eof,
+ .idle = in->idle,
+ .ts_range = {MP_NOPTS_VALUE, MP_NOPTS_VALUE},
+ };
+ for (int n = 0; n < in->d_user->num_streams; n++) {
+ struct demux_stream *ds = in->d_user->streams[n]->ds;
+ if (ds->active) {
+ r->underrun |= !ds->head;
+ r->ts_range[0] = MP_PTS_MAX(r->ts_range[0], ds->base_ts);
+ r->ts_range[1] = MP_PTS_MIN(r->ts_range[1], ds->last_ts);
+ }
+ }
+ r->idle &= !r->underrun;
+ return DEMUXER_CTRL_OK;
+ }
}
return DEMUXER_CTRL_DONTKNOW;
}
diff --git a/demux/demux.h b/demux/demux.h
index 92ef461077..d0f70c6f57 100644
--- a/demux/demux.h
+++ b/demux/demux.h
@@ -54,6 +54,12 @@ enum demux_ctrl {
DEMUXER_CTRL_RESYNC,
DEMUXER_CTRL_IDENTIFY_PROGRAM,
DEMUXER_CTRL_STREAM_CTRL,
+ DEMUXER_CTRL_GET_READER_STATE,
+};
+
+struct demux_ctrl_reader_state {
+ bool eof, underrun, idle;
+ double ts_range[2]; // start, end
};
struct demux_ctrl_stream_ctrl {