blob: 469aac7576a2d399a54e06d9c8aa25a87b9f0a01 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.org28e20752013-07-10 00:45:36 +00009 */
10
henrike@webrtc.org28e20752013-07-10 00:45:36 +000011package org.webrtc;
12
Magnus Jedvert1a759c62018-04-24 15:11:02 +020013import javax.annotation.Nullable;
14
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000015/**
Magnus Jedvert7640fcf2016-09-21 16:20:03 +020016 * Java wrapper of native AndroidVideoTrackSource.
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000017 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010018@JNINamespace("webrtc::jni")
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019public class VideoSource extends MediaSource {
Magnus Jedvert1a759c62018-04-24 15:11:02 +020020 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 Jedvert26b9e122018-05-02 14:41:22 +020078
79 public void dispose() {
80 if (surfaceTextureHelper != null) {
81 surfaceTextureHelper.dispose();
82 }
83 }
Magnus Jedvert1a759c62018-04-24 15:11:02 +020084 }
85
Magnus Jedvert26b9e122018-05-02 14:41:22 +020086 private final NativeCapturerObserver capturerObserver;
Magnus Jedvert1a759c62018-04-24 15:11:02 +020087
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 public VideoSource(long nativeSource) {
89 super(nativeSource);
Magnus Jedvert1a759c62018-04-24 15:11:02 +020090 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.org28e20752013-07-10 00:45:36 +000097 }
Magnus Jedvert7640fcf2016-09-21 16:20:03 +020098
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 Jedvert1a759c62018-04-24 15:11:02 +0200109 public VideoCapturer.CapturerObserver getCapturerObserver() {
110 return capturerObserver;
111 }
112
Magnus Jedvert26b9e122018-05-02 14:41:22 +0200113 @Override
114 public void dispose() {
115 capturerObserver.dispose();
116 super.dispose();
117 }
118
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100119 private static native void nativeAdaptOutputFormat(long source, int width, int height, int fps);
Magnus Jedvert1a759c62018-04-24 15:11:02 +0200120 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.org28e20752013-07-10 00:45:36 +0000124}