Commit 096f8520 by eguven Committed by Oliver Woodman

Use manifest URL sha1 hash as the content id.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=130293908
parent 63ab601d
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package com.google.android.exoplayer2.util; package com.google.android.exoplayer2.util;
import android.test.MoreAsserts;
import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.testutil.TestUtil;
import java.text.ParseException; import java.text.ParseException;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -145,6 +146,20 @@ public class UtilTest extends TestCase { ...@@ -145,6 +146,20 @@ public class UtilTest extends TestCase {
assertEquals(1407322800000L, Util.parseXsDateTime("2014-08-06T11:00:00Z")); assertEquals(1407322800000L, Util.parseXsDateTime("2014-08-06T11:00:00Z"));
} }
public void testGetHexStringByteArray() throws Exception {
assertHexStringByteArray("", new byte[] {});
assertHexStringByteArray("01", new byte[] {1});
assertHexStringByteArray("FF", new byte[] {(byte) 255});
assertHexStringByteArray("01020304", new byte[] {1, 2, 3, 4});
assertHexStringByteArray("0123456789ABCDEF",
new byte[] {1, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF});
}
private void assertHexStringByteArray(String hex, byte[] array) {
assertEquals(hex, Util.getHexString(array));
MoreAsserts.assertEquals(array, Util.getBytesFromHexString(hex));
}
public void testUnescapeInvalidFileName() { public void testUnescapeInvalidFileName() {
assertNull(Util.unescapeFileName("%a")); assertNull(Util.unescapeFileName("%a"));
assertNull(Util.unescapeFileName("%xyz")); assertNull(Util.unescapeFileName("%xyz"));
......
...@@ -109,7 +109,7 @@ public final class DashMediaSource implements MediaSource { ...@@ -109,7 +109,7 @@ public final class DashMediaSource implements MediaSource {
this.chunkSourceFactory = chunkSourceFactory; this.chunkSourceFactory = chunkSourceFactory;
this.minLoadableRetryCount = minLoadableRetryCount; this.minLoadableRetryCount = minLoadableRetryCount;
eventDispatcher = new EventDispatcher(eventHandler, eventListener); eventDispatcher = new EventDispatcher(eventHandler, eventListener);
manifestParser = new DashManifestParser(); manifestParser = new DashManifestParser(generateContentId());
manifestCallback = new ManifestCallback(); manifestCallback = new ManifestCallback();
manifestUriLock = new Object(); manifestUriLock = new Object();
periodsById = new SparseArray<>(); periodsById = new SparseArray<>();
...@@ -441,6 +441,10 @@ public final class DashMediaSource implements MediaSource { ...@@ -441,6 +441,10 @@ public final class DashMediaSource implements MediaSource {
} }
} }
private String generateContentId() {
return Util.sha1(manifestUri.toString());
}
private static final class PeriodSeekInfo { private static final class PeriodSeekInfo {
public static PeriodSeekInfo createPeriodSeekInfo(Period period, long durationUs) { public static PeriodSeekInfo createPeriodSeekInfo(Period period, long durationUs) {
......
...@@ -33,8 +33,11 @@ import java.io.ByteArrayOutputStream; ...@@ -33,8 +33,11 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException; import java.text.ParseException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
...@@ -112,6 +115,8 @@ public final class Util { ...@@ -112,6 +115,8 @@ public final class Util {
private static final Pattern ESCAPED_CHARACTER_PATTERN = Pattern.compile("%([A-Fa-f0-9]{2})"); private static final Pattern ESCAPED_CHARACTER_PATTERN = Pattern.compile("%([A-Fa-f0-9]{2})");
private static final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
private Util() {} private Util() {}
/** /**
...@@ -636,6 +641,17 @@ public final class Util { ...@@ -636,6 +641,17 @@ public final class Util {
return data; return data;
} }
/** Returns a hex string representation of the given byte array. */
public static String getHexString(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
int i = 0;
for (byte v : bytes) {
hexChars[i++] = HEX_DIGITS[(v >> 4) & 0xf];
hexChars[i++] = HEX_DIGITS[v & 0xf];
}
return new String(hexChars);
}
/** /**
* Returns a string with comma delimited simple names of each object's class. * Returns a string with comma delimited simple names of each object's class.
* *
...@@ -839,4 +855,17 @@ public final class Util { ...@@ -839,4 +855,17 @@ public final class Util {
} }
return builder.toString(); return builder.toString();
} }
/** Returns the SHA-1 digest of input as a hex string. */
public static String sha1(String input) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
byte[] bytes = input.getBytes("UTF-8");
digest.update(bytes, 0, bytes.length);
return getHexString(digest.digest());
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
} }
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