Skip to content

Commit

Permalink
Final patch for RTMP timestamp
Browse files Browse the repository at this point in the history
Extended timestamp in chunk type 3 should not be forgotten.

Signed-off-by: Leo Ma <begeekmyfriend@gmail.com>
  • Loading branch information
begeekmyfriend committed May 12, 2016
1 parent 9e35f22 commit 5ee43c6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 27 deletions.
34 changes: 9 additions & 25 deletions app/src/main/java/net/ossrs/sea/rtmp/packets/RtmpHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class RtmpHeader {
* Note: docstrings are adapted from the official Adobe RTMP spec:
* http://www.adobe.com/devnet/rtmp/
*/
public static enum MessageType {
public enum MessageType {

/**
* Protocol control message 1
Expand Down Expand Up @@ -146,7 +146,7 @@ public static enum MessageType {
}
}

private MessageType(int value) {
MessageType(int value) {
this.value = (byte) value;
}

Expand All @@ -164,20 +164,19 @@ public static MessageType valueOf(byte messageTypeId) {
}
}

public static enum ChunkType {
public enum ChunkType {

/** Full 12-byte RTMP chunk header */
TYPE_0_FULL(0x00, 12),
TYPE_0_FULL(0x00),
/** Relative 8-byte RTMP chunk header (message stream ID is not included) */
TYPE_1_RELATIVE_LARGE(0x01, 8),
TYPE_1_RELATIVE_LARGE(0x01),
/** Relative 4-byte RTMP chunk header (only timestamp delta) */
TYPE_2_RELATIVE_TIMESTAMP_ONLY(0x02, 4),
TYPE_2_RELATIVE_TIMESTAMP_ONLY(0x02),
/** Relative 1-byte RTMP chunk header (no "real" header, just the 1-byte indicating chunk header type & chunk stream ID) */
TYPE_3_RELATIVE_SINGLE_BYTE(0x03, 1);
TYPE_3_RELATIVE_SINGLE_BYTE(0x03);
/** The byte value of this chunk header type */
private byte value;
/** The full size (in bytes) of this RTMP header (including the basic header byte) */
private int size;
private static final Map<Byte, ChunkType> quickLookupMap = new HashMap<Byte, ChunkType>();

static {
Expand All @@ -186,20 +185,15 @@ public static enum ChunkType {
}
}

private ChunkType(int byteValue, int fullHeaderSize) {
ChunkType(int byteValue) {
this.value = (byte) byteValue;
this.size = fullHeaderSize;
}

/** Returns the byte value of this chunk header type */
public byte getValue() {
return value;
}

public int getSize() {
return size;
}

public static ChunkType valueOf(byte chunkHeaderType) {
if (quickLookupMap.containsKey(chunkHeaderType)) {
return quickLookupMap.get(chunkHeaderType);
Expand Down Expand Up @@ -309,7 +303,7 @@ private void readHeaderImpl(InputStream in, RtmpSessionInfo rtmpSessionInfo) thr
}
}

public void writeTo(OutputStream out, final ChunkStreamInfo chunkStreamInfo) throws IOException {
public void writeTo(OutputStream out, ChunkType chunkType, final ChunkStreamInfo chunkStreamInfo) throws IOException {
// Write basic header byte
out.write(((byte) (chunkType.getValue() << 6) | chunkStreamId));
switch (chunkType) {
Expand Down Expand Up @@ -423,14 +417,4 @@ public void setMessageType(MessageType messageType) {
public void setPacketLength(int packetLength) {
this.packetLength = packetLength;
}

public void writeAggregateHeaderByte(OutputStream out) throws IOException {
// Aggregate header 0x11 : 11.. ....
out.write(0xC0 | chunkStreamId);
}

public static void writeAggregateHeaderByte(OutputStream out, int chunkStreamId) throws IOException {
// Aggregate header 0x11 : 11.. ....
out.write(0xC0 | chunkStreamId);
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/net/ossrs/sea/rtmp/packets/RtmpPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void writeTo(OutputStream out, final int chunkSize, final ChunkStreamInfo
byte[] body = baos.toByteArray();
header.setPacketLength(body.length);
// Write header for first chunk
header.writeTo(out, chunkStreamInfo);
header.writeTo(out, RtmpHeader.ChunkType.TYPE_0_FULL, chunkStreamInfo);
int remainingBytes = body.length;
int pos = 0;
while (remainingBytes > chunkSize) {
Expand All @@ -41,7 +41,7 @@ public void writeTo(OutputStream out, final int chunkSize, final ChunkStreamInfo
remainingBytes -= chunkSize;
pos += chunkSize;
// Write header for remain chunk
header.writeAggregateHeaderByte(out);
header.writeTo(out, RtmpHeader.ChunkType.TYPE_3_RELATIVE_SINGLE_BYTE, chunkStreamInfo);
}
out.write(body, pos, remainingBytes);
}
Expand Down

0 comments on commit 5ee43c6

Please sign in to comment.