blob: 6c528fd05bbb1efeafcfea2a9d449b646535fc0d [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) {
Sami Kalliomäkic21cf042019-04-10 13:44:58 +020062 final VideoProcessor.FrameAdaptationParameters parameters =
Magnus Jedvert9025bd52019-02-06 14:48:57 +010063 nativeAndroidVideoTrackSource.adaptFrame(frame);
Magnus Jedverte9652ca2019-02-18 16:12:00 +010064 synchronized (videoProcessorLock) {
65 if (videoProcessor != null) {
Sami Kalliomäkic21cf042019-04-10 13:44:58 +020066 videoProcessor.onFrameCaptured(frame, parameters);
Magnus Jedverte9652ca2019-02-18 16:12:00 +010067 return;
68 }
69 }
Sami Kalliomäkic21cf042019-04-10 13:44:58 +020070
71 VideoFrame adaptedFrame = VideoProcessor.applyFrameAdaptationParameters(frame, parameters);
72 if (adaptedFrame != null) {
73 nativeAndroidVideoTrackSource.onFrameCaptured(adaptedFrame);
74 adaptedFrame.release();
75 }
Magnus Jedvert99b275d2019-02-05 16:39:41 +010076 }
77 };
Magnus Jedvert1a759c62018-04-24 15:11:02 +020078
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 public VideoSource(long nativeSource) {
80 super(nativeSource);
Magnus Jedvert99b275d2019-02-05 16:39:41 +010081 this.nativeAndroidVideoTrackSource = new NativeAndroidVideoTrackSource(nativeSource);
Magnus Jedvert1a759c62018-04-24 15:11:02 +020082 }
83
Magnus Jedvert7640fcf2016-09-21 16:20:03 +020084 /**
85 * Calling this function will cause frames to be scaled down to the requested resolution. Also,
86 * frames will be cropped to match the requested aspect ratio, and frames will be dropped to match
87 * the requested fps. The requested aspect ratio is orientation agnostic and will be adjusted to
88 * maintain the input orientation, so it doesn't matter if e.g. 1280x720 or 720x1280 is requested.
89 */
90 public void adaptOutputFormat(int width, int height, int fps) {
Magnus Jedvert06aa2092018-10-26 14:00:18 +020091 final int maxSide = Math.max(width, height);
92 final int minSide = Math.min(width, height);
93 adaptOutputFormat(maxSide, minSide, minSide, maxSide, fps);
94 }
95
96 /**
97 * Same as above, but allows setting two different target resolutions depending on incoming
98 * frame orientation. This gives more fine-grained control and can e.g. be used to force landscape
99 * video to be cropped to portrait video.
100 */
101 public void adaptOutputFormat(
102 int landscapeWidth, int landscapeHeight, int portraitWidth, int portraitHeight, int fps) {
Magnus Jedvert99b275d2019-02-05 16:39:41 +0100103 adaptOutputFormat(new AspectRatio(landscapeWidth, landscapeHeight),
104 /* maxLandscapePixelCount= */ landscapeWidth * landscapeHeight,
105 new AspectRatio(portraitWidth, portraitHeight),
106 /* maxPortraitPixelCount= */ portraitWidth * portraitHeight, fps);
107 }
108
109 /** Same as above, with even more control as each constraint is optional. */
110 public void adaptOutputFormat(AspectRatio targetLandscapeAspectRatio,
111 @Nullable Integer maxLandscapePixelCount, AspectRatio targetPortraitAspectRatio,
112 @Nullable Integer maxPortraitPixelCount, @Nullable Integer maxFps) {
113 nativeAndroidVideoTrackSource.adaptOutputFormat(targetLandscapeAspectRatio,
114 maxLandscapePixelCount, targetPortraitAspectRatio, maxPortraitPixelCount, maxFps);
Magnus Jedvert7640fcf2016-09-21 16:20:03 +0200115 }
116
Jakob Ivarssonc5ec54e2019-11-12 17:30:45 +0100117 public void setIsScreencast(boolean isScreencast) {
118 nativeAndroidVideoTrackSource.setIsScreencast(isScreencast);
119 }
120
Magnus Jedverte9652ca2019-02-18 16:12:00 +0100121 /**
122 * Hook for injecting a custom video processor before frames are passed onto WebRTC. The frames
123 * will be cropped and scaled depending on CPU and network conditions before they are passed to
124 * the video processor. Frames will be delivered to the video processor on the same thread they
125 * are passed to this object. The video processor is allowed to deliver the processed frames
126 * back on any thread.
127 */
128 public void setVideoProcessor(@Nullable VideoProcessor newVideoProcessor) {
129 synchronized (videoProcessorLock) {
130 if (videoProcessor != null) {
131 videoProcessor.setSink(/* sink= */ null);
132 if (isCapturerRunning) {
133 videoProcessor.onCapturerStopped();
134 }
135 }
136 videoProcessor = newVideoProcessor;
137 if (newVideoProcessor != null) {
138 newVideoProcessor.setSink(nativeAndroidVideoTrackSource::onFrameCaptured);
139 if (isCapturerRunning) {
140 newVideoProcessor.onCapturerStarted(/* success= */ true);
141 }
142 }
143 }
144 }
145
Sami Kalliomäki05b552f2018-07-05 17:06:51 +0200146 public CapturerObserver getCapturerObserver() {
Magnus Jedvert1a759c62018-04-24 15:11:02 +0200147 return capturerObserver;
148 }
149
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200150 /** Returns a pointer to webrtc::VideoTrackSourceInterface. */
151 long getNativeVideoTrackSource() {
152 return getNativeMediaSource();
153 }
Magnus Jedverte9652ca2019-02-18 16:12:00 +0100154
155 @Override
156 public void dispose() {
157 setVideoProcessor(/* newVideoProcessor= */ null);
158 super.dispose();
159 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160}