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