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