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 | /** |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 100 | * Creates quality based scaling setting. |
| 101 | * |
| 102 | * @param on True if quality scaling is turned on. |
| 103 | */ |
| 104 | public ScalingSettings(boolean on) { |
| 105 | this.on = on; |
| 106 | this.low = null; |
| 107 | this.high = null; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Creates quality based scaling settings with custom thresholds. |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 112 | * |
| 113 | * @param on True if quality scaling is turned on. |
| 114 | * @param low Average QP at which to scale up the resolution. |
| 115 | * @param high Average QP at which to scale down the resolution. |
| 116 | */ |
| 117 | public ScalingSettings(boolean on, int low, int high) { |
| 118 | this.on = on; |
| 119 | this.low = low; |
| 120 | this.high = high; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | public interface Callback { |
| 125 | /** Call to return an encoded frame. */ |
| 126 | void onEncodedFrame(EncodedImage frame, CodecSpecificInfo info); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Initializes the encoding process. Call before any calls to encode. |
| 131 | */ |
Magnus Jedvert | 56231d0 | 2017-10-31 17:47:06 +0100 | [diff] [blame] | 132 | @CalledByNative VideoCodecStatus initEncode(Settings settings, Callback encodeCallback); |
| 133 | |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 134 | /** |
| 135 | * Releases the encoder. No more calls to encode will be made after this call. |
| 136 | */ |
Magnus Jedvert | 56231d0 | 2017-10-31 17:47:06 +0100 | [diff] [blame] | 137 | @CalledByNative VideoCodecStatus release(); |
| 138 | |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 139 | /** |
| 140 | * Requests the encoder to encode a frame. |
| 141 | */ |
Magnus Jedvert | 56231d0 | 2017-10-31 17:47:06 +0100 | [diff] [blame] | 142 | @CalledByNative VideoCodecStatus encode(VideoFrame frame, EncodeInfo info); |
| 143 | |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 144 | /** |
| 145 | * Informs the encoder of the packet loss and the round-trip time of the network. |
| 146 | * |
| 147 | * @param packetLoss How many packets are lost on average per 255 packets. |
| 148 | * @param roundTripTimeMs Round-trip time of the network in milliseconds. |
| 149 | */ |
Magnus Jedvert | 56231d0 | 2017-10-31 17:47:06 +0100 | [diff] [blame] | 150 | @CalledByNative VideoCodecStatus setChannelParameters(short packetLoss, long roundTripTimeMs); |
| 151 | |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 152 | /** Sets the bitrate allocation and the target framerate for the encoder. */ |
Magnus Jedvert | 56231d0 | 2017-10-31 17:47:06 +0100 | [diff] [blame] | 153 | @CalledByNative VideoCodecStatus setRateAllocation(BitrateAllocation allocation, int framerate); |
| 154 | |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 155 | /** 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] | 156 | @CalledByNative ScalingSettings getScalingSettings(); |
| 157 | |
Sami Kalliomäki | 5f5fc68 | 2017-10-19 11:34:08 +0200 | [diff] [blame] | 158 | /** |
| 159 | * Should return a descriptive name for the implementation. Gets called once and cached. May be |
| 160 | * called from arbitrary thread. |
| 161 | */ |
Magnus Jedvert | 56231d0 | 2017-10-31 17:47:06 +0100 | [diff] [blame] | 162 | @CalledByNative String getImplementationName(); |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 163 | } |