blob: f4836c96ba91c6ec5cff2b8619c3d6d6bff17326 [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
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +010013import org.webrtc.EncodedImage;
14
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020015/**
16 * 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 +020017 * encoding thread. The encoder may be constructed on a different thread and changing thread after
18 * calling release is allowed.
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020019 */
20public interface VideoEncoder {
21 /** Settings passed to the encoder by WebRTC. */
22 public class Settings {
23 public final int numberOfCores;
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070024 public final int width;
25 public final int height;
26 public final int startBitrate; // Kilobits per second.
27 public final int maxFramerate;
sakal07a3bd72017-09-04 03:57:21 -070028 public final boolean automaticResizeOn;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020029
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +010030 @CalledByNative("Settings")
sakal07a3bd72017-09-04 03:57:21 -070031 public Settings(int numberOfCores, int width, int height, int startBitrate, int maxFramerate,
32 boolean automaticResizeOn) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020033 this.numberOfCores = numberOfCores;
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070034 this.width = width;
35 this.height = height;
36 this.startBitrate = startBitrate;
37 this.maxFramerate = maxFramerate;
sakal07a3bd72017-09-04 03:57:21 -070038 this.automaticResizeOn = automaticResizeOn;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020039 }
40 }
41
42 /** Additional info for encoding. */
43 public class EncodeInfo {
44 public final EncodedImage.FrameType[] frameTypes;
45
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +010046 @CalledByNative("EncodeInfo")
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020047 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 Mellem5c4eebb2017-06-12 09:21:03 -070068 public final int[][] bitratesBbs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020069
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 Jedvert1f2a3e72017-11-23 16:56:44 +010074 @CalledByNative("BitrateAllocation")
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070075 public BitrateAllocation(int[][] bitratesBbs) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020076 this.bitratesBbs = bitratesBbs;
77 }
78
79 /**
80 * Gets the total bitrate allocated for all layers.
81 */
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070082 public int getSum() {
83 int sum = 0;
84 for (int[] spatialLayer : bitratesBbs) {
85 for (int bitrate : spatialLayer) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020086 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;
sakal07a3bd72017-09-04 03:57:21 -070096 public final Integer low;
97 public final Integer high;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020098
99 /**
Niels Möllerd0dd90b2018-02-08 10:15:34 +0100100 * 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 /**
sakal07a3bd72017-09-04 03:57:21 -0700126 * Creates quality based scaling setting.
127 *
128 * @param on True if quality scaling is turned on.
129 */
Niels Möllerd0dd90b2018-02-08 10:15:34 +0100130 @Deprecated
sakal07a3bd72017-09-04 03:57:21 -0700131 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äkie2410e92017-06-02 14:46:12 +0200139 *
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öllerd0dd90b2018-02-08 10:15:34 +0100144 @Deprecated
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200145 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äki11c51dd2018-02-07 12:50:47 +0100153 /**
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äkie2410e92017-06-02 14:46:12 +0200157 void onEncodedFrame(EncodedImage frame, CodecSpecificInfo info);
158 }
159
160 /**
161 * Initializes the encoding process. Call before any calls to encode.
162 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100163 @CalledByNative VideoCodecStatus initEncode(Settings settings, Callback encodeCallback);
164
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200165 /**
166 * Releases the encoder. No more calls to encode will be made after this call.
167 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100168 @CalledByNative VideoCodecStatus release();
169
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200170 /**
171 * Requests the encoder to encode a frame.
172 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100173 @CalledByNative VideoCodecStatus encode(VideoFrame frame, EncodeInfo info);
174
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200175 /**
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 Jedvert56231d02017-10-31 17:47:06 +0100181 @CalledByNative VideoCodecStatus setChannelParameters(short packetLoss, long roundTripTimeMs);
182
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200183 /** Sets the bitrate allocation and the target framerate for the encoder. */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100184 @CalledByNative VideoCodecStatus setRateAllocation(BitrateAllocation allocation, int framerate);
185
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200186 /** Any encoder that wants to use WebRTC provided quality scaler must implement this method. */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100187 @CalledByNative ScalingSettings getScalingSettings();
188
Sami Kalliomäki5f5fc682017-10-19 11:34:08 +0200189 /**
190 * Should return a descriptive name for the implementation. Gets called once and cached. May be
191 * called from arbitrary thread.
192 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100193 @CalledByNative String getImplementationName();
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200194}