blob: 1fb96011d0ae08daedb027f0cb5490adeba3b7e5 [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
15 * encoding thread.
16 */
17public interface VideoEncoder {
18 /** Settings passed to the encoder by WebRTC. */
19 public class Settings {
20 public final int numberOfCores;
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070021 public final int width;
22 public final int height;
23 public final int startBitrate; // Kilobits per second.
24 public final int maxFramerate;
sakal07a3bd72017-09-04 03:57:21 -070025 public final boolean automaticResizeOn;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020026
sakal07a3bd72017-09-04 03:57:21 -070027 public Settings(int numberOfCores, int width, int height, int startBitrate, int maxFramerate,
28 boolean automaticResizeOn) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020029 this.numberOfCores = numberOfCores;
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070030 this.width = width;
31 this.height = height;
32 this.startBitrate = startBitrate;
33 this.maxFramerate = maxFramerate;
sakal07a3bd72017-09-04 03:57:21 -070034 this.automaticResizeOn = automaticResizeOn;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020035 }
36 }
37
38 /** Additional info for encoding. */
39 public class EncodeInfo {
40 public final EncodedImage.FrameType[] frameTypes;
41
42 public EncodeInfo(EncodedImage.FrameType[] frameTypes) {
43 this.frameTypes = frameTypes;
44 }
45 }
46
47 // TODO(sakal): Add values to these classes as necessary.
48 /** Codec specific information about the encoded frame. */
49 public class CodecSpecificInfo {}
50
51 public class CodecSpecificInfoVP8 extends CodecSpecificInfo {}
52
53 public class CodecSpecificInfoVP9 extends CodecSpecificInfo {}
54
55 public class CodecSpecificInfoH264 extends CodecSpecificInfo {}
56
57 /**
58 * Represents bitrate allocated for an encoder to produce frames. Bitrate can be divided between
59 * spatial and temporal layers.
60 */
61 public class BitrateAllocation {
62 // First index is the spatial layer and second the temporal layer.
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070063 public final int[][] bitratesBbs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020064
65 /**
66 * Initializes the allocation with a two dimensional array of bitrates. The first index of the
67 * array is the spatial layer and the second index in the temporal layer.
68 */
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070069 public BitrateAllocation(int[][] bitratesBbs) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020070 this.bitratesBbs = bitratesBbs;
71 }
72
73 /**
74 * Gets the total bitrate allocated for all layers.
75 */
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070076 public int getSum() {
77 int sum = 0;
78 for (int[] spatialLayer : bitratesBbs) {
79 for (int bitrate : spatialLayer) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020080 sum += bitrate;
81 }
82 }
83 return sum;
84 }
85 }
86
87 /** Settings for WebRTC quality based scaling. */
88 public class ScalingSettings {
89 public final boolean on;
sakal07a3bd72017-09-04 03:57:21 -070090 public final Integer low;
91 public final Integer high;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020092
93 /**
sakal07a3bd72017-09-04 03:57:21 -070094 * Creates quality based scaling setting.
95 *
96 * @param on True if quality scaling is turned on.
97 */
98 public ScalingSettings(boolean on) {
99 this.on = on;
100 this.low = null;
101 this.high = null;
102 }
103
104 /**
105 * Creates quality based scaling settings with custom thresholds.
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200106 *
107 * @param on True if quality scaling is turned on.
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(boolean on, int low, int high) {
112 this.on = on;
113 this.low = low;
114 this.high = high;
115 }
116 }
117
118 public interface Callback {
119 /** Call to return an encoded frame. */
120 void onEncodedFrame(EncodedImage frame, CodecSpecificInfo info);
121 }
122
123 /**
124 * Initializes the encoding process. Call before any calls to encode.
125 */
Magnus Jedvertf4810dd2017-09-27 11:56:45 +0000126 VideoCodecStatus initEncode(Settings settings, Callback encodeCallback);
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200127 /**
128 * Releases the encoder. No more calls to encode will be made after this call.
129 */
Magnus Jedvertf4810dd2017-09-27 11:56:45 +0000130 VideoCodecStatus release();
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200131 /**
132 * Requests the encoder to encode a frame.
133 */
Magnus Jedvertf4810dd2017-09-27 11:56:45 +0000134 VideoCodecStatus encode(VideoFrame frame, EncodeInfo info);
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200135 /**
136 * Informs the encoder of the packet loss and the round-trip time of the network.
137 *
138 * @param packetLoss How many packets are lost on average per 255 packets.
139 * @param roundTripTimeMs Round-trip time of the network in milliseconds.
140 */
Magnus Jedvertf4810dd2017-09-27 11:56:45 +0000141 VideoCodecStatus setChannelParameters(short packetLoss, long roundTripTimeMs);
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200142 /** Sets the bitrate allocation and the target framerate for the encoder. */
Magnus Jedvertf4810dd2017-09-27 11:56:45 +0000143 VideoCodecStatus setRateAllocation(BitrateAllocation allocation, int framerate);
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200144 /** Any encoder that wants to use WebRTC provided quality scaler must implement this method. */
Magnus Jedvertf4810dd2017-09-27 11:56:45 +0000145 ScalingSettings getScalingSettings();
sakal07a3bd72017-09-04 03:57:21 -0700146 /** Should return a descriptive name for the implementation. Gets called once and cached. */
Magnus Jedvertf4810dd2017-09-27 11:56:45 +0000147 String getImplementationName();
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200148}