blob: b37f5aa5074b3fbe2f2a7f68d29bc706e84cbb6e [file] [log] [blame]
Sami Kalliomäkie2410e92017-06-02 14:46:12 +02001/*
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
11package org.webrtc;
12
Artem Titarenko69540f42018-12-10 12:30:46 +010013import android.support.annotation.Nullable;
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +010014import org.webrtc.EncodedImage;
15
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020016/**
17 * Interface for a video encoder that can be used with WebRTC. All calls will be made on the
Sami Kalliomäki5f5fc682017-10-19 11:34:08 +020018 * encoding thread. The encoder may be constructed on a different thread and changing thread after
19 * calling release is allowed.
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020020 */
21public interface VideoEncoder {
22 /** Settings passed to the encoder by WebRTC. */
23 public class Settings {
24 public final int numberOfCores;
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070025 public final int width;
26 public final int height;
27 public final int startBitrate; // Kilobits per second.
28 public final int maxFramerate;
Rasmus Brandt85f20cb2018-08-23 13:52:45 +020029 public final int numberOfSimulcastStreams;
sakal07a3bd72017-09-04 03:57:21 -070030 public final boolean automaticResizeOn;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020031
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +010032 @CalledByNative("Settings")
sakal07a3bd72017-09-04 03:57:21 -070033 public Settings(int numberOfCores, int width, int height, int startBitrate, int maxFramerate,
Rasmus Brandt85f20cb2018-08-23 13:52:45 +020034 int numberOfSimulcastStreams, boolean automaticResizeOn) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020035 this.numberOfCores = numberOfCores;
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070036 this.width = width;
37 this.height = height;
38 this.startBitrate = startBitrate;
39 this.maxFramerate = maxFramerate;
Rasmus Brandt85f20cb2018-08-23 13:52:45 +020040 this.numberOfSimulcastStreams = numberOfSimulcastStreams;
sakal07a3bd72017-09-04 03:57:21 -070041 this.automaticResizeOn = automaticResizeOn;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020042 }
43 }
44
45 /** Additional info for encoding. */
46 public class EncodeInfo {
47 public final EncodedImage.FrameType[] frameTypes;
48
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +010049 @CalledByNative("EncodeInfo")
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020050 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 Mellem5c4eebb2017-06-12 09:21:03 -070071 public final int[][] bitratesBbs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020072
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 Jedvert1f2a3e72017-11-23 16:56:44 +010077 @CalledByNative("BitrateAllocation")
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070078 public BitrateAllocation(int[][] bitratesBbs) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020079 this.bitratesBbs = bitratesBbs;
80 }
81
82 /**
83 * Gets the total bitrate allocated for all layers.
84 */
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070085 public int getSum() {
86 int sum = 0;
87 for (int[] spatialLayer : bitratesBbs) {
88 for (int bitrate : spatialLayer) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020089 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äkie7592d82018-03-22 13:32:44 +010099 @Nullable public final Integer low;
100 @Nullable public final Integer high;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200101
102 /**
Niels Möllerd0dd90b2018-02-08 10:15:34 +0100103 * 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 /**
sakal07a3bd72017-09-04 03:57:21 -0700129 * Creates quality based scaling setting.
130 *
131 * @param on True if quality scaling is turned on.
132 */
Niels Möllerd0dd90b2018-02-08 10:15:34 +0100133 @Deprecated
sakal07a3bd72017-09-04 03:57:21 -0700134 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äkie2410e92017-06-02 14:46:12 +0200142 *
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öllerd0dd90b2018-02-08 10:15:34 +0100147 @Deprecated
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200148 public ScalingSettings(boolean on, int low, int high) {
149 this.on = on;
150 this.low = low;
151 this.high = high;
152 }
Sami Kalliomäki8619e8a2018-04-17 14:44:36 +0200153
154 @Override
155 public String toString() {
156 return on ? "[ " + low + ", " + high + " ]" : "OFF";
157 }
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200158 }
159
160 public interface Callback {
Sami Kalliomäki11c51dd2018-02-07 12:50:47 +0100161 /**
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äkie2410e92017-06-02 14:46:12 +0200165 void onEncodedFrame(EncodedImage frame, CodecSpecificInfo info);
166 }
167
168 /**
Rasmus Brandt42a2fc92018-07-09 13:38:01 +0200169 * 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äkie2410e92017-06-02 14:46:12 +0200199 * Initializes the encoding process. Call before any calls to encode.
200 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100201 @CalledByNative VideoCodecStatus initEncode(Settings settings, Callback encodeCallback);
202
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200203 /**
204 * Releases the encoder. No more calls to encode will be made after this call.
205 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100206 @CalledByNative VideoCodecStatus release();
207
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200208 /**
209 * Requests the encoder to encode a frame.
210 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100211 @CalledByNative VideoCodecStatus encode(VideoFrame frame, EncodeInfo info);
212
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200213 /** Sets the bitrate allocation and the target framerate for the encoder. */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100214 @CalledByNative VideoCodecStatus setRateAllocation(BitrateAllocation allocation, int framerate);
215
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200216 /** Any encoder that wants to use WebRTC provided quality scaler must implement this method. */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100217 @CalledByNative ScalingSettings getScalingSettings();
218
Sami Kalliomäki5f5fc682017-10-19 11:34:08 +0200219 /**
220 * Should return a descriptive name for the implementation. Gets called once and cached. May be
221 * called from arbitrary thread.
222 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100223 @CalledByNative String getImplementationName();
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200224}