• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2016 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 org.junit.Assert.assertEquals;
14 
15 import android.os.Environment;
16 import android.support.test.filters.SmallTest;
17 import java.io.File;
18 import java.io.IOException;
19 import java.io.RandomAccessFile;
20 import java.nio.ByteBuffer;
21 import java.nio.charset.Charset;
22 import org.chromium.base.test.BaseJUnit4ClassRunner;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 
27 @RunWith(BaseJUnit4ClassRunner.class)
28 public class VideoFileRendererTest {
29   @Before
setUp()30   public void setUp() {
31     NativeLibrary.initialize(new NativeLibrary.DefaultLoader(), TestConstants.NATIVE_LIBRARY);
32   }
33 
34   @Test
35   @SmallTest
testYuvRenderingToFile()36   public void testYuvRenderingToFile() throws InterruptedException, IOException {
37     EglBase eglBase = EglBase.create();
38     final String videoOutPath = Environment.getExternalStorageDirectory().getPath()
39         + "/chromium_tests_root/testvideoout.y4m";
40     int frameWidth = 4;
41     int frameHeight = 4;
42     VideoFileRenderer videoFileRenderer =
43         new VideoFileRenderer(videoOutPath, frameWidth, frameHeight, eglBase.getEglBaseContext());
44 
45     String[] frames = {
46         "THIS IS JUST SOME TEXT x", "THE SECOND FRAME qwerty.", "HERE IS THE THRID FRAME!"};
47 
48     for (String frameStr : frames) {
49       int[] planeSizes = {
50           frameWidth * frameWidth, frameWidth * frameHeight / 4, frameWidth * frameHeight / 4};
51       int[] yuvStrides = {frameWidth, frameWidth / 2, frameWidth / 2};
52 
53       ByteBuffer[] yuvPlanes = new ByteBuffer[3];
54       byte[] frameBytes = frameStr.getBytes(Charset.forName("US-ASCII"));
55       int pos = 0;
56       for (int i = 0; i < 3; i++) {
57         yuvPlanes[i] = ByteBuffer.allocateDirect(planeSizes[i]);
58         yuvPlanes[i].put(frameBytes, pos, planeSizes[i]);
59         yuvPlanes[i].rewind();
60         pos += planeSizes[i];
61       }
62 
63       VideoFrame.I420Buffer buffer =
64           JavaI420Buffer.wrap(frameWidth, frameHeight, yuvPlanes[0], yuvStrides[0], yuvPlanes[1],
65               yuvStrides[1], yuvPlanes[2], yuvStrides[2], null /* releaseCallback */);
66 
67       VideoFrame frame = new VideoFrame(buffer, 0 /* rotation */, 0 /* timestampNs */);
68       videoFileRenderer.onFrame(frame);
69       frame.release();
70     }
71     videoFileRenderer.release();
72 
73     RandomAccessFile writtenFile = new RandomAccessFile(videoOutPath, "r");
74     try {
75       int length = (int) writtenFile.length();
76       byte[] data = new byte[length];
77       writtenFile.readFully(data);
78       String fileContent = new String(data, Charset.forName("US-ASCII"));
79       String expected = "YUV4MPEG2 C420 W4 H4 Ip F30:1 A1:1\n"
80           + "FRAME\n"
81           + "THIS IS JUST SOME TEXT xFRAME\n"
82           + "THE SECOND FRAME qwerty.FRAME\n"
83           + "HERE IS THE THRID FRAME!";
84       assertEquals(expected, fileContent);
85     } finally {
86       writtenFile.close();
87     }
88 
89     new File(videoOutPath).delete();
90   }
91 }
92