Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SntpClient periodically re-syncs the offset to NTP server #1794

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add getter/setter and disable re-initialization by default
  • Loading branch information
marcbaechinger committed Oct 21, 2024
commit 70f2d516a0c50e7adcf545056695a2b3792618b0
3 changes: 3 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
* 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 @@ -49,9 +49,6 @@ public final class SntpClient {
/** The default maximum time, in milliseconds, to wait for the SNTP request to complete. */
public static final int DEFAULT_TIMEOUT_MS = 1_000;

/** Max time to allow before re-initializing with NTP server, default is 10 minutes */
private static final long MAX_ELAPSED_MS_TILL_UPDATE = 60 * 10 * 1_000L;

/** Callback for calls to {@link #initialize(Loader, InitializationCallback)}. */
public interface InitializationCallback {

Expand Down Expand Up @@ -99,6 +96,9 @@ 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;

Expand Down Expand Up @@ -126,7 +126,6 @@ public static void setNtpHost(String ntpHost) {
if (!SntpClient.ntpHost.equals(ntpHost)) {
SntpClient.ntpHost = ntpHost;
isInitialized = false;
lastUpdateElapsedRealtime = C.TIME_UNSET;
}
}
}
Expand Down Expand Up @@ -155,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 @@ -163,9 +180,10 @@ public static void setTimeoutMs(int timeoutMs) {
*/
public static boolean isInitialized() {
synchronized (valueLock) {
if (lastUpdateElapsedRealtime != C.TIME_UNSET) {
if (lastUpdateElapsedRealtime != C.TIME_UNSET
&& maxElapsedTimeUntilUpdateMs != C.TIME_UNSET) {
long deltaLastUpdate = SystemClock.elapsedRealtime() - lastUpdateElapsedRealtime;
isInitialized = isInitialized && deltaLastUpdate < MAX_ELAPSED_MS_TILL_UPDATE;
isInitialized = isInitialized && deltaLastUpdate < maxElapsedTimeUntilUpdateMs;
}
return isInitialized;
}
Expand Down