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 | |
Artem Titarenko | 69540f4 | 2018-12-10 12:30:46 +0100 | [diff] [blame] | 13 | import android.support.annotation.Nullable; |
Magnus Jedvert | 1a759c6 | 2018-04-24 15:11:02 +0200 | [diff] [blame] | 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 | */ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 18 | public class VideoSource extends MediaSource { |
Magnus Jedvert | 99b275d | 2019-02-05 16:39:41 +0100 | [diff] [blame] | 19 | /** 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 Jedvert | e9652ca | 2019-02-18 16:12:00 +0100 | [diff] [blame] | 33 | private final Object videoProcessorLock = new Object(); |
| 34 | @Nullable private VideoProcessor videoProcessor; |
| 35 | private boolean isCapturerRunning; |
| 36 | |
Magnus Jedvert | 99b275d | 2019-02-05 16:39:41 +0100 | [diff] [blame] | 37 | private final CapturerObserver capturerObserver = new CapturerObserver() { |
| 38 | @Override |
| 39 | public void onCapturerStarted(boolean success) { |
| 40 | nativeAndroidVideoTrackSource.setState(success); |
Magnus Jedvert | e9652ca | 2019-02-18 16:12:00 +0100 | [diff] [blame] | 41 | synchronized (videoProcessorLock) { |
| 42 | isCapturerRunning = success; |
| 43 | if (videoProcessor != null) { |
| 44 | videoProcessor.onCapturerStarted(success); |
| 45 | } |
| 46 | } |
Magnus Jedvert | 99b275d | 2019-02-05 16:39:41 +0100 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public void onCapturerStopped() { |
| 51 | nativeAndroidVideoTrackSource.setState(/* isLive= */ false); |
Magnus Jedvert | e9652ca | 2019-02-18 16:12:00 +0100 | [diff] [blame] | 52 | synchronized (videoProcessorLock) { |
| 53 | isCapturerRunning = false; |
| 54 | if (videoProcessor != null) { |
| 55 | videoProcessor.onCapturerStopped(); |
| 56 | } |
| 57 | } |
Magnus Jedvert | 99b275d | 2019-02-05 16:39:41 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void onFrameCaptured(VideoFrame frame) { |
Sami Kalliomäki | c21cf04 | 2019-04-10 13:44:58 +0200 | [diff] [blame] | 62 | final VideoProcessor.FrameAdaptationParameters parameters = |
Magnus Jedvert | 9025bd5 | 2019-02-06 14:48:57 +0100 | [diff] [blame] | 63 | nativeAndroidVideoTrackSource.adaptFrame(frame); |
Magnus Jedvert | e9652ca | 2019-02-18 16:12:00 +0100 | [diff] [blame] | 64 | synchronized (videoProcessorLock) { |
| 65 | if (videoProcessor != null) { |
Sami Kalliomäki | c21cf04 | 2019-04-10 13:44:58 +0200 | [diff] [blame] | 66 | videoProcessor.onFrameCaptured(frame, parameters); |
Magnus Jedvert | e9652ca | 2019-02-18 16:12:00 +0100 | [diff] [blame] | 67 | return; |
| 68 | } |
| 69 | } |
Sami Kalliomäki | c21cf04 | 2019-04-10 13:44:58 +0200 | [diff] [blame] | 70 | |
| 71 | VideoFrame adaptedFrame = VideoProcessor.applyFrameAdaptationParameters(frame, parameters); |
| 72 | if (adaptedFrame != null) { |
| 73 | nativeAndroidVideoTrackSource.onFrameCaptured(adaptedFrame); |
| 74 | adaptedFrame.release(); |
| 75 | } |
Magnus Jedvert | 99b275d | 2019-02-05 16:39:41 +0100 | [diff] [blame] | 76 | } |
| 77 | }; |
Magnus Jedvert | 1a759c6 | 2018-04-24 15:11:02 +0200 | [diff] [blame] | 78 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 79 | public VideoSource(long nativeSource) { |
| 80 | super(nativeSource); |
Magnus Jedvert | 99b275d | 2019-02-05 16:39:41 +0100 | [diff] [blame] | 81 | this.nativeAndroidVideoTrackSource = new NativeAndroidVideoTrackSource(nativeSource); |
Magnus Jedvert | 1a759c6 | 2018-04-24 15:11:02 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Magnus Jedvert | 7640fcf | 2016-09-21 16:20:03 +0200 | [diff] [blame] | 84 | /** |
| 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 Jedvert | 06aa209 | 2018-10-26 14:00:18 +0200 | [diff] [blame] | 91 | 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 Jedvert | 99b275d | 2019-02-05 16:39:41 +0100 | [diff] [blame] | 103 | 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 Jedvert | 7640fcf | 2016-09-21 16:20:03 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Jakob Ivarsson | c5ec54e | 2019-11-12 17:30:45 +0100 | [diff] [blame^] | 117 | public void setIsScreencast(boolean isScreencast) { |
| 118 | nativeAndroidVideoTrackSource.setIsScreencast(isScreencast); |
| 119 | } |
| 120 | |
Magnus Jedvert | e9652ca | 2019-02-18 16:12:00 +0100 | [diff] [blame] | 121 | /** |
| 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äki | 05b552f | 2018-07-05 17:06:51 +0200 | [diff] [blame] | 146 | public CapturerObserver getCapturerObserver() { |
Magnus Jedvert | 1a759c6 | 2018-04-24 15:11:02 +0200 | [diff] [blame] | 147 | return capturerObserver; |
| 148 | } |
| 149 | |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 150 | /** Returns a pointer to webrtc::VideoTrackSourceInterface. */ |
| 151 | long getNativeVideoTrackSource() { |
| 152 | return getNativeMediaSource(); |
| 153 | } |
Magnus Jedvert | e9652ca | 2019-02-18 16:12:00 +0100 | [diff] [blame] | 154 | |
| 155 | @Override |
| 156 | public void dispose() { |
| 157 | setVideoProcessor(/* newVideoProcessor= */ null); |
| 158 | super.dispose(); |
| 159 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 160 | } |