1 /* 2 * Copyright 2018 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 package org.webrtc; 12 13 import static com.google.common.truth.Truth.assertThat; 14 import static com.google.common.truth.Truth.assertWithMessage; 15 16 import java.nio.ByteBuffer; 17 import java.util.Random; 18 19 /** 20 * Helper methods for {@link HardwareVideoEncoderTest} and {@link AndroidVideoDecoderTest}. 21 */ 22 class CodecTestHelper { assertEqualContents(byte[] expected, ByteBuffer actual, int offset, int size)23 static void assertEqualContents(byte[] expected, ByteBuffer actual, int offset, int size) { 24 assertThat(size).isEqualTo(expected.length); 25 assertThat(actual.capacity()).isAtLeast(offset + size); 26 for (int i = 0; i < expected.length; i++) { 27 assertWithMessage("At index: " + i).that(actual.get(offset + i)).isEqualTo(expected[i]); 28 } 29 } 30 generateRandomData(int length)31 static byte[] generateRandomData(int length) { 32 Random random = new Random(); 33 byte[] data = new byte[length]; 34 random.nextBytes(data); 35 return data; 36 } 37 wrapI420(int width, int height, byte[] data)38 static VideoFrame.I420Buffer wrapI420(int width, int height, byte[] data) { 39 final int posY = 0; 40 final int posU = width * height; 41 final int posV = posU + width * height / 4; 42 final int endV = posV + width * height / 4; 43 44 ByteBuffer buffer = ByteBuffer.allocateDirect(data.length); 45 buffer.put(data); 46 47 buffer.limit(posU); 48 buffer.position(posY); 49 ByteBuffer dataY = buffer.slice(); 50 51 buffer.limit(posV); 52 buffer.position(posU); 53 ByteBuffer dataU = buffer.slice(); 54 55 buffer.limit(endV); 56 buffer.position(posV); 57 ByteBuffer dataV = buffer.slice(); 58 59 return JavaI420Buffer.wrap(width, height, dataY, width, dataU, width / 2, dataV, width / 2, 60 /* releaseCallback= */ null); 61 } 62 } 63