blob: 27088468a38ccbfefc172bd82e9ff5f4f476e6b6 [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
13/**
14 * 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 +020015 * encoding thread. The encoder may be constructed on a different thread and changing thread after
16 * calling release is allowed.
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020017 */
18public interface VideoEncoder {
19 /** Settings passed to the encoder by WebRTC. */
20 public class Settings {
21 public final int numberOfCores;
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070022 public final int width;
23 public final int height;
24 public final int startBitrate; // Kilobits per second.
25 public final int maxFramerate;
sakal07a3bd72017-09-04 03:57:21 -070026 public final boolean automaticResizeOn;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020027
sakal07a3bd72017-09-04 03:57:21 -070028 public Settings(int numberOfCores, int width, int height, int startBitrate, int maxFramerate,
29 boolean automaticResizeOn) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020030 this.numberOfCores = numberOfCores;
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070031 this.width = width;
32 this.height = height;
33 this.startBitrate = startBitrate;
34 this.maxFramerate = maxFramerate;
sakal07a3bd72017-09-04 03:57:21 -070035 this.automaticResizeOn = automaticResizeOn;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020036 }
37 }
38
39 /** Additional info for encoding. */
40 public class EncodeInfo {
41 public final EncodedImage.FrameType[] frameTypes;
42
43 public EncodeInfo(EncodedImage.FrameType[] frameTypes) {
44 this.frameTypes = frameTypes;
45 }
46 }
47
48 // TODO(sakal): Add values to these classes as necessary.
49 /** Codec specific information about the encoded frame. */
50 public class CodecSpecificInfo {}
51
52 public class CodecSpecificInfoVP8 extends CodecSpecificInfo {}
53
54 public class CodecSpecificInfoVP9 extends CodecSpecificInfo {}
55
56 public class CodecSpecificInfoH264 extends CodecSpecificInfo {}
57
58 /**
59 * Represents bitrate allocated for an encoder to produce frames. Bitrate can be divided between
60 * spatial and temporal layers.
61 */
62 public class BitrateAllocation {
63 // First index is the spatial layer and second the temporal layer.
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070064 public final int[][] bitratesBbs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020065
66 /**
67 * Initializes the allocation with a two dimensional array of bitrates. The first index of the
68 * array is the spatial layer and the second index in the temporal layer.
69 */
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070070 public BitrateAllocation(int[][] bitratesBbs) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020071 this.bitratesBbs = bitratesBbs;
72 }
73
74 /**
75 * Gets the total bitrate allocated for all layers.
76 */
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070077 public int getSum() {
78 int sum = 0;
79 for (int[] spatialLayer : bitratesBbs) {
80 for (int bitrate : spatialLayer) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020081 sum += bitrate;
82 }
83 }
84 return sum;
85 }
86 }
87
88 /** Settings for WebRTC quality based scaling. */
89 public class ScalingSettings {
90 public final boolean on;
sakal07a3bd72017-09-04 03:57:21 -070091 public final Integer low;
92 public final Integer high;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020093
94 /**
sakal07a3bd72017-09-04 03:57:21 -070095 * Creates quality based scaling setting.
96 *
97 * @param on True if quality scaling is turned on.
98 */
99 public ScalingSettings(boolean on) {
100 this.on = on;
101 this.low = null;
102 this.high = null;
103 }
104
105 /**
106 * Creates quality based scaling settings with custom thresholds.
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200107 *
108 * @param on True if quality scaling is turned on.
109 * @param low Average QP at which to scale up the resolution.
110 * @param high Average QP at which to scale down the resolution.
111 */
112 public ScalingSettings(boolean on, int low, int high) {
113 this.on = on;
114 this.low = low;
115 this.high = high;
116 }
117 }
118
119 public interface Callback {
120 /** Call to return an encoded frame. */
121 void onEncodedFrame(EncodedImage frame, CodecSpecificInfo info);
122 }
123
124 /**
125 * Initializes the encoding process. Call before any calls to encode.
126 */
Magnus Jedvertf4810dd2017-09-27 11:56:45 +0000127 VideoCodecStatus initEncode(Settings settings, Callback encodeCallback);
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200128 /**
129 * Releases the encoder. No more calls to encode will be made after this call.
130 */
Magnus Jedvertf4810dd2017-09-27 11:56:45 +0000131 VideoCodecStatus release();
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200132 /**
133 * Requests the encoder to encode a frame.
134 */
Magnus Jedvertf4810dd2017-09-27 11:56:45 +0000135 VideoCodecStatus encode(VideoFrame frame, EncodeInfo info);
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200136 /**
137 * Informs the encoder of the packet loss and the round-trip time of the network.
138 *
139 * @param packetLoss How many packets are lost on average per 255 packets.
140 * @param roundTripTimeMs Round-trip time of the network in milliseconds.
141 */
Magnus Jedvertf4810dd2017-09-27 11:56:45 +0000142 VideoCodecStatus setChannelParameters(short packetLoss, long roundTripTimeMs);
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200143 /** Sets the bitrate allocation and the target framerate for the encoder. */
Magnus Jedvertf4810dd2017-09-27 11:56:45 +0000144 VideoCodecStatus setRateAllocation(BitrateAllocation allocation, int framerate);
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200145 /** Any encoder that wants to use WebRTC provided quality scaler must implement this method. */
Magnus Jedvertf4810dd2017-09-27 11:56:45 +0000146 ScalingSettings getScalingSettings();
Sami Kalliomäki5f5fc682017-10-19 11:34:08 +0200147 /**
148 * Should return a descriptive name for the implementation. Gets called once and cached. May be
149 * called from arbitrary thread.
150 */
Magnus Jedvertf4810dd2017-09-27 11:56:45 +0000151 String getImplementationName();
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200152}