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 | 75db552 | 2018-01-22 14:19:16 +0100 | [diff] [blame] | 20 | import java.util.concurrent.BlockingQueue; |
Sami Kalliomäki | cb98b11 | 2017-10-16 11:20:26 +0200 | [diff] [blame] | 21 | import java.util.concurrent.CountDownLatch; |
Sami Kalliomäki | 75db552 | 2018-01-22 14:19:16 +0100 | [diff] [blame] | 22 | import java.util.concurrent.LinkedBlockingQueue; |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Can be used to save the video frames to file. |
| 26 | */ |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 27 | @JNINamespace("webrtc::jni") |
Sami Kalliomäki | 75db552 | 2018-01-22 14:19:16 +0100 | [diff] [blame] | 28 | public class VideoFileRenderer implements VideoRenderer.Callbacks, VideoSink { |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 29 | private static final String TAG = "VideoFileRenderer"; |
| 30 | |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 31 | private final HandlerThread renderThread; |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 32 | private final Handler renderThreadHandler; |
| 33 | private final FileOutputStream videoOutFile; |
mandermo | eef94d9 | 2017-01-19 09:02:29 -0800 | [diff] [blame] | 34 | private final String outputFileName; |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 35 | private final int outputFileWidth; |
| 36 | private final int outputFileHeight; |
| 37 | private final int outputFrameSize; |
| 38 | private final ByteBuffer outputFrameBuffer; |
magjed | 1cb4823 | 2016-10-20 03:19:16 -0700 | [diff] [blame] | 39 | private EglBase eglBase; |
| 40 | private YuvConverter yuvConverter; |
mandermo | eef94d9 | 2017-01-19 09:02:29 -0800 | [diff] [blame] | 41 | private ArrayList<ByteBuffer> rawFrames = new ArrayList<>(); |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 42 | |
| 43 | public VideoFileRenderer(String outputFile, int outputFileWidth, int outputFileHeight, |
magjed | 1cb4823 | 2016-10-20 03:19:16 -0700 | [diff] [blame] | 44 | final EglBase.Context sharedContext) throws IOException { |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 45 | if ((outputFileWidth % 2) == 1 || (outputFileHeight % 2) == 1) { |
| 46 | throw new IllegalArgumentException("Does not support uneven width or height"); |
| 47 | } |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 48 | |
mandermo | eef94d9 | 2017-01-19 09:02:29 -0800 | [diff] [blame] | 49 | this.outputFileName = outputFile; |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 50 | this.outputFileWidth = outputFileWidth; |
| 51 | this.outputFileHeight = outputFileHeight; |
| 52 | |
| 53 | outputFrameSize = outputFileWidth * outputFileHeight * 3 / 2; |
| 54 | outputFrameBuffer = ByteBuffer.allocateDirect(outputFrameSize); |
| 55 | |
| 56 | videoOutFile = new FileOutputStream(outputFile); |
| 57 | videoOutFile.write( |
| 58 | ("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] | 59 | .getBytes(Charset.forName("US-ASCII"))); |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 60 | |
| 61 | renderThread = new HandlerThread(TAG); |
| 62 | renderThread.start(); |
| 63 | renderThreadHandler = new Handler(renderThread.getLooper()); |
magjed | 1cb4823 | 2016-10-20 03:19:16 -0700 | [diff] [blame] | 64 | |
| 65 | ThreadUtils.invokeAtFrontUninterruptibly(renderThreadHandler, new Runnable() { |
| 66 | @Override |
| 67 | public void run() { |
| 68 | eglBase = EglBase.create(sharedContext, EglBase.CONFIG_PIXEL_BUFFER); |
| 69 | eglBase.createDummyPbufferSurface(); |
| 70 | eglBase.makeCurrent(); |
| 71 | yuvConverter = new YuvConverter(); |
| 72 | } |
| 73 | }); |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
Sami Kalliomäki | 75db552 | 2018-01-22 14:19:16 +0100 | [diff] [blame] | 77 | public void renderFrame(final VideoRenderer.I420Frame i420Frame) { |
| 78 | final VideoFrame frame = i420Frame.toVideoFrame(); |
| 79 | onFrame(frame); |
| 80 | frame.release(); |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Sami Kalliomäki | 75db552 | 2018-01-22 14:19:16 +0100 | [diff] [blame] | 83 | @Override |
| 84 | public void onFrame(VideoFrame frame) { |
| 85 | frame.retain(); |
| 86 | renderThreadHandler.post(() -> renderFrameOnRenderThread(frame)); |
| 87 | } |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 88 | |
Sami Kalliomäki | 75db552 | 2018-01-22 14:19:16 +0100 | [diff] [blame] | 89 | private void renderFrameOnRenderThread(VideoFrame frame) { |
| 90 | final VideoFrame.Buffer buffer = frame.getBuffer(); |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 91 | |
Sami Kalliomäki | 75db552 | 2018-01-22 14:19:16 +0100 | [diff] [blame] | 92 | // If the frame is rotated, it will be applied after cropAndScale. Therefore, if the frame is |
| 93 | // rotated by 90 degrees, swap width and height. |
| 94 | final int targetWidth = frame.getRotation() % 180 == 0 ? outputFileWidth : outputFileHeight; |
| 95 | final int targetHeight = frame.getRotation() % 180 == 0 ? outputFileHeight : outputFileWidth; |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 96 | |
Sami Kalliomäki | 75db552 | 2018-01-22 14:19:16 +0100 | [diff] [blame] | 97 | final float frameAspectRatio = (float) buffer.getWidth() / (float) buffer.getHeight(); |
| 98 | final float fileAspectRatio = (float) targetWidth / (float) targetHeight; |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 99 | |
Sami Kalliomäki | 75db552 | 2018-01-22 14:19:16 +0100 | [diff] [blame] | 100 | // Calculate cropping to equalize the aspect ratio. |
| 101 | int cropWidth = buffer.getWidth(); |
| 102 | int cropHeight = buffer.getHeight(); |
| 103 | if (fileAspectRatio > frameAspectRatio) { |
| 104 | cropHeight *= frameAspectRatio / fileAspectRatio; |
| 105 | } else { |
| 106 | cropWidth *= fileAspectRatio / frameAspectRatio; |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 107 | } |
Sami Kalliomäki | 75db552 | 2018-01-22 14:19:16 +0100 | [diff] [blame] | 108 | |
| 109 | final int cropX = (buffer.getWidth() - cropWidth) / 2; |
| 110 | final int cropY = (buffer.getHeight() - cropHeight) / 2; |
| 111 | |
| 112 | final VideoFrame.Buffer scaledBuffer = |
| 113 | buffer.cropAndScale(cropX, cropY, cropWidth, cropHeight, targetWidth, targetHeight); |
| 114 | frame.release(); |
| 115 | |
| 116 | final VideoFrame.I420Buffer i420 = scaledBuffer.toI420(); |
| 117 | scaledBuffer.release(); |
| 118 | |
| 119 | ByteBuffer byteBuffer = JniCommon.nativeAllocateByteBuffer(outputFrameSize); |
| 120 | YuvHelper.I420Rotate(i420.getDataY(), i420.getStrideY(), i420.getDataU(), i420.getStrideU(), |
| 121 | i420.getDataV(), i420.getStrideV(), byteBuffer, i420.getWidth(), i420.getHeight(), |
| 122 | frame.getRotation()); |
| 123 | i420.release(); |
| 124 | |
| 125 | byteBuffer.rewind(); |
| 126 | rawFrames.add(byteBuffer); |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Magnus Jedvert | 894c400 | 2016-10-21 15:05:01 +0200 | [diff] [blame] | 129 | /** |
| 130 | * Release all resources. All already posted frames will be rendered first. |
| 131 | */ |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 132 | public void release() { |
Magnus Jedvert | 894c400 | 2016-10-21 15:05:01 +0200 | [diff] [blame] | 133 | final CountDownLatch cleanupBarrier = new CountDownLatch(1); |
Sami Kalliomäki | 75db552 | 2018-01-22 14:19:16 +0100 | [diff] [blame] | 134 | renderThreadHandler.post(() -> { |
| 135 | yuvConverter.release(); |
| 136 | eglBase.release(); |
| 137 | renderThread.quit(); |
| 138 | cleanupBarrier.countDown(); |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 139 | }); |
Magnus Jedvert | 894c400 | 2016-10-21 15:05:01 +0200 | [diff] [blame] | 140 | ThreadUtils.awaitUninterruptibly(cleanupBarrier); |
mandermo | eef94d9 | 2017-01-19 09:02:29 -0800 | [diff] [blame] | 141 | try { |
| 142 | for (ByteBuffer buffer : rawFrames) { |
Sami Kalliomäki | bde473e | 2017-10-30 13:34:41 +0100 | [diff] [blame] | 143 | videoOutFile.write("FRAME\n".getBytes(Charset.forName("US-ASCII"))); |
mandermo | eef94d9 | 2017-01-19 09:02:29 -0800 | [diff] [blame] | 144 | |
| 145 | byte[] data = new byte[outputFrameSize]; |
| 146 | buffer.get(data); |
| 147 | |
| 148 | videoOutFile.write(data); |
| 149 | |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 150 | JniCommon.nativeFreeByteBuffer(buffer); |
mandermo | eef94d9 | 2017-01-19 09:02:29 -0800 | [diff] [blame] | 151 | } |
| 152 | videoOutFile.close(); |
Sami Kalliomäki | 75db552 | 2018-01-22 14:19:16 +0100 | [diff] [blame] | 153 | Logging.d(TAG, |
| 154 | "Video written to disk as " + outputFileName + ". Number frames are " + rawFrames.size() |
| 155 | + " and the dimension of the frames are " + outputFileWidth + "x" + outputFileHeight |
| 156 | + "."); |
mandermo | eef94d9 | 2017-01-19 09:02:29 -0800 | [diff] [blame] | 157 | } catch (IOException e) { |
| 158 | Logging.e(TAG, "Error writing video to disk", e); |
| 159 | } |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 160 | } |
mandermo | 64e1a32 | 2016-10-18 08:47:51 -0700 | [diff] [blame] | 161 | } |