OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/linked_ptr.h" | 5 #include "media/cast/logging/logging_stats.h" |
6 | |
7 #include <math.h> | |
8 | |
9 #include "base/logging.h" | |
6 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
7 #include "media/cast/logging/logging_stats.h" | |
8 | 11 |
9 namespace media { | 12 namespace media { |
10 namespace cast { | 13 namespace cast { |
11 | 14 |
12 LoggingStats::LoggingStats() | 15 LoggingStats::LoggingStats() |
13 : frame_stats_(), | 16 : frame_stats_(), |
14 packet_stats_(), | 17 packet_stats_(), |
15 generic_stats_(), | 18 generic_stats_() { |
16 start_time_() { | |
17 memset(counts_, 0, sizeof(counts_)); | |
18 memset(start_time_, 0, sizeof(start_time_)); | |
19 } | 19 } |
20 | 20 |
21 LoggingStats::~LoggingStats() {} | 21 LoggingStats::~LoggingStats() {} |
22 | 22 |
23 void LoggingStats::Reset() { | 23 void LoggingStats::Reset() { |
24 frame_stats_.clear(); | 24 frame_stats_.clear(); |
25 packet_stats_.clear(); | 25 packet_stats_.clear(); |
26 generic_stats_.clear(); | 26 generic_stats_.clear(); |
27 memset(counts_, 0, sizeof(counts_)); | |
28 } | 27 } |
29 | 28 |
30 void LoggingStats::InsertFrameEvent(const base::TimeTicks& time_of_event, | 29 void LoggingStats::InsertFrameEvent(const base::TimeTicks& time_of_event, |
31 CastLoggingEvent event, | 30 CastLoggingEvent event, |
32 uint32 rtp_timestamp, | 31 uint32 rtp_timestamp, |
33 uint32 frame_id) { | 32 uint32 frame_id) { |
34 InsertBaseFrameEvent(time_of_event, event, frame_id, rtp_timestamp); | 33 InsertBaseFrameEvent(time_of_event, event, frame_id, rtp_timestamp); |
35 } | 34 } |
36 | 35 |
37 void LoggingStats::InsertFrameEventWithSize( | 36 void LoggingStats::InsertFrameEventWithSize( |
38 const base::TimeTicks& time_of_event, | 37 const base::TimeTicks& time_of_event, |
39 CastLoggingEvent event, | 38 CastLoggingEvent event, |
40 uint32 rtp_timestamp, | 39 uint32 rtp_timestamp, |
41 uint32 frame_id, | 40 uint32 frame_id, |
42 int frame_size) { | 41 int frame_size) { |
43 InsertBaseFrameEvent(time_of_event, event, frame_id, rtp_timestamp); | 42 InsertBaseFrameEvent(time_of_event, event, frame_id, rtp_timestamp); |
44 // Update size. | 43 // Update size. |
45 FrameStatsMap::iterator it = frame_stats_.find(event); | 44 FrameStatsMap::iterator it = frame_stats_.find(event); |
46 DCHECK(it != frame_stats_.end()); | 45 DCHECK(it != frame_stats_.end()); |
47 it->second->bitrate_kbps += frame_size; | 46 it->second.sum_size += frame_size; |
48 } | 47 } |
49 | 48 |
50 void LoggingStats::InsertFrameEventWithDelay( | 49 void LoggingStats::InsertFrameEventWithDelay( |
51 const base::TimeTicks& time_of_event, | 50 const base::TimeTicks& time_of_event, |
52 CastLoggingEvent event, | 51 CastLoggingEvent event, |
53 uint32 rtp_timestamp, | 52 uint32 rtp_timestamp, |
54 uint32 frame_id, | 53 uint32 frame_id, |
55 base::TimeDelta delay) { | 54 base::TimeDelta delay) { |
56 InsertBaseFrameEvent(time_of_event, event, frame_id, rtp_timestamp); | 55 InsertBaseFrameEvent(time_of_event, event, frame_id, rtp_timestamp); |
57 // Update size. | 56 // Update size. |
58 FrameStatsMap::iterator it = frame_stats_.find(event); | 57 FrameStatsMap::iterator it = frame_stats_.find(event); |
59 DCHECK(it != frame_stats_.end()); | 58 DCHECK(it != frame_stats_.end()); |
60 // Using the average delay as a counter, will divide by the counter when | 59 it->second.sum_delay += delay; |
61 // triggered. | 60 if (delay > it->second.max_delay || it->second.event_counter == 1) |
62 it->second->avg_delay_ms += delay.InMilliseconds(); | 61 it->second.max_delay = delay; |
63 if (delay.InMilliseconds() > it->second->max_delay_ms) | 62 if (delay < it->second.min_delay || it->second.event_counter == 1) |
64 it->second->max_delay_ms = delay.InMilliseconds(); | 63 it->second.min_delay = delay; |
65 if ((delay.InMilliseconds() < it->second->min_delay_ms) || | |
66 (counts_[event] == 1) ) | |
67 it->second->min_delay_ms = delay.InMilliseconds(); | |
68 } | 64 } |
69 | 65 |
70 void LoggingStats::InsertBaseFrameEvent(const base::TimeTicks& time_of_event, | 66 void LoggingStats::InsertBaseFrameEvent(const base::TimeTicks& time_of_event, |
71 CastLoggingEvent event, | 67 CastLoggingEvent event, |
72 uint32 frame_id, | 68 uint32 frame_id, |
73 uint32 rtp_timestamp) { | 69 uint32 rtp_timestamp) { |
74 // Does this belong to an existing event? | 70 // Does this belong to an existing event? |
75 FrameStatsMap::iterator it = frame_stats_.find(event); | 71 FrameStatsMap::iterator it = frame_stats_.find(event); |
76 if (it == frame_stats_.end()) { | 72 if (it == frame_stats_.end()) { |
77 // New event. | 73 // New event. |
78 start_time_[event] = time_of_event; | 74 FrameLogStats stats; |
79 linked_ptr<FrameLogStats> stats(new FrameLogStats()); | 75 stats.first_event_time = time_of_event; |
76 stats.last_event_time = time_of_event; | |
77 stats.event_counter = 1; | |
80 frame_stats_.insert(std::make_pair(event, stats)); | 78 frame_stats_.insert(std::make_pair(event, stats)); |
79 } else { | |
80 it->second.last_event_time = time_of_event; | |
81 ++(it->second.event_counter); | |
81 } | 82 } |
82 | |
83 ++counts_[event]; | |
84 } | 83 } |
85 | 84 |
86 void LoggingStats::InsertPacketEvent(const base::TimeTicks& time_of_event, | 85 void LoggingStats::InsertPacketEvent(const base::TimeTicks& time_of_event, |
87 CastLoggingEvent event, | 86 CastLoggingEvent event, |
88 uint32 rtp_timestamp, | 87 uint32 rtp_timestamp, |
89 uint32 frame_id, | 88 uint32 frame_id, |
90 uint16 packet_id, | 89 uint16 packet_id, |
91 uint16 max_packet_id, | 90 uint16 max_packet_id, |
92 size_t size) { | 91 size_t size) { |
93 // Does this packet belong to an existing event? | 92 // Does this packet belong to an existing event? |
94 PacketStatsMap::iterator it = packet_stats_.find(event); | 93 PacketStatsMap::iterator it = packet_stats_.find(event); |
95 if (it == packet_stats_.end()) { | 94 if (it == packet_stats_.end()) { |
96 // New event. | 95 // New event. |
97 start_time_[event] = time_of_event; | 96 PacketLogStats stats; |
98 packet_stats_.insert(std::make_pair(event, size)); | 97 stats.first_event_time = time_of_event; |
98 stats.last_event_time = time_of_event; | |
99 stats.sum_size = size; | |
100 stats.event_counter = 1; | |
101 packet_stats_.insert(std::make_pair(event, stats)); | |
99 } else { | 102 } else { |
100 // Add to existing. | 103 // Add to an existing event. |
101 it->second += size; | 104 it->second.sum_size += size; |
105 ++(it->second.event_counter); | |
102 } | 106 } |
103 ++counts_[event]; | |
104 } | 107 } |
105 | 108 |
106 void LoggingStats::InsertGenericEvent(const base::TimeTicks& time_of_event, | 109 void LoggingStats::InsertGenericEvent(const base::TimeTicks& time_of_event, |
107 CastLoggingEvent event, int value) { | 110 CastLoggingEvent event, int value) { |
108 // Does this event belong to an existing event? | 111 // Does this event belong to an existing event? |
109 GenericStatsMap::iterator it = generic_stats_.find(event); | 112 GenericStatsMap::iterator it = generic_stats_.find(event); |
110 if (it == generic_stats_.end()) { | 113 if (it == generic_stats_.end()) { |
111 // New event. | 114 // New event. |
112 start_time_[event] = time_of_event; | 115 GenericLogStats stats; |
113 generic_stats_.insert(std::make_pair(event, value)); | 116 stats.first_event_time = time_of_event; |
117 stats.last_event_time = time_of_event; | |
118 stats.sum = value; | |
119 stats.sum_squared = pow(value, 2); | |
imcheng
2014/01/23 21:17:18
nit: Why not just value * value?
mikhal1
2014/01/23 23:02:17
Done.
mikhal1
2014/01/23 23:02:17
Done.
| |
120 stats.min = value; | |
121 stats.max = value; | |
122 stats.event_counter = 1; | |
123 generic_stats_.insert(std::make_pair(event, stats)); | |
114 } else { | 124 } else { |
115 // Add to existing (will be used to compute average). | 125 // Add to existing event. |
116 it->second += value; | 126 it->second.sum += value; |
127 it->second.sum_squared += pow(value, 2); | |
128 ++(it->second.event_counter); | |
129 it->second.last_event_time = time_of_event; | |
130 if (it->second.min > value) { | |
131 it->second.min = value; | |
132 } else if (it->second.max < value) { | |
133 it->second.max = value; | |
134 } | |
117 } | 135 } |
118 ++counts_[event]; | |
119 } | 136 } |
120 | 137 |
121 const FrameStatsMap* LoggingStats::GetFrameStatsData( | 138 FrameStatsMap LoggingStats::GetFrameStatsData() const { |
122 const base::TimeTicks& now) { | 139 return frame_stats_; |
123 // Compute framerate and bitrate (when available). | |
124 FrameStatsMap::iterator it; | |
125 for (it = frame_stats_.begin(); it != frame_stats_.end(); ++it) { | |
126 base::TimeDelta time_diff = now - start_time_[it->first]; | |
127 it->second->framerate_fps = counts_[it->first] / time_diff.InSecondsF(); | |
128 if (it->second->bitrate_kbps > 0) { | |
129 it->second->bitrate_kbps = (8 / 1000) * it->second->bitrate_kbps / | |
130 time_diff.InSecondsF(); | |
131 } | |
132 if (it->second->avg_delay_ms > 0) | |
133 it->second->avg_delay_ms /= counts_[it->first]; | |
134 } | |
135 return &frame_stats_; | |
136 } | 140 } |
137 | 141 |
138 const PacketStatsMap* LoggingStats::GetPacketStatsData( | 142 PacketStatsMap LoggingStats::GetPacketStatsData() const { |
139 const base::TimeTicks& now) { | 143 return packet_stats_; |
140 PacketStatsMap::iterator it; | |
141 for (it = packet_stats_.begin(); it != packet_stats_.end(); ++it) { | |
142 if (counts_[it->first] == 0) continue; | |
143 base::TimeDelta time_diff = now - start_time_[it->first]; | |
144 it->second = (8 / 1000) * it->second / time_diff.InSecondsF(); | |
145 } | |
146 return &packet_stats_; | |
147 } | 144 } |
148 | 145 |
149 const GenericStatsMap* LoggingStats::GetGenericStatsData() { | 146 GenericStatsMap LoggingStats::GetGenericStatsData() const { |
150 // Compute averages. | 147 return generic_stats_; |
151 GenericStatsMap::iterator it; | |
152 for (it = generic_stats_.begin(); it != generic_stats_.end(); ++it) { | |
153 it->second /= counts_[ it->first]; | |
154 } | |
155 return &generic_stats_; | |
156 } | 148 } |
157 | 149 |
158 } // namespace cast | 150 } // namespace cast |
159 } // namespace media | 151 } // namespace media |
OLD | NEW |