Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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. |
| 9 | */ |
| 10 | |
| 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 | 1f2a3e7 | 2017-11-23 16:56:44 +0100 | [diff] [blame] | 14 | import org.webrtc.EncodedImage; |
| 15 | |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 16 | /** |
| 17 | * Interface for a video encoder that can be used with WebRTC. All calls will be made on the |
Sami Kalliomäki | 5f5fc68 | 2017-10-19 11:34:08 +0200 | [diff] [blame] | 18 | * encoding thread. The encoder may be constructed on a different thread and changing thread after |
| 19 | * calling release is allowed. |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 20 | */ |
| 21 | public interface VideoEncoder { |
| 22 | /** Settings passed to the encoder by WebRTC. */ |
| 23 | public class Settings { |
| 24 | public final int numberOfCores; |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 25 | public final int width; |
| 26 | public final int height; |
| 27 | public final int startBitrate; // Kilobits per second. |
| 28 | public final int maxFramerate; |
Rasmus Brandt | 85f20cb | 2018-08-23 13:52:45 +0200 | [diff] [blame] | 29 | public final int numberOfSimulcastStreams; |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 30 | public final boolean automaticResizeOn; |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 31 | public final Capabilities capabilities; |
| 32 | |
| 33 | // TODO(bugs.webrtc.org/10720): Remove. |
| 34 | @Deprecated |
| 35 | public Settings(int numberOfCores, int width, int height, int startBitrate, int maxFramerate, |
| 36 | int numberOfSimulcastStreams, boolean automaticResizeOn) { |
| 37 | this(numberOfCores, width, height, startBitrate, maxFramerate, numberOfSimulcastStreams, |
| 38 | automaticResizeOn, new VideoEncoder.Capabilities(false /* lossNotification */)); |
| 39 | } |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 40 | |
Magnus Jedvert | 1f2a3e7 | 2017-11-23 16:56:44 +0100 | [diff] [blame] | 41 | @CalledByNative("Settings") |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 42 | public Settings(int numberOfCores, int width, int height, int startBitrate, int maxFramerate, |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 43 | int numberOfSimulcastStreams, boolean automaticResizeOn, Capabilities capabilities) { |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 44 | this.numberOfCores = numberOfCores; |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 45 | this.width = width; |
| 46 | this.height = height; |
| 47 | this.startBitrate = startBitrate; |
| 48 | this.maxFramerate = maxFramerate; |
Rasmus Brandt | 85f20cb | 2018-08-23 13:52:45 +0200 | [diff] [blame] | 49 | this.numberOfSimulcastStreams = numberOfSimulcastStreams; |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 50 | this.automaticResizeOn = automaticResizeOn; |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 51 | this.capabilities = capabilities; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** Capabilities (loss notification, etc.) passed to the encoder by WebRTC. */ |
| 56 | public class Capabilities { |
| 57 | /** |
| 58 | * The remote side has support for the loss notification RTCP feedback message format, and will |
| 59 | * be sending these feedback messages if necessary. |
| 60 | */ |
| 61 | public final boolean lossNotification; |
| 62 | |
| 63 | @CalledByNative("Capabilities") |
| 64 | public Capabilities(boolean lossNotification) { |
| 65 | this.lossNotification = lossNotification; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | /** Additional info for encoding. */ |
| 70 | public class EncodeInfo { |
| 71 | public final EncodedImage.FrameType[] frameTypes; |
| 72 | |
Magnus Jedvert | 1f2a3e7 | 2017-11-23 16:56:44 +0100 | [diff] [blame] | 73 | @CalledByNative("EncodeInfo") |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 74 | public EncodeInfo(EncodedImage.FrameType[] frameTypes) { |
| 75 | this.frameTypes = frameTypes; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // TODO(sakal): Add values to these classes as necessary. |
| 80 | /** Codec specific information about the encoded frame. */ |
| 81 | public class CodecSpecificInfo {} |
| 82 | |
| 83 | public class CodecSpecificInfoVP8 extends CodecSpecificInfo {} |
| 84 | |
| 85 | public class CodecSpecificInfoVP9 extends CodecSpecificInfo {} |
| 86 | |
| 87 | public class CodecSpecificInfoH264 extends CodecSpecificInfo {} |
| 88 | |
| 89 | /** |
| 90 | * Represents bitrate allocated for an encoder to produce frames. Bitrate can be divided between |
| 91 | * spatial and temporal layers. |
| 92 | */ |
| 93 | public class BitrateAllocation { |
| 94 | // First index is the spatial layer and second the temporal layer. |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 95 | public final int[][] bitratesBbs; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * Initializes the allocation with a two dimensional array of bitrates. The first index of the |
| 99 | * array is the spatial layer and the second index in the temporal layer. |
| 100 | */ |
Magnus Jedvert | 1f2a3e7 | 2017-11-23 16:56:44 +0100 | [diff] [blame] | 101 | @CalledByNative("BitrateAllocation") |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 102 | public BitrateAllocation(int[][] bitratesBbs) { |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 103 | this.bitratesBbs = bitratesBbs; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Gets the total bitrate allocated for all layers. |
| 108 | */ |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 109 | public int getSum() { |
| 110 | int sum = 0; |
| 111 | for (int[] spatialLayer : bitratesBbs) { |
| 112 | for (int bitrate : spatialLayer) { |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 113 | sum += bitrate; |
| 114 | } |
| 115 | } |
| 116 | return sum; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** Settings for WebRTC quality based scaling. */ |
| 121 | public class ScalingSettings { |
| 122 | public final boolean on; |
Sami Kalliomäki | e7592d8 | 2018-03-22 13:32:44 +0100 | [diff] [blame] | 123 | @Nullable public final Integer low; |
| 124 | @Nullable public final Integer high; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 125 | |
| 126 | /** |
Niels Möller | d0dd90b | 2018-02-08 10:15:34 +0100 | [diff] [blame] | 127 | * Settings to disable quality based scaling. |
| 128 | */ |
| 129 | public static final ScalingSettings OFF = new ScalingSettings(); |
| 130 | |
| 131 | /** |
| 132 | * Creates settings to enable quality based scaling. |
| 133 | * |
| 134 | * @param low Average QP at which to scale up the resolution. |
| 135 | * @param high Average QP at which to scale down the resolution. |
| 136 | */ |
| 137 | public ScalingSettings(int low, int high) { |
| 138 | this.on = true; |
| 139 | this.low = low; |
| 140 | this.high = high; |
| 141 | } |
| 142 | |
| 143 | private ScalingSettings() { |
| 144 | this.on = false; |
| 145 | this.low = null; |
| 146 | this.high = null; |
| 147 | } |
| 148 | |
| 149 | // TODO(bugs.webrtc.org/8830): Below constructors are deprecated. |
| 150 | // Default thresholds are going away, so thresholds have to be set |
| 151 | // when scaling is on. |
| 152 | /** |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 153 | * Creates quality based scaling setting. |
| 154 | * |
| 155 | * @param on True if quality scaling is turned on. |
| 156 | */ |
Niels Möller | d0dd90b | 2018-02-08 10:15:34 +0100 | [diff] [blame] | 157 | @Deprecated |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 158 | public ScalingSettings(boolean on) { |
| 159 | this.on = on; |
| 160 | this.low = null; |
| 161 | this.high = null; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Creates quality based scaling settings with custom thresholds. |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 166 | * |
| 167 | * @param on True if quality scaling is turned on. |
| 168 | * @param low Average QP at which to scale up the resolution. |
| 169 | * @param high Average QP at which to scale down the resolution. |
| 170 | */ |
Niels Möller | d0dd90b | 2018-02-08 10:15:34 +0100 | [diff] [blame] | 171 | @Deprecated |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 172 | public ScalingSettings(boolean on, int low, int high) { |
| 173 | this.on = on; |
| 174 | this.low = low; |
| 175 | this.high = high; |
| 176 | } |
Sami Kalliomäki | 8619e8a | 2018-04-17 14:44:36 +0200 | [diff] [blame] | 177 | |
| 178 | @Override |
| 179 | public String toString() { |
| 180 | return on ? "[ " + low + ", " + high + " ]" : "OFF"; |
| 181 | } |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | public interface Callback { |
Sami Kalliomäki | 11c51dd | 2018-02-07 12:50:47 +0100 | [diff] [blame] | 185 | /** |
| 186 | * Call to return an encoded frame. It is safe to assume the byte buffer held by |frame| is not |
| 187 | * accessed after the call to this method returns. |
| 188 | */ |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 189 | void onEncodedFrame(EncodedImage frame, CodecSpecificInfo info); |
| 190 | } |
| 191 | |
| 192 | /** |
Rasmus Brandt | 42a2fc9 | 2018-07-09 13:38:01 +0200 | [diff] [blame] | 193 | * The encoder implementation backing this interface is either 1) a Java |
| 194 | * encoder (e.g., an Android platform encoder), or alternatively 2) a native |
| 195 | * encoder (e.g., a software encoder or a C++ encoder adapter). |
| 196 | * |
| 197 | * For case 1), createNativeVideoEncoder() should return zero. |
| 198 | * In this case, we expect the native library to call the encoder through |
| 199 | * JNI using the Java interface declared below. |
| 200 | * |
| 201 | * For case 2), createNativeVideoEncoder() should return a non-zero value. |
| 202 | * In this case, we expect the native library to treat the returned value as |
| 203 | * a raw pointer of type webrtc::VideoEncoder* (ownership is transferred to |
| 204 | * the caller). The native library should then directly call the |
| 205 | * webrtc::VideoEncoder interface without going through JNI. All calls to |
| 206 | * the Java interface methods declared below should thus throw an |
| 207 | * UnsupportedOperationException. |
| 208 | */ |
| 209 | @CalledByNative |
| 210 | default long createNativeVideoEncoder() { |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Returns true if the encoder is backed by hardware. |
| 216 | */ |
| 217 | @CalledByNative |
| 218 | default boolean isHardwareEncoder() { |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | /** |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 223 | * Initializes the encoding process. Call before any calls to encode. |
| 224 | */ |
Magnus Jedvert | 56231d0 | 2017-10-31 17:47:06 +0100 | [diff] [blame] | 225 | @CalledByNative VideoCodecStatus initEncode(Settings settings, Callback encodeCallback); |
| 226 | |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 227 | /** |
| 228 | * Releases the encoder. No more calls to encode will be made after this call. |
| 229 | */ |
Magnus Jedvert | 56231d0 | 2017-10-31 17:47:06 +0100 | [diff] [blame] | 230 | @CalledByNative VideoCodecStatus release(); |
| 231 | |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 232 | /** |
| 233 | * Requests the encoder to encode a frame. |
| 234 | */ |
Magnus Jedvert | 56231d0 | 2017-10-31 17:47:06 +0100 | [diff] [blame] | 235 | @CalledByNative VideoCodecStatus encode(VideoFrame frame, EncodeInfo info); |
| 236 | |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 237 | /** Sets the bitrate allocation and the target framerate for the encoder. */ |
Magnus Jedvert | 56231d0 | 2017-10-31 17:47:06 +0100 | [diff] [blame] | 238 | @CalledByNative VideoCodecStatus setRateAllocation(BitrateAllocation allocation, int framerate); |
| 239 | |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 240 | /** Any encoder that wants to use WebRTC provided quality scaler must implement this method. */ |
Magnus Jedvert | 56231d0 | 2017-10-31 17:47:06 +0100 | [diff] [blame] | 241 | @CalledByNative ScalingSettings getScalingSettings(); |
| 242 | |
Sami Kalliomäki | 5f5fc68 | 2017-10-19 11:34:08 +0200 | [diff] [blame] | 243 | /** |
| 244 | * Should return a descriptive name for the implementation. Gets called once and cached. May be |
| 245 | * called from arbitrary thread. |
| 246 | */ |
Magnus Jedvert | 56231d0 | 2017-10-31 17:47:06 +0100 | [diff] [blame] | 247 | @CalledByNative String getImplementationName(); |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 248 | } |