blob: 847a7bd9860e304485145f2cb79150166f5ffbfd [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
Artem Titarenko69540f42018-12-10 12:30:46 +010013import android.support.annotation.Nullable;
Magnus Jedvert1a759c62018-04-24 15:11:02 +020014
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 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018public class VideoSource extends MediaSource {
Magnus Jedvert99b275d2019-02-05 16:39:41 +010019 /** Simple aspect ratio clas for use in constraining output format. */
20 public static class AspectRatio {
21 public static final AspectRatio UNDEFINED = new AspectRatio(/* width= */ 0, /* height= */ 0);
22
23 public final int width;
24 public final int height;
25
26 public AspectRatio(int width, int height) {
27 this.width = width;
28 this.height = height;
29 }
30 }
31
32 private final NativeAndroidVideoTrackSource nativeAndroidVideoTrackSource;
Magnus Jedverte9652ca2019-02-18 16:12:00 +010033 private final Object videoProcessorLock = new Object();
34 @Nullable private VideoProcessor videoProcessor;
35 private boolean isCapturerRunning;
36
Magnus Jedvert99b275d2019-02-05 16:39:41 +010037 private final CapturerObserver capturerObserver = new CapturerObserver() {
38 @Override
39 public void onCapturerStarted(boolean success) {
40 nativeAndroidVideoTrackSource.setState(success);
Magnus Jedverte9652ca2019-02-18 16:12:00 +010041 synchronized (videoProcessorLock) {
42 isCapturerRunning = success;
43 if (videoProcessor != null) {
44 videoProcessor.onCapturerStarted(success);
45 }
46 }
Magnus Jedvert99b275d2019-02-05 16:39:41 +010047 }
48
49 @Override
50 public void onCapturerStopped() {
51 nativeAndroidVideoTrackSource.setState(/* isLive= */ false);
Magnus Jedverte9652ca2019-02-18 16:12:00 +010052 synchronized (videoProcessorLock) {
53 isCapturerRunning = false;
54 if (videoProcessor != null) {
55 videoProcessor.onCapturerStopped();
56 }
57 }
Magnus Jedvert99b275d2019-02-05 16:39:41 +010058 }
59
60 @Override
61 public void onFrameCaptured(VideoFrame frame) {
Magnus Jedvert9025bd52019-02-06 14:48:57 +010062 final NativeAndroidVideoTrackSource.FrameAdaptationParameters parameters =
63 nativeAndroidVideoTrackSource.adaptFrame(frame);
64 if (parameters == null) {
65 // Drop frame.
66 return;
67 }
68
69 final VideoFrame.Buffer adaptedBuffer =
70 frame.getBuffer().cropAndScale(parameters.cropX, parameters.cropY, parameters.cropWidth,
71 parameters.cropHeight, parameters.scaleWidth, parameters.scaleHeight);
Magnus Jedverte9652ca2019-02-18 16:12:00 +010072 final VideoFrame adaptedFrame =
73 new VideoFrame(adaptedBuffer, frame.getRotation(), parameters.timestampNs);
74
75 synchronized (videoProcessorLock) {
76 if (videoProcessor != null) {
77 videoProcessor.onFrameCaptured(adaptedFrame);
78 adaptedBuffer.release();
79 return;
80 }
81 }
82 nativeAndroidVideoTrackSource.onFrameCaptured(adaptedFrame);
Magnus Jedvert9025bd52019-02-06 14:48:57 +010083 adaptedBuffer.release();
Magnus Jedvert99b275d2019-02-05 16:39:41 +010084 }
85 };
Magnus Jedvert1a759c62018-04-24 15:11:02 +020086
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 public VideoSource(long nativeSource) {
88 super(nativeSource);
Magnus Jedvert99b275d2019-02-05 16:39:41 +010089 this.nativeAndroidVideoTrackSource = new NativeAndroidVideoTrackSource(nativeSource);
Magnus Jedvert1a759c62018-04-24 15:11:02 +020090 }
91
Magnus Jedvert7640fcf2016-09-21 16:20:03 +020092 /**
93 * Calling this function will cause frames to be scaled down to the requested resolution. Also,
94 * frames will be cropped to match the requested aspect ratio, and frames will be dropped to match
95 * the requested fps. The requested aspect ratio is orientation agnostic and will be adjusted to
96 * maintain the input orientation, so it doesn't matter if e.g. 1280x720 or 720x1280 is requested.
97 */
98 public void adaptOutputFormat(int width, int height, int fps) {
Magnus Jedvert06aa2092018-10-26 14:00:18 +020099 final int maxSide = Math.max(width, height);
100 final int minSide = Math.min(width, height);
101 adaptOutputFormat(maxSide, minSide, minSide, maxSide, fps);
102 }
103
104 /**
105 * Same as above, but allows setting two different target resolutions depending on incoming
106 * frame orientation. This gives more fine-grained control and can e.g. be used to force landscape
107 * video to be cropped to portrait video.
108 */
109 public void adaptOutputFormat(
110 int landscapeWidth, int landscapeHeight, int portraitWidth, int portraitHeight, int fps) {
Magnus Jedvert99b275d2019-02-05 16:39:41 +0100111 adaptOutputFormat(new AspectRatio(landscapeWidth, landscapeHeight),
112 /* maxLandscapePixelCount= */ landscapeWidth * landscapeHeight,
113 new AspectRatio(portraitWidth, portraitHeight),
114 /* maxPortraitPixelCount= */ portraitWidth * portraitHeight, fps);
115 }
116
117 /** Same as above, with even more control as each constraint is optional. */
118 public void adaptOutputFormat(AspectRatio targetLandscapeAspectRatio,
119 @Nullable Integer maxLandscapePixelCount, AspectRatio targetPortraitAspectRatio,
120 @Nullable Integer maxPortraitPixelCount, @Nullable Integer maxFps) {
121 nativeAndroidVideoTrackSource.adaptOutputFormat(targetLandscapeAspectRatio,
122 maxLandscapePixelCount, targetPortraitAspectRatio, maxPortraitPixelCount, maxFps);
Magnus Jedvert7640fcf2016-09-21 16:20:03 +0200123 }
124
Magnus Jedverte9652ca2019-02-18 16:12:00 +0100125 /**
126 * Hook for injecting a custom video processor before frames are passed onto WebRTC. The frames
127 * will be cropped and scaled depending on CPU and network conditions before they are passed to
128 * the video processor. Frames will be delivered to the video processor on the same thread they
129 * are passed to this object. The video processor is allowed to deliver the processed frames
130 * back on any thread.
131 */
132 public void setVideoProcessor(@Nullable VideoProcessor newVideoProcessor) {
133 synchronized (videoProcessorLock) {
134 if (videoProcessor != null) {
135 videoProcessor.setSink(/* sink= */ null);
136 if (isCapturerRunning) {
137 videoProcessor.onCapturerStopped();
138 }
139 }
140 videoProcessor = newVideoProcessor;
141 if (newVideoProcessor != null) {
142 newVideoProcessor.setSink(nativeAndroidVideoTrackSource::onFrameCaptured);
143 if (isCapturerRunning) {
144 newVideoProcessor.onCapturerStarted(/* success= */ true);
145 }
146 }
147 }
148 }
149
Sami Kalliomäki05b552f2018-07-05 17:06:51 +0200150 public CapturerObserver getCapturerObserver() {
Magnus Jedvert1a759c62018-04-24 15:11:02 +0200151 return capturerObserver;
152 }
153
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200154 /** Returns a pointer to webrtc::VideoTrackSourceInterface. */
155 long getNativeVideoTrackSource() {
156 return getNativeMediaSource();
157 }
Magnus Jedverte9652ca2019-02-18 16:12:00 +0100158
159 @Override
160 public void dispose() {
161 setVideoProcessor(/* newVideoProcessor= */ null);
162 super.dispose();
163 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164}