demux: convert cache updates to nanoseconds

As a bonus, we can remove the awkward and horribly named MP_SECOND_US.
This commit is contained in:
Dudemanguy 2023-10-11 11:41:15 -05:00
parent de9b800879
commit 5cda1a5deb
2 changed files with 6 additions and 9 deletions

View File

@ -2537,7 +2537,7 @@ static bool thread_work(struct demux_internal *in)
} }
if (read_packet(in)) if (read_packet(in))
return true; // read_packet unlocked, so recheck conditions return true; // read_packet unlocked, so recheck conditions
if (mp_time_us() >= in->next_cache_update) { if (mp_time_ns() >= in->next_cache_update) {
update_cache(in); update_cache(in);
return true; return true;
} }
@ -2556,7 +2556,7 @@ static void *demux_thread(void *pctx)
if (thread_work(in)) if (thread_work(in))
continue; continue;
pthread_cond_signal(&in->wakeup); pthread_cond_signal(&in->wakeup);
struct timespec until = mp_time_us_to_realtime(in->next_cache_update); struct timespec until = mp_time_ns_to_realtime(in->next_cache_update);
pthread_cond_timedwait(&in->wakeup, &in->lock, &until); pthread_cond_timedwait(&in->wakeup, &in->lock, &until);
} }
@ -4130,9 +4130,9 @@ static void update_cache(struct demux_internal *in)
struct demuxer *demuxer = in->d_thread; struct demuxer *demuxer = in->d_thread;
struct stream *stream = demuxer->stream; struct stream *stream = demuxer->stream;
int64_t now = mp_time_us(); int64_t now = mp_time_ns();
int64_t diff = now - in->last_speed_query; int64_t diff = now - in->last_speed_query;
bool do_update = diff >= MP_SECOND_US; bool do_update = diff >= MP_TIME_S_TO_NS(1);
// Don't lock while querying the stream. // Don't lock while querying the stream.
pthread_mutex_unlock(&in->lock); pthread_mutex_unlock(&in->lock);
@ -4162,14 +4162,14 @@ static void update_cache(struct demux_internal *in)
uint64_t bytes = in->cache_unbuffered_read_bytes; uint64_t bytes = in->cache_unbuffered_read_bytes;
in->cache_unbuffered_read_bytes = 0; in->cache_unbuffered_read_bytes = 0;
in->last_speed_query = now; in->last_speed_query = now;
double speed = bytes / (diff / (double)MP_SECOND_US); double speed = bytes / (diff / (double)MP_TIME_S_TO_NS(1));
in->bytes_per_second = 0.5 * in->speed_query_prev_sample + in->bytes_per_second = 0.5 * in->speed_query_prev_sample +
0.5 * speed; 0.5 * speed;
in->speed_query_prev_sample = speed; in->speed_query_prev_sample = speed;
} }
// The idea is to update as long as there is "activity". // The idea is to update as long as there is "activity".
if (in->bytes_per_second) if (in->bytes_per_second)
in->next_cache_update = now + MP_SECOND_US + 1; in->next_cache_update = now + MP_TIME_S_TO_NS(1) + MP_TIME_US_TO_NS(1);
} }
static void dumper_close(struct demux_internal *in) static void dumper_close(struct demux_internal *in)

View File

@ -60,9 +60,6 @@ void mp_end_hires_timers(int resolution_ms);
#define MP_TIME_NS_TO_MS(ns) ((ns) / (double)1000000) #define MP_TIME_NS_TO_MS(ns) ((ns) / (double)1000000)
#define MP_TIME_NS_TO_US(ns) ((ns) / (double)1000) #define MP_TIME_NS_TO_US(ns) ((ns) / (double)1000)
// Duration of a second in mpv time.
#define MP_SECOND_US (1000 * 1000)
// Add a time in seconds to the given time in microseconds, and return it. // Add a time in seconds to the given time in microseconds, and return it.
// Takes care of possible overflows. Never returns a negative or 0 time. // Takes care of possible overflows. Never returns a negative or 0 time.
int64_t mp_time_us_add(int64_t time_us, double timeout_sec); int64_t mp_time_us_add(int64_t time_us, double timeout_sec);