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