OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 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_SEND_DELAY_STATS_H_ | 11 #ifndef WEBRTC_MODULES_VIDEO_CODING_PROTECTION_BITRATE_CALCULATOR_H_ |
12 #define WEBRTC_VIDEO_SEND_DELAY_STATS_H_ | 12 #define WEBRTC_MODULES_VIDEO_CODING_PROTECTION_BITRATE_CALCULATOR_H_ |
13 | 13 |
14 #include <map> | 14 #include <list> |
15 #include <memory> | 15 #include <memory> |
16 #include <set> | |
17 | 16 |
18 #include "webrtc/base/criticalsection.h" | 17 #include "webrtc/base/criticalsection.h" |
19 #include "webrtc/base/thread_annotations.h" | |
20 #include "webrtc/common_types.h" | |
21 #include "webrtc/modules/include/module_common_types.h" | 18 #include "webrtc/modules/include/module_common_types.h" |
| 19 #include "webrtc/modules/video_coding/include/video_coding.h" |
| 20 #include "webrtc/modules/video_coding/media_opt_util.h" |
22 #include "webrtc/system_wrappers/include/clock.h" | 21 #include "webrtc/system_wrappers/include/clock.h" |
23 #include "webrtc/video_send_stream.h" | |
24 | 22 |
25 namespace webrtc { | 23 namespace webrtc { |
26 | 24 |
27 class SendDelayStats : public SendPacketObserver { | 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 { |
28 public: | 38 public: |
29 explicit SendDelayStats(Clock* clock); | 39 ProtectionBitrateCalculator(Clock* clock, |
30 virtual ~SendDelayStats(); | 40 VCMProtectionCallback* protection_callback); |
| 41 ~ProtectionBitrateCalculator(); |
31 | 42 |
32 // Adds the configured ssrcs for the rtp streams. | 43 void SetProtectionMethod(bool enable_fec, bool enable_nack); |
33 // Stats will be calculated for these streams. | |
34 void AddSsrcs(const VideoSendStream::Config& config); | |
35 | 44 |
36 // Called when a packet is sent (leaving socket). | 45 // Informs media optimization of initial encoding state. |
37 bool OnSentPacket(int packet_id, int64_t time_ms); | 46 void SetEncodingData(uint32_t estimated_bitrate_bps, |
| 47 uint16_t width, |
| 48 uint16_t height, |
| 49 uint32_t frame_rate, |
| 50 size_t num_temporal_layers, |
| 51 size_t max_payload_size); |
38 | 52 |
39 protected: | 53 // Returns target rate for the encoder given the channel parameters. |
40 // From SendPacketObserver. | 54 // Inputs: estimated_bitrate_bps - the estimated network bitrate in bits/s. |
41 // Called when a packet is sent to the transport. | 55 // actual_framerate - encoder frame rate. |
42 void OnSendPacket(uint16_t packet_id, | 56 // fraction_lost - packet loss rate in % in the network. |
43 int64_t capture_time_ms, | 57 // round_trip_time_ms - round trip time in milliseconds. |
44 uint32_t ssrc) override; | 58 uint32_t SetTargetRates(uint32_t estimated_bitrate_bps, |
| 59 int actual_framerate, |
| 60 uint8_t fraction_lost, |
| 61 int64_t round_trip_time_ms); |
| 62 |
| 63 // Informs of encoded output. |
| 64 void UpdateWithEncodedData(const EncodedImage& encoded_image); |
45 | 65 |
46 private: | 66 private: |
47 // Map holding sent packets (mapped by sequence number). | 67 struct EncodedFrameSample; |
48 struct SequenceNumberOlderThan { | 68 enum { kBitrateAverageWinMs = 1000 }; |
49 bool operator()(uint16_t seq1, uint16_t seq2) const { | |
50 return IsNewerSequenceNumber(seq2, seq1); | |
51 } | |
52 }; | |
53 struct Packet { | |
54 Packet(uint32_t ssrc, int64_t capture_time_ms, int64_t send_time_ms) | |
55 : ssrc(ssrc), | |
56 capture_time_ms(capture_time_ms), | |
57 send_time_ms(send_time_ms) {} | |
58 uint32_t ssrc; | |
59 int64_t capture_time_ms; | |
60 int64_t send_time_ms; | |
61 }; | |
62 typedef std::map<uint16_t, Packet, SequenceNumberOlderThan> PacketMap; | |
63 | |
64 class SampleCounter { | |
65 public: | |
66 SampleCounter() : sum(0), num_samples(0) {} | |
67 ~SampleCounter() {} | |
68 void Add(int sample); | |
69 int Avg(int min_required_samples) const; | |
70 | |
71 private: | |
72 int sum; | |
73 int num_samples; | |
74 }; | |
75 | |
76 void UpdateHistograms(); | |
77 void RemoveOld(int64_t now, PacketMap* packets) | |
78 EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
79 | 69 |
80 Clock* const clock_; | 70 Clock* const clock_; |
81 rtc::CriticalSection crit_; | 71 VCMProtectionCallback* const protection_callback_; |
82 | 72 |
83 PacketMap packets_ GUARDED_BY(crit_); | 73 rtc::CriticalSection crit_sect_; |
84 size_t num_old_packets_ GUARDED_BY(crit_); | 74 std::unique_ptr<media_optimization::VCMLossProtectionLogic> loss_prot_logic_ |
85 size_t num_skipped_packets_ GUARDED_BY(crit_); | 75 GUARDED_BY(crit_sect_); |
| 76 size_t max_payload_size_ GUARDED_BY(crit_sect_); |
86 | 77 |
87 std::set<uint32_t> ssrcs_ GUARDED_BY(crit_); | 78 RTC_DISALLOW_COPY_AND_ASSIGN(ProtectionBitrateCalculator); |
88 std::map<uint32_t, SampleCounter> send_delay_counters_ | |
89 GUARDED_BY(crit_); // Mapped by SSRC. | |
90 }; | 79 }; |
91 | 80 |
92 } // namespace webrtc | 81 } // namespace webrtc |
93 #endif // WEBRTC_VIDEO_SEND_DELAY_STATS_H_ | 82 #endif // WEBRTC_MODULES_VIDEO_CODING_PROTECTION_BITRATE_CALCULATOR_H_ |
OLD | NEW |