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 | |
Magnus Jedvert | 1f2a3e7 | 2017-11-23 16:56:44 +0100 | [diff] [blame] | 13 | import org.webrtc.EncodedImage; |
| 14 | |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 15 | /** |
| 16 | * 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] | 17 | * encoding thread. The encoder may be constructed on a different thread and changing thread after |
| 18 | * calling release is allowed. |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 19 | */ |
| 20 | public interface VideoEncoder { |
| 21 | /** Settings passed to the encoder by WebRTC. */ |
| 22 | public class Settings { |
| 23 | public final int numberOfCores; |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 24 | public final int width; |
| 25 | public final int height; |
| 26 | public final int startBitrate; // Kilobits per second. |
| 27 | public final int maxFramerate; |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 28 | public final boolean automaticResizeOn; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 29 | |
Magnus Jedvert | 1f2a3e7 | 2017-11-23 16:56:44 +0100 | [diff] [blame] | 30 | @CalledByNative("Settings") |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 31 | public Settings(int numberOfCores, int width, int height, int startBitrate, int maxFramerate, |
| 32 | boolean automaticResizeOn) { |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 33 | this.numberOfCores = numberOfCores; |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 34 | this.width = width; |
| 35 | this.height = height; |
| 36 | this.startBitrate = startBitrate; |
| 37 | this.maxFramerate = maxFramerate; |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 38 | this.automaticResizeOn = automaticResizeOn; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
| 42 | /** Additional info for encoding. */ |
| 43 | public class EncodeInfo { |
| 44 | public final EncodedImage.FrameType[] frameTypes; |
| 45 | |
Magnus Jedvert | 1f2a3e7 | 2017-11-23 16:56:44 +0100 | [diff] [blame] | 46 | @CalledByNative("EncodeInfo") |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 47 | public EncodeInfo(EncodedImage.FrameType[] frameTypes) { |
| 48 | this.frameTypes = frameTypes; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // TODO(sakal): Add values to these classes as necessary. |
| 53 | /** Codec specific information about the encoded frame. */ |
| 54 | public class CodecSpecificInfo {} |
| 55 | |
| 56 | public class CodecSpecificInfoVP8 extends CodecSpecificInfo {} |
| 57 | |
| 58 | public class CodecSpecificInfoVP9 extends CodecSpecificInfo {} |
| 59 | |
| 60 | public class CodecSpecificInfoH264 extends CodecSpecificInfo {} |
| 61 | |
| 62 | /** |
| 63 | * Represents bitrate allocated for an encoder to produce frames. Bitrate can be divided between |
| 64 | * spatial and temporal layers. |
| 65 | */ |
| 66 | public class BitrateAllocation { |
| 67 | // First index is the spatial layer and second the temporal layer. |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 68 | public final int[][] bitratesBbs; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Initializes the allocation with a two dimensional array of bitrates. The first index of the |
| 72 | * array is the spatial layer and the second index in the temporal layer. |
| 73 | */ |
Magnus Jedvert | 1f2a3e7 | 2017-11-23 16:56:44 +0100 | [diff] [blame] | 74 | @CalledByNative("BitrateAllocation") |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 75 | public BitrateAllocation(int[][] bitratesBbs) { |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 76 | this.bitratesBbs = bitratesBbs; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Gets the total bitrate allocated for all layers. |
| 81 | */ |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 82 | public int getSum() { |
| 83 | int sum = 0; |
| 84 | for (int[] spatialLayer : bitratesBbs) { |
| 85 | for (int bitrate : spatialLayer) { |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 86 | sum += bitrate; |
| 87 | } |
| 88 | } |
| 89 | return sum; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** Settings for WebRTC quality based scaling. */ |
| 94 | public class ScalingSettings { |
| 95 | public final boolean on; |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 96 | public final Integer low; |
| 97 | public final Integer high; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 98 | |
| 99 | /** |
Niels Möller | d0dd90b | 2018-02-08 10:15:34 +0100 | [diff] [blame] | 100 | * Settings to disable quality based scaling. |
| 101 | */ |
| 102 | public static final ScalingSettings OFF = new ScalingSettings(); |
| 103 | |
| 104 | /** |
| 105 | * Creates settings to enable quality based scaling. |
| 106 | * |
| 107 | * @param low Average QP at which to scale up the resolution. |
| 108 | * @param high Average QP at which to scale down the resolution. |
| 109 | */ |
| 110 | public ScalingSettings(int low, int high) { |
| 111 | this.on = true; |
| 112 | this.low = low; |
| 113 | this.high = high; |
| 114 | } |
| 115 | |
| 116 | private ScalingSettings() { |
| 117 | this.on = false; |
| 118 | this.low = null; |
| 119 | this.high = null; |
| 120 | } |
| 121 | |
| 122 | // TODO(bugs.webrtc.org/8830): Below constructors are deprecated. |
| 123 | // Default thresholds are going away, so thresholds have to be set |
| 124 | // when scaling is on. |
| 125 | /** |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 126 | * Creates quality based scaling setting. |
| 127 | * |
| 128 | * @param on True if quality scaling is turned on. |
| 129 | */ |
Niels Möller | d0dd90b | 2018-02-08 10:15:34 +0100 | [diff] [blame] | 130 | @Deprecated |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 131 | public ScalingSettings(boolean on) { |
| 132 | this.on = on; |
| 133 | this.low = null; |
| 134 | this.high = null; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Creates quality based scaling settings with custom thresholds. |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 139 | * |
| 140 | * @param on True if quality scaling is turned on. |
| 141 | * @param low Average QP at which to scale up the resolution. |
| 142 | * @param high Average QP at which to scale down the resolution. |
| 143 | */ |
Niels Möller | d0dd90b | 2018-02-08 10:15:34 +0100 | [diff] [blame] | 144 | @Deprecated |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 145 | public ScalingSettings(boolean on, int low, int high) { |
| 146 | this.on = on; |
| 147 | this.low = low; |
| 148 | this.high = high; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | public interface Callback { |
Sami Kalliomäki | 11c51dd | 2018-02-07 12:50:47 +0100 | [diff] [blame] | 153 | /** |
| 154 | * Call to return an encoded frame. It is safe to assume the byte buffer held by |frame| is not |
| 155 | * accessed after the call to this method returns. |
| 156 | */ |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 157 | void onEncodedFrame(EncodedImage frame, CodecSpecificInfo info); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Initializes the encoding process. Call before any calls to encode. |
| 162 | */ |
Magnus Jedvert | 56231d0 | 2017-10-31 17:47:06 +0100 | [diff] [blame] | 163 | @CalledByNative VideoCodecStatus initEncode(Settings settings, Callback encodeCallback); |
| 164 | |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 165 | /** |
| 166 | * Releases the encoder. No more calls to encode will be made after this call. |
| 167 | */ |
Magnus Jedvert | 56231d0 | 2017-10-31 17:47:06 +0100 | [diff] [blame] | 168 | @CalledByNative VideoCodecStatus release(); |
| 169 | |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 170 | /** |
| 171 | * Requests the encoder to encode a frame. |
| 172 | */ |
Magnus Jedvert | 56231d0 | 2017-10-31 17:47:06 +0100 | [diff] [blame] | 173 | @CalledByNative VideoCodecStatus encode(VideoFrame frame, EncodeInfo info); |
| 174 | |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 175 | /** |
| 176 | * Informs the encoder of the packet loss and the round-trip time of the network. |
| 177 | * |
| 178 | * @param packetLoss How many packets are lost on average per 255 packets. |
| 179 | * @param roundTripTimeMs Round-trip time of the network in milliseconds. |
| 180 | */ |
Magnus Jedvert | 56231d0 | 2017-10-31 17:47:06 +0100 | [diff] [blame] | 181 | @CalledByNative VideoCodecStatus setChannelParameters(short packetLoss, long roundTripTimeMs); |
| 182 | |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 183 | /** Sets the bitrate allocation and the target framerate for the encoder. */ |
Magnus Jedvert | 56231d0 | 2017-10-31 17:47:06 +0100 | [diff] [blame] | 184 | @CalledByNative VideoCodecStatus setRateAllocation(BitrateAllocation allocation, int framerate); |
| 185 | |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 186 | /** 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] | 187 | @CalledByNative ScalingSettings getScalingSettings(); |
| 188 | |
Sami Kalliomäki | 5f5fc68 | 2017-10-19 11:34:08 +0200 | [diff] [blame] | 189 | /** |
| 190 | * Should return a descriptive name for the implementation. Gets called once and cached. May be |
| 191 | * called from arbitrary thread. |
| 192 | */ |
Magnus Jedvert | 56231d0 | 2017-10-31 17:47:06 +0100 | [diff] [blame] | 193 | @CalledByNative String getImplementationName(); |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 194 | } |