henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 11 | package org.webrtc; |
| 12 | |
Magnus Jedvert | 1a759c6 | 2018-04-24 15:11:02 +0200 | [diff] [blame] | 13 | import javax.annotation.Nullable; |
| 14 | |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 15 | /** |
Magnus Jedvert | 7640fcf | 2016-09-21 16:20:03 +0200 | [diff] [blame] | 16 | * Java wrapper of native AndroidVideoTrackSource. |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 17 | */ |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 18 | @JNINamespace("webrtc::jni") |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 19 | public class VideoSource extends MediaSource { |
Magnus Jedvert | 1a759c6 | 2018-04-24 15:11:02 +0200 | [diff] [blame] | 20 | private static class NativeCapturerObserver implements VideoCapturer.CapturerObserver { |
| 21 | private final long nativeSource; |
| 22 | // TODO(bugs.webrtc.org/9181): Remove. |
| 23 | @Nullable private final SurfaceTextureHelper surfaceTextureHelper; |
| 24 | |
| 25 | public NativeCapturerObserver(long nativeSource) { |
| 26 | this.nativeSource = nativeSource; |
| 27 | this.surfaceTextureHelper = null; |
| 28 | } |
| 29 | |
| 30 | // TODO(bugs.webrtc.org/9181): Remove. |
| 31 | public NativeCapturerObserver(long nativeSource, SurfaceTextureHelper surfaceTextureHelper) { |
| 32 | this.nativeSource = nativeSource; |
| 33 | this.surfaceTextureHelper = surfaceTextureHelper; |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public void onCapturerStarted(boolean success) { |
| 38 | nativeCapturerStarted(nativeSource, success); |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public void onCapturerStopped() { |
| 43 | nativeCapturerStopped(nativeSource); |
| 44 | } |
| 45 | |
| 46 | // TODO(bugs.webrtc.org/9181): Remove. |
| 47 | @Override |
| 48 | @SuppressWarnings("deprecation") |
| 49 | public void onByteBufferFrameCaptured( |
| 50 | byte[] data, int width, int height, int rotation, long timestampNs) { |
| 51 | // This NV21Buffer is not possible to retain. This is safe only because the native code will |
| 52 | // always call cropAndScale() and directly make a deep copy of the buffer. |
| 53 | final VideoFrame.Buffer nv21Buffer = |
| 54 | new NV21Buffer(data, width, height, null /* releaseCallback */); |
| 55 | final VideoFrame frame = new VideoFrame(nv21Buffer, rotation, timestampNs); |
| 56 | onFrameCaptured(frame); |
| 57 | frame.release(); |
| 58 | } |
| 59 | |
| 60 | // TODO(bugs.webrtc.org/9181): Remove. |
| 61 | @Override |
| 62 | @SuppressWarnings("deprecation") |
| 63 | public void onTextureFrameCaptured(int width, int height, int oesTextureId, |
| 64 | float[] transformMatrix, int rotation, long timestampNs) { |
| 65 | final VideoFrame.Buffer buffer = surfaceTextureHelper.createTextureBuffer( |
| 66 | width, height, RendererCommon.convertMatrixToAndroidGraphicsMatrix(transformMatrix)); |
| 67 | final VideoFrame frame = new VideoFrame(buffer, rotation, timestampNs); |
| 68 | onFrameCaptured(frame); |
| 69 | frame.release(); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void onFrameCaptured(VideoFrame frame) { |
| 74 | nativeOnFrameCaptured(nativeSource, frame.getBuffer().getWidth(), |
| 75 | frame.getBuffer().getHeight(), frame.getRotation(), frame.getTimestampNs(), |
| 76 | frame.getBuffer()); |
| 77 | } |
Magnus Jedvert | 26b9e12 | 2018-05-02 14:41:22 +0200 | [diff] [blame] | 78 | |
| 79 | public void dispose() { |
| 80 | if (surfaceTextureHelper != null) { |
| 81 | surfaceTextureHelper.dispose(); |
| 82 | } |
| 83 | } |
Magnus Jedvert | 1a759c6 | 2018-04-24 15:11:02 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Magnus Jedvert | 26b9e12 | 2018-05-02 14:41:22 +0200 | [diff] [blame] | 86 | private final NativeCapturerObserver capturerObserver; |
Magnus Jedvert | 1a759c6 | 2018-04-24 15:11:02 +0200 | [diff] [blame] | 87 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 88 | public VideoSource(long nativeSource) { |
| 89 | super(nativeSource); |
Magnus Jedvert | 1a759c6 | 2018-04-24 15:11:02 +0200 | [diff] [blame] | 90 | this.capturerObserver = new NativeCapturerObserver(nativeSource); |
| 91 | } |
| 92 | |
| 93 | // TODO(bugs.webrtc.org/9181): Remove. |
| 94 | VideoSource(long nativeSource, SurfaceTextureHelper surfaceTextureHelper) { |
| 95 | super(nativeSource); |
| 96 | this.capturerObserver = new NativeCapturerObserver(nativeSource, surfaceTextureHelper); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 97 | } |
Magnus Jedvert | 7640fcf | 2016-09-21 16:20:03 +0200 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * Calling this function will cause frames to be scaled down to the requested resolution. Also, |
| 101 | * frames will be cropped to match the requested aspect ratio, and frames will be dropped to match |
| 102 | * the requested fps. The requested aspect ratio is orientation agnostic and will be adjusted to |
| 103 | * maintain the input orientation, so it doesn't matter if e.g. 1280x720 or 720x1280 is requested. |
| 104 | */ |
| 105 | public void adaptOutputFormat(int width, int height, int fps) { |
| 106 | nativeAdaptOutputFormat(nativeSource, width, height, fps); |
| 107 | } |
| 108 | |
Magnus Jedvert | 1a759c6 | 2018-04-24 15:11:02 +0200 | [diff] [blame] | 109 | public VideoCapturer.CapturerObserver getCapturerObserver() { |
| 110 | return capturerObserver; |
| 111 | } |
| 112 | |
Magnus Jedvert | 26b9e12 | 2018-05-02 14:41:22 +0200 | [diff] [blame] | 113 | @Override |
| 114 | public void dispose() { |
| 115 | capturerObserver.dispose(); |
| 116 | super.dispose(); |
| 117 | } |
| 118 | |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 119 | private static native void nativeAdaptOutputFormat(long source, int width, int height, int fps); |
Magnus Jedvert | 1a759c6 | 2018-04-24 15:11:02 +0200 | [diff] [blame] | 120 | private static native void nativeCapturerStarted(long source, boolean success); |
| 121 | private static native void nativeCapturerStopped(long source); |
| 122 | private static native void nativeOnFrameCaptured( |
| 123 | long source, int width, int height, int rotation, long timestampNs, VideoFrame.Buffer frame); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 124 | } |