From c60baabb1c0b6b2f700ec605cb309d5704b30b66 Mon Sep 17 00:00:00 2001 From: rohks Date: Mon, 15 Jul 2024 12:33:25 -0700 Subject: [PATCH] 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 --- RELEASENOTES.md | 2 ++ .../media3/exoplayer/util/SntpClient.java | 34 +++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 855d04aaa2..625741f3b2 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -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`. diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/util/SntpClient.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/util/SntpClient.java index 2aa5112ec2..629c919134 100644 --- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/util/SntpClient.java +++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/util/SntpClient.java @@ -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. + * + *

The default is {@link #DEFAULT_TIMEOUT_MS}. + * + *

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);