demux: slightly cleanup network speed reporting

It was an ugly hack, and the next commit will make it even uglier.
Slightly reduce the ugliness to prevent death of too many brain cells,
though it's still an ugly hack.

The cleanup is really minor, but I guess the following commit would be
much worse otherwise. In particular, this commit checks accesses
(instead of having a public field with evil access rules), which should
avoid misunderstandings and incorrect use. Strictly speaking, the added
field is redundant, but the next commit complicates it a bit.
This commit is contained in:
wm4 2019-01-05 08:49:31 +01:00
parent 27a09b42ed
commit ebf183eeec
3 changed files with 32 additions and 8 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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);
}
}