OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 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 #include "webrtc/modules/rtp_rtcp/source/bitrate.h" | 11 #include "webrtc/modules/rtp_rtcp/source/bitrate.h" |
12 | 12 |
13 #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h" | 13 #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h" |
14 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | |
15 | 14 |
16 namespace webrtc { | 15 namespace webrtc { |
17 | 16 |
18 Bitrate::Bitrate(Clock* clock, Observer* observer) | 17 Bitrate::Bitrate(Clock* clock, Observer* observer) |
19 : clock_(clock), | 18 : clock_(clock), |
20 crit_(CriticalSectionWrapper::CreateCriticalSection()), | |
21 packet_rate_(0), | 19 packet_rate_(0), |
22 bitrate_(0), | 20 bitrate_(0), |
23 bitrate_next_idx_(0), | 21 bitrate_next_idx_(0), |
24 time_last_rate_update_(0), | 22 time_last_rate_update_(0), |
25 bytes_count_(0), | 23 bytes_count_(0), |
26 packet_count_(0), | 24 packet_count_(0), |
27 observer_(observer) { | 25 observer_(observer) { |
28 memset(packet_rate_array_, 0, sizeof(packet_rate_array_)); | 26 memset(packet_rate_array_, 0, sizeof(packet_rate_array_)); |
29 memset(bitrate_diff_ms_, 0, sizeof(bitrate_diff_ms_)); | 27 memset(bitrate_diff_ms_, 0, sizeof(bitrate_diff_ms_)); |
30 memset(bitrate_array_, 0, sizeof(bitrate_array_)); | 28 memset(bitrate_array_, 0, sizeof(bitrate_array_)); |
31 } | 29 } |
32 | 30 |
33 Bitrate::~Bitrate() {} | 31 Bitrate::~Bitrate() {} |
34 | 32 |
35 void Bitrate::Update(const size_t bytes) { | 33 void Bitrate::Update(const size_t bytes) { |
36 CriticalSectionScoped cs(crit_.get()); | 34 rtc::CritScope cs(&crit_); |
37 bytes_count_ += bytes; | 35 bytes_count_ += bytes; |
38 packet_count_++; | 36 packet_count_++; |
39 } | 37 } |
40 | 38 |
41 uint32_t Bitrate::PacketRate() const { | 39 uint32_t Bitrate::PacketRate() const { |
42 CriticalSectionScoped cs(crit_.get()); | 40 rtc::CritScope cs(&crit_); |
43 return packet_rate_; | 41 return packet_rate_; |
44 } | 42 } |
45 | 43 |
46 uint32_t Bitrate::BitrateLast() const { | 44 uint32_t Bitrate::BitrateLast() const { |
47 CriticalSectionScoped cs(crit_.get()); | 45 rtc::CritScope cs(&crit_); |
48 return bitrate_; | 46 return bitrate_; |
49 } | 47 } |
50 | 48 |
51 uint32_t Bitrate::BitrateNow() const { | 49 uint32_t Bitrate::BitrateNow() const { |
52 CriticalSectionScoped cs(crit_.get()); | 50 rtc::CritScope cs(&crit_); |
53 int64_t now = clock_->TimeInMilliseconds(); | 51 int64_t now = clock_->TimeInMilliseconds(); |
54 int64_t diff_ms = now - time_last_rate_update_; | 52 int64_t diff_ms = now - time_last_rate_update_; |
55 | 53 |
56 if (diff_ms > 10000) { // 10 seconds. | 54 if (diff_ms > 10000) { // 10 seconds. |
57 // Too high difference, ignore. | 55 // Too high difference, ignore. |
58 return bitrate_; | 56 return bitrate_; |
59 } | 57 } |
60 int64_t bits_since_last_rate_update = 8 * bytes_count_ * 1000; | 58 int64_t bits_since_last_rate_update = 8 * bytes_count_ * 1000; |
61 | 59 |
62 // We have to consider the time when the measurement was done: | 60 // We have to consider the time when the measurement was done: |
63 // ((bits/sec * sec) + (bits)) / sec. | 61 // ((bits/sec * sec) + (bits)) / sec. |
64 int64_t bitrate = (static_cast<uint64_t>(bitrate_) * 1000 + | 62 int64_t bitrate = (static_cast<uint64_t>(bitrate_) * 1000 + |
65 bits_since_last_rate_update) / (1000 + diff_ms); | 63 bits_since_last_rate_update) / (1000 + diff_ms); |
66 return static_cast<uint32_t>(bitrate); | 64 return static_cast<uint32_t>(bitrate); |
67 } | 65 } |
68 | 66 |
69 int64_t Bitrate::time_last_rate_update() const { | 67 int64_t Bitrate::time_last_rate_update() const { |
70 CriticalSectionScoped cs(crit_.get()); | 68 rtc::CritScope cs(&crit_); |
71 return time_last_rate_update_; | 69 return time_last_rate_update_; |
72 } | 70 } |
73 | 71 |
74 // Triggered by timer. | 72 // Triggered by timer. |
75 void Bitrate::Process() { | 73 void Bitrate::Process() { |
76 BitrateStatistics stats; | 74 BitrateStatistics stats; |
77 { | 75 { |
78 CriticalSectionScoped cs(crit_.get()); | 76 rtc::CritScope cs(&crit_); |
79 int64_t now = clock_->CurrentNtpInMilliseconds(); | 77 int64_t now = clock_->CurrentNtpInMilliseconds(); |
80 int64_t diff_ms = now - time_last_rate_update_; | 78 int64_t diff_ms = now - time_last_rate_update_; |
81 | 79 |
82 if (diff_ms < 100) { | 80 if (diff_ms < 100) { |
83 // Not enough data, wait... | 81 // Not enough data, wait... |
84 return; | 82 return; |
85 } | 83 } |
86 if (diff_ms > 10000) { // 10 seconds. | 84 if (diff_ms > 10000) { // 10 seconds. |
87 // Too high difference, ignore. | 85 // Too high difference, ignore. |
88 time_last_rate_update_ = now; | 86 time_last_rate_update_ = now; |
(...skipping 25 matching lines...) Expand all Loading... |
114 stats.bitrate_bps = bitrate_; | 112 stats.bitrate_bps = bitrate_; |
115 stats.packet_rate = packet_rate_; | 113 stats.packet_rate = packet_rate_; |
116 stats.timestamp_ms = now; | 114 stats.timestamp_ms = now; |
117 } | 115 } |
118 | 116 |
119 if (observer_) | 117 if (observer_) |
120 observer_->BitrateUpdated(stats); | 118 observer_->BitrateUpdated(stats); |
121 } | 119 } |
122 | 120 |
123 } // namespace webrtc | 121 } // namespace webrtc |
OLD | NEW |