blob: 8dec2f5448159bc335165ba77243f2191f884182 [file] [log] [blame]
magjed614d5b72016-11-15 06:30:54 -08001/*
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
ilnikd60d06a2017-04-05 03:02:20 -070018#include "webrtc/api/video_codecs/video_encoder.h"
magjed509e4fe2016-11-18 01:34:11 -080019#include "webrtc/media/base/codec.h"
magjed614d5b72016-11-15 06:30:54 -080020
21namespace 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.
26class VideoEncoderSoftwareFallbackWrapper : public VideoEncoder {
27 public:
magjedf52d34d2017-08-29 00:58:52 -070028 VideoEncoderSoftwareFallbackWrapper(
29 const cricket::VideoCodec& codec,
30 std::unique_ptr<webrtc::VideoEncoder> encoder);
magjed614d5b72016-11-15 06:30:54 -080031
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ång08127a92016-11-16 16:41:30 +010044 int32_t SetRateAllocation(const BitrateAllocation& bitrate_allocation,
45 uint32_t framerate) override;
magjed614d5b72016-11-15 06:30:54 -080046 bool SupportsNativeHandle() const override;
kthelgason876222f2016-11-29 01:44:11 -080047 ScalingSettings GetScalingSettings() const override;
kthelgason535dbd32017-01-26 00:36:31 -080048 const char *ImplementationName() const override;
magjed614d5b72016-11-15 06:30:54 -080049
50 private:
51 bool InitFallbackEncoder();
52
asapersson22c76c42017-08-16 00:53:59 -070053 // 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
asapersson142fcc92017-08-17 08:58:54 -070056 // 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.
asapersson22c76c42017-08-16 00:53:59 -070060 class ForcedFallbackParams {
61 public:
62 bool ShouldStart(uint32_t bitrate_kbps, const VideoCodec& codec);
asapersson142fcc92017-08-17 08:58:54 -070063 bool ShouldStop(uint32_t bitrate_kbps, const VideoCodec& codec) const;
asapersson22c76c42017-08-16 00:53:59 -070064 void Reset() { start_ms.reset(); }
65 bool IsValid(const VideoCodec& codec) const {
asapersson142fcc92017-08-17 08:58:54 -070066 return codec.width * codec.height <= kMaxPixelsStart;
asapersson22c76c42017-08-16 00:53:59 -070067 }
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;
asapersson142fcc92017-08-17 08:58:54 -070072 const int kMaxPixelsStart = 320 * 240;
73 const int kMinPixelsStop = 320 * 180;
asapersson22c76c42017-08-16 00:53:59 -070074 };
75
76 bool RequestForcedFallback();
77 bool TryReleaseForcedFallbackEncoder();
78 bool TryReInitForcedFallbackEncoder();
79 void ValidateSettingsForForcedFallback();
80 bool IsForcedFallbackActive() const;
emircan82fac892017-08-23 14:19:50 -070081 void MaybeModifyCodecForFallback();
asapersson22c76c42017-08-16 00:53:59 -070082
magjed614d5b72016-11-15 06:30:54 -080083 // 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ång08127a92016-11-16 16:41:30 +010091 BitrateAllocation bitrate_allocation_;
magjed614d5b72016-11-15 06:30:54 -080092 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
emircan82fac892017-08-23 14:19:50 -070099 cricket::VideoCodec codec_;
magjedf52d34d2017-08-29 00:58:52 -0700100 std::unique_ptr<webrtc::VideoEncoder> encoder_;
magjed614d5b72016-11-15 06:30:54 -0800101
102 std::unique_ptr<webrtc::VideoEncoder> fallback_encoder_;
103 std::string fallback_implementation_name_;
104 EncodedImageCallback* callback_;
asapersson22c76c42017-08-16 00:53:59 -0700105
106 bool forced_fallback_possible_;
107 ForcedFallbackParams forced_fallback_;
magjed614d5b72016-11-15 06:30:54 -0800108};
109
110} // namespace webrtc
111
112#endif // WEBRTC_MEDIA_ENGINE_VIDEOENCODERSOFTWAREFALLBACKWRAPPER_H_