|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2024 Stream.io Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.webrtc; |
| 17 | + |
| 18 | +/** |
| 19 | + * Original source: https://github.com/shiguredo/sora-android-sdk/blob/3cc88e806ab2f2327bf304207 |
| 20 | + * 2e98d6da9df4408/sora-android-sdk/src/main/kotlin/jp/shiguredo/sora/sdk/codec/HardwareVideoEnco |
| 21 | + * derWrapperFactory.kt |
| 22 | + */ |
| 23 | +class HardwareVideoEncoderWrapper implements VideoEncoder { |
| 24 | + |
| 25 | + private static final String TAG = "HardwareVideoEncoderWrapper"; |
| 26 | + |
| 27 | + private final VideoEncoder internalEncoder; |
| 28 | + private final int alignment; |
| 29 | + |
| 30 | + public HardwareVideoEncoderWrapper(VideoEncoder internalEncoder, int alignment) { |
| 31 | + this.internalEncoder = internalEncoder; |
| 32 | + this.alignment = alignment; |
| 33 | + } |
| 34 | + |
| 35 | + private static class CropSizeCalculator { |
| 36 | + |
| 37 | + private static final String TAG = "CropSizeCalculator"; |
| 38 | + |
| 39 | + private final int alignment; |
| 40 | + private final int originalWidth; |
| 41 | + private final int originalHeight; |
| 42 | + private final int cropX; |
| 43 | + private final int cropY; |
| 44 | + |
| 45 | + public CropSizeCalculator(int alignment, int originalWidth, int originalHeight) { |
| 46 | + this.alignment = alignment; |
| 47 | + this.originalWidth = originalWidth; |
| 48 | + this.originalHeight = originalHeight; |
| 49 | + this.cropX = originalWidth % alignment; |
| 50 | + this.cropY = originalHeight % alignment; |
| 51 | + if (originalWidth != 0 && originalHeight != 0) { |
| 52 | + Logging.v(TAG, "init(): alignment=" + alignment + |
| 53 | + " size=" + originalWidth + "x" + originalHeight + " => " + getCroppedWidth() + "x" + getCroppedHeight()); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public int getCroppedWidth() { |
| 58 | + return originalWidth - cropX; |
| 59 | + } |
| 60 | + |
| 61 | + public int getCroppedHeight() { |
| 62 | + return originalHeight - cropY; |
| 63 | + } |
| 64 | + |
| 65 | + public boolean isCropRequired() { |
| 66 | + return cropX != 0 || cropY != 0; |
| 67 | + } |
| 68 | + |
| 69 | + public boolean hasFrameSizeChanged(int nextWidth, int nextHeight) { |
| 70 | + if (originalWidth == nextWidth && originalHeight == nextHeight) { |
| 71 | + return false; |
| 72 | + } else { |
| 73 | + Logging.v(TAG, "frame size has changed: " + |
| 74 | + originalWidth + "x" + originalHeight + " => " + nextWidth + "x" + nextHeight); |
| 75 | + return true; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private CropSizeCalculator calculator = new CropSizeCalculator(1, 0, 0); |
| 81 | + |
| 82 | + private VideoCodecStatus retryWithoutCropping(int width, int height, Runnable retryFunc) { |
| 83 | + Logging.v(TAG, "retrying without resolution adjustment"); |
| 84 | + calculator = new CropSizeCalculator(1, width, height); |
| 85 | + retryFunc.run(); |
| 86 | + return VideoCodecStatus.OK; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public VideoCodecStatus initEncode(VideoEncoder.Settings originalSettings, VideoEncoder.Callback callback) { |
| 91 | + calculator = new CropSizeCalculator(alignment, originalSettings.width, originalSettings.height); |
| 92 | + if (!calculator.isCropRequired()) { |
| 93 | + return internalEncoder.initEncode(originalSettings, callback); |
| 94 | + } else { |
| 95 | + VideoEncoder.Settings croppedSettings = new VideoEncoder.Settings( |
| 96 | + originalSettings.numberOfCores, |
| 97 | + calculator.getCroppedWidth(), |
| 98 | + calculator.getCroppedHeight(), |
| 99 | + originalSettings.startBitrate, |
| 100 | + originalSettings.maxFramerate, |
| 101 | + originalSettings.numberOfSimulcastStreams, |
| 102 | + originalSettings.automaticResizeOn, |
| 103 | + originalSettings.capabilities |
| 104 | + ); |
| 105 | + try { |
| 106 | + VideoCodecStatus result = internalEncoder.initEncode(croppedSettings, callback); |
| 107 | + if (result == VideoCodecStatus.FALLBACK_SOFTWARE) { |
| 108 | + Logging.e(TAG, "internalEncoder.initEncode() returned FALLBACK_SOFTWARE: " + |
| 109 | + "croppedSettings " + croppedSettings); |
| 110 | + return retryWithoutCropping( |
| 111 | + originalSettings.width, |
| 112 | + originalSettings.height, |
| 113 | + () -> internalEncoder.initEncode(originalSettings, callback) |
| 114 | + ); |
| 115 | + } else { |
| 116 | + return result; |
| 117 | + } |
| 118 | + } catch (Exception e) { |
| 119 | + Logging.e(TAG, "internalEncoder.initEncode() failed", e); |
| 120 | + return retryWithoutCropping( |
| 121 | + originalSettings.width, |
| 122 | + originalSettings.height, |
| 123 | + () -> internalEncoder.initEncode(originalSettings, callback) |
| 124 | + ); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public VideoCodecStatus release() { |
| 131 | + return internalEncoder.release(); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public VideoCodecStatus encode(VideoFrame frame, VideoEncoder.EncodeInfo encodeInfo) { |
| 136 | + if (calculator.hasFrameSizeChanged(frame.getBuffer().getWidth(), frame.getBuffer().getHeight())) { |
| 137 | + calculator = new CropSizeCalculator(alignment, frame.getBuffer().getWidth(), frame.getBuffer().getHeight()); |
| 138 | + } |
| 139 | + if (!calculator.isCropRequired()) { |
| 140 | + return internalEncoder.encode(frame, encodeInfo); |
| 141 | + } else { |
| 142 | + int croppedWidth = calculator.getCroppedWidth(); |
| 143 | + int croppedHeight = calculator.getCroppedHeight(); |
| 144 | + VideoFrame.Buffer croppedBuffer = frame.getBuffer().cropAndScale( |
| 145 | + calculator.cropX / 2, |
| 146 | + calculator.cropY / 2, |
| 147 | + croppedWidth, |
| 148 | + croppedHeight, |
| 149 | + croppedWidth, |
| 150 | + croppedHeight |
| 151 | + ); |
| 152 | + VideoFrame croppedFrame = new VideoFrame(croppedBuffer, frame.getRotation(), frame.getTimestampNs()); |
| 153 | + try { |
| 154 | + VideoCodecStatus result = internalEncoder.encode(croppedFrame, encodeInfo); |
| 155 | + if (result == VideoCodecStatus.FALLBACK_SOFTWARE) { |
| 156 | + Logging.e(TAG, "internalEncoder.encode() returned FALLBACK_SOFTWARE"); |
| 157 | + return retryWithoutCropping( |
| 158 | + frame.getBuffer().getWidth(), |
| 159 | + frame.getBuffer().getHeight(), |
| 160 | + () -> internalEncoder.encode(frame, encodeInfo) |
| 161 | + ); |
| 162 | + } else { |
| 163 | + return result; |
| 164 | + } |
| 165 | + } catch (Exception e) { |
| 166 | + Logging.e(TAG, "internalEncoder.encode() failed", e); |
| 167 | + return retryWithoutCropping( |
| 168 | + frame.getBuffer().getWidth(), |
| 169 | + frame.getBuffer().getHeight(), |
| 170 | + () -> internalEncoder.encode(frame, encodeInfo) |
| 171 | + ); |
| 172 | + } finally { |
| 173 | + croppedBuffer.release(); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public VideoCodecStatus setRateAllocation(VideoEncoder.BitrateAllocation allocation, int frameRate) { |
| 180 | + return internalEncoder.setRateAllocation(allocation, frameRate); |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public VideoEncoder.ScalingSettings getScalingSettings() { |
| 185 | + return internalEncoder.getScalingSettings(); |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public String getImplementationName() { |
| 190 | + return internalEncoder.getImplementationName(); |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public long createNativeVideoEncoder() { |
| 195 | + return internalEncoder.createNativeVideoEncoder(); |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public boolean isHardwareEncoder() { |
| 200 | + return internalEncoder.isHardwareEncoder(); |
| 201 | + } |
| 202 | + |
| 203 | + @Override |
| 204 | + public VideoCodecStatus setRates(VideoEncoder.RateControlParameters rcParameters) { |
| 205 | + return internalEncoder.setRates(rcParameters); |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + public VideoEncoder.ResolutionBitrateLimits[] getResolutionBitrateLimits() { |
| 210 | + return internalEncoder.getResolutionBitrateLimits(); |
| 211 | + } |
| 212 | + |
| 213 | + @Override |
| 214 | + public VideoEncoder.EncoderInfo getEncoderInfo() { |
| 215 | + return internalEncoder.getEncoderInfo(); |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | + |
0 commit comments