Deduplicate functions for parsing hex strings.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=117149421
This commit is contained in:
olly 2016-03-14 10:56:40 -07:00 committed by Oliver Woodman
parent 554817cca6
commit 64d7f2f846
6 changed files with 34 additions and 47 deletions

View File

@ -16,9 +16,9 @@
package com.google.android.exoplayer.extractor.mp3; package com.google.android.exoplayer.extractor.mp3;
import com.google.android.exoplayer.C; import com.google.android.exoplayer.C;
import com.google.android.exoplayer.testutil.TestUtil;
import com.google.android.exoplayer.util.MpegAudioHeader; import com.google.android.exoplayer.util.MpegAudioHeader;
import com.google.android.exoplayer.util.ParsableByteArray; import com.google.android.exoplayer.util.ParsableByteArray;
import com.google.android.exoplayer.util.Util;
import android.test.InstrumentationTestCase; import android.test.InstrumentationTestCase;
@ -29,7 +29,7 @@ public final class XingSeekerTest extends InstrumentationTestCase {
// Xing header/payload from http://storage.googleapis.com/exoplayer-test-media-0/play.mp3. // Xing header/payload from http://storage.googleapis.com/exoplayer-test-media-0/play.mp3.
private static final int XING_FRAME_HEADER_DATA = 0xFFFB3000; private static final int XING_FRAME_HEADER_DATA = 0xFFFB3000;
private static final byte[] XING_FRAME_PAYLOAD = TestUtil.createByteArray( private static final byte[] XING_FRAME_PAYLOAD = Util.getBytesFromHexString(
"00000007000008dd000e7919000205080a0d0f1214171a1c1e212426292c2e303336383b3d404245484a4c4f5254" "00000007000008dd000e7919000205080a0d0f1214171a1c1e212426292c2e303336383b3d404245484a4c4f5254"
+ "575a5c5e616466696b6e707376787a7d808285878a8c8f929496999c9ea1a4a6a8abaeb0b3b5b8babdc0c2c4c7" + "575a5c5e616466696b6e707376787a7d808285878a8c8f929496999c9ea1a4a6a8abaeb0b3b5b8babdc0c2c4c7"
+ "cacccfd2d4d6d9dcdee1e3e6e8ebeef0f2f5f8fafd"); + "cacccfd2d4d6d9dcdee1e3e6e8ebeef0f2f5f8fafd");

View File

@ -39,21 +39,21 @@ import java.util.List;
public final class Mp4ExtractorTest extends TestCase { public final class Mp4ExtractorTest extends TestCase {
/** String of hexadecimal bytes containing the video stsd payload from an AVC video. */ /** String of hexadecimal bytes containing the video stsd payload from an AVC video. */
private static final byte[] VIDEO_STSD_PAYLOAD = TestUtil.createByteArray( private static final byte[] VIDEO_STSD_PAYLOAD = Util.getBytesFromHexString(
"00000000000000010000009961766331000000000000000100000000000000000000000000000000050002d00048" "00000000000000010000009961766331000000000000000100000000000000000000000000000000050002d00048"
+ "000000480000000000000001000000000000000000000000000000000000000000000000000000000000000000" + "000000480000000000000001000000000000000000000000000000000000000000000000000000000000000000"
+ "18ffff0000002f617663430164001fffe100186764001facb402802dd80880000003008000001e078c19500100" + "18ffff0000002f617663430164001fffe100186764001facb402802dd80880000003008000001e078c19500100"
+ "0468ee3cb000000014627472740000e35c0042a61000216cb8"); + "0468ee3cb000000014627472740000e35c0042a61000216cb8");
private static final byte[] VIDEO_HDLR_PAYLOAD = TestUtil.createByteArray( private static final byte[] VIDEO_HDLR_PAYLOAD = Util.getBytesFromHexString(
"000000000000000076696465"); "000000000000000076696465");
private static final byte[] VIDEO_MDHD_PAYLOAD = TestUtil.createByteArray( private static final byte[] VIDEO_MDHD_PAYLOAD = Util.getBytesFromHexString(
"0000000000000000cf6c48890000001e00001c8a55c40000"); "0000000000000000cf6c48890000001e00001c8a55c40000");
private static final int TIMESCALE = 30; private static final int TIMESCALE = 30;
private static final int VIDEO_WIDTH = 1280; private static final int VIDEO_WIDTH = 1280;
private static final int VIDEO_HEIGHT = 720; private static final int VIDEO_HEIGHT = 720;
/** String of hexadecimal bytes containing the video stsd payload for an mp4v track. */ /** String of hexadecimal bytes containing the video stsd payload for an mp4v track. */
private static final byte[] VIDEO_STSD_MP4V_PAYLOAD = TestUtil.createByteArray( private static final byte[] VIDEO_STSD_MP4V_PAYLOAD = Util.getBytesFromHexString(
"0000000000000001000000A36D703476000000000000000100000000000000000000000000000000014000B40048" "0000000000000001000000A36D703476000000000000000100000000000000000000000000000000014000B40048"
+ "000000480000000000000001000000000000000000000000000000000000000000000000000000000000000000" + "000000480000000000000001000000000000000000000000000000000000000000000000000000000000000000"
+ "18FFFF0000004D6573647300000000033F00000004372011001A400004CF280002F1180528000001B001000001" + "18FFFF0000004D6573647300000000033F00000004372011001A400004CF280002F1180528000001B001000001"
@ -62,26 +62,26 @@ public final class Mp4ExtractorTest extends TestCase {
private static final int VIDEO_MP4V_HEIGHT = 180; private static final int VIDEO_MP4V_HEIGHT = 180;
/** String of hexadecimal bytes containing the audio stsd payload from an AAC track. */ /** String of hexadecimal bytes containing the audio stsd payload from an AAC track. */
private static final byte[] AUDIO_STSD_PAYLOAD = TestUtil.createByteArray( private static final byte[] AUDIO_STSD_PAYLOAD = Util.getBytesFromHexString(
"0000000000000001000000596d703461000000000000000100000000000000000001001000000000ac4400000000" "0000000000000001000000596d703461000000000000000100000000000000000001001000000000ac4400000000"
+ "003565736473000000000327000000041f401500023e00024bc000023280051012080000000000000000000000" + "003565736473000000000327000000041f401500023e00024bc000023280051012080000000000000000000000"
+ "000000060102"); + "000000060102");
private static final byte[] AUDIO_HDLR_PAYLOAD = TestUtil.createByteArray( private static final byte[] AUDIO_HDLR_PAYLOAD = Util.getBytesFromHexString(
"0000000000000000736f756e"); "0000000000000000736f756e");
private static final byte[] AUDIO_MDHD_PAYLOAD = TestUtil.createByteArray( private static final byte[] AUDIO_MDHD_PAYLOAD = Util.getBytesFromHexString(
"00000000cf6c4889cf6c488a0000ac4400a3e40055c40000"); "00000000cf6c4889cf6c488a0000ac4400a3e40055c40000");
/** String of hexadecimal bytes for an ftyp payload with major_brand mp41 and minor_version 0. **/ /** String of hexadecimal bytes for an ftyp payload with major_brand mp41 and minor_version 0. **/
private static final byte[] FTYP_PAYLOAD = TestUtil.createByteArray("6d70343100000000"); private static final byte[] FTYP_PAYLOAD = Util.getBytesFromHexString("6d70343100000000");
/** String of hexadecimal bytes containing an mvhd payload from an AVC/AAC video. */ /** String of hexadecimal bytes containing an mvhd payload from an AVC/AAC video. */
private static final byte[] MVHD_PAYLOAD = TestUtil.createByteArray( private static final byte[] MVHD_PAYLOAD = Util.getBytesFromHexString(
"00000000cf6c4888cf6c48880000025800023ad40001000001000000000000000000000000010000000000000000" "00000000cf6c4888cf6c48880000025800023ad40001000001000000000000000000000000010000000000000000"
+ "000000000000000100000000000000000000000000004000000000000000000000000000000000000000000000" + "000000000000000100000000000000000000000000004000000000000000000000000000000000000000000000"
+ "000000000000000003"); + "000000000000000003");
/** String of hexadecimal bytes containing a tkhd payload with an unknown duration. */ /** String of hexadecimal bytes containing a tkhd payload with an unknown duration. */
private static final byte[] TKHD_PAYLOAD = TestUtil.createByteArray( private static final byte[] TKHD_PAYLOAD = Util.getBytesFromHexString(
"00000007D1F0C7BFD1F0C7BF0000000000000000FFFFFFFF00000000000000000000000000000000000100000000" "00000007D1F0C7BFD1F0C7BF0000000000000000FFFFFFFF00000000000000000000000000000000000100000000"
+ "0000000000000000000000010000000000000000000000000000400000000780000004380000"); + "0000000000000000000000010000000000000000000000000000400000000780000004380000");

View File

@ -81,15 +81,6 @@ public class TestUtil {
return source; return source;
} }
public static byte[] createByteArray(String hexBytes) {
byte[] result = new byte[hexBytes.length() / 2];
for (int i = 0; i < result.length; i++) {
result[i] = (byte) ((Character.digit(hexBytes.charAt(i * 2), 16) << 4)
+ Character.digit(hexBytes.charAt(i * 2 + 1), 16));
}
return result;
}
/** /**
* Converts an array of integers in the range [0, 255] into an equivalent byte array. * Converts an array of integers in the range [0, 255] into an equivalent byte array.
* *

View File

@ -147,8 +147,8 @@ public class NalUnitUtilTest extends TestCase {
} }
private static void assertUnescapeMatchesExpected(String input, String expectedOutput) { private static void assertUnescapeMatchesExpected(String input, String expectedOutput) {
byte[] bitstream = getByteArrayForHexString(input); byte[] bitstream = Util.getBytesFromHexString(input);
byte[] expectedOutputBitstream = getByteArrayForHexString(expectedOutput); byte[] expectedOutputBitstream = Util.getBytesFromHexString(expectedOutput);
int count = NalUnitUtil.unescapeStream(bitstream, bitstream.length); int count = NalUnitUtil.unescapeStream(bitstream, bitstream.length);
assertEquals(expectedOutputBitstream.length, count); assertEquals(expectedOutputBitstream.length, count);
byte[] outputBitstream = new byte[count]; byte[] outputBitstream = new byte[count];
@ -156,15 +156,4 @@ public class NalUnitUtilTest extends TestCase {
assertTrue(Arrays.equals(expectedOutputBitstream, outputBitstream)); assertTrue(Arrays.equals(expectedOutputBitstream, outputBitstream));
} }
private static byte[] getByteArrayForHexString(String hexString) {
int length = hexString.length();
Assertions.checkArgument(length % 2 == 0);
byte[] result = new byte[length / 2];
for (int i = 0; i < result.length; i++) {
result[i] = (byte) ((Character.digit(hexString.charAt(i * 2), 16) << 4)
+ Character.digit(hexString.charAt(i * 2 + 1), 16));
}
return result;
}
} }

View File

@ -24,7 +24,9 @@ import com.google.android.exoplayer.upstream.UriLoadable;
import com.google.android.exoplayer.util.Assertions; import com.google.android.exoplayer.util.Assertions;
import com.google.android.exoplayer.util.CodecSpecificDataUtil; import com.google.android.exoplayer.util.CodecSpecificDataUtil;
import com.google.android.exoplayer.util.MimeTypes; import com.google.android.exoplayer.util.MimeTypes;
import com.google.android.exoplayer.util.Util;
import android.text.TextUtils;
import android.util.Base64; import android.util.Base64;
import android.util.Pair; import android.util.Pair;
@ -647,8 +649,8 @@ public class SmoothStreamingManifestParser implements UriLoadable.Parser<SmoothS
private static List<byte[]> buildCodecSpecificData(String codecSpecificDataString) { private static List<byte[]> buildCodecSpecificData(String codecSpecificDataString) {
ArrayList<byte[]> csd = new ArrayList<>(); ArrayList<byte[]> csd = new ArrayList<>();
if (codecSpecificDataString != null && !codecSpecificDataString.isEmpty()) { if (!TextUtils.isEmpty(codecSpecificDataString)) {
byte[] codecPrivateData = hexStringToByteArray(codecSpecificDataString); byte[] codecPrivateData = Util.getBytesFromHexString(codecSpecificDataString);
byte[][] split = CodecSpecificDataUtil.splitNalUnits(codecPrivateData); byte[][] split = CodecSpecificDataUtil.splitNalUnits(codecPrivateData);
if (split == null) { if (split == null) {
csd.add(codecPrivateData); csd.add(codecPrivateData);
@ -684,17 +686,6 @@ public class SmoothStreamingManifestParser implements UriLoadable.Parser<SmoothS
return null; return null;
} }
private static byte[] hexStringToByteArray(String hexString) {
int length = hexString.length();
byte[] data = new byte[length / 2];
for (int i = 0; i < data.length; i++) {
int stringOffset = i * 2;
data[i] = (byte) ((Character.digit(hexString.charAt(stringOffset), 16) << 4)
+ Character.digit(hexString.charAt(stringOffset + 1), 16));
}
return data;
}
} }
} }

View File

@ -668,6 +668,22 @@ public final class Util {
return dataStringBuilder.toString(); return dataStringBuilder.toString();
} }
/**
* Returns a byte array containing values parsed from the hex string provided.
*
* @param hexString The hex string to convert to bytes.
* @return A byte array containing values parsed from the hex string provided.
*/
public static byte[] getBytesFromHexString(String hexString) {
byte[] data = new byte[hexString.length() / 2];
for (int i = 0; i < data.length; i++) {
int stringOffset = i * 2;
data[i] = (byte) ((Character.digit(hexString.charAt(stringOffset), 16) << 4)
+ Character.digit(hexString.charAt(stringOffset + 1), 16));
}
return data;
}
/** /**
* 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.
* *