Commit 01e661f2 by tonihei Committed by Oliver Woodman

Add NTP client to get time offset for live streams without UTCTiming.

Dash live streams require that the client has an accurate wall clock
time and in absence of a UTCTiming element, this is assumed to be the
NTP time.

This change adds NTP time offset resolution for DASH live streams
without such timing elements.

PiperOrigin-RevId: 289098796
parent 3e08e421
......@@ -54,6 +54,7 @@ import com.google.android.exoplayer2.upstream.ParsingLoadable;
import com.google.android.exoplayer2.upstream.TransferListener;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.SntpClient;
import com.google.android.exoplayer2.util.Util;
import java.io.BufferedReader;
import java.io.IOException;
......@@ -821,8 +822,12 @@ public final class DashMediaSource extends BaseMediaSource {
}
if (oldPeriodCount == 0) {
if (manifest.dynamic && manifest.utcTiming != null) {
resolveUtcTimingElement(manifest.utcTiming);
if (manifest.dynamic) {
if (manifest.utcTiming != null) {
resolveUtcTimingElement(manifest.utcTiming);
} else {
loadNtpTimeOffset();
}
} else {
processManifest(true);
}
......@@ -915,6 +920,9 @@ public final class DashMediaSource extends BaseMediaSource {
} else if (Util.areEqual(scheme, "urn:mpeg:dash:utc:http-xsdate:2014")
|| Util.areEqual(scheme, "urn:mpeg:dash:utc:http-xsdate:2012")) {
resolveUtcTimingElementHttp(timingElement, new XsDateTimeParser());
} else if (Util.areEqual(scheme, "urn:mpeg:dash:utc:ntp:2014")
|| Util.areEqual(scheme, "urn:mpeg:dash:utc:ntp:2012")) {
loadNtpTimeOffset();
} else {
// Unsupported scheme.
onUtcTimestampResolutionError(new IOException("Unsupported UTC timing scheme"));
......@@ -936,13 +944,29 @@ public final class DashMediaSource extends BaseMediaSource {
C.DATA_TYPE_TIME_SYNCHRONIZATION, parser), new UtcTimestampCallback(), 1);
}
private void loadNtpTimeOffset() {
SntpClient.initialize(
loader,
new SntpClient.InitializationCallback() {
@Override
public void onInitialized() {
onUtcTimestampResolved(SntpClient.getElapsedRealtimeOffsetMs());
}
@Override
public void onInitializationFailed(IOException error) {
onUtcTimestampResolutionError(error);
}
});
}
private void onUtcTimestampResolved(long elapsedRealtimeOffsetMs) {
this.elapsedRealtimeOffsetMs = elapsedRealtimeOffsetMs;
processManifest(true);
}
private void onUtcTimestampResolutionError(IOException error) {
Log.e(TAG, "Failed to resolve UtcTiming element.", error);
Log.e(TAG, "Failed to resolve time offset.", error);
// Be optimistic and continue in the hope that the device clock is correct.
processManifest(true);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment