blob: 4f97fb0c0d1699e89f9b7fc702a0e3f03c61e0a7 [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 /**
sakal07a3bd72017-09-04 03:57:21 -0700100 * 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äkie2410e92017-06-02 14:46:12 +0200112 *
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 Jedvert56231d02017-10-31 17:47:06 +0100132 @CalledByNative VideoCodecStatus initEncode(Settings settings, Callback encodeCallback);
133
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200134 /**
135 * Releases the encoder. No more calls to encode will be made after this call.
136 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100137 @CalledByNative VideoCodecStatus release();
138
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200139 /**
140 * Requests the encoder to encode a frame.
141 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100142 @CalledByNative VideoCodecStatus encode(VideoFrame frame, EncodeInfo info);
143
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200144 /**
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 Jedvert56231d02017-10-31 17:47:06 +0100150 @CalledByNative VideoCodecStatus setChannelParameters(short packetLoss, long roundTripTimeMs);
151
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200152 /** Sets the bitrate allocation and the target framerate for the encoder. */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100153 @CalledByNative VideoCodecStatus setRateAllocation(BitrateAllocation allocation, int framerate);
154
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200155 /** Any encoder that wants to use WebRTC provided quality scaler must implement this method. */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100156 @CalledByNative ScalingSettings getScalingSettings();
157
Sami Kalliomäki5f5fc682017-10-19 11:34:08 +0200158 /**
159 * Should return a descriptive name for the implementation. Gets called once and cached. May be
160 * called from arbitrary thread.
161 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100162 @CalledByNative String getImplementationName();
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200163}