Skip to content

Commit 08e55d8

Browse files
copybara-githubivanbuper
authored andcommitted
Merge pull request #1794 from stevemayhew:p-fix-ntp-time-update-main
PiperOrigin-RevId: 689121191 (cherry picked from commit b5615d5)
1 parent a44079b commit 08e55d8

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

RELEASENOTES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
This release includes the following changes since the
88
[1.5.0-beta01 release](#150-beta01-2024-10-30):
99

10+
* ExoPlayer:
11+
* Add a setter to `SntpClient` to set the max elapsed time since the last
12+
update after which the client is re-initialized
13+
([#1794](https://github.com/androidx/media/pull/1794)).
1014
* Extractors:
1115
* Fix media duration parsing in `mdhd` box of MP4 files to handle `-1`
1216
values ([#1819](https://github.com/androidx/media/issues/1819)).

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)