Commit cdad156b by Oliver Woodman

Further tweaks to ScriptTagPayloadReader

parent dff17f24
...@@ -19,7 +19,6 @@ import com.google.android.exoplayer.C; ...@@ -19,7 +19,6 @@ import com.google.android.exoplayer.C;
import com.google.android.exoplayer.extractor.TrackOutput; import com.google.android.exoplayer.extractor.TrackOutput;
import com.google.android.exoplayer.util.ParsableByteArray; import com.google.android.exoplayer.util.ParsableByteArray;
import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
...@@ -63,29 +62,29 @@ import java.util.Map; ...@@ -63,29 +62,29 @@ import java.util.Map;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
protected void parsePayload(ParsableByteArray data, long timeUs) { protected void parsePayload(ParsableByteArray data, long timeUs) {
String name = readAmfName(data); int nameType = readAmfType(data);
if (NAME_METADATA.equals(name)) { if (nameType != AMF_TYPE_STRING) {
// Not interested. // Should never happen.
return;
}
String name = readAmfString(data);
if (!NAME_METADATA.equals(name)) {
// We're only interested in metadata.
return; return;
} }
int type = readAmfType(data); int type = readAmfType(data);
if (type != AMF_TYPE_ECMA_ARRAY) { if (type != AMF_TYPE_ECMA_ARRAY) {
// Not interested. // Should never happen.
return; return;
} }
// Set the duration. // Set the duration to the metadata's value, if present.
Map<String, Object> metadata = (Map<String, Object>) readAmfData(data, type); Map<String, Object> metadata = (Map<String, Object>) readAmfData(data, type);
if (metadata.containsKey(KEY_DURATION)) { if (metadata.containsKey(KEY_DURATION)) {
double durationSeconds = (double) metadata.get(KEY_DURATION); double durationSeconds = (double) metadata.get(KEY_DURATION);
setDurationUs((long) durationSeconds * C.MICROS_PER_SECOND); setDurationUs((long) (durationSeconds * C.MICROS_PER_SECOND));
} }
} }
private String readAmfName(ParsableByteArray data) {
int size = data.readUnsignedShort();
return new String(data.data, data.getPosition(), size);
}
private int readAmfType(ParsableByteArray data) { private int readAmfType(ParsableByteArray data) {
return data.readUnsignedByte(); return data.readUnsignedByte();
} }
...@@ -93,19 +92,19 @@ import java.util.Map; ...@@ -93,19 +92,19 @@ import java.util.Map;
private Object readAmfData(ParsableByteArray data, int type) { private Object readAmfData(ParsableByteArray data, int type) {
switch (type) { switch (type) {
case AMF_TYPE_NUMBER: case AMF_TYPE_NUMBER:
return readAMFDouble(data); return readAmfDouble(data);
case AMF_TYPE_BOOLEAN: case AMF_TYPE_BOOLEAN:
return readAMFBoolean(data); return readAmfBoolean(data);
case AMF_TYPE_STRING: case AMF_TYPE_STRING:
return readAMFString(data); return readAmfString(data);
case AMF_TYPE_OBJECT: case AMF_TYPE_OBJECT:
return readAMFObject(data); return readAmfObject(data);
case AMF_TYPE_ECMA_ARRAY: case AMF_TYPE_ECMA_ARRAY:
return readAMFEcmaArray(data); return readAmfEcmaArray(data);
case AMF_TYPE_STRICT_ARRAY: case AMF_TYPE_STRICT_ARRAY:
return readAMFStrictArray(data); return readAmfStrictArray(data);
case AMF_TYPE_DATE: case AMF_TYPE_DATE:
return readAMFDate(data); return readAmfDate(data);
default: default:
return null; return null;
} }
...@@ -117,20 +116,18 @@ import java.util.Map; ...@@ -117,20 +116,18 @@ import java.util.Map;
* @param data The buffer from which to read. * @param data The buffer from which to read.
* @return The value read from the buffer. * @return The value read from the buffer.
*/ */
private Boolean readAMFBoolean(ParsableByteArray data) { private Boolean readAmfBoolean(ParsableByteArray data) {
return data.readUnsignedByte() == 1; return data.readUnsignedByte() == 1;
} }
/** /**
* Read a double number from an AMF encoded buffer * Read a double number from an AMF encoded buffer.
* *
* @param data The buffer from which to read. * @param data The buffer from which to read.
* @return The value read from the buffer. * @return The value read from the buffer.
*/ */
private Double readAMFDouble(ParsableByteArray data) { private Double readAmfDouble(ParsableByteArray data) {
byte []b = new byte[8]; return Double.longBitsToDouble(data.readLong());
data.readBytes(b, 0, b.length);
return ByteBuffer.wrap(b).getDouble();
} }
/** /**
...@@ -139,11 +136,11 @@ import java.util.Map; ...@@ -139,11 +136,11 @@ import java.util.Map;
* @param data The buffer from which to read. * @param data The buffer from which to read.
* @return The value read from the buffer. * @return The value read from the buffer.
*/ */
private String readAMFString(ParsableByteArray data) { private String readAmfString(ParsableByteArray data) {
int size = data.readUnsignedShort(); int size = data.readUnsignedShort();
byte []b = new byte[size]; int position = data.getPosition();
data.readBytes(b, 0, b.length); data.skipBytes(size);
return new String(b); return new String(data.data, position, size);
} }
/** /**
...@@ -152,7 +149,7 @@ import java.util.Map; ...@@ -152,7 +149,7 @@ import java.util.Map;
* @param data The buffer from which to read. * @param data The buffer from which to read.
* @return The value read from the buffer. * @return The value read from the buffer.
*/ */
private Object readAMFStrictArray(ParsableByteArray data) { private Object readAmfStrictArray(ParsableByteArray data) {
long count = data.readUnsignedInt(); long count = data.readUnsignedInt();
ArrayList<Object> list = new ArrayList<>(); ArrayList<Object> list = new ArrayList<>();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
...@@ -168,11 +165,11 @@ import java.util.Map; ...@@ -168,11 +165,11 @@ import java.util.Map;
* @param data The buffer from which to read. * @param data The buffer from which to read.
* @return The value read from the buffer. * @return The value read from the buffer.
*/ */
private Object readAMFObject(ParsableByteArray data) { private Object readAmfObject(ParsableByteArray data) {
HashMap<String, Object> array = new HashMap<>(); HashMap<String, Object> array = new HashMap<>();
while (true) { while (true) {
String key = readAMFString(data); String key = readAmfString(data);
int type = data.readUnsignedByte(); int type = readAmfType(data);
if (type == AMF_TYPE_END_MARKER) { if (type == AMF_TYPE_END_MARKER) {
break; break;
} }
...@@ -187,12 +184,12 @@ import java.util.Map; ...@@ -187,12 +184,12 @@ import java.util.Map;
* @param data The buffer from which to read. * @param data The buffer from which to read.
* @return The value read from the buffer. * @return The value read from the buffer.
*/ */
private Object readAMFEcmaArray(ParsableByteArray data) { private Object readAmfEcmaArray(ParsableByteArray data) {
long count = data.readUnsignedInt(); long count = data.readUnsignedInt();
HashMap<String, Object> array = new HashMap<>(); HashMap<String, Object> array = new HashMap<>();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
String key = readAMFString(data); String key = readAmfString(data);
int type = data.readUnsignedByte(); int type = readAmfType(data);
array.put(key, readAmfData(data, type)); array.put(key, readAmfData(data, type));
} }
return array; return array;
...@@ -204,8 +201,8 @@ import java.util.Map; ...@@ -204,8 +201,8 @@ import java.util.Map;
* @param data The buffer from which to read. * @param data The buffer from which to read.
* @return The value read from the buffer. * @return The value read from the buffer.
*/ */
private Date readAMFDate(ParsableByteArray data) { private Date readAmfDate(ParsableByteArray data) {
final Date date = new Date((long) readAMFDouble(data).doubleValue()); final Date date = new Date((long) readAmfDouble(data).doubleValue());
data.readUnsignedShort(); data.readUnsignedShort();
return date; return date;
} }
......
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