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; |
| 33 | private final CapturerObserver capturerObserver = new CapturerObserver() { |
| 34 | @Override |
| 35 | public void onCapturerStarted(boolean success) { |
| 36 | nativeAndroidVideoTrackSource.setState(success); |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public void onCapturerStopped() { |
| 41 | nativeAndroidVideoTrackSource.setState(/* isLive= */ false); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public void onFrameCaptured(VideoFrame frame) { |
Magnus Jedvert | 9025bd5 | 2019-02-06 14:48:57 +0100 | [diff] [blame^] | 46 | final NativeAndroidVideoTrackSource.FrameAdaptationParameters parameters = |
| 47 | nativeAndroidVideoTrackSource.adaptFrame(frame); |
| 48 | if (parameters == null) { |
| 49 | // Drop frame. |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | final VideoFrame.Buffer adaptedBuffer = |
| 54 | frame.getBuffer().cropAndScale(parameters.cropX, parameters.cropY, parameters.cropWidth, |
| 55 | parameters.cropHeight, parameters.scaleWidth, parameters.scaleHeight); |
| 56 | // TODO(magjed): Add video processing hook here. |
| 57 | nativeAndroidVideoTrackSource.onFrameCaptured( |
| 58 | new VideoFrame(adaptedBuffer, frame.getRotation(), parameters.timestampNs)); |
| 59 | adaptedBuffer.release(); |
Magnus Jedvert | 99b275d | 2019-02-05 16:39:41 +0100 | [diff] [blame] | 60 | } |
| 61 | }; |
Magnus Jedvert | 1a759c6 | 2018-04-24 15:11:02 +0200 | [diff] [blame] | 62 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 63 | public VideoSource(long nativeSource) { |
| 64 | super(nativeSource); |
Magnus Jedvert | 99b275d | 2019-02-05 16:39:41 +0100 | [diff] [blame] | 65 | this.nativeAndroidVideoTrackSource = new NativeAndroidVideoTrackSource(nativeSource); |
Magnus Jedvert | 1a759c6 | 2018-04-24 15:11:02 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Magnus Jedvert | 7640fcf | 2016-09-21 16:20:03 +0200 | [diff] [blame] | 68 | /** |
| 69 | * Calling this function will cause frames to be scaled down to the requested resolution. Also, |
| 70 | * frames will be cropped to match the requested aspect ratio, and frames will be dropped to match |
| 71 | * the requested fps. The requested aspect ratio is orientation agnostic and will be adjusted to |
| 72 | * maintain the input orientation, so it doesn't matter if e.g. 1280x720 or 720x1280 is requested. |
| 73 | */ |
| 74 | public void adaptOutputFormat(int width, int height, int fps) { |
Magnus Jedvert | 06aa209 | 2018-10-26 14:00:18 +0200 | [diff] [blame] | 75 | final int maxSide = Math.max(width, height); |
| 76 | final int minSide = Math.min(width, height); |
| 77 | adaptOutputFormat(maxSide, minSide, minSide, maxSide, fps); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Same as above, but allows setting two different target resolutions depending on incoming |
| 82 | * frame orientation. This gives more fine-grained control and can e.g. be used to force landscape |
| 83 | * video to be cropped to portrait video. |
| 84 | */ |
| 85 | public void adaptOutputFormat( |
| 86 | int landscapeWidth, int landscapeHeight, int portraitWidth, int portraitHeight, int fps) { |
Magnus Jedvert | 99b275d | 2019-02-05 16:39:41 +0100 | [diff] [blame] | 87 | adaptOutputFormat(new AspectRatio(landscapeWidth, landscapeHeight), |
| 88 | /* maxLandscapePixelCount= */ landscapeWidth * landscapeHeight, |
| 89 | new AspectRatio(portraitWidth, portraitHeight), |
| 90 | /* maxPortraitPixelCount= */ portraitWidth * portraitHeight, fps); |
| 91 | } |
| 92 | |
| 93 | /** Same as above, with even more control as each constraint is optional. */ |
| 94 | public void adaptOutputFormat(AspectRatio targetLandscapeAspectRatio, |
| 95 | @Nullable Integer maxLandscapePixelCount, AspectRatio targetPortraitAspectRatio, |
| 96 | @Nullable Integer maxPortraitPixelCount, @Nullable Integer maxFps) { |
| 97 | nativeAndroidVideoTrackSource.adaptOutputFormat(targetLandscapeAspectRatio, |
| 98 | maxLandscapePixelCount, targetPortraitAspectRatio, maxPortraitPixelCount, maxFps); |
Magnus Jedvert | 7640fcf | 2016-09-21 16:20:03 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Sami Kalliomäki | 05b552f | 2018-07-05 17:06:51 +0200 | [diff] [blame] | 101 | public CapturerObserver getCapturerObserver() { |
Magnus Jedvert | 1a759c6 | 2018-04-24 15:11:02 +0200 | [diff] [blame] | 102 | return capturerObserver; |
| 103 | } |
| 104 | |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 105 | /** Returns a pointer to webrtc::VideoTrackSourceInterface. */ |
| 106 | long getNativeVideoTrackSource() { |
| 107 | return getNativeMediaSource(); |
| 108 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | } |