Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 1 | /* |
| 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_MODULES_VIDEO_CODING_PROTECTION_BITRATE_CALCULATOR_H_ |
| 12 | #define WEBRTC_MODULES_VIDEO_CODING_PROTECTION_BITRATE_CALCULATOR_H_ |
| 13 | |
| 14 | #include <list> |
| 15 | #include <memory> |
| 16 | |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 17 | #include "webrtc/modules/include/module_common_types.h" |
| 18 | #include "webrtc/modules/video_coding/include/video_coding.h" |
| 19 | #include "webrtc/modules/video_coding/media_opt_util.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 20 | #include "webrtc/rtc_base/criticalsection.h" |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 21 | #include "webrtc/system_wrappers/include/clock.h" |
| 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | // ProtectionBitrateCalculator calculates how much of the allocated network |
| 26 | // capacity that can be used by an encoder and how much that |
| 27 | // is needed for redundant packets such as FEC and NACK. It uses an |
| 28 | // implementation of |VCMProtectionCallback| to set new FEC parameters and get |
| 29 | // the bitrate currently used for FEC and NACK. |
| 30 | // Usage: |
| 31 | // Setup by calling SetProtectionMethod and SetEncodingData. |
| 32 | // For each encoded image, call UpdateWithEncodedData. |
| 33 | // Each time the bandwidth estimate change, call SetTargetRates. SetTargetRates |
| 34 | // will return the bitrate that can be used by an encoder. |
| 35 | // A lock is used to protect internal states, so methods can be called on an |
| 36 | // arbitrary thread. |
| 37 | class ProtectionBitrateCalculator { |
| 38 | public: |
| 39 | ProtectionBitrateCalculator(Clock* clock, |
| 40 | VCMProtectionCallback* protection_callback); |
| 41 | ~ProtectionBitrateCalculator(); |
| 42 | |
| 43 | void SetProtectionMethod(bool enable_fec, bool enable_nack); |
| 44 | |
| 45 | // Informs media optimization of initial encoding state. |
perkj | c2c24f7 | 2016-07-11 01:47:32 -0700 | [diff] [blame] | 46 | void SetEncodingData(size_t width, |
| 47 | size_t height, |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 48 | size_t num_temporal_layers, |
| 49 | size_t max_payload_size); |
| 50 | |
| 51 | // Returns target rate for the encoder given the channel parameters. |
| 52 | // Inputs: estimated_bitrate_bps - the estimated network bitrate in bits/s. |
| 53 | // actual_framerate - encoder frame rate. |
| 54 | // fraction_lost - packet loss rate in % in the network. |
| 55 | // round_trip_time_ms - round trip time in milliseconds. |
| 56 | uint32_t SetTargetRates(uint32_t estimated_bitrate_bps, |
| 57 | int actual_framerate, |
| 58 | uint8_t fraction_lost, |
| 59 | int64_t round_trip_time_ms); |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 60 | // Informs of encoded output. |
| 61 | void UpdateWithEncodedData(const EncodedImage& encoded_image); |
| 62 | |
| 63 | private: |
| 64 | struct EncodedFrameSample; |
| 65 | enum { kBitrateAverageWinMs = 1000 }; |
| 66 | |
| 67 | Clock* const clock_; |
| 68 | VCMProtectionCallback* const protection_callback_; |
| 69 | |
| 70 | rtc::CriticalSection crit_sect_; |
| 71 | std::unique_ptr<media_optimization::VCMLossProtectionLogic> loss_prot_logic_ |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 72 | RTC_GUARDED_BY(crit_sect_); |
| 73 | size_t max_payload_size_ RTC_GUARDED_BY(crit_sect_); |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 74 | |
| 75 | RTC_DISALLOW_COPY_AND_ASSIGN(ProtectionBitrateCalculator); |
| 76 | }; |
| 77 | |
| 78 | } // namespace webrtc |
| 79 | #endif // WEBRTC_MODULES_VIDEO_CODING_PROTECTION_BITRATE_CALCULATOR_H_ |