blob: 8061c008c65a7fd6f18f873e9738ee3ee12ea268 [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:
magjed509e4fe2016-11-18 01:34:11 -080028 VideoEncoderSoftwareFallbackWrapper(const cricket::VideoCodec& codec,
magjed614d5b72016-11-15 06:30:54 -080029 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ång08127a92016-11-16 16:41:30 +010043 int32_t SetRateAllocation(const BitrateAllocation& bitrate_allocation,
44 uint32_t framerate) override;
magjed614d5b72016-11-15 06:30:54 -080045 bool SupportsNativeHandle() const override;
kthelgason876222f2016-11-29 01:44:11 -080046 ScalingSettings GetScalingSettings() const override;
kthelgason535dbd32017-01-26 00:36:31 -080047 const char *ImplementationName() const override;
magjed614d5b72016-11-15 06:30:54 -080048
49 private:
50 bool InitFallbackEncoder();
51
asapersson22c76c42017-08-16 00:53:59 -070052 // 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
asapersson142fcc92017-08-17 08:58:54 -070055 // 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.
asapersson22c76c42017-08-16 00:53:59 -070059 class ForcedFallbackParams {
60 public:
61 bool ShouldStart(uint32_t bitrate_kbps, const VideoCodec& codec);
asapersson142fcc92017-08-17 08:58:54 -070062 bool ShouldStop(uint32_t bitrate_kbps, const VideoCodec& codec) const;
asapersson22c76c42017-08-16 00:53:59 -070063 void Reset() { start_ms.reset(); }
64 bool IsValid(const VideoCodec& codec) const {
asapersson142fcc92017-08-17 08:58:54 -070065 return codec.width * codec.height <= kMaxPixelsStart;
asapersson22c76c42017-08-16 00:53:59 -070066 }
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;
asapersson142fcc92017-08-17 08:58:54 -070071 const int kMaxPixelsStart = 320 * 240;
72 const int kMinPixelsStop = 320 * 180;
asapersson22c76c42017-08-16 00:53:59 -070073 };
74
75 bool RequestForcedFallback();
76 bool TryReleaseForcedFallbackEncoder();
77 bool TryReInitForcedFallbackEncoder();
78 void ValidateSettingsForForcedFallback();
79 bool IsForcedFallbackActive() const;
emircan82fac892017-08-23 14:19:50 -070080 void MaybeModifyCodecForFallback();
asapersson22c76c42017-08-16 00:53:59 -070081
magjed614d5b72016-11-15 06:30:54 -080082 // Settings used in the last InitEncode call and used if a dynamic fallback to
83 // software is required.
84 VideoCodec codec_settings_;
85 int32_t number_of_cores_;
86 size_t max_payload_size_;
87
88 // The last bitrate/framerate set, and a flag for noting they are set.
89 bool rates_set_;
Erik Språng08127a92016-11-16 16:41:30 +010090 BitrateAllocation bitrate_allocation_;
magjed614d5b72016-11-15 06:30:54 -080091 uint32_t framerate_;
92
93 // The last channel parameters set, and a flag for noting they are set.
94 bool channel_parameters_set_;
95 uint32_t packet_loss_;
96 int64_t rtt_;
97
emircan82fac892017-08-23 14:19:50 -070098 cricket::VideoCodec codec_;
magjed614d5b72016-11-15 06:30:54 -080099 webrtc::VideoEncoder* const encoder_;
100
101 std::unique_ptr<webrtc::VideoEncoder> fallback_encoder_;
102 std::string fallback_implementation_name_;
103 EncodedImageCallback* callback_;
asapersson22c76c42017-08-16 00:53:59 -0700104
105 bool forced_fallback_possible_;
106 ForcedFallbackParams forced_fallback_;
magjed614d5b72016-11-15 06:30:54 -0800107};
108
109} // namespace webrtc
110
111#endif // WEBRTC_MEDIA_ENGINE_VIDEOENCODERSOFTWAREFALLBACKWRAPPER_H_