diff --git a/demux/demux.c b/demux/demux.c index 042e3a9cc3..2c252e6c20 100644 --- a/demux/demux.c +++ b/demux/demux.c @@ -219,6 +219,8 @@ struct demux_internal { // -- Access from demuxer thread only bool enable_recording; struct mp_recorder *recorder; + int64_t slave_unbuffered_read_bytes; // value repoted from demuxer impl. + int64_t cache_unbuffered_read_bytes; // for demux_reader_state.bytes_per_second }; // A continuous range of cached packets for all enabled streams. @@ -3036,6 +3038,19 @@ void demux_block_reading(struct demuxer *demuxer, bool block) pthread_mutex_unlock(&in->lock); } +static void update_bytes_read(struct demux_internal *in) +{ + struct demuxer *demuxer = in->d_thread; + struct stream *stream = demuxer->stream; + + int64_t new = stream->total_unbuffered_read_bytes + + in->slave_unbuffered_read_bytes; + stream->total_unbuffered_read_bytes = 0; + in->slave_unbuffered_read_bytes = 0; + + in->cache_unbuffered_read_bytes += new; +} + // must be called not locked static void update_cache(struct demux_internal *in) { @@ -3048,8 +3063,7 @@ static void update_cache(struct demux_internal *in) int64_t stream_size = stream_get_size(stream); stream_control(stream, STREAM_CTRL_GET_METADATA, &stream_metadata); - demuxer->total_unbuffered_read_bytes += stream->total_unbuffered_read_bytes; - stream->total_unbuffered_read_bytes = 0; + update_bytes_read(in); pthread_mutex_lock(&in->lock); @@ -3068,8 +3082,8 @@ static void update_cache(struct demux_internal *in) int64_t now = mp_time_us(); int64_t diff = now - in->last_speed_query; if (diff >= MP_SECOND_US) { - uint64_t bytes = demuxer->total_unbuffered_read_bytes; - demuxer->total_unbuffered_read_bytes = 0; + uint64_t bytes = in->cache_unbuffered_read_bytes; + in->cache_unbuffered_read_bytes = 0; in->last_speed_query = now; in->bytes_per_second = bytes / (diff / (double)MP_SECOND_US); } @@ -3080,6 +3094,17 @@ static void update_cache(struct demux_internal *in) pthread_mutex_unlock(&in->lock); } +// Used by demuxers to report the amount of transferred bytes. This is for +// streams which circumvent demuxer->stream (stream statistics are handled by +// demux.c itself). +void demux_report_unbuffered_read_bytes(struct demuxer *demuxer, int64_t new) +{ + struct demux_internal *in = demuxer->in; + assert(demuxer == in->d_thread); + + in->slave_unbuffered_read_bytes += new; +} + void demux_get_bitrate_stats(struct demuxer *demuxer, double *rates) { struct demux_internal *in = demuxer->in; diff --git a/demux/demux.h b/demux/demux.h index 156021007d..09f6895544 100644 --- a/demux/demux.h +++ b/demux/demux.h @@ -222,9 +222,6 @@ typedef struct demuxer { // Triggered when ending demuxing forcefully. Usually bound to the stream too. struct mp_cancel *cancel; - // Demuxer thread only. - uint64_t total_unbuffered_read_bytes; - // Since the demuxer can run in its own thread, and the stream is not // thread-safe, only the demuxer is allowed to access the stream directly. // Also note that the stream can get replaced if fully_read is set. @@ -294,6 +291,8 @@ void demux_update(demuxer_t *demuxer); void demux_disable_cache(demuxer_t *demuxer); bool demux_is_network_cached(demuxer_t *demuxer); +void demux_report_unbuffered_read_bytes(struct demuxer *demuxer, int64_t new); + struct sh_stream *demuxer_stream_by_demuxer_id(struct demuxer *d, enum stream_type t, int id); diff --git a/demux/demux_lavf.c b/demux/demux_lavf.c index c53c00a8c3..a021542500 100644 --- a/demux/demux_lavf.c +++ b/demux/demux_lavf.c @@ -235,7 +235,7 @@ static void update_read_stats(struct demuxer *demuxer) int64_t cur = nest->id->bytes_read; int64_t new = cur - nest->last_bytes; nest->last_bytes = cur; - demuxer->total_unbuffered_read_bytes += new; + demux_report_unbuffered_read_bytes(demuxer, new); } }