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