blob: b739e99aa6b6d48c6c208ff3b8b79aeaf7096e43 [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
Sami Kalliomäkie7592d82018-03-22 13:32:44 +010013import javax.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;
sakal07a3bd72017-09-04 03:57:21 -070029 public final boolean automaticResizeOn;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020030
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +010031 @CalledByNative("Settings")
sakal07a3bd72017-09-04 03:57:21 -070032 public Settings(int numberOfCores, int width, int height, int startBitrate, int maxFramerate,
33 boolean automaticResizeOn) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020034 this.numberOfCores = numberOfCores;
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070035 this.width = width;
36 this.height = height;
37 this.startBitrate = startBitrate;
38 this.maxFramerate = maxFramerate;
sakal07a3bd72017-09-04 03:57:21 -070039 this.automaticResizeOn = automaticResizeOn;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020040 }
41 }
42
43 /** Additional info for encoding. */
44 public class EncodeInfo {
45 public final EncodedImage.FrameType[] frameTypes;
46
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +010047 @CalledByNative("EncodeInfo")
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020048 public EncodeInfo(EncodedImage.FrameType[] frameTypes) {
49 this.frameTypes = frameTypes;
50 }
51 }
52
53 // TODO(sakal): Add values to these classes as necessary.
54 /** Codec specific information about the encoded frame. */
55 public class CodecSpecificInfo {}
56
57 public class CodecSpecificInfoVP8 extends CodecSpecificInfo {}
58
59 public class CodecSpecificInfoVP9 extends CodecSpecificInfo {}
60
61 public class CodecSpecificInfoH264 extends CodecSpecificInfo {}
62
63 /**
64 * Represents bitrate allocated for an encoder to produce frames. Bitrate can be divided between
65 * spatial and temporal layers.
66 */
67 public class BitrateAllocation {
68 // First index is the spatial layer and second the temporal layer.
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070069 public final int[][] bitratesBbs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020070
71 /**
72 * Initializes the allocation with a two dimensional array of bitrates. The first index of the
73 * array is the spatial layer and the second index in the temporal layer.
74 */
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +010075 @CalledByNative("BitrateAllocation")
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070076 public BitrateAllocation(int[][] bitratesBbs) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020077 this.bitratesBbs = bitratesBbs;
78 }
79
80 /**
81 * Gets the total bitrate allocated for all layers.
82 */
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070083 public int getSum() {
84 int sum = 0;
85 for (int[] spatialLayer : bitratesBbs) {
86 for (int bitrate : spatialLayer) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020087 sum += bitrate;
88 }
89 }
90 return sum;
91 }
92 }
93
94 /** Settings for WebRTC quality based scaling. */
95 public class ScalingSettings {
96 public final boolean on;
Sami Kalliomäkie7592d82018-03-22 13:32:44 +010097 @Nullable public final Integer low;
98 @Nullable public final Integer high;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020099
100 /**
Niels Möllerd0dd90b2018-02-08 10:15:34 +0100101 * Settings to disable quality based scaling.
102 */
103 public static final ScalingSettings OFF = new ScalingSettings();
104
105 /**
106 * Creates settings to enable quality based scaling.
107 *
108 * @param low Average QP at which to scale up the resolution.
109 * @param high Average QP at which to scale down the resolution.
110 */
111 public ScalingSettings(int low, int high) {
112 this.on = true;
113 this.low = low;
114 this.high = high;
115 }
116
117 private ScalingSettings() {
118 this.on = false;
119 this.low = null;
120 this.high = null;
121 }
122
123 // TODO(bugs.webrtc.org/8830): Below constructors are deprecated.
124 // Default thresholds are going away, so thresholds have to be set
125 // when scaling is on.
126 /**
sakal07a3bd72017-09-04 03:57:21 -0700127 * Creates quality based scaling setting.
128 *
129 * @param on True if quality scaling is turned on.
130 */
Niels Möllerd0dd90b2018-02-08 10:15:34 +0100131 @Deprecated
sakal07a3bd72017-09-04 03:57:21 -0700132 public ScalingSettings(boolean on) {
133 this.on = on;
134 this.low = null;
135 this.high = null;
136 }
137
138 /**
139 * Creates quality based scaling settings with custom thresholds.
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200140 *
141 * @param on True if quality scaling is turned on.
142 * @param low Average QP at which to scale up the resolution.
143 * @param high Average QP at which to scale down the resolution.
144 */
Niels Möllerd0dd90b2018-02-08 10:15:34 +0100145 @Deprecated
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200146 public ScalingSettings(boolean on, int low, int high) {
147 this.on = on;
148 this.low = low;
149 this.high = high;
150 }
Sami Kalliomäki8619e8a2018-04-17 14:44:36 +0200151
152 @Override
153 public String toString() {
154 return on ? "[ " + low + ", " + high + " ]" : "OFF";
155 }
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200156 }
157
158 public interface Callback {
Sami Kalliomäki11c51dd2018-02-07 12:50:47 +0100159 /**
160 * Call to return an encoded frame. It is safe to assume the byte buffer held by |frame| is not
161 * accessed after the call to this method returns.
162 */
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200163 void onEncodedFrame(EncodedImage frame, CodecSpecificInfo info);
164 }
165
166 /**
Rasmus Brandt42a2fc92018-07-09 13:38:01 +0200167 * The encoder implementation backing this interface is either 1) a Java
168 * encoder (e.g., an Android platform encoder), or alternatively 2) a native
169 * encoder (e.g., a software encoder or a C++ encoder adapter).
170 *
171 * For case 1), createNativeVideoEncoder() should return zero.
172 * In this case, we expect the native library to call the encoder through
173 * JNI using the Java interface declared below.
174 *
175 * For case 2), createNativeVideoEncoder() should return a non-zero value.
176 * In this case, we expect the native library to treat the returned value as
177 * a raw pointer of type webrtc::VideoEncoder* (ownership is transferred to
178 * the caller). The native library should then directly call the
179 * webrtc::VideoEncoder interface without going through JNI. All calls to
180 * the Java interface methods declared below should thus throw an
181 * UnsupportedOperationException.
182 */
183 @CalledByNative
184 default long createNativeVideoEncoder() {
185 return 0;
186 }
187
188 /**
189 * Returns true if the encoder is backed by hardware.
190 */
191 @CalledByNative
192 default boolean isHardwareEncoder() {
193 return true;
194 }
195
196 /**
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200197 * Initializes the encoding process. Call before any calls to encode.
198 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100199 @CalledByNative VideoCodecStatus initEncode(Settings settings, Callback encodeCallback);
200
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200201 /**
202 * Releases the encoder. No more calls to encode will be made after this call.
203 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100204 @CalledByNative VideoCodecStatus release();
205
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200206 /**
207 * Requests the encoder to encode a frame.
208 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100209 @CalledByNative VideoCodecStatus encode(VideoFrame frame, EncodeInfo info);
210
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200211 /**
212 * Informs the encoder of the packet loss and the round-trip time of the network.
213 *
214 * @param packetLoss How many packets are lost on average per 255 packets.
215 * @param roundTripTimeMs Round-trip time of the network in milliseconds.
216 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100217 @CalledByNative VideoCodecStatus setChannelParameters(short packetLoss, long roundTripTimeMs);
218
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200219 /** Sets the bitrate allocation and the target framerate for the encoder. */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100220 @CalledByNative VideoCodecStatus setRateAllocation(BitrateAllocation allocation, int framerate);
221
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200222 /** Any encoder that wants to use WebRTC provided quality scaler must implement this method. */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100223 @CalledByNative ScalingSettings getScalingSettings();
224
Sami Kalliomäki5f5fc682017-10-19 11:34:08 +0200225 /**
226 * Should return a descriptive name for the implementation. Gets called once and cached. May be
227 * called from arbitrary thread.
228 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100229 @CalledByNative String getImplementationName();
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200230}