blob: ea7e13d353ba3846ae669b35e413f617bda8f78c [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
55 // than |kMaxPixels|.
56 // If the bitrate is above |high_kbps|, the forced fallback is requested to
57 // immediately be stopped.
58 class ForcedFallbackParams {
59 public:
60 bool ShouldStart(uint32_t bitrate_kbps, const VideoCodec& codec);
61 bool ShouldStop(uint32_t bitrate_kbps) const;
62 void Reset() { start_ms.reset(); }
63 bool IsValid(const VideoCodec& codec) const {
64 return codec.width * codec.height <= kMaxPixels;
65 }
66 rtc::Optional<int64_t> start_ms; // Set when bitrate is below |low_kbps|.
67 uint32_t low_kbps = 100;
68 uint32_t high_kbps = 150;
69 int64_t min_low_ms = 10000;
70 const int kMaxPixels = 320 * 240;
71 };
72
73 bool RequestForcedFallback();
74 bool TryReleaseForcedFallbackEncoder();
75 bool TryReInitForcedFallbackEncoder();
76 void ValidateSettingsForForcedFallback();
77 bool IsForcedFallbackActive() const;
78
magjed614d5b72016-11-15 06:30:54 -080079 // Settings used in the last InitEncode call and used if a dynamic fallback to
80 // software is required.
81 VideoCodec codec_settings_;
82 int32_t number_of_cores_;
83 size_t max_payload_size_;
84
85 // The last bitrate/framerate set, and a flag for noting they are set.
86 bool rates_set_;
Erik Språng08127a92016-11-16 16:41:30 +010087 BitrateAllocation bitrate_allocation_;
magjed614d5b72016-11-15 06:30:54 -080088 uint32_t framerate_;
89
90 // The last channel parameters set, and a flag for noting they are set.
91 bool channel_parameters_set_;
92 uint32_t packet_loss_;
93 int64_t rtt_;
94
magjed509e4fe2016-11-18 01:34:11 -080095 const cricket::VideoCodec codec_;
magjed614d5b72016-11-15 06:30:54 -080096 webrtc::VideoEncoder* const encoder_;
97
98 std::unique_ptr<webrtc::VideoEncoder> fallback_encoder_;
99 std::string fallback_implementation_name_;
100 EncodedImageCallback* callback_;
asapersson22c76c42017-08-16 00:53:59 -0700101
102 bool forced_fallback_possible_;
103 ForcedFallbackParams forced_fallback_;
magjed614d5b72016-11-15 06:30:54 -0800104};
105
106} // namespace webrtc
107
108#endif // WEBRTC_MEDIA_ENGINE_VIDEOENCODERSOFTWAREFALLBACKWRAPPER_H_