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 |
Sami Kalliomäki | 5f5fc68 | 2017-10-19 11:34:08 +0200 | [diff] [blame^] | 15 | * encoding thread. The encoder may be constructed on a different thread and changing thread after |
| 16 | * calling release is allowed. |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 17 | */ |
| 18 | public interface VideoEncoder { |
| 19 | /** Settings passed to the encoder by WebRTC. */ |
| 20 | public class Settings { |
| 21 | public final int numberOfCores; |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 22 | public final int width; |
| 23 | public final int height; |
| 24 | public final int startBitrate; // Kilobits per second. |
| 25 | public final int maxFramerate; |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 26 | public final boolean automaticResizeOn; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 27 | |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 28 | public Settings(int numberOfCores, int width, int height, int startBitrate, int maxFramerate, |
| 29 | boolean automaticResizeOn) { |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 30 | this.numberOfCores = numberOfCores; |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 31 | this.width = width; |
| 32 | this.height = height; |
| 33 | this.startBitrate = startBitrate; |
| 34 | this.maxFramerate = maxFramerate; |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 35 | this.automaticResizeOn = automaticResizeOn; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 36 | } |
| 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 Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 64 | public final int[][] bitratesBbs; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 65 | |
| 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 Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 70 | public BitrateAllocation(int[][] bitratesBbs) { |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 71 | this.bitratesBbs = bitratesBbs; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Gets the total bitrate allocated for all layers. |
| 76 | */ |
Bjorn Mellem | 5c4eebb | 2017-06-12 09:21:03 -0700 | [diff] [blame] | 77 | public int getSum() { |
| 78 | int sum = 0; |
| 79 | for (int[] spatialLayer : bitratesBbs) { |
| 80 | for (int bitrate : spatialLayer) { |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 81 | 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; |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 91 | public final Integer low; |
| 92 | public final Integer high; |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 93 | |
| 94 | /** |
sakal | 07a3bd7 | 2017-09-04 03:57:21 -0700 | [diff] [blame] | 95 | * 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äki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 107 | * |
| 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 Jedvert | f4810dd | 2017-09-27 11:56:45 +0000 | [diff] [blame] | 127 | VideoCodecStatus initEncode(Settings settings, Callback encodeCallback); |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 128 | /** |
| 129 | * Releases the encoder. No more calls to encode will be made after this call. |
| 130 | */ |
Magnus Jedvert | f4810dd | 2017-09-27 11:56:45 +0000 | [diff] [blame] | 131 | VideoCodecStatus release(); |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 132 | /** |
| 133 | * Requests the encoder to encode a frame. |
| 134 | */ |
Magnus Jedvert | f4810dd | 2017-09-27 11:56:45 +0000 | [diff] [blame] | 135 | VideoCodecStatus encode(VideoFrame frame, EncodeInfo info); |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 136 | /** |
| 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 Jedvert | f4810dd | 2017-09-27 11:56:45 +0000 | [diff] [blame] | 142 | VideoCodecStatus setChannelParameters(short packetLoss, long roundTripTimeMs); |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 143 | /** Sets the bitrate allocation and the target framerate for the encoder. */ |
Magnus Jedvert | f4810dd | 2017-09-27 11:56:45 +0000 | [diff] [blame] | 144 | VideoCodecStatus setRateAllocation(BitrateAllocation allocation, int framerate); |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 145 | /** 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] | 146 | ScalingSettings getScalingSettings(); |
Sami Kalliomäki | 5f5fc68 | 2017-10-19 11:34:08 +0200 | [diff] [blame^] | 147 | /** |
| 148 | * Should return a descriptive name for the implementation. Gets called once and cached. May be |
| 149 | * called from arbitrary thread. |
| 150 | */ |
Magnus Jedvert | f4810dd | 2017-09-27 11:56:45 +0000 | [diff] [blame] | 151 | String getImplementationName(); |
Sami Kalliomäki | e2410e9 | 2017-06-02 14:46:12 +0200 | [diff] [blame] | 152 | } |