Merge pull request #1794 from stevemayhew:p-fix-ntp-time-update-main

PiperOrigin-RevId: 689121191
(cherry picked from commit b5615d5e919b297def6450b45320a3165c34548c)
This commit is contained in:
Copybara-Service 2024-10-23 15:22:22 -07:00 committed by Iván Budnik
parent a44079b516
commit 08e55d81ef
2 changed files with 34 additions and 0 deletions

View File

@ -7,6 +7,10 @@
This release includes the following changes since the
[1.5.0-beta01 release](#150-beta01-2024-10-30):
* ExoPlayer:
* Add a setter to `SntpClient` to set the max elapsed time since the last
update after which the client is re-initialized
([#1794](https://github.com/androidx/media/pull/1794)).
* Extractors:
* Fix media duration parsing in `mdhd` box of MP4 files to handle `-1`
values ([#1819](https://github.com/androidx/media/issues/1819)).

View File

@ -96,6 +96,12 @@ public final class SntpClient {
@GuardedBy("valueLock")
private static int timeoutMs = DEFAULT_TIMEOUT_MS;
@GuardedBy("valueLock")
private static long maxElapsedTimeUntilUpdateMs = C.TIME_UNSET;
@GuardedBy("valueLock")
private static long lastUpdateElapsedRealtime = C.TIME_UNSET;
private SntpClient() {}
/** Returns the NTP host address used to retrieve {@link #getElapsedRealtimeOffsetMs()}. */
@ -148,6 +154,24 @@ public final class SntpClient {
}
}
/**
* Sets the maximum time to elapse until the client is re-initialized, in milliseconds.
*
* <p>The default is {@link C#TIME_UNSET} to never re-initialize.
*/
public static void setMaxElapsedTimeUntilUpdateMs(long maxElapsedTimeUntilUpdateMs) {
synchronized (valueLock) {
SntpClient.maxElapsedTimeUntilUpdateMs = maxElapsedTimeUntilUpdateMs;
}
}
/** Returns the maximum time to elapse until the client is re-initialized, in milliseconds. */
public static long getMaxElapsedTimeUntilUpdateMs() {
synchronized (valueLock) {
return maxElapsedTimeUntilUpdateMs;
}
}
/**
* Returns whether the device time offset has already been loaded.
*
@ -156,6 +180,11 @@ public final class SntpClient {
*/
public static boolean isInitialized() {
synchronized (valueLock) {
if (lastUpdateElapsedRealtime != C.TIME_UNSET
&& maxElapsedTimeUntilUpdateMs != C.TIME_UNSET) {
long deltaLastUpdate = SystemClock.elapsedRealtime() - lastUpdateElapsedRealtime;
isInitialized = isInitialized && deltaLastUpdate < maxElapsedTimeUntilUpdateMs;
}
return isInitialized;
}
}
@ -353,6 +382,7 @@ public final class SntpClient {
}
long offsetMs = loadNtpTimeOffsetMs();
synchronized (valueLock) {
lastUpdateElapsedRealtime = SystemClock.elapsedRealtime();
elapsedRealtimeOffsetMs = offsetMs;
isInitialized = true;
}