Skip to content

Commit 51d2359

Browse files
zhiyua-gitminbi
authored andcommitted
[KinesisVideo] Remove trailing zeros before putting frame into SDK (#971)
1 parent 6dd65ad commit 51d2359

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.amazonaws.services.kinesisvideo;
2+
3+
import com.amazonaws.kinesisvideo.producer.KinesisVideoFrame;
4+
5+
import org.junit.Test;
6+
7+
import java.nio.ByteBuffer;
8+
9+
import android.support.test.runner.AndroidJUnit4;
10+
11+
import static com.amazonaws.kinesisvideo.producer.FrameFlags.FRAME_FLAG_NONE;
12+
import static junit.framework.Assert.assertEquals;
13+
14+
@RunWith(AndroidJUnit4.class)
15+
public class KinesisVideoFrameTest {
16+
@Test
17+
public void testTrailingZeroRemoved() {
18+
byte[] rawData = new byte[] {1, 2, 3, 0, 0, 0, 0, 0};
19+
KinesisVideoFrame frame = new KinesisVideoFrame(0, FRAME_FLAG_NONE, 0,
20+
0, 1, ByteBuffer.wrap(rawData));
21+
assertEquals(3, frame.getSize());
22+
byte[] actualDataPassed = new byte[frame.getSize()];
23+
frame.getData().get(actualDataPassed);
24+
for (int i = 0; i < actualDataPassed.length; i++) {
25+
assertEquals(rawData[i], actualDataPassed[i]);
26+
}
27+
}
28+
}

aws-android-sdk-kinesisvideo/src/main/java/com/amazonaws/kinesisvideo/producer/KinesisVideoFrame.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public KinesisVideoFrame(int index, int flags, long decodingTs, long presentatio
6969
mPresentationTs = presentationTs;
7070
mDuration = duration;
7171
mData = requireNonNull(data);
72+
// In some devices encoder would generate frames with more than 3 trailing zeros
73+
// which is not allowed by AnnexB specification
74+
removeTrailingZeros();
7275
}
7376

7477
public int getIndex() {
@@ -116,4 +119,13 @@ public ByteBuffer getData() {
116119
.append(", mPresentationTs=").append(mPresentationTs).append(", mDuration=").append(mDuration)
117120
.append(", mData=").append(mData).append("}").toString();
118121
}
122+
123+
private void removeTrailingZeros() {
124+
for (int index = mData.limit() - 1; index > mData.position(); index--) {
125+
if (mData.get(index) != 0) {
126+
mData.limit(index + 1);
127+
break;
128+
}
129+
}
130+
}
119131
}

0 commit comments

Comments
 (0)