1 // Copyright 2014 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.
7 #include "media/cast/logging/stats_event_subscriber.h"
9 #include "base/format_macros.h"
10 #include "base/logging.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/values.h"
14 #define STAT_ENUM_TO_STRING(enum) \
23 using media::cast::CastLoggingEvent
;
24 using media::cast::EventMediaType
;
26 const size_t kMaxPacketEventTimeMapSize
= 1000;
28 bool IsReceiverEvent(CastLoggingEvent event
) {
29 return event
== FRAME_DECODED
30 || event
== FRAME_PLAYOUT
31 || event
== FRAME_ACK_SENT
32 || event
== PACKET_RECEIVED
;
37 StatsEventSubscriber::SimpleHistogram::SimpleHistogram(int64 min
,
40 : min_(min
), max_(max
), width_(width
), buckets_((max
- min
) / width
+ 2) {
41 CHECK_GT(buckets_
.size(), 2u);
42 CHECK_EQ(0, (max_
- min_
) % width_
);
45 StatsEventSubscriber::SimpleHistogram::~SimpleHistogram() {
48 void StatsEventSubscriber::SimpleHistogram::Add(int64 sample
) {
51 } else if (sample
>= max_
) {
54 size_t index
= 1 + (sample
- min_
) / width_
;
55 DCHECK_LT(index
, buckets_
.size());
60 void StatsEventSubscriber::SimpleHistogram::Reset() {
61 buckets_
.assign(buckets_
.size(), 0);
64 scoped_ptr
<base::ListValue
>
65 StatsEventSubscriber::SimpleHistogram::GetHistogram() const {
66 scoped_ptr
<base::ListValue
> histo(new base::ListValue
);
68 scoped_ptr
<base::DictionaryValue
> bucket(new base::DictionaryValue
);
70 if (buckets_
.front()) {
71 bucket
->SetInteger(base::StringPrintf("<%" PRId64
, min_
),
73 histo
->Append(bucket
.release());
76 for (size_t i
= 1; i
< buckets_
.size() - 1; i
++) {
79 bucket
.reset(new base::DictionaryValue
);
80 int64 lower
= min_
+ (i
- 1) * width_
;
81 int64 upper
= lower
+ width_
- 1;
83 base::StringPrintf("%" PRId64
"-%" PRId64
, lower
, upper
),
85 histo
->Append(bucket
.release());
88 if (buckets_
.back()) {
89 bucket
.reset(new base::DictionaryValue
);
90 bucket
->SetInteger(base::StringPrintf(">=%" PRId64
, max_
),
92 histo
->Append(bucket
.release());
97 StatsEventSubscriber::StatsEventSubscriber(
98 EventMediaType event_media_type
,
99 base::TickClock
* clock
,
100 ReceiverTimeOffsetEstimator
* offset_estimator
)
101 : event_media_type_(event_media_type
),
103 offset_estimator_(offset_estimator
),
104 capture_latency_datapoints_(0),
105 encode_time_datapoints_(0),
106 queueing_latency_datapoints_(0),
107 network_latency_datapoints_(0),
108 packet_latency_datapoints_(0),
109 frame_latency_datapoints_(0),
110 e2e_latency_datapoints_(0),
111 num_frames_dropped_by_encoder_(0),
113 start_time_(clock_
->NowTicks()) {
114 DCHECK(event_media_type
== AUDIO_EVENT
|| event_media_type
== VIDEO_EVENT
);
119 StatsEventSubscriber::~StatsEventSubscriber() {
120 DCHECK(thread_checker_
.CalledOnValidThread());
123 void StatsEventSubscriber::OnReceiveFrameEvent(const FrameEvent
& frame_event
) {
124 DCHECK(thread_checker_
.CalledOnValidThread());
126 CastLoggingEvent type
= frame_event
.type
;
127 if (frame_event
.media_type
!= event_media_type_
)
130 FrameStatsMap::iterator it
= frame_stats_
.find(type
);
131 if (it
== frame_stats_
.end()) {
133 stats
.event_counter
= 1;
134 stats
.sum_size
= frame_event
.size
;
135 stats
.sum_delay
= frame_event
.delay_delta
;
136 frame_stats_
.insert(std::make_pair(type
, stats
));
138 ++(it
->second
.event_counter
);
139 it
->second
.sum_size
+= frame_event
.size
;
140 it
->second
.sum_delay
+= frame_event
.delay_delta
;
143 bool is_receiver_event
= IsReceiverEvent(type
);
144 UpdateFirstLastEventTime(frame_event
.timestamp
, is_receiver_event
);
146 if (type
== FRAME_CAPTURE_BEGIN
) {
147 RecordFrameCaptureTime(frame_event
);
148 } else if (type
== FRAME_CAPTURE_END
) {
149 RecordCaptureLatency(frame_event
);
150 } else if (type
== FRAME_ENCODED
) {
151 RecordEncodeLatency(frame_event
);
152 } else if (type
== FRAME_ACK_SENT
) {
153 RecordFrameTxLatency(frame_event
);
154 } else if (type
== FRAME_PLAYOUT
) {
155 RecordE2ELatency(frame_event
);
156 base::TimeDelta delay_delta
= frame_event
.delay_delta
;
158 // Positive delay_delta means the frame is late.
159 if (delay_delta
> base::TimeDelta()) {
161 histograms_
[LATE_FRAME_MS_HISTO
]->Add(delay_delta
.InMillisecondsF());
165 if (is_receiver_event
)
166 UpdateLastResponseTime(frame_event
.timestamp
);
169 void StatsEventSubscriber::OnReceivePacketEvent(
170 const PacketEvent
& packet_event
) {
171 DCHECK(thread_checker_
.CalledOnValidThread());
173 CastLoggingEvent type
= packet_event
.type
;
174 if (packet_event
.media_type
!= event_media_type_
)
177 PacketStatsMap::iterator it
= packet_stats_
.find(type
);
178 if (it
== packet_stats_
.end()) {
179 PacketLogStats stats
;
180 stats
.event_counter
= 1;
181 stats
.sum_size
= packet_event
.size
;
182 packet_stats_
.insert(std::make_pair(type
, stats
));
184 ++(it
->second
.event_counter
);
185 it
->second
.sum_size
+= packet_event
.size
;
188 bool is_receiver_event
= IsReceiverEvent(type
);
189 UpdateFirstLastEventTime(packet_event
.timestamp
, is_receiver_event
);
191 if (type
== PACKET_SENT_TO_NETWORK
||
192 type
== PACKET_RECEIVED
) {
193 RecordPacketRelatedLatencies(packet_event
);
194 } else if (type
== PACKET_RETRANSMITTED
) {
195 // We only measure network latency using packets that doesn't have to be
196 // retransmitted as there is precisely one sent-receive timestamp pairs.
197 ErasePacketSentTime(packet_event
);
200 if (is_receiver_event
)
201 UpdateLastResponseTime(packet_event
.timestamp
);
204 void StatsEventSubscriber::UpdateFirstLastEventTime(base::TimeTicks timestamp
,
205 bool is_receiver_event
) {
206 if (is_receiver_event
) {
207 base::TimeDelta receiver_offset
;
208 if (!GetReceiverOffset(&receiver_offset
))
210 timestamp
-= receiver_offset
;
213 if (first_event_time_
.is_null()) {
214 first_event_time_
= timestamp
;
216 first_event_time_
= std::min(first_event_time_
, timestamp
);
218 if (last_event_time_
.is_null()) {
219 last_event_time_
= timestamp
;
221 last_event_time_
= std::max(last_event_time_
, timestamp
);
225 scoped_ptr
<base::DictionaryValue
> StatsEventSubscriber::GetStats() const {
227 GetStatsInternal(&stats_map
);
228 scoped_ptr
<base::DictionaryValue
> ret(new base::DictionaryValue
);
230 scoped_ptr
<base::DictionaryValue
> stats(new base::DictionaryValue
);
231 for (StatsMap::const_iterator it
= stats_map
.begin(); it
!= stats_map
.end();
233 // Round to 3 digits after the decimal point.
234 stats
->SetDouble(CastStatToString(it
->first
),
235 round(it
->second
* 1000.0) / 1000.0);
238 // Populate all histograms.
239 for (HistogramMap::const_iterator it
= histograms_
.begin();
240 it
!= histograms_
.end();
242 stats
->Set(CastStatToString(it
->first
),
243 it
->second
->GetHistogram().release());
246 ret
->Set(event_media_type_
== AUDIO_EVENT
? "audio" : "video",
252 void StatsEventSubscriber::Reset() {
253 DCHECK(thread_checker_
.CalledOnValidThread());
255 frame_stats_
.clear();
256 packet_stats_
.clear();
257 total_capture_latency_
= base::TimeDelta();
258 capture_latency_datapoints_
= 0;
259 total_encode_time_
= base::TimeDelta();
260 encode_time_datapoints_
= 0;
261 total_queueing_latency_
= base::TimeDelta();
262 queueing_latency_datapoints_
= 0;
263 total_network_latency_
= base::TimeDelta();
264 network_latency_datapoints_
= 0;
265 total_packet_latency_
= base::TimeDelta();
266 packet_latency_datapoints_
= 0;
267 total_frame_latency_
= base::TimeDelta();
268 frame_latency_datapoints_
= 0;
269 total_e2e_latency_
= base::TimeDelta();
270 e2e_latency_datapoints_
= 0;
271 num_frames_dropped_by_encoder_
= 0;
272 num_frames_late_
= 0;
273 recent_frame_infos_
.clear();
274 packet_sent_times_
.clear();
275 start_time_
= clock_
->NowTicks();
276 last_response_received_time_
= base::TimeTicks();
277 for (HistogramMap::iterator it
= histograms_
.begin(); it
!= histograms_
.end();
282 first_event_time_
= base::TimeTicks();
283 last_event_time_
= base::TimeTicks();
287 const char* StatsEventSubscriber::CastStatToString(CastStat stat
) {
289 STAT_ENUM_TO_STRING(CAPTURE_FPS
);
290 STAT_ENUM_TO_STRING(ENCODE_FPS
);
291 STAT_ENUM_TO_STRING(DECODE_FPS
);
292 STAT_ENUM_TO_STRING(AVG_CAPTURE_LATENCY_MS
);
293 STAT_ENUM_TO_STRING(AVG_ENCODE_TIME_MS
);
294 STAT_ENUM_TO_STRING(AVG_QUEUEING_LATENCY_MS
);
295 STAT_ENUM_TO_STRING(AVG_NETWORK_LATENCY_MS
);
296 STAT_ENUM_TO_STRING(AVG_PACKET_LATENCY_MS
);
297 STAT_ENUM_TO_STRING(AVG_FRAME_LATENCY_MS
);
298 STAT_ENUM_TO_STRING(AVG_E2E_LATENCY_MS
);
299 STAT_ENUM_TO_STRING(ENCODE_KBPS
);
300 STAT_ENUM_TO_STRING(TRANSMISSION_KBPS
);
301 STAT_ENUM_TO_STRING(RETRANSMISSION_KBPS
);
302 STAT_ENUM_TO_STRING(MS_SINCE_LAST_RECEIVER_RESPONSE
);
303 STAT_ENUM_TO_STRING(NUM_FRAMES_CAPTURED
);
304 STAT_ENUM_TO_STRING(NUM_FRAMES_DROPPED_BY_ENCODER
);
305 STAT_ENUM_TO_STRING(NUM_FRAMES_LATE
);
306 STAT_ENUM_TO_STRING(NUM_PACKETS_SENT
);
307 STAT_ENUM_TO_STRING(NUM_PACKETS_RETRANSMITTED
);
308 STAT_ENUM_TO_STRING(NUM_PACKETS_RECEIVED
);
309 STAT_ENUM_TO_STRING(NUM_PACKETS_RTX_REJECTED
);
310 STAT_ENUM_TO_STRING(FIRST_EVENT_TIME_MS
);
311 STAT_ENUM_TO_STRING(LAST_EVENT_TIME_MS
);
312 STAT_ENUM_TO_STRING(CAPTURE_LATENCY_MS_HISTO
);
313 STAT_ENUM_TO_STRING(ENCODE_TIME_MS_HISTO
);
314 STAT_ENUM_TO_STRING(QUEUEING_LATENCY_MS_HISTO
);
315 STAT_ENUM_TO_STRING(NETWORK_LATENCY_MS_HISTO
);
316 STAT_ENUM_TO_STRING(PACKET_LATENCY_MS_HISTO
);
317 STAT_ENUM_TO_STRING(FRAME_LATENCY_MS_HISTO
);
318 STAT_ENUM_TO_STRING(E2E_LATENCY_MS_HISTO
);
319 STAT_ENUM_TO_STRING(LATE_FRAME_MS_HISTO
);
325 const int kDefaultMaxLatencyBucketMs
= 800;
326 const int kDefaultBucketWidthMs
= 20;
328 // For small latency values.
329 const int kSmallMaxLatencyBucketMs
= 100;
330 const int kSmallBucketWidthMs
= 5;
332 // For large latency values.
333 const int kLargeMaxLatencyBucketMs
= 1200;
334 const int kLargeBucketWidthMs
= 50;
336 void StatsEventSubscriber::InitHistograms() {
337 histograms_
[E2E_LATENCY_MS_HISTO
].reset(
338 new SimpleHistogram(0, kLargeMaxLatencyBucketMs
,
339 kLargeBucketWidthMs
));
340 histograms_
[QUEUEING_LATENCY_MS_HISTO
].reset(
341 new SimpleHistogram(0, kDefaultMaxLatencyBucketMs
,
342 kDefaultBucketWidthMs
));
343 histograms_
[NETWORK_LATENCY_MS_HISTO
].reset(
344 new SimpleHistogram(0, kDefaultMaxLatencyBucketMs
,
345 kDefaultBucketWidthMs
));
346 histograms_
[PACKET_LATENCY_MS_HISTO
].reset(
347 new SimpleHistogram(0, kDefaultMaxLatencyBucketMs
,
348 kDefaultBucketWidthMs
));
349 histograms_
[FRAME_LATENCY_MS_HISTO
].reset(
350 new SimpleHistogram(0, kDefaultMaxLatencyBucketMs
,
351 kDefaultBucketWidthMs
));
352 histograms_
[LATE_FRAME_MS_HISTO
].reset(
353 new SimpleHistogram(0, kDefaultMaxLatencyBucketMs
,
354 kDefaultBucketWidthMs
));
355 histograms_
[CAPTURE_LATENCY_MS_HISTO
].reset(
356 new SimpleHistogram(0, kSmallMaxLatencyBucketMs
,
357 kSmallBucketWidthMs
));
358 histograms_
[ENCODE_TIME_MS_HISTO
].reset(
359 new SimpleHistogram(0, kSmallMaxLatencyBucketMs
,
360 kSmallBucketWidthMs
));
363 void StatsEventSubscriber::GetStatsInternal(StatsMap
* stats_map
) const {
364 DCHECK(thread_checker_
.CalledOnValidThread());
368 base::TimeTicks end_time
= clock_
->NowTicks();
371 end_time
, FRAME_CAPTURE_BEGIN
, CAPTURE_FPS
, stats_map
);
373 end_time
, FRAME_ENCODED
, ENCODE_FPS
, stats_map
);
375 end_time
, FRAME_DECODED
, DECODE_FPS
, stats_map
);
376 PopulateFrameBitrateStat(end_time
, stats_map
);
377 PopulatePacketBitrateStat(end_time
,
378 PACKET_SENT_TO_NETWORK
,
381 PopulatePacketBitrateStat(end_time
,
382 PACKET_RETRANSMITTED
,
385 PopulateFrameCountStat(FRAME_CAPTURE_END
, NUM_FRAMES_CAPTURED
, stats_map
);
386 PopulatePacketCountStat(PACKET_SENT_TO_NETWORK
, NUM_PACKETS_SENT
, stats_map
);
387 PopulatePacketCountStat(
388 PACKET_RETRANSMITTED
, NUM_PACKETS_RETRANSMITTED
, stats_map
);
389 PopulatePacketCountStat(PACKET_RECEIVED
, NUM_PACKETS_RECEIVED
, stats_map
);
390 PopulatePacketCountStat(
391 PACKET_RTX_REJECTED
, NUM_PACKETS_RTX_REJECTED
, stats_map
);
393 if (capture_latency_datapoints_
> 0) {
394 double avg_capture_latency_ms
=
395 total_capture_latency_
.InMillisecondsF() /
396 capture_latency_datapoints_
;
398 std::make_pair(AVG_CAPTURE_LATENCY_MS
, avg_capture_latency_ms
));
401 if (encode_time_datapoints_
> 0) {
402 double avg_encode_time_ms
=
403 total_encode_time_
.InMillisecondsF() /
404 encode_time_datapoints_
;
406 std::make_pair(AVG_ENCODE_TIME_MS
, avg_encode_time_ms
));
409 if (queueing_latency_datapoints_
> 0) {
410 double avg_queueing_latency_ms
=
411 total_queueing_latency_
.InMillisecondsF() /
412 queueing_latency_datapoints_
;
414 std::make_pair(AVG_QUEUEING_LATENCY_MS
, avg_queueing_latency_ms
));
417 if (network_latency_datapoints_
> 0) {
418 double avg_network_latency_ms
=
419 total_network_latency_
.InMillisecondsF() /
420 network_latency_datapoints_
;
422 std::make_pair(AVG_NETWORK_LATENCY_MS
, avg_network_latency_ms
));
425 if (packet_latency_datapoints_
> 0) {
426 double avg_packet_latency_ms
=
427 total_packet_latency_
.InMillisecondsF() /
428 packet_latency_datapoints_
;
430 std::make_pair(AVG_PACKET_LATENCY_MS
, avg_packet_latency_ms
));
433 if (frame_latency_datapoints_
> 0) {
434 double avg_frame_latency_ms
=
435 total_frame_latency_
.InMillisecondsF() / frame_latency_datapoints_
;
437 std::make_pair(AVG_FRAME_LATENCY_MS
, avg_frame_latency_ms
));
440 if (e2e_latency_datapoints_
> 0) {
441 double avg_e2e_latency_ms
=
442 total_e2e_latency_
.InMillisecondsF() / e2e_latency_datapoints_
;
443 stats_map
->insert(std::make_pair(AVG_E2E_LATENCY_MS
, avg_e2e_latency_ms
));
446 if (!last_response_received_time_
.is_null()) {
448 std::make_pair(MS_SINCE_LAST_RECEIVER_RESPONSE
,
449 (end_time
- last_response_received_time_
).InMillisecondsF()));
452 stats_map
->insert(std::make_pair(NUM_FRAMES_DROPPED_BY_ENCODER
,
453 num_frames_dropped_by_encoder_
));
454 stats_map
->insert(std::make_pair(NUM_FRAMES_LATE
, num_frames_late_
));
455 if (!first_event_time_
.is_null()) {
456 stats_map
->insert(std::make_pair(
458 (first_event_time_
- base::TimeTicks::UnixEpoch()).InMillisecondsF()));
460 if (!last_event_time_
.is_null()) {
461 stats_map
->insert(std::make_pair(
463 (last_event_time_
- base::TimeTicks::UnixEpoch()).InMillisecondsF()));
467 StatsEventSubscriber::SimpleHistogram
*
468 StatsEventSubscriber::GetHistogramForTesting(
469 CastStat stats
) const {
470 DCHECK(histograms_
.find(stats
) != histograms_
.end());
471 return histograms_
.find(stats
)->second
.get();
474 bool StatsEventSubscriber::GetReceiverOffset(base::TimeDelta
* offset
) {
475 base::TimeDelta receiver_offset_lower_bound
;
476 base::TimeDelta receiver_offset_upper_bound
;
477 if (!offset_estimator_
->GetReceiverOffsetBounds(
478 &receiver_offset_lower_bound
, &receiver_offset_upper_bound
)) {
482 *offset
= (receiver_offset_lower_bound
+ receiver_offset_upper_bound
) / 2;
486 void StatsEventSubscriber::MaybeInsertFrameInfo(RtpTimestamp rtp_timestamp
,
487 const FrameInfo
& frame_info
) {
488 // No need to insert if |rtp_timestamp| is the smaller than every key in the
489 // map as it is just going to get erased anyway.
490 if (recent_frame_infos_
.size() == kMaxFrameInfoMapSize
&&
491 rtp_timestamp
< recent_frame_infos_
.begin()->first
) {
495 recent_frame_infos_
.insert(std::make_pair(rtp_timestamp
, frame_info
));
497 if (recent_frame_infos_
.size() >= kMaxFrameInfoMapSize
) {
498 FrameInfoMap::iterator erase_it
= recent_frame_infos_
.begin();
499 if (erase_it
->second
.encode_end_time
.is_null())
500 num_frames_dropped_by_encoder_
++;
501 recent_frame_infos_
.erase(erase_it
);
505 void StatsEventSubscriber::RecordFrameCaptureTime(
506 const FrameEvent
& frame_event
) {
507 FrameInfo frame_info
;
508 frame_info
.capture_time
= frame_event
.timestamp
;
509 MaybeInsertFrameInfo(frame_event
.rtp_timestamp
, frame_info
);
512 void StatsEventSubscriber::RecordCaptureLatency(const FrameEvent
& frame_event
) {
513 FrameInfoMap::iterator it
=
514 recent_frame_infos_
.find(frame_event
.rtp_timestamp
);
515 if (it
== recent_frame_infos_
.end()) {
519 if (!it
->second
.capture_time
.is_null()) {
520 base::TimeDelta latency
= frame_event
.timestamp
- it
->second
.capture_time
;
521 total_capture_latency_
+= latency
;
522 capture_latency_datapoints_
++;
523 histograms_
[CAPTURE_LATENCY_MS_HISTO
]->Add(latency
.InMillisecondsF());
526 it
->second
.capture_end_time
= frame_event
.timestamp
;
529 void StatsEventSubscriber::RecordEncodeLatency(const FrameEvent
& frame_event
) {
530 FrameInfoMap::iterator it
=
531 recent_frame_infos_
.find(frame_event
.rtp_timestamp
);
532 if (it
== recent_frame_infos_
.end()) {
533 FrameInfo frame_info
;
534 frame_info
.encode_end_time
= frame_event
.timestamp
;
535 MaybeInsertFrameInfo(frame_event
.rtp_timestamp
, frame_info
);
539 if (!it
->second
.capture_end_time
.is_null()) {
540 base::TimeDelta latency
=
541 frame_event
.timestamp
- it
->second
.capture_end_time
;
542 total_encode_time_
+= latency
;
543 encode_time_datapoints_
++;
544 histograms_
[ENCODE_TIME_MS_HISTO
]->Add(latency
.InMillisecondsF());
547 it
->second
.encode_end_time
= frame_event
.timestamp
;
550 void StatsEventSubscriber::RecordFrameTxLatency(const FrameEvent
& frame_event
) {
551 FrameInfoMap::iterator it
=
552 recent_frame_infos_
.find(frame_event
.rtp_timestamp
);
553 if (it
== recent_frame_infos_
.end())
556 if (it
->second
.encode_end_time
.is_null())
559 base::TimeDelta receiver_offset
;
560 if (!GetReceiverOffset(&receiver_offset
))
563 base::TimeTicks sender_time
= frame_event
.timestamp
- receiver_offset
;
564 base::TimeDelta latency
= sender_time
- it
->second
.encode_end_time
;
565 total_frame_latency_
+= latency
;
566 frame_latency_datapoints_
++;
567 histograms_
[FRAME_LATENCY_MS_HISTO
]->Add(latency
.InMillisecondsF());
570 void StatsEventSubscriber::RecordE2ELatency(const FrameEvent
& frame_event
) {
571 base::TimeDelta receiver_offset
;
572 if (!GetReceiverOffset(&receiver_offset
))
575 FrameInfoMap::iterator it
=
576 recent_frame_infos_
.find(frame_event
.rtp_timestamp
);
577 if (it
== recent_frame_infos_
.end())
580 base::TimeTicks playout_time
= frame_event
.timestamp
- receiver_offset
;
581 base::TimeDelta latency
= playout_time
- it
->second
.capture_time
;
582 total_e2e_latency_
+= latency
;
583 e2e_latency_datapoints_
++;
584 histograms_
[E2E_LATENCY_MS_HISTO
]->Add(latency
.InMillisecondsF());
587 void StatsEventSubscriber::UpdateLastResponseTime(
588 base::TimeTicks receiver_time
) {
589 base::TimeDelta receiver_offset
;
590 if (!GetReceiverOffset(&receiver_offset
))
592 base::TimeTicks sender_time
= receiver_time
- receiver_offset
;
593 last_response_received_time_
= sender_time
;
596 void StatsEventSubscriber::ErasePacketSentTime(
597 const PacketEvent
& packet_event
) {
598 std::pair
<RtpTimestamp
, uint16
> key(
599 std::make_pair(packet_event
.rtp_timestamp
, packet_event
.packet_id
));
600 packet_sent_times_
.erase(key
);
603 void StatsEventSubscriber::RecordPacketRelatedLatencies(
604 const PacketEvent
& packet_event
) {
605 // Log queueing latency.
606 if (packet_event
.type
== PACKET_SENT_TO_NETWORK
) {
607 FrameInfoMap::iterator it
=
608 recent_frame_infos_
.find(packet_event
.rtp_timestamp
);
609 if (it
!= recent_frame_infos_
.end()) {
610 base::TimeDelta latency
=
611 packet_event
.timestamp
- it
->second
.encode_end_time
;
612 total_queueing_latency_
+= latency
;
613 queueing_latency_datapoints_
++;
614 histograms_
[QUEUEING_LATENCY_MS_HISTO
]->Add(
615 latency
.InMillisecondsF());
619 // Log network latency and total packet latency;
620 base::TimeDelta receiver_offset
;
621 if (!GetReceiverOffset(&receiver_offset
))
624 std::pair
<RtpTimestamp
, uint16
> key(
625 std::make_pair(packet_event
.rtp_timestamp
, packet_event
.packet_id
));
626 PacketEventTimeMap::iterator it
= packet_sent_times_
.find(key
);
627 if (it
== packet_sent_times_
.end()) {
628 std::pair
<base::TimeTicks
, CastLoggingEvent
> value
=
629 std::make_pair(packet_event
.timestamp
, packet_event
.type
);
630 packet_sent_times_
.insert(std::make_pair(key
, value
));
631 if (packet_sent_times_
.size() > kMaxPacketEventTimeMapSize
)
632 packet_sent_times_
.erase(packet_sent_times_
.begin());
634 std::pair
<base::TimeTicks
, CastLoggingEvent
> value
= it
->second
;
635 CastLoggingEvent recorded_type
= value
.second
;
637 base::TimeTicks packet_sent_time
;
638 base::TimeTicks packet_received_time
;
639 if (recorded_type
== PACKET_SENT_TO_NETWORK
&&
640 packet_event
.type
== PACKET_RECEIVED
) {
641 packet_sent_time
= value
.first
;
642 packet_received_time
= packet_event
.timestamp
;
644 } else if (recorded_type
== PACKET_RECEIVED
&&
645 packet_event
.type
== PACKET_SENT_TO_NETWORK
) {
646 packet_sent_time
= packet_event
.timestamp
;
647 packet_received_time
= value
.first
;
651 packet_sent_times_
.erase(it
);
653 // Subtract by offset.
654 packet_received_time
-= receiver_offset
;
655 base::TimeDelta latency_delta
= packet_received_time
- packet_sent_time
;
657 total_network_latency_
+= latency_delta
;
658 network_latency_datapoints_
++;
659 histograms_
[NETWORK_LATENCY_MS_HISTO
]->Add(
660 latency_delta
.InMillisecondsF());
662 // Log total network latency.
663 FrameInfoMap::iterator frame_it
=
664 recent_frame_infos_
.find(packet_event
.rtp_timestamp
);
665 if (frame_it
!= recent_frame_infos_
.end()) {
666 base::TimeDelta latency
=
667 packet_received_time
- frame_it
->second
.encode_end_time
;
668 total_packet_latency_
+= latency
;
669 packet_latency_datapoints_
++;
670 histograms_
[PACKET_LATENCY_MS_HISTO
]->Add(
671 latency
.InMillisecondsF());
677 void StatsEventSubscriber::PopulateFpsStat(base::TimeTicks end_time
,
678 CastLoggingEvent event
,
680 StatsMap
* stats_map
) const {
681 FrameStatsMap::const_iterator it
= frame_stats_
.find(event
);
682 if (it
!= frame_stats_
.end()) {
684 base::TimeDelta duration
= (end_time
- start_time_
);
685 int count
= it
->second
.event_counter
;
686 if (duration
> base::TimeDelta())
687 fps
= count
/ duration
.InSecondsF();
688 stats_map
->insert(std::make_pair(stat
, fps
));
692 void StatsEventSubscriber::PopulateFrameCountStat(CastLoggingEvent event
,
694 StatsMap
* stats_map
) const {
695 FrameStatsMap::const_iterator it
= frame_stats_
.find(event
);
696 if (it
!= frame_stats_
.end()) {
697 stats_map
->insert(std::make_pair(stat
, it
->second
.event_counter
));
701 void StatsEventSubscriber::PopulatePacketCountStat(CastLoggingEvent event
,
703 StatsMap
* stats_map
) const {
704 PacketStatsMap::const_iterator it
= packet_stats_
.find(event
);
705 if (it
!= packet_stats_
.end()) {
706 stats_map
->insert(std::make_pair(stat
, it
->second
.event_counter
));
710 void StatsEventSubscriber::PopulateFrameBitrateStat(base::TimeTicks end_time
,
711 StatsMap
* stats_map
) const {
712 FrameStatsMap::const_iterator it
= frame_stats_
.find(FRAME_ENCODED
);
713 if (it
!= frame_stats_
.end()) {
715 base::TimeDelta duration
= end_time
- start_time_
;
716 if (duration
> base::TimeDelta()) {
717 kbps
= it
->second
.sum_size
/ duration
.InMillisecondsF() * 8;
720 stats_map
->insert(std::make_pair(ENCODE_KBPS
, kbps
));
724 void StatsEventSubscriber::PopulatePacketBitrateStat(
725 base::TimeTicks end_time
,
726 CastLoggingEvent event
,
728 StatsMap
* stats_map
) const {
729 PacketStatsMap::const_iterator it
= packet_stats_
.find(event
);
730 if (it
!= packet_stats_
.end()) {
732 base::TimeDelta duration
= end_time
- start_time_
;
733 if (duration
> base::TimeDelta()) {
734 kbps
= it
->second
.sum_size
/ duration
.InMillisecondsF() * 8;
737 stats_map
->insert(std::make_pair(stat
, kbps
));
741 StatsEventSubscriber::FrameLogStats::FrameLogStats()
742 : event_counter(0), sum_size(0) {}
743 StatsEventSubscriber::FrameLogStats::~FrameLogStats() {}
745 StatsEventSubscriber::PacketLogStats::PacketLogStats()
746 : event_counter(0), sum_size(0) {}
747 StatsEventSubscriber::PacketLogStats::~PacketLogStats() {}
749 StatsEventSubscriber::FrameInfo::FrameInfo() : encoded(false) {
751 StatsEventSubscriber::FrameInfo::~FrameInfo() {