Skip to content

Commit b5615d5

Browse files
Merge pull request #1794 from stevemayhew:p-fix-ntp-time-update-main
PiperOrigin-RevId: 689121191
2 parents 7da71f7 + 70f2d51 commit b5615d5

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

RELEASENOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ This release includes the following changes since the
109109
* Remove `Renderer[]` parameter from `LoadControl.onTracksSelected()` as
110110
`DefaultLoadControl` implementation can retrieve the stream types from
111111
`ExoTrackSelection[]`.
112+
* Add a setter to `SntpClient` to set the max elapsed time since the last
113+
update after which the client is re-initialized
114+
([#1794](https://github.com/androidx/media/pull/1794)).
112115
* Deprecated `DefaultLoadControl.calculateTargetBufferBytes(Renderer[],
113116
ExoTrackSelection[])` and marked method as final to prevent overrides.
114117
The new

libraries/exoplayer/src/main/java/androidx/media3/exoplayer/util/SntpClient.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ public interface InitializationCallback {
9696
@GuardedBy("valueLock")
9797
private static int timeoutMs = DEFAULT_TIMEOUT_MS;
9898

99+
@GuardedBy("valueLock")
100+
private static long maxElapsedTimeUntilUpdateMs = C.TIME_UNSET;
101+
102+
@GuardedBy("valueLock")
103+
private static long lastUpdateElapsedRealtime = C.TIME_UNSET;
104+
99105
private SntpClient() {}
100106

101107
/** Returns the NTP host address used to retrieve {@link #getElapsedRealtimeOffsetMs()}. */
@@ -148,6 +154,24 @@ public static void setTimeoutMs(int timeoutMs) {
148154
}
149155
}
150156

157+
/**
158+
* Sets the maximum time to elapse until the client is re-initialized, in milliseconds.
159+
*
160+
*

The default is {@link C#TIME_UNSET} to never re-initialize.

161+
*/
162+
public static void setMaxElapsedTimeUntilUpdateMs(long maxElapsedTimeUntilUpdateMs) {
163+
synchronized (valueLock) {
164+
SntpClient.maxElapsedTimeUntilUpdateMs = maxElapsedTimeUntilUpdateMs;
165+
}
166+
}
167+
168+
/** Returns the maximum time to elapse until the client is re-initialized, in milliseconds. */
169+
public static long getMaxElapsedTimeUntilUpdateMs() {
170+
synchronized (valueLock) {
171+
return maxElapsedTimeUntilUpdateMs;
172+
}
173+
}
174+
151175
/**
152176
* Returns whether the device time offset has already been loaded.
153177
*
@@ -156,6 +180,11 @@ public static void setTimeoutMs(int timeoutMs) {
156180
*/
157181
public static boolean isInitialized() {
158182
synchronized (valueLock) {
183+
if (lastUpdateElapsedRealtime != C.TIME_UNSET
184+
&& maxElapsedTimeUntilUpdateMs != C.TIME_UNSET) {
185+
long deltaLastUpdate = SystemClock.elapsedRealtime() - lastUpdateElapsedRealtime;
186+
isInitialized = isInitialized && deltaLastUpdate < maxElapsedTimeUntilUpdateMs;
187+
}
159188
return isInitialized;
160189
}
161190
}
@@ -353,6 +382,7 @@ public void load() throws IOException {
353382
}
354383
long offsetMs = loadNtpTimeOffsetMs();
355384
synchronized (valueLock) {
385+
lastUpdateElapsedRealtime = SystemClock.elapsedRealtime();
356386
elapsedRealtimeOffsetMs = offsetMs;
357387
isInitialized = true;
358388
}

0 commit comments

Comments
 (0)