Relanding http://https://github.com/google/ExoPlayer/commit/9788750ddb23b2064dddf99d6e1ea491b2e45cea, with some changes applied to improve primarily readability, naming, and nullness checks. PiperOrigin-RevId: 406101742