blob: af656bb4b3d9d0148078d9ef5b1dde99c92f90df [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 }
151 }
152
153 public interface Callback {
Sami Kalliomäki11c51dd2018-02-07 12:50:47 +0100154 /**
155 * Call to return an encoded frame. It is safe to assume the byte buffer held by |frame| is not
156 * accessed after the call to this method returns.
157 */
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200158 void onEncodedFrame(EncodedImage frame, CodecSpecificInfo info);
159 }
160
161 /**
162 * Initializes the encoding process. Call before any calls to encode.
163 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100164 @CalledByNative VideoCodecStatus initEncode(Settings settings, Callback encodeCallback);
165
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200166 /**
167 * Releases the encoder. No more calls to encode will be made after this call.
168 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100169 @CalledByNative VideoCodecStatus release();
170
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200171 /**
172 * Requests the encoder to encode a frame.
173 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100174 @CalledByNative VideoCodecStatus encode(VideoFrame frame, EncodeInfo info);
175
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200176 /**
177 * Informs the encoder of the packet loss and the round-trip time of the network.
178 *
179 * @param packetLoss How many packets are lost on average per 255 packets.
180 * @param roundTripTimeMs Round-trip time of the network in milliseconds.
181 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100182 @CalledByNative VideoCodecStatus setChannelParameters(short packetLoss, long roundTripTimeMs);
183
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200184 /** Sets the bitrate allocation and the target framerate for the encoder. */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100185 @CalledByNative VideoCodecStatus setRateAllocation(BitrateAllocation allocation, int framerate);
186
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200187 /** Any encoder that wants to use WebRTC provided quality scaler must implement this method. */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100188 @CalledByNative ScalingSettings getScalingSettings();
189
Sami Kalliomäki5f5fc682017-10-19 11:34:08 +0200190 /**
191 * Should return a descriptive name for the implementation. Gets called once and cached. May be
192 * called from arbitrary thread.
193 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100194 @CalledByNative String getImplementationName();
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200195}