Skip to content

Commit

Permalink
Merge pull request #1794 from stevemayhew:p-fix-ntp-time-update-main
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 689121191
  • Loading branch information
copybara-github committed Oct 23, 2024
2 parents 7da71f7 + 70f2d51 commit b5615d5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ This release includes the following changes since the
* Remove `Renderer[]` parameter from `LoadControl.onTracksSelected()` as
`DefaultLoadControl` implementation can retrieve the stream types from
`ExoTrackSelection[]`.
* 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)).
* Deprecated `DefaultLoadControl.calculateTargetBufferBytes(Renderer[],
ExoTrackSelection[])` and marked method as final to prevent overrides.
The new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ public interface InitializationCallback {
@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()}. */
Expand Down Expand Up @@ -148,6 +154,24 @@ public static void setTimeoutMs(int timeoutMs) {
}
}

/**
* 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.
*
Expand All @@ -156,6 +180,11 @@ public static void setTimeoutMs(int timeoutMs) {
*/
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;
}
}
Expand Down Expand Up @@ -353,6 +382,7 @@ public void load() throws IOException {
}
long offsetMs = loadNtpTimeOffsetMs();
synchronized (valueLock) {
lastUpdateElapsedRealtime = SystemClock.elapsedRealtime();
elapsedRealtimeOffsetMs = offsetMs;
isInitialized = true;
}
Expand Down

0 comments on commit b5615d5

Please sign in to comment.