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
3 // found in the LICENSE file.
5 #include "media/cast/logging/logging_stats.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
13 LoggingStats::LoggingStats()
19 LoggingStats::~LoggingStats() {}
21 void LoggingStats::Reset() {
23 packet_stats_
.clear();
24 generic_stats_
.clear();
27 void LoggingStats::InsertFrameEvent(const base::TimeTicks
& time_of_event
,
28 CastLoggingEvent event
,
31 InsertBaseFrameEvent(time_of_event
, event
, frame_id
, rtp_timestamp
);
34 void LoggingStats::InsertFrameEventWithSize(
35 const base::TimeTicks
& time_of_event
,
36 CastLoggingEvent event
,
40 InsertBaseFrameEvent(time_of_event
, event
, frame_id
, rtp_timestamp
);
42 FrameStatsMap::iterator it
= frame_stats_
.find(event
);
43 DCHECK(it
!= frame_stats_
.end());
44 it
->second
.sum_size
+= frame_size
;
47 void LoggingStats::InsertFrameEventWithDelay(
48 const base::TimeTicks
& time_of_event
,
49 CastLoggingEvent event
,
52 base::TimeDelta delay
) {
53 InsertBaseFrameEvent(time_of_event
, event
, frame_id
, rtp_timestamp
);
55 FrameStatsMap::iterator it
= frame_stats_
.find(event
);
56 DCHECK(it
!= frame_stats_
.end());
57 it
->second
.sum_delay
+= delay
;
58 if (delay
> it
->second
.max_delay
|| it
->second
.event_counter
== 1)
59 it
->second
.max_delay
= delay
;
60 if (delay
< it
->second
.min_delay
|| it
->second
.event_counter
== 1)
61 it
->second
.min_delay
= delay
;
64 void LoggingStats::InsertBaseFrameEvent(const base::TimeTicks
& time_of_event
,
65 CastLoggingEvent event
,
67 uint32 rtp_timestamp
) {
68 // Does this belong to an existing event?
69 FrameStatsMap::iterator it
= frame_stats_
.find(event
);
70 if (it
== frame_stats_
.end()) {
73 stats
.first_event_time
= time_of_event
;
74 stats
.last_event_time
= time_of_event
;
75 stats
.event_counter
= 1;
76 frame_stats_
.insert(std::make_pair(event
, stats
));
78 it
->second
.last_event_time
= time_of_event
;
79 ++(it
->second
.event_counter
);
83 void LoggingStats::InsertPacketEvent(const base::TimeTicks
& time_of_event
,
84 CastLoggingEvent event
,
90 // Does this packet belong to an existing event?
91 PacketStatsMap::iterator it
= packet_stats_
.find(event
);
92 if (it
== packet_stats_
.end()) {
95 stats
.first_event_time
= time_of_event
;
96 stats
.last_event_time
= time_of_event
;
97 stats
.sum_size
= size
;
98 stats
.event_counter
= 1;
99 packet_stats_
.insert(std::make_pair(event
, stats
));
101 // Add to an existing event.
102 it
->second
.sum_size
+= size
;
103 ++(it
->second
.event_counter
);
107 void LoggingStats::InsertGenericEvent(const base::TimeTicks
& time_of_event
,
108 CastLoggingEvent event
, int value
) {
109 // Does this event belong to an existing event?
110 GenericStatsMap::iterator it
= generic_stats_
.find(event
);
111 if (it
== generic_stats_
.end()) {
113 GenericLogStats stats
;
114 stats
.first_event_time
= time_of_event
;
115 stats
.last_event_time
= time_of_event
;
117 stats
.sum_squared
= value
* value
;
120 stats
.event_counter
= 1;
121 generic_stats_
.insert(std::make_pair(event
, stats
));
123 // Add to existing event.
124 it
->second
.sum
+= value
;
125 it
->second
.sum_squared
+= value
* value
;
126 ++(it
->second
.event_counter
);
127 it
->second
.last_event_time
= time_of_event
;
128 if (it
->second
.min
> value
) {
129 it
->second
.min
= value
;
130 } else if (it
->second
.max
< value
) {
131 it
->second
.max
= value
;
136 FrameStatsMap
LoggingStats::GetFrameStatsData() const {
140 PacketStatsMap
LoggingStats::GetPacketStatsData() const {
141 return packet_stats_
;
144 GenericStatsMap
LoggingStats::GetGenericStatsData() const {
145 return generic_stats_
;