OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_VIDEO_ENCODER_H_ | 11 #ifndef WEBRTC_VIDEO_ENCODER_H_ |
12 #define WEBRTC_VIDEO_ENCODER_H_ | 12 #define WEBRTC_VIDEO_ENCODER_H_ |
13 | 13 |
14 #include <string> | 14 #include <string> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
| 17 #include "webrtc/base/rollingaccumulator.h" |
17 #include "webrtc/common_types.h" | 18 #include "webrtc/common_types.h" |
18 #include "webrtc/typedefs.h" | 19 #include "webrtc/typedefs.h" |
19 #include "webrtc/video_frame.h" | 20 #include "webrtc/video_frame.h" |
20 | 21 |
21 namespace webrtc { | 22 namespace webrtc { |
22 | 23 |
23 class RTPFragmentationHeader; | 24 class RTPFragmentationHeader; |
24 // TODO(pbos): Expose these through a public (root) header or change these APIs. | 25 // TODO(pbos): Expose these through a public (root) header or change these APIs. |
25 struct CodecSpecificInfo; | 26 struct CodecSpecificInfo; |
26 struct VideoCodec; | 27 struct VideoCodec; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 // - bitrate : New target bit rate | 119 // - bitrate : New target bit rate |
119 // - framerate : The target frame rate | 120 // - framerate : The target frame rate |
120 // | 121 // |
121 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise. | 122 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise. |
122 virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate) = 0; | 123 virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate) = 0; |
123 | 124 |
124 virtual int32_t SetPeriodicKeyFrames(bool enable) { return -1; } | 125 virtual int32_t SetPeriodicKeyFrames(bool enable) { return -1; } |
125 virtual void OnDroppedFrame() {} | 126 virtual void OnDroppedFrame() {} |
126 virtual int GetTargetFramerate() { return -1; } | 127 virtual int GetTargetFramerate() { return -1; } |
127 virtual bool SupportsNativeHandle() const { return false; } | 128 virtual bool SupportsNativeHandle() const { return false; } |
| 129 virtual bool IsHardwareEncoder() const { return false; } |
128 virtual const char* ImplementationName() const { return "unknown"; } | 130 virtual const char* ImplementationName() const { return "unknown"; } |
129 }; | 131 }; |
130 | 132 |
131 // Class used to wrap external VideoEncoders to provide a fallback option on | 133 // Class used to wrap external VideoEncoders to provide a fallback option on |
132 // software encoding when a hardware encoder fails to encode a stream due to | 134 // software encoding when a hardware encoder fails to encode a stream due to |
133 // hardware restrictions, such as max resolution. | 135 // hardware restrictions, such as max resolution. |
134 class VideoEncoderSoftwareFallbackWrapper : public VideoEncoder { | 136 class VideoEncoderSoftwareFallbackWrapper : public VideoEncoder { |
135 public: | 137 public: |
136 VideoEncoderSoftwareFallbackWrapper(VideoCodecType codec_type, | 138 VideoEncoderSoftwareFallbackWrapper(VideoCodecType codec_type, |
137 webrtc::VideoEncoder* encoder); | 139 webrtc::VideoEncoder* encoder); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 uint32_t packet_loss_; | 176 uint32_t packet_loss_; |
175 int64_t rtt_; | 177 int64_t rtt_; |
176 | 178 |
177 const EncoderType encoder_type_; | 179 const EncoderType encoder_type_; |
178 webrtc::VideoEncoder* const encoder_; | 180 webrtc::VideoEncoder* const encoder_; |
179 | 181 |
180 rtc::scoped_ptr<webrtc::VideoEncoder> fallback_encoder_; | 182 rtc::scoped_ptr<webrtc::VideoEncoder> fallback_encoder_; |
181 std::string fallback_implementation_name_; | 183 std::string fallback_implementation_name_; |
182 EncodedImageCallback* callback_; | 184 EncodedImageCallback* callback_; |
183 }; | 185 }; |
| 186 |
| 187 // Class used to wrap hardware encoders that don't properly respect their set |
| 188 // bitrates. This class will observe input vs output bitrate, and dynamically |
| 189 // adjust the bitrate setting on the wrapped encoder so that its output matches |
| 190 // the rate we need. |
| 191 class HardwareVideoEncoderWrapper : public VideoEncoder { |
| 192 public: |
| 193 HardwareVideoEncoderWrapper(webrtc::VideoEncoder* encoder); |
| 194 virtual ~HardwareVideoEncoderWrapper() {} |
| 195 |
| 196 int32_t InitEncode(const VideoCodec* codec_settings, |
| 197 int32_t number_of_cores, |
| 198 size_t max_payload_size) override; |
| 199 |
| 200 int32_t RegisterEncodeCompleteCallback( |
| 201 EncodedImageCallback* callback) override; |
| 202 |
| 203 int32_t Release() override; |
| 204 int32_t Encode(const VideoFrame& frame, |
| 205 const CodecSpecificInfo* codec_specific_info, |
| 206 const std::vector<FrameType>* frame_types) override; |
| 207 int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) override; |
| 208 |
| 209 int32_t SetRates(uint32_t bitrate, uint32_t framerate) override; |
| 210 void OnDroppedFrame() override; |
| 211 int GetTargetFramerate() override; |
| 212 bool SupportsNativeHandle() const override; |
| 213 bool IsHardwareEncoder() const override; |
| 214 const char* ImplementationName() const override; |
| 215 |
| 216 // Called when wrapped encoder reports an encoded frame. |
| 217 int32_t OnFrameEncoded(const EncodedImage& encoded_image, |
| 218 const CodecSpecificInfo* codec_specific_info, |
| 219 const RTPFragmentationHeader* fragmentation); |
| 220 |
| 221 private: |
| 222 static const uint32_t kBitrateUpdateIntervalMs = 2000; // 2s. |
| 223 static const uint32_t kBitrateTolerancePct = 5; // 5 percent of original. |
| 224 |
| 225 // Updates the bitrate set on the wrapped encoder with relevant penalties. |
| 226 void UpdateBitrate(uint32_t current_time_ms); |
| 227 // Updates the bitrate tracker with a new sample. |
| 228 void UpdateBitrateTracker(size_t frame_size, uint32_t current_time_ms); |
| 229 |
| 230 // Wrapped encoder. |
| 231 webrtc::VideoEncoder* const encoder_; |
| 232 // The callback registered with this wrapper. |
| 233 EncodedImageCallback* callback_; |
| 234 // The callback registered with wrapped encoder. |
| 235 rtc::scoped_ptr<EncodedImageCallback> wrapped_callback_; |
| 236 // Simple moving average bitrate estimator. |
| 237 rtc::RollingAccumulator<float> bitrate_tracker_; |
| 238 |
| 239 // The bitrate set in SetRates. |
| 240 uint32_t original_bitrate_; |
| 241 // The framerate set in SetRates. |
| 242 uint32_t original_frame_rate_; |
| 243 // The bitrate used to call SetRates on the wrapped encoder. |
| 244 uint32_t adjusted_bitrate_; |
| 245 // The last time we update the bitrate. |
| 246 uint32_t last_bitrate_update_time_ms_; |
| 247 // The timestamp of the last frame received from the wrapped encoder. |
| 248 uint32_t last_frame_time_ms_; |
| 249 }; |
| 250 |
184 } // namespace webrtc | 251 } // namespace webrtc |
185 #endif // WEBRTC_VIDEO_ENCODER_H_ | 252 #endif // WEBRTC_VIDEO_ENCODER_H_ |
OLD | NEW |