1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
/*
* Copyright 2017 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
package org.webrtc;
import androidx.annotation.Nullable;
import org.webrtc.EncodedImage;
/**
* Interface for a video encoder that can be used with WebRTC. All calls will be made on the
* encoding thread. The encoder may be constructed on a different thread and changing thread after
* calling release is allowed.
*/
public interface VideoEncoder {
/** Settings passed to the encoder by WebRTC. */
public class Settings {
public final int numberOfCores;
public final int width;
public final int height;
public final int startBitrate; // Kilobits per second.
public final int maxFramerate;
public final int numberOfSimulcastStreams;
public final boolean automaticResizeOn;
public final Capabilities capabilities;
// TODO(bugs.webrtc.org/10720): Remove.
@Deprecated
public Settings(int numberOfCores, int width, int height, int startBitrate, int maxFramerate,
int numberOfSimulcastStreams, boolean automaticResizeOn) {
this(numberOfCores, width, height, startBitrate, maxFramerate, numberOfSimulcastStreams,
automaticResizeOn, new VideoEncoder.Capabilities(false /* lossNotification */));
}
@CalledByNative("Settings")
public Settings(int numberOfCores, int width, int height, int startBitrate, int maxFramerate,
int numberOfSimulcastStreams, boolean automaticResizeOn, Capabilities capabilities) {
this.numberOfCores = numberOfCores;
this.width = width;
this.height = height;
this.startBitrate = startBitrate;
this.maxFramerate = maxFramerate;
this.numberOfSimulcastStreams = numberOfSimulcastStreams;
this.automaticResizeOn = automaticResizeOn;
this.capabilities = capabilities;
}
}
/** Capabilities (loss notification, etc.) passed to the encoder by WebRTC. */
public class Capabilities {
/**
* The remote side has support for the loss notification RTCP feedback message format, and will
* be sending these feedback messages if necessary.
*/
public final boolean lossNotification;
@CalledByNative("Capabilities")
public Capabilities(boolean lossNotification) {
this.lossNotification = lossNotification;
}
}
/** Additional info for encoding. */
public class EncodeInfo {
public final EncodedImage.FrameType[] frameTypes;
@CalledByNative("EncodeInfo")
public EncodeInfo(EncodedImage.FrameType[] frameTypes) {
this.frameTypes = frameTypes;
}
}
// TODO(sakal): Add values to these classes as necessary.
/** Codec specific information about the encoded frame. */
public class CodecSpecificInfo {}
public class CodecSpecificInfoVP8 extends CodecSpecificInfo {}
public class CodecSpecificInfoVP9 extends CodecSpecificInfo {}
public class CodecSpecificInfoH264 extends CodecSpecificInfo {}
public class CodecSpecificInfoAV1 extends CodecSpecificInfo {}
/**
* Represents bitrate allocated for an encoder to produce frames. Bitrate can be divided between
* spatial and temporal layers.
*/
public class BitrateAllocation {
// First index is the spatial layer and second the temporal layer.
public final int[][] bitratesBbs;
/**
* Initializes the allocation with a two dimensional array of bitrates. The first index of the
* array is the spatial layer and the second index in the temporal layer.
*/
@CalledByNative("BitrateAllocation")
public BitrateAllocation(int[][] bitratesBbs) {
this.bitratesBbs = bitratesBbs;
}
/**
* Gets the total bitrate allocated for all layers.
*/
public int getSum() {
int sum = 0;
for (int[] spatialLayer : bitratesBbs) {
for (int bitrate : spatialLayer) {
sum += bitrate;
}
}
return sum;
}
}
/** Settings for WebRTC quality based scaling. */
public class ScalingSettings {
public final boolean on;
@Nullable public final Integer low;
@Nullable public final Integer high;
/**
* Settings to disable quality based scaling.
*/
public static final ScalingSettings OFF = new ScalingSettings();
/**
* Creates settings to enable quality based scaling.
*
* @param low Average QP at which to scale up the resolution.
* @param high Average QP at which to scale down the resolution.
*/
public ScalingSettings(int low, int high) {
this.on = true;
this.low = low;
this.high = high;
}
private ScalingSettings() {
this.on = false;
this.low = null;
this.high = null;
}
// TODO(bugs.webrtc.org/8830): Below constructors are deprecated.
// Default thresholds are going away, so thresholds have to be set
// when scaling is on.
/**
* Creates quality based scaling setting.
*
* @param on True if quality scaling is turned on.
*/
@Deprecated
public ScalingSettings(boolean on) {
this.on = on;
this.low = null;
this.high = null;
}
/**
* Creates quality based scaling settings with custom thresholds.
*
* @param on True if quality scaling is turned on.
* @param low Average QP at which to scale up the resolution.
* @param high Average QP at which to scale down the resolution.
*/
@Deprecated
public ScalingSettings(boolean on, int low, int high) {
this.on = on;
this.low = low;
this.high = high;
}
@Override
public String toString() {
return on ? "[ " + low + ", " + high + " ]" : "OFF";
}
}
/**
* Bitrate limits for resolution.
*/
public class ResolutionBitrateLimits {
/**
* Maximum size of video frame, in pixels, the bitrate limits are intended for.
*/
public final int frameSizePixels;
/**
* Recommended minimum bitrate to start encoding.
*/
public final int minStartBitrateBps;
/**
* Recommended minimum bitrate.
*/
public final int minBitrateBps;
/**
* Recommended maximum bitrate.
*/
public final int maxBitrateBps;
public ResolutionBitrateLimits(
int frameSizePixels, int minStartBitrateBps, int minBitrateBps, int maxBitrateBps) {
this.frameSizePixels = frameSizePixels;
this.minStartBitrateBps = minStartBitrateBps;
this.minBitrateBps = minBitrateBps;
this.maxBitrateBps = maxBitrateBps;
}
@CalledByNative("ResolutionBitrateLimits")
public int getFrameSizePixels() {
return frameSizePixels;
}
@CalledByNative("ResolutionBitrateLimits")
public int getMinStartBitrateBps() {
return minStartBitrateBps;
}
@CalledByNative("ResolutionBitrateLimits")
public int getMinBitrateBps() {
return minBitrateBps;
}
@CalledByNative("ResolutionBitrateLimits")
public int getMaxBitrateBps() {
return maxBitrateBps;
}
}
/** Rate control parameters. */
public class RateControlParameters {
/**
* Adjusted target bitrate, per spatial/temporal layer. May be lower or higher than the target
* depending on encoder behaviour.
*/
public final BitrateAllocation bitrate;
/**
* Target framerate, in fps. A value <= 0.0 is invalid and should be interpreted as framerate
* target not available. In this case the encoder should fall back to the max framerate
* specified in `codec_settings` of the last InitEncode() call.
*/
public final double framerateFps;
@CalledByNative("RateControlParameters")
public RateControlParameters(BitrateAllocation bitrate, double framerateFps) {
this.bitrate = bitrate;
this.framerateFps = framerateFps;
}
}
/**
* Metadata about the Encoder.
*/
public class EncoderInfo {
/**
* The width and height of the incoming video frames should be divisible by
* |requested_resolution_alignment|
*/
public final int requestedResolutionAlignment;
/**
* Same as above but if true, each simulcast layer should also be divisible by
* |requested_resolution_alignment|.
*/
public final boolean applyAlignmentToAllSimulcastLayers;
public EncoderInfo(
int requestedResolutionAlignment, boolean applyAlignmentToAllSimulcastLayers) {
this.requestedResolutionAlignment = requestedResolutionAlignment;
this.applyAlignmentToAllSimulcastLayers = applyAlignmentToAllSimulcastLayers;
}
@CalledByNative("EncoderInfo")
public int getRequestedResolutionAlignment() {
return requestedResolutionAlignment;
}
@CalledByNative("EncoderInfo")
public boolean getApplyAlignmentToAllSimulcastLayers() {
return applyAlignmentToAllSimulcastLayers;
}
}
public interface Callback {
/**
* Old encoders assume that the byte buffer held by `frame` is not accessed after the call to
* this method returns. If the pipeline downstream needs to hold on to the buffer, it then has
* to make its own copy. We want to move to a model where no copying is needed, and instead use
* retain()/release() to signal to the encoder when it is safe to reuse the buffer.
*
* Over the transition, implementations of this class should use the maybeRetain() method if
* they want to keep a reference to the buffer, and fall back to copying if that method returns
* false.
*/
void onEncodedFrame(EncodedImage frame, CodecSpecificInfo info);
}
/**
* The encoder implementation backing this interface is either 1) a Java
* encoder (e.g., an Android platform encoder), or alternatively 2) a native
* encoder (e.g., a software encoder or a C++ encoder adapter).
*
* For case 1), createNative() should return zero.
* In this case, we expect the native library to call the encoder through
* JNI using the Java interface declared below.
*
* For case 2), createNative() should return a non-zero value.
* In this case, we expect the native library to treat the returned value as
* a raw pointer of type webrtc::VideoEncoder* (ownership is transferred to
* the caller). The native library should then directly call the
* webrtc::VideoEncoder interface without going through JNI. All calls to
* the Java interface methods declared below should thus throw an
* UnsupportedOperationException.
*/
@CalledByNative
default long createNative(long webrtcEnvRef) {
return 0;
}
/**
* Returns true if the encoder is backed by hardware.
*/
@CalledByNative
default boolean isHardwareEncoder() {
return true;
}
/**
* Initializes the encoding process. Call before any calls to encode.
*/
@CalledByNative VideoCodecStatus initEncode(Settings settings, Callback encodeCallback);
/**
* Releases the encoder. No more calls to encode will be made after this call.
*/
@CalledByNative VideoCodecStatus release();
/**
* Requests the encoder to encode a frame.
*/
@CalledByNative VideoCodecStatus encode(VideoFrame frame, EncodeInfo info);
/** Sets the bitrate allocation and the target framerate for the encoder. */
VideoCodecStatus setRateAllocation(BitrateAllocation allocation, int framerate);
/** Sets the bitrate allocation and the target framerate for the encoder. */
default @CalledByNative VideoCodecStatus setRates(RateControlParameters rcParameters) {
// Round frame rate up to avoid overshoots.
int framerateFps = (int) Math.ceil(rcParameters.framerateFps);
return setRateAllocation(rcParameters.bitrate, framerateFps);
}
/** Any encoder that wants to use WebRTC provided quality scaler must implement this method. */
@CalledByNative ScalingSettings getScalingSettings();
/** Returns the list of bitrate limits. */
@CalledByNative
default ResolutionBitrateLimits[] getResolutionBitrateLimits() {
// TODO(ssilkin): Update downstream projects and remove default implementation.
ResolutionBitrateLimits bitrate_limits[] = {};
return bitrate_limits;
}
/**
* Should return a descriptive name for the implementation. Gets called once and cached. May be
* called from arbitrary thread.
*/
@CalledByNative String getImplementationName();
@CalledByNative
default EncoderInfo getEncoderInfo() {
return new EncoderInfo(
/* requestedResolutionAlignment= */ 1, /* applyAlignmentToAllSimulcastLayers= */ false);
}
}
|