mirror of
https://github.com/mpv-player/mpv
synced 2024-12-26 17:12:36 +00:00
misc/random: seed using libavutil/random_seed
When starting multiple processes of `mpv --shuffle` in parallel, sometimes the random seed happens to be identical, so files are played in the same random order. mp_rand_seed(0) now uses a random seed provided by libavutil, and only falls back to time in case of failure.
This commit is contained in:
parent
53f2619dbd
commit
c365e2f7b1
@ -20,7 +20,10 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <libavutil/random_seed.h>
|
||||
|
||||
#include "osdep/threads.h"
|
||||
#include "osdep/timer.h"
|
||||
#include "random.h"
|
||||
|
||||
static uint64_t state[4];
|
||||
@ -44,6 +47,16 @@ void mp_rand_seed(uint64_t seed)
|
||||
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
||||
seed = 42;
|
||||
#endif
|
||||
|
||||
if (seed == 0) {
|
||||
uint8_t buf[sizeof(seed)];
|
||||
if (av_random_bytes(buf, sizeof(buf)) < 0) {
|
||||
seed = mp_raw_time_ns();
|
||||
} else {
|
||||
memcpy(&seed, buf, sizeof(seed));
|
||||
}
|
||||
}
|
||||
|
||||
mp_mutex_lock(&state_mutex);
|
||||
state[0] = seed;
|
||||
for (int i = 1; i < 4; i++)
|
||||
|
@ -24,7 +24,7 @@
|
||||
|
||||
/*
|
||||
* Initialize the pseudo-random number generator's state with
|
||||
* the given 64-bit seed.
|
||||
* the given 64-bit seed. If the seed is 0, it is randomized.
|
||||
*/
|
||||
void mp_rand_seed(uint64_t seed);
|
||||
|
||||
|
@ -22,7 +22,6 @@
|
||||
|
||||
#include "common/common.h"
|
||||
#include "common/msg.h"
|
||||
#include "misc/random.h"
|
||||
#include "threads.h"
|
||||
#include "timer.h"
|
||||
|
||||
@ -32,7 +31,6 @@ static mp_once timer_init_once = MP_STATIC_ONCE_INITIALIZER;
|
||||
static void do_timer_init(void)
|
||||
{
|
||||
mp_raw_time_init();
|
||||
mp_rand_seed(mp_raw_time_ns());
|
||||
raw_time_offset = mp_raw_time_ns();
|
||||
assert(raw_time_offset > 0);
|
||||
}
|
||||
|
@ -30,6 +30,7 @@
|
||||
#include "mpv_talloc.h"
|
||||
|
||||
#include "misc/dispatch.h"
|
||||
#include "misc/random.h"
|
||||
#include "misc/thread_pool.h"
|
||||
#include "osdep/io.h"
|
||||
#include "osdep/terminal.h"
|
||||
@ -263,6 +264,7 @@ struct MPContext *mp_create(void)
|
||||
talloc_enable_leak_report();
|
||||
|
||||
mp_time_init();
|
||||
mp_rand_seed(0);
|
||||
|
||||
struct MPContext *mpctx = talloc(NULL, MPContext);
|
||||
*mpctx = (struct MPContext){
|
||||
|
Loading…
Reference in New Issue
Block a user