Allow changing SNTP client timeout

Added a method to set the timeout for the SNTP request.

Also changed the default timeout to 5s instead of 10s as it seemed quite high.

Issue: androidx/media#1540
PiperOrigin-RevId: 652566008
This commit is contained in:
rohks 2024-07-15 12:33:25 -07:00 committed by Copybara-Service
parent 0d2bf49d6a
commit c60baabb1c
2 changed files with 33 additions and 3 deletions

View File

@ -24,6 +24,8 @@
* `MediaCodecVideoRenderer` avoids decoding samples that are neither
rendered nor used as reference by other samples.
* Add `BasePreloadManager.Listener` to propagate preload events to apps.
* Allow changing SNTP client timeout
([#1540](https://github.com/androidx/media/issues/1540)).
* Transformer:
* Add `SurfaceAssetLoader`, which supports queueing video data to
Transformer via a `Surface`.

View File

@ -43,6 +43,9 @@ public final class SntpClient {
/** The default NTP host address used to retrieve {@link #getElapsedRealtimeOffsetMs()}. */
public static final String DEFAULT_NTP_HOST = "time.android.com";
/** The default maximum time, in milliseconds, to wait for the SNTP request to complete. */
public static final int DEFAULT_TIMEOUT_MS = 5_000;
/** Callback for calls to {@link #initialize(Loader, InitializationCallback)}. */
public interface InitializationCallback {
@ -57,8 +60,6 @@ public final class SntpClient {
void onInitializationFailed(IOException error);
}
private static final int TIMEOUT_MS = 10_000;
private static final int ORIGINATE_TIME_OFFSET = 24;
private static final int RECEIVE_TIME_OFFSET = 32;
private static final int TRANSMIT_TIME_OFFSET = 40;
@ -88,6 +89,9 @@ public final class SntpClient {
@GuardedBy("valueLock")
private static String ntpHost = DEFAULT_NTP_HOST;
@GuardedBy("valueLock")
private static int timeoutMs = DEFAULT_TIMEOUT_MS;
private SntpClient() {}
/** Returns the NTP host address used to retrieve {@link #getElapsedRealtimeOffsetMs()}. */
@ -116,6 +120,30 @@ public final class SntpClient {
}
}
/** Returns the maximum time to wait for the SNTP request to complete, in milliseconds. */
public static int getTimeoutMs() {
synchronized (valueLock) {
return timeoutMs;
}
}
/**
* Sets the maximum time to wait for the SNTP request to complete, in milliseconds.
*
* <p>The default is {@link #DEFAULT_TIMEOUT_MS}.
*
* <p>If the new timeout is different from the previous one, the NTP client will be {@link
* #isInitialized()} uninitialized} again.
*/
public static void setTimeoutMs(int timeoutMs) {
synchronized (valueLock) {
if (SntpClient.timeoutMs != timeoutMs) {
SntpClient.timeoutMs = timeoutMs;
isInitialized = false;
}
}
}
/**
* Returns whether the device time offset has already been loaded.
*
@ -165,7 +193,7 @@ public final class SntpClient {
private static long loadNtpTimeOffsetMs() throws IOException {
InetAddress address = InetAddress.getByName(getNtpHost());
try (DatagramSocket socket = new DatagramSocket()) {
socket.setSoTimeout(TIMEOUT_MS);
socket.setSoTimeout(getTimeoutMs());
byte[] buffer = new byte[NTP_PACKET_SIZE];
DatagramPacket request = new DatagramPacket(buffer, buffer.length, address, NTP_PORT);