magjed | 614d5b7 | 2016-11-15 06:30:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 | #ifndef WEBRTC_MEDIA_ENGINE_VIDEOENCODERSOFTWAREFALLBACKWRAPPER_H_ |
| 12 | #define WEBRTC_MEDIA_ENGINE_VIDEOENCODERSOFTWAREFALLBACKWRAPPER_H_ |
| 13 | |
| 14 | #include <memory> |
| 15 | #include <string> |
| 16 | #include <vector> |
| 17 | |
ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 18 | #include "webrtc/api/video_codecs/video_encoder.h" |
magjed | 509e4fe | 2016-11-18 01:34:11 -0800 | [diff] [blame] | 19 | #include "webrtc/media/base/codec.h" |
magjed | 614d5b7 | 2016-11-15 06:30:54 -0800 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | // Class used to wrap external VideoEncoders to provide a fallback option on |
| 24 | // software encoding when a hardware encoder fails to encode a stream due to |
| 25 | // hardware restrictions, such as max resolution. |
| 26 | class VideoEncoderSoftwareFallbackWrapper : public VideoEncoder { |
| 27 | public: |
magjed | f52d34d | 2017-08-29 00:58:52 -0700 | [diff] [blame^] | 28 | VideoEncoderSoftwareFallbackWrapper( |
| 29 | const cricket::VideoCodec& codec, |
| 30 | std::unique_ptr<webrtc::VideoEncoder> encoder); |
magjed | 614d5b7 | 2016-11-15 06:30:54 -0800 | [diff] [blame] | 31 | |
| 32 | int32_t InitEncode(const VideoCodec* codec_settings, |
| 33 | int32_t number_of_cores, |
| 34 | size_t max_payload_size) override; |
| 35 | |
| 36 | int32_t RegisterEncodeCompleteCallback( |
| 37 | EncodedImageCallback* callback) override; |
| 38 | |
| 39 | int32_t Release() override; |
| 40 | int32_t Encode(const VideoFrame& frame, |
| 41 | const CodecSpecificInfo* codec_specific_info, |
| 42 | const std::vector<FrameType>* frame_types) override; |
| 43 | int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) override; |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 44 | int32_t SetRateAllocation(const BitrateAllocation& bitrate_allocation, |
| 45 | uint32_t framerate) override; |
magjed | 614d5b7 | 2016-11-15 06:30:54 -0800 | [diff] [blame] | 46 | bool SupportsNativeHandle() const override; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 47 | ScalingSettings GetScalingSettings() const override; |
kthelgason | 535dbd3 | 2017-01-26 00:36:31 -0800 | [diff] [blame] | 48 | const char *ImplementationName() const override; |
magjed | 614d5b7 | 2016-11-15 06:30:54 -0800 | [diff] [blame] | 49 | |
| 50 | private: |
| 51 | bool InitFallbackEncoder(); |
| 52 | |
asapersson | 22c76c4 | 2017-08-16 00:53:59 -0700 | [diff] [blame] | 53 | // If |forced_fallback_possible_| is true: |
| 54 | // The forced fallback is requested if the target bitrate is below |low_kbps| |
| 55 | // for more than |min_low_ms| and the input video resolution is not larger |
asapersson | 142fcc9 | 2017-08-17 08:58:54 -0700 | [diff] [blame] | 56 | // than |kMaxPixelsStart|. |
| 57 | // If the bitrate is above |high_kbps| and the resolution is not smaller than |
| 58 | // |kMinPixelsStop|, the forced fallback is requested to immediately be |
| 59 | // stopped. |
asapersson | 22c76c4 | 2017-08-16 00:53:59 -0700 | [diff] [blame] | 60 | class ForcedFallbackParams { |
| 61 | public: |
| 62 | bool ShouldStart(uint32_t bitrate_kbps, const VideoCodec& codec); |
asapersson | 142fcc9 | 2017-08-17 08:58:54 -0700 | [diff] [blame] | 63 | bool ShouldStop(uint32_t bitrate_kbps, const VideoCodec& codec) const; |
asapersson | 22c76c4 | 2017-08-16 00:53:59 -0700 | [diff] [blame] | 64 | void Reset() { start_ms.reset(); } |
| 65 | bool IsValid(const VideoCodec& codec) const { |
asapersson | 142fcc9 | 2017-08-17 08:58:54 -0700 | [diff] [blame] | 66 | return codec.width * codec.height <= kMaxPixelsStart; |
asapersson | 22c76c4 | 2017-08-16 00:53:59 -0700 | [diff] [blame] | 67 | } |
| 68 | rtc::Optional<int64_t> start_ms; // Set when bitrate is below |low_kbps|. |
| 69 | uint32_t low_kbps = 100; |
| 70 | uint32_t high_kbps = 150; |
| 71 | int64_t min_low_ms = 10000; |
asapersson | 142fcc9 | 2017-08-17 08:58:54 -0700 | [diff] [blame] | 72 | const int kMaxPixelsStart = 320 * 240; |
| 73 | const int kMinPixelsStop = 320 * 180; |
asapersson | 22c76c4 | 2017-08-16 00:53:59 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | bool RequestForcedFallback(); |
| 77 | bool TryReleaseForcedFallbackEncoder(); |
| 78 | bool TryReInitForcedFallbackEncoder(); |
| 79 | void ValidateSettingsForForcedFallback(); |
| 80 | bool IsForcedFallbackActive() const; |
emircan | 82fac89 | 2017-08-23 14:19:50 -0700 | [diff] [blame] | 81 | void MaybeModifyCodecForFallback(); |
asapersson | 22c76c4 | 2017-08-16 00:53:59 -0700 | [diff] [blame] | 82 | |
magjed | 614d5b7 | 2016-11-15 06:30:54 -0800 | [diff] [blame] | 83 | // Settings used in the last InitEncode call and used if a dynamic fallback to |
| 84 | // software is required. |
| 85 | VideoCodec codec_settings_; |
| 86 | int32_t number_of_cores_; |
| 87 | size_t max_payload_size_; |
| 88 | |
| 89 | // The last bitrate/framerate set, and a flag for noting they are set. |
| 90 | bool rates_set_; |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 91 | BitrateAllocation bitrate_allocation_; |
magjed | 614d5b7 | 2016-11-15 06:30:54 -0800 | [diff] [blame] | 92 | uint32_t framerate_; |
| 93 | |
| 94 | // The last channel parameters set, and a flag for noting they are set. |
| 95 | bool channel_parameters_set_; |
| 96 | uint32_t packet_loss_; |
| 97 | int64_t rtt_; |
| 98 | |
emircan | 82fac89 | 2017-08-23 14:19:50 -0700 | [diff] [blame] | 99 | cricket::VideoCodec codec_; |
magjed | f52d34d | 2017-08-29 00:58:52 -0700 | [diff] [blame^] | 100 | std::unique_ptr<webrtc::VideoEncoder> encoder_; |
magjed | 614d5b7 | 2016-11-15 06:30:54 -0800 | [diff] [blame] | 101 | |
| 102 | std::unique_ptr<webrtc::VideoEncoder> fallback_encoder_; |
| 103 | std::string fallback_implementation_name_; |
| 104 | EncodedImageCallback* callback_; |
asapersson | 22c76c4 | 2017-08-16 00:53:59 -0700 | [diff] [blame] | 105 | |
| 106 | bool forced_fallback_possible_; |
| 107 | ForcedFallbackParams forced_fallback_; |
magjed | 614d5b7 | 2016-11-15 06:30:54 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | } // namespace webrtc |
| 111 | |
| 112 | #endif // WEBRTC_MEDIA_ENGINE_VIDEOENCODERSOFTWAREFALLBACKWRAPPER_H_ |