| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "media/cast/logging/logging_stats.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/values.h" | |
| 10 | |
| 11 namespace media { | |
| 12 namespace cast { | |
| 13 | |
| 14 LoggingStats::LoggingStats() | |
| 15 : frame_stats_(), | |
| 16 packet_stats_(), | |
| 17 generic_stats_() { | |
| 18 } | |
| 19 | |
| 20 LoggingStats::~LoggingStats() {} | |
| 21 | |
| 22 void LoggingStats::Reset() { | |
| 23 frame_stats_.clear(); | |
| 24 packet_stats_.clear(); | |
| 25 generic_stats_.clear(); | |
| 26 } | |
| 27 | |
| 28 void LoggingStats::InsertFrameEvent(const base::TimeTicks& time_of_event, | |
| 29 CastLoggingEvent event, | |
| 30 uint32 rtp_timestamp, | |
| 31 uint32 frame_id) { | |
| 32 InsertBaseFrameEvent(time_of_event, event, frame_id, rtp_timestamp); | |
| 33 } | |
| 34 | |
| 35 void LoggingStats::InsertFrameEventWithSize( | |
| 36 const base::TimeTicks& time_of_event, | |
| 37 CastLoggingEvent event, | |
| 38 uint32 rtp_timestamp, | |
| 39 uint32 frame_id, | |
| 40 int frame_size) { | |
| 41 InsertBaseFrameEvent(time_of_event, event, frame_id, rtp_timestamp); | |
| 42 // Update size. | |
| 43 FrameStatsMap::iterator it = frame_stats_.find(event); | |
| 44 DCHECK(it != frame_stats_.end()); | |
| 45 it->second.sum_size += frame_size; | |
| 46 } | |
| 47 | |
| 48 void LoggingStats::InsertFrameEventWithDelay( | |
| 49 const base::TimeTicks& time_of_event, | |
| 50 CastLoggingEvent event, | |
| 51 uint32 rtp_timestamp, | |
| 52 uint32 frame_id, | |
| 53 base::TimeDelta delay) { | |
| 54 InsertBaseFrameEvent(time_of_event, event, frame_id, rtp_timestamp); | |
| 55 // Update size. | |
| 56 FrameStatsMap::iterator it = frame_stats_.find(event); | |
| 57 DCHECK(it != frame_stats_.end()); | |
| 58 it->second.sum_delay += delay; | |
| 59 if (delay > it->second.max_delay || it->second.event_counter == 1) | |
| 60 it->second.max_delay = delay; | |
| 61 if (delay < it->second.min_delay || it->second.event_counter == 1) | |
| 62 it->second.min_delay = delay; | |
| 63 } | |
| 64 | |
| 65 void LoggingStats::InsertBaseFrameEvent(const base::TimeTicks& time_of_event, | |
| 66 CastLoggingEvent event, | |
| 67 uint32 frame_id, | |
| 68 uint32 rtp_timestamp) { | |
| 69 // Does this belong to an existing event? | |
| 70 FrameStatsMap::iterator it = frame_stats_.find(event); | |
| 71 if (it == frame_stats_.end()) { | |
| 72 // New event. | |
| 73 FrameLogStats stats; | |
| 74 stats.first_event_time = time_of_event; | |
| 75 stats.last_event_time = time_of_event; | |
| 76 stats.event_counter = 1; | |
| 77 frame_stats_.insert(std::make_pair(event, stats)); | |
| 78 } else { | |
| 79 ++(it->second.event_counter); | |
| 80 it->second.last_event_time = time_of_event; | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 void LoggingStats::InsertPacketEvent(const base::TimeTicks& time_of_event, | |
| 85 CastLoggingEvent event, | |
| 86 uint32 rtp_timestamp, | |
| 87 uint32 frame_id, | |
| 88 uint16 packet_id, | |
| 89 uint16 max_packet_id, | |
| 90 size_t size) { | |
| 91 // Does this packet belong to an existing event? | |
| 92 PacketStatsMap::iterator it = packet_stats_.find(event); | |
| 93 if (it == packet_stats_.end()) { | |
| 94 // New event. | |
| 95 PacketLogStats stats; | |
| 96 stats.first_event_time = time_of_event; | |
| 97 stats.last_event_time = time_of_event; | |
| 98 stats.sum_size = size; | |
| 99 stats.event_counter = 1; | |
| 100 packet_stats_.insert(std::make_pair(event, stats)); | |
| 101 } else { | |
| 102 // Add to an existing event. | |
| 103 it->second.sum_size += size; | |
| 104 ++(it->second.event_counter); | |
| 105 it->second.last_event_time = time_of_event; | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 void LoggingStats::InsertGenericEvent(const base::TimeTicks& time_of_event, | |
| 110 CastLoggingEvent event, int value) { | |
| 111 // Does this event belong to an existing event? | |
| 112 GenericStatsMap::iterator it = generic_stats_.find(event); | |
| 113 if (it == generic_stats_.end()) { | |
| 114 // New event. | |
| 115 GenericLogStats stats; | |
| 116 stats.first_event_time = time_of_event; | |
| 117 stats.last_event_time = time_of_event; | |
| 118 stats.sum = value; | |
| 119 stats.sum_squared = value * value; | |
| 120 stats.min = value; | |
| 121 stats.max = value; | |
| 122 stats.event_counter = 1; | |
| 123 generic_stats_.insert(std::make_pair(event, stats)); | |
| 124 } else { | |
| 125 // Add to existing event. | |
| 126 it->second.sum += value; | |
| 127 it->second.sum_squared += value * value; | |
| 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 } | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 FrameStatsMap LoggingStats::GetFrameStatsData(EventMediaType media_type) const { | |
| 139 DCHECK(media_type == AUDIO_EVENT || media_type == VIDEO_EVENT); | |
| 140 | |
| 141 FrameStatsMap frame_map_to_return; | |
| 142 for (FrameStatsMap::const_iterator it = frame_stats_.begin(); | |
| 143 it != frame_stats_.end(); | |
| 144 ++it) { | |
| 145 if (GetEventMediaType(it->first) == media_type) { | |
| 146 frame_map_to_return.insert(std::make_pair(it->first, it->second)); | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 return frame_map_to_return; | |
| 151 } | |
| 152 | |
| 153 PacketStatsMap LoggingStats::GetPacketStatsData( | |
| 154 EventMediaType media_type) const { | |
| 155 DCHECK(media_type == AUDIO_EVENT || media_type == VIDEO_EVENT); | |
| 156 | |
| 157 PacketStatsMap packet_map_to_return; | |
| 158 for (PacketStatsMap::const_iterator it = packet_stats_.begin(); | |
| 159 it != packet_stats_.end(); | |
| 160 ++it) { | |
| 161 if (GetEventMediaType(it->first) == media_type) { | |
| 162 packet_map_to_return.insert(std::make_pair(it->first, it->second)); | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 return packet_map_to_return; | |
| 167 } | |
| 168 | |
| 169 GenericStatsMap LoggingStats::GetGenericStatsData() const { | |
| 170 return generic_stats_; | |
| 171 } | |
| 172 | |
| 173 scoped_ptr<base::DictionaryValue> ConvertStats( | |
| 174 const FrameStatsMap& frame_stats_map, | |
| 175 const PacketStatsMap& packet_stats_map) { | |
| 176 scoped_ptr<base::DictionaryValue> overall_stats(new base::DictionaryValue); | |
| 177 | |
| 178 scoped_ptr<base::DictionaryValue> overall_frame_stats( | |
| 179 new base::DictionaryValue); | |
| 180 for (FrameStatsMap::const_iterator it = frame_stats_map.begin(); | |
| 181 it != frame_stats_map.end(); | |
| 182 ++it) { | |
| 183 scoped_ptr<base::DictionaryValue> frame_stats(new base::DictionaryValue); | |
| 184 | |
| 185 frame_stats->SetDouble("firstEventTime", | |
| 186 it->second.first_event_time.ToInternalValue()); | |
| 187 frame_stats->SetDouble("lastEventTime", | |
| 188 it->second.last_event_time.ToInternalValue()); | |
| 189 frame_stats->SetInteger("count", it->second.event_counter); | |
| 190 frame_stats->SetInteger("sizeTotal", | |
| 191 static_cast<int>(it->second.sum_size)); | |
| 192 frame_stats->SetInteger("minDelayMs", | |
| 193 it->second.min_delay.InMilliseconds()); | |
| 194 frame_stats->SetInteger("maxDelayMs", | |
| 195 it->second.max_delay.InMilliseconds()); | |
| 196 frame_stats->SetInteger("sumDelayMs", | |
| 197 it->second.sum_delay.InMilliseconds()); | |
| 198 | |
| 199 overall_frame_stats->Set(CastLoggingToString(it->first), | |
| 200 frame_stats.release()); | |
| 201 } | |
| 202 | |
| 203 overall_stats->Set("frameStats", overall_frame_stats.release()); | |
| 204 | |
| 205 scoped_ptr<base::DictionaryValue> overall_packet_stats( | |
| 206 new base::DictionaryValue); | |
| 207 for (PacketStatsMap::const_iterator it = packet_stats_map.begin(); | |
| 208 it != packet_stats_map.end(); | |
| 209 ++it) { | |
| 210 scoped_ptr<base::DictionaryValue> packet_stats(new base::DictionaryValue); | |
| 211 | |
| 212 packet_stats->SetDouble("firstEventTime", | |
| 213 it->second.first_event_time.ToInternalValue()); | |
| 214 packet_stats->SetDouble("lastEventTime", | |
| 215 it->second.last_event_time.ToInternalValue()); | |
| 216 packet_stats->SetDouble("lastEventTime", | |
| 217 it->second.last_event_time.ToInternalValue()); | |
| 218 packet_stats->SetInteger("count", it->second.event_counter); | |
| 219 packet_stats->SetInteger("sizeTotal", | |
| 220 static_cast<int>(it->second.sum_size)); | |
| 221 | |
| 222 overall_packet_stats->Set(CastLoggingToString(it->first), | |
| 223 packet_stats.release()); | |
| 224 } | |
| 225 | |
| 226 overall_stats->Set("packetStats", overall_packet_stats.release()); | |
| 227 | |
| 228 return overall_stats.Pass(); | |
| 229 } | |
| 230 | |
| 231 } // namespace cast | |
| 232 } // namespace media | |
| OLD | NEW |