Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
SDK
/
exoplayer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
30be792a
authored
Sep 24, 2020
by
tonihei
Committed by
Oliver Woodman
Oct 17, 2020
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Switch SntpClient to time.android.com and allow to set host.
PiperOrigin-RevId: 333480727
parent
64aa634f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
4 deletions
library/core/src/main/java/com/google/android/exoplayer2/util/SntpClient.java
library/core/src/main/java/com/google/android/exoplayer2/util/SntpClient.java
View file @
30be792a
...
...
@@ -27,6 +27,7 @@ import java.net.DatagramPacket;
import
java.net.DatagramSocket
;
import
java.net.InetAddress
;
import
java.util.Arrays
;
import
java.util.ConcurrentModificationException
;
/**
* Static utility to retrieve the device time offset using SNTP.
...
...
@@ -37,6 +38,9 @@ import java.util.Arrays;
*/
public
final
class
SntpClient
{
/** The default NTP host address used to retrieve {@link #getElapsedRealtimeOffsetMs()}. */
public
static
final
String
DEFAULT_NTP_HOST
=
"time.android.com"
;
/** Callback for calls to {@link #initialize(Loader, InitializationCallback)}. */
public
interface
InitializationCallback
{
...
...
@@ -51,7 +55,6 @@ public final class SntpClient {
void
onInitializationFailed
(
IOException
error
);
}
private
static
final
String
NTP_HOST
=
"pool.ntp.org"
;
private
static
final
int
TIMEOUT_MS
=
10_000
;
private
static
final
int
ORIGINATE_TIME_OFFSET
=
24
;
...
...
@@ -80,8 +83,37 @@ public final class SntpClient {
@GuardedBy
(
"valueLock"
)
private
static
long
elapsedRealtimeOffsetMs
;
@GuardedBy
(
"valueLock"
)
private
static
String
ntpHost
=
DEFAULT_NTP_HOST
;
private
SntpClient
()
{}
/** Returns the NTP host address used to retrieve {@link #getElapsedRealtimeOffsetMs()}. */
public
static
String
getNtpHost
()
{
synchronized
(
valueLock
)
{
return
ntpHost
;
}
}
/**
* Sets the NTP host address used to retrieve {@link #getElapsedRealtimeOffsetMs()}.
*
* <p>The default is {@link #DEFAULT_NTP_HOST}.
*
* <p>If the new host address is different from the previous one, the NTP client will be {@link
* #isInitialized()} uninitialized} again.
*
* @param ntpHost The NTP host address.
*/
public
static
void
setNtpHost
(
String
ntpHost
)
{
synchronized
(
valueLock
)
{
if
(!
SntpClient
.
ntpHost
.
equals
(
ntpHost
))
{
SntpClient
.
ntpHost
=
ntpHost
;
isInitialized
=
false
;
}
}
}
/**
* Returns whether the device time offset has already been loaded.
*
...
...
@@ -129,7 +161,7 @@ public final class SntpClient {
}
private
static
long
loadNtpTimeOffsetMs
()
throws
IOException
{
InetAddress
address
=
InetAddress
.
getByName
(
NTP_HOST
);
InetAddress
address
=
InetAddress
.
getByName
(
getNtpHost
()
);
try
(
DatagramSocket
socket
=
new
DatagramSocket
())
{
socket
.
setSoTimeout
(
TIMEOUT_MS
);
byte
[]
buffer
=
new
byte
[
NTP_PACKET_SIZE
];
...
...
@@ -282,9 +314,14 @@ public final class SntpClient {
@Override
public
void
onLoadCompleted
(
Loadable
loadable
,
long
elapsedRealtimeMs
,
long
loadDurationMs
)
{
Assertions
.
checkState
(
SntpClient
.
isInitialized
());
if
(
callback
!=
null
)
{
callback
.
onInitialized
();
if
(!
SntpClient
.
isInitialized
())
{
// This may happen in the unlikely edge case of someone calling setNtpHost between the end
// of the load method and this callback.
callback
.
onInitializationFailed
(
new
IOException
(
new
ConcurrentModificationException
()));
}
else
{
callback
.
onInitialized
();
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment