Skip to content

Commit

Permalink
misc/random: seed using libavutil/random_seed
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
frostschutz authored and sfan5 committed Sep 28, 2024
1 parent 53f2619 commit c365e2f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
13 changes: 13 additions & 0 deletions misc/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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++)
Expand Down
2 changes: 1 addition & 1 deletion misc/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 0 additions & 2 deletions osdep/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

#include "common/common.h"
#include "common/msg.h"
#include "misc/random.h"
#include "threads.h"
#include "timer.h"

Expand All @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions player/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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){
Expand Down

0 comments on commit c365e2f

Please sign in to comment.