mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 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 | */ |
sakal | e1674ef | 2017-01-11 06:22:56 -0800 | [diff] [blame] | 10 | |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 11 | package org.webrtc; |
| 12 | |
| 13 | import android.os.Handler; |
| 14 | import android.os.HandlerThread; |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 15 | import java.io.FileOutputStream; |
| 16 | import java.io.IOException; |
Magnus Jedvert | 894c400 | 2016-10-21 15:05:01 +0200 | [diff] [blame] | 17 | import java.nio.ByteBuffer; |
Sami Kalliomäki | bde473e | 2017-10-30 13:34:41 +0100 | [diff] [blame^] | 18 | import java.nio.charset.Charset; |
mandermo | eef94d9 | 2017-01-19 09:02:29 -0800 | [diff] [blame] | 19 | import java.util.ArrayList; |
Sami Kalliomäki | cb98b11 | 2017-10-16 11:20:26 +0200 | [diff] [blame] | 20 | import java.util.concurrent.CountDownLatch; |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * Can be used to save the video frames to file. |
| 24 | */ |
| 25 | public class VideoFileRenderer implements VideoRenderer.Callbacks { |
magjed | 282d49f | 2017-04-13 04:17:02 -0700 | [diff] [blame] | 26 | static { |
| 27 | System.loadLibrary("jingle_peerconnection_so"); |
| 28 | } |
| 29 | |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 30 | private static final String TAG = "VideoFileRenderer"; |
| 31 | |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 32 | private final HandlerThread renderThread; |
| 33 | private final Object handlerLock = new Object(); |
| 34 | private final Handler renderThreadHandler; |
| 35 | private final FileOutputStream videoOutFile; |
mandermo | eef94d9 | 2017-01-19 09:02:29 -0800 | [diff] [blame] | 36 | private final String outputFileName; |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 37 | private final int outputFileWidth; |
| 38 | private final int outputFileHeight; |
| 39 | private final int outputFrameSize; |
| 40 | private final ByteBuffer outputFrameBuffer; |
magjed | 1cb4823 | 2016-10-20 03:19:16 -0700 | [diff] [blame] | 41 | private EglBase eglBase; |
| 42 | private YuvConverter yuvConverter; |
mandermo | eef94d9 | 2017-01-19 09:02:29 -0800 | [diff] [blame] | 43 | private ArrayList<ByteBuffer> rawFrames = new ArrayList<>(); |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 44 | |
| 45 | public VideoFileRenderer(String outputFile, int outputFileWidth, int outputFileHeight, |
magjed | 1cb4823 | 2016-10-20 03:19:16 -0700 | [diff] [blame] | 46 | final EglBase.Context sharedContext) throws IOException { |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 47 | if ((outputFileWidth % 2) == 1 || (outputFileHeight % 2) == 1) { |
| 48 | throw new IllegalArgumentException("Does not support uneven width or height"); |
| 49 | } |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 50 | |
mandermo | eef94d9 | 2017-01-19 09:02:29 -0800 | [diff] [blame] | 51 | this.outputFileName = outputFile; |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 52 | this.outputFileWidth = outputFileWidth; |
| 53 | this.outputFileHeight = outputFileHeight; |
| 54 | |
| 55 | outputFrameSize = outputFileWidth * outputFileHeight * 3 / 2; |
| 56 | outputFrameBuffer = ByteBuffer.allocateDirect(outputFrameSize); |
| 57 | |
| 58 | videoOutFile = new FileOutputStream(outputFile); |
| 59 | videoOutFile.write( |
| 60 | ("YUV4MPEG2 C420 W" + outputFileWidth + " H" + outputFileHeight + " Ip F30:1 A1:1\n") |
Sami Kalliomäki | bde473e | 2017-10-30 13:34:41 +0100 | [diff] [blame^] | 61 | .getBytes(Charset.forName("US-ASCII"))); |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 62 | |
| 63 | renderThread = new HandlerThread(TAG); |
| 64 | renderThread.start(); |
| 65 | renderThreadHandler = new Handler(renderThread.getLooper()); |
magjed | 1cb4823 | 2016-10-20 03:19:16 -0700 | [diff] [blame] | 66 | |
| 67 | ThreadUtils.invokeAtFrontUninterruptibly(renderThreadHandler, new Runnable() { |
| 68 | @Override |
| 69 | public void run() { |
| 70 | eglBase = EglBase.create(sharedContext, EglBase.CONFIG_PIXEL_BUFFER); |
| 71 | eglBase.createDummyPbufferSurface(); |
| 72 | eglBase.makeCurrent(); |
| 73 | yuvConverter = new YuvConverter(); |
| 74 | } |
| 75 | }); |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void renderFrame(final VideoRenderer.I420Frame frame) { |
| 80 | renderThreadHandler.post(new Runnable() { |
| 81 | @Override |
| 82 | public void run() { |
| 83 | renderFrameOnRenderThread(frame); |
| 84 | } |
| 85 | }); |
| 86 | } |
| 87 | |
Sami Kalliomäki | 9828beb | 2017-10-26 16:21:22 +0200 | [diff] [blame] | 88 | // TODO(sakal): yuvConverter.convert is deprecated. This will be removed once this file is updated |
| 89 | // to implement VideoSink instead of VideoRenderer.Callbacks. |
| 90 | @SuppressWarnings("deprecation") |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 91 | private void renderFrameOnRenderThread(VideoRenderer.I420Frame frame) { |
| 92 | final float frameAspectRatio = (float) frame.rotatedWidth() / (float) frame.rotatedHeight(); |
| 93 | |
| 94 | final float[] rotatedSamplingMatrix = |
| 95 | RendererCommon.rotateTextureMatrix(frame.samplingMatrix, frame.rotationDegree); |
| 96 | final float[] layoutMatrix = RendererCommon.getLayoutMatrix( |
| 97 | false, frameAspectRatio, (float) outputFileWidth / outputFileHeight); |
| 98 | final float[] texMatrix = RendererCommon.multiplyMatrices(rotatedSamplingMatrix, layoutMatrix); |
| 99 | |
| 100 | try { |
Sami Kalliomäki | cb98b11 | 2017-10-16 11:20:26 +0200 | [diff] [blame] | 101 | ByteBuffer buffer = JniCommon.allocateNativeByteBuffer(outputFrameSize); |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 102 | if (!frame.yuvFrame) { |
| 103 | yuvConverter.convert(outputFrameBuffer, outputFileWidth, outputFileHeight, outputFileWidth, |
| 104 | frame.textureId, texMatrix); |
| 105 | |
| 106 | int stride = outputFileWidth; |
| 107 | byte[] data = outputFrameBuffer.array(); |
| 108 | int offset = outputFrameBuffer.arrayOffset(); |
| 109 | |
| 110 | // Write Y |
mandermo | eef94d9 | 2017-01-19 09:02:29 -0800 | [diff] [blame] | 111 | buffer.put(data, offset, outputFileWidth * outputFileHeight); |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 112 | |
| 113 | // Write U |
| 114 | for (int r = outputFileHeight; r < outputFileHeight * 3 / 2; ++r) { |
mandermo | eef94d9 | 2017-01-19 09:02:29 -0800 | [diff] [blame] | 115 | buffer.put(data, offset + r * stride, stride / 2); |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // Write V |
| 119 | for (int r = outputFileHeight; r < outputFileHeight * 3 / 2; ++r) { |
mandermo | eef94d9 | 2017-01-19 09:02:29 -0800 | [diff] [blame] | 120 | buffer.put(data, offset + r * stride + stride / 2, stride / 2); |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 121 | } |
| 122 | } else { |
| 123 | nativeI420Scale(frame.yuvPlanes[0], frame.yuvStrides[0], frame.yuvPlanes[1], |
| 124 | frame.yuvStrides[1], frame.yuvPlanes[2], frame.yuvStrides[2], frame.width, frame.height, |
| 125 | outputFrameBuffer, outputFileWidth, outputFileHeight); |
mandermo | eef94d9 | 2017-01-19 09:02:29 -0800 | [diff] [blame] | 126 | |
| 127 | buffer.put(outputFrameBuffer.array(), outputFrameBuffer.arrayOffset(), outputFrameSize); |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 128 | } |
mandermo | eef94d9 | 2017-01-19 09:02:29 -0800 | [diff] [blame] | 129 | buffer.rewind(); |
| 130 | rawFrames.add(buffer); |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 131 | } finally { |
| 132 | VideoRenderer.renderFrameDone(frame); |
| 133 | } |
| 134 | } |
| 135 | |
Magnus Jedvert | 894c400 | 2016-10-21 15:05:01 +0200 | [diff] [blame] | 136 | /** |
| 137 | * Release all resources. All already posted frames will be rendered first. |
| 138 | */ |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 139 | public void release() { |
Magnus Jedvert | 894c400 | 2016-10-21 15:05:01 +0200 | [diff] [blame] | 140 | final CountDownLatch cleanupBarrier = new CountDownLatch(1); |
| 141 | renderThreadHandler.post(new Runnable() { |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 142 | @Override |
| 143 | public void run() { |
magjed | 1cb4823 | 2016-10-20 03:19:16 -0700 | [diff] [blame] | 144 | yuvConverter.release(); |
| 145 | eglBase.release(); |
| 146 | renderThread.quit(); |
Magnus Jedvert | 894c400 | 2016-10-21 15:05:01 +0200 | [diff] [blame] | 147 | cleanupBarrier.countDown(); |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 148 | } |
| 149 | }); |
Magnus Jedvert | 894c400 | 2016-10-21 15:05:01 +0200 | [diff] [blame] | 150 | ThreadUtils.awaitUninterruptibly(cleanupBarrier); |
mandermo | eef94d9 | 2017-01-19 09:02:29 -0800 | [diff] [blame] | 151 | try { |
| 152 | for (ByteBuffer buffer : rawFrames) { |
Sami Kalliomäki | bde473e | 2017-10-30 13:34:41 +0100 | [diff] [blame^] | 153 | videoOutFile.write("FRAME\n".getBytes(Charset.forName("US-ASCII"))); |
mandermo | eef94d9 | 2017-01-19 09:02:29 -0800 | [diff] [blame] | 154 | |
| 155 | byte[] data = new byte[outputFrameSize]; |
| 156 | buffer.get(data); |
| 157 | |
| 158 | videoOutFile.write(data); |
| 159 | |
Sami Kalliomäki | cb98b11 | 2017-10-16 11:20:26 +0200 | [diff] [blame] | 160 | JniCommon.freeNativeByteBuffer(buffer); |
mandermo | eef94d9 | 2017-01-19 09:02:29 -0800 | [diff] [blame] | 161 | } |
| 162 | videoOutFile.close(); |
| 163 | Logging.d(TAG, "Video written to disk as " + outputFileName + ". Number frames are " |
| 164 | + rawFrames.size() + " and the dimension of the frames are " + outputFileWidth + "x" |
| 165 | + outputFileHeight + "."); |
| 166 | } catch (IOException e) { |
| 167 | Logging.e(TAG, "Error writing video to disk", e); |
| 168 | } |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | public static native void nativeI420Scale(ByteBuffer srcY, int strideY, ByteBuffer srcU, |
| 172 | int strideU, ByteBuffer srcV, int strideV, int width, int height, ByteBuffer dst, |
| 173 | int dstWidth, int dstHeight); |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 174 | } |