Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 11 | package 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 | */ |
| 17 | public interface VideoEncoder { |
| 18 | /** Settings passed to the encoder by WebRTC. */ |
| 19 | public class Settings { |
| 20 | public final int numberOfCores; |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 21 | public final int width; |
| 22 | public final int height; |
| 23 | public final int startBitrate; // Kilobits per second. |
| 24 | public final int maxFramerate; |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 25 | public final boolean automaticResizeOn; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 26 | |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 27 | public Settings(int numberOfCores, int width, int height, int startBitrate, int maxFramerate, |
| 28 | boolean automaticResizeOn) { |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 29 | this.numberOfCores = numberOfCores; |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 30 | this.width = width; |
| 31 | this.height = height; |
| 32 | this.startBitrate = startBitrate; |
| 33 | this.maxFramerate = maxFramerate; |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 34 | this.automaticResizeOn = automaticResizeOn; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 35 | } |
| 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 Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 63 | public final int[][] bitratesBbs; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 64 | |
| 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 Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 69 | public BitrateAllocation(int[][] bitratesBbs) { |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 70 | this.bitratesBbs = bitratesBbs; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Gets the total bitrate allocated for all layers. |
| 75 | */ |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 76 | public int getSum() { |
| 77 | int sum = 0; |
| 78 | for (int[] spatialLayer : bitratesBbs) { |
| 79 | for (int bitrate : spatialLayer) { |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 80 | 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; |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 90 | public final Integer low; |
| 91 | public final Integer high; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 92 | |
| 93 | /** |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 94 | * 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äki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 106 | * |
| 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 Jedvert | f4810dd | 2017-09-27 11:56:45 +0000 | [diff] [blame] | 126 | VideoCodecStatus initEncode(Settings settings, Callback encodeCallback); |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 127 | /** |
| 128 | * Releases the encoder. No more calls to encode will be made after this call. |
| 129 | */ |
Magnus Jedvert | f4810dd | 2017-09-27 11:56:45 +0000 | [diff] [blame] | 130 | VideoCodecStatus release(); |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 131 | /** |
| 132 | * Requests the encoder to encode a frame. |
| 133 | */ |
Magnus Jedvert | f4810dd | 2017-09-27 11:56:45 +0000 | [diff] [blame] | 134 | VideoCodecStatus encode(VideoFrame frame, EncodeInfo info); |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 135 | /** |
| 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 Jedvert | f4810dd | 2017-09-27 11:56:45 +0000 | [diff] [blame] | 141 | VideoCodecStatus setChannelParameters(short packetLoss, long roundTripTimeMs); |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 142 | /** Sets the bitrate allocation and the target framerate for the encoder. */ |
Magnus Jedvert | f4810dd | 2017-09-27 11:56:45 +0000 | [diff] [blame] | 143 | VideoCodecStatus setRateAllocation(BitrateAllocation allocation, int framerate); |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 144 | /** Any encoder that wants to use WebRTC provided quality scaler must implement this method. */ |
Magnus Jedvert | f4810dd | 2017-09-27 11:56:45 +0000 | [diff] [blame] | 145 | ScalingSettings getScalingSettings(); |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 146 | /** Should return a descriptive name for the implementation. Gets called once and cached. */ |
Magnus Jedvert | f4810dd | 2017-09-27 11:56:45 +0000 | [diff] [blame] | 147 | String getImplementationName(); |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 148 | } |