blob: a9a349c221fd83c4cfd8aa35df0a279639fbdbd9 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MEDIA_ENGINE_VIDEOENCODERSOFTWAREFALLBACKWRAPPER_H_
12#define MEDIA_ENGINE_VIDEOENCODERSOFTWAREFALLBACKWRAPPER_H_
magjed614d5b72016-11-15 06:30:54 -080013
14#include <memory>
15#include <string>
16#include <vector>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/video_codecs/video_encoder.h"
19#include "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(
Magnus Jedvertee92d622017-11-13 15:26:17 +010029 std::unique_ptr<webrtc::VideoEncoder> sw_encoder,
30 std::unique_ptr<webrtc::VideoEncoder> hw_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:
Åsa Persson45bbc8a2017-11-13 10:16:47 +010054 // The forced fallback is requested if the resolution is less than or equal to
55 // |max_pixels_|. The resolution is allowed to be scaled down to
56 // |min_pixels_|.
asapersson22c76c42017-08-16 00:53:59 -070057 class ForcedFallbackParams {
58 public:
asapersson22c76c42017-08-16 00:53:59 -070059 bool IsValid(const VideoCodec& codec) const {
Åsa Persson45bbc8a2017-11-13 10:16:47 +010060 return codec.width * codec.height <= max_pixels_;
asapersson22c76c42017-08-16 00:53:59 -070061 }
Åsa Persson45bbc8a2017-11-13 10:16:47 +010062
63 bool active_ = false;
64 int min_pixels_ = 320 * 180;
65 int max_pixels_ = 320 * 240;
asapersson22c76c42017-08-16 00:53:59 -070066 };
67
Åsa Persson45bbc8a2017-11-13 10:16:47 +010068 bool TryInitForcedFallbackEncoder();
asapersson22c76c42017-08-16 00:53:59 -070069 bool TryReInitForcedFallbackEncoder();
70 void ValidateSettingsForForcedFallback();
71 bool IsForcedFallbackActive() const;
emircan82fac892017-08-23 14:19:50 -070072 void MaybeModifyCodecForFallback();
asapersson22c76c42017-08-16 00:53:59 -070073
magjed614d5b72016-11-15 06:30:54 -080074 // Settings used in the last InitEncode call and used if a dynamic fallback to
75 // software is required.
76 VideoCodec codec_settings_;
77 int32_t number_of_cores_;
78 size_t max_payload_size_;
79
80 // The last bitrate/framerate set, and a flag for noting they are set.
81 bool rates_set_;
Erik Språng08127a92016-11-16 16:41:30 +010082 BitrateAllocation bitrate_allocation_;
magjed614d5b72016-11-15 06:30:54 -080083 uint32_t framerate_;
84
85 // The last channel parameters set, and a flag for noting they are set.
86 bool channel_parameters_set_;
87 uint32_t packet_loss_;
88 int64_t rtt_;
89
Magnus Jedvertee92d622017-11-13 15:26:17 +010090 bool use_fallback_encoder_;
91 const std::unique_ptr<webrtc::VideoEncoder> encoder_;
magjed614d5b72016-11-15 06:30:54 -080092
Magnus Jedvertee92d622017-11-13 15:26:17 +010093 const std::unique_ptr<webrtc::VideoEncoder> fallback_encoder_;
magjed614d5b72016-11-15 06:30:54 -080094 EncodedImageCallback* callback_;
asapersson22c76c42017-08-16 00:53:59 -070095
96 bool forced_fallback_possible_;
97 ForcedFallbackParams forced_fallback_;
magjed614d5b72016-11-15 06:30:54 -080098};
99
100} // namespace webrtc
101
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200102#endif // MEDIA_ENGINE_VIDEOENCODERSOFTWAREFALLBACKWRAPPER_H_