summaryrefslogtreecommitdiff
path: root/media/cast/logging/logging_stats.cc
blob: 84fdbf7a615d90aea4435359f799904c8f834d94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/memory/linked_ptr.h"
#include "base/memory/scoped_ptr.h"
#include "media/cast/logging/logging_stats.h"

namespace media {
namespace cast {

LoggingStats::LoggingStats(base::TickClock* clock)
    : frame_stats_(),
      packet_stats_(),
      generic_stats_(),
      start_time_(),
      clock_(clock) {
  memset(counts_, 0, sizeof(counts_));
  memset(start_time_, 0, sizeof(start_time_));
}

LoggingStats::~LoggingStats() {}

void LoggingStats::Reset() {
  frame_stats_.clear();
  packet_stats_.clear();
  generic_stats_.clear();
  memset(counts_, 0, sizeof(counts_));
}

void LoggingStats::InsertFrameEvent(CastLoggingEvent event,
                                    uint32 rtp_timestamp,
                                    uint32 frame_id) {
  InsertBaseFrameEvent(event, frame_id, rtp_timestamp);
}

void LoggingStats::InsertFrameEventWithSize(CastLoggingEvent event,
                                            uint32 rtp_timestamp,
                                            uint32 frame_id,
                                            int frame_size) {
  InsertBaseFrameEvent(event, frame_id, rtp_timestamp);
  // Update size.
  FrameStatsMap::iterator it = frame_stats_.find(event);
  DCHECK(it != frame_stats_.end());
  it->second->bitrate_kbps += frame_size;
}

void LoggingStats::InsertFrameEventWithDelay(CastLoggingEvent event,
                                             uint32 rtp_timestamp,
                                             uint32 frame_id,
                                             base::TimeDelta delay) {
  InsertBaseFrameEvent(event, frame_id, rtp_timestamp);
  // Update size.
  FrameStatsMap::iterator it = frame_stats_.find(event);
  DCHECK(it != frame_stats_.end());
  // Using the average delay as a counter, will divide by the counter when
  // triggered.
  it->second->avg_delay_ms += delay.InMilliseconds();
  if (delay.InMilliseconds() > it->second->max_delay_ms)
    it->second->max_delay_ms = delay.InMilliseconds();
  if ((delay.InMilliseconds() < it->second->min_delay_ms) ||
      (counts_[event] == 1) )
    it->second->min_delay_ms = delay.InMilliseconds();
}

void LoggingStats::InsertBaseFrameEvent(CastLoggingEvent event,
                                        uint32 frame_id,
                                        uint32 rtp_timestamp) {
  // Does this belong to an existing event?
  FrameStatsMap::iterator it = frame_stats_.find(event);
  if (it == frame_stats_.end()) {
    // New event.
    start_time_[event] = clock_->NowTicks();
    linked_ptr<FrameLogStats> stats(new FrameLogStats());
    frame_stats_.insert(std::make_pair(event, stats));
  }

  ++counts_[event];
}

void LoggingStats::InsertPacketEvent(CastLoggingEvent event,
                                     uint32 rtp_timestamp,
                                     uint32 frame_id,
                                     uint16 packet_id,
                                     uint16 max_packet_id,
                                     size_t size) {
  // Does this packet belong to an existing event?
  PacketStatsMap::iterator it = packet_stats_.find(event);
  if (it == packet_stats_.end()) {
    // New event.
    start_time_[event] = clock_->NowTicks();
    packet_stats_.insert(std::make_pair(event, size));
  } else {
    // Add to existing.
    it->second += size;
  }
  ++counts_[event];
}

void LoggingStats::InsertGenericEvent(CastLoggingEvent event, int value) {
  // Does this event belong to an existing event?
  GenericStatsMap::iterator it = generic_stats_.find(event);
  if (it == generic_stats_.end()) {
    // New event.
    start_time_[event] = clock_->NowTicks();
    generic_stats_.insert(std::make_pair(event, value));
  } else {
    // Add to existing (will be used to compute average).
    it->second += value;
  }
   ++counts_[event];
}

const FrameStatsMap* LoggingStats::GetFrameStatsData() {
  // Compute framerate and bitrate (when available).
  FrameStatsMap::iterator it;
  for (it = frame_stats_.begin(); it != frame_stats_.end(); ++it) {
    base::TimeDelta time_diff = clock_->NowTicks() - start_time_[it->first];
    it->second->framerate_fps = counts_[it->first] / time_diff.InSecondsF();
    if (it->second->bitrate_kbps > 0) {
      it->second->bitrate_kbps = (8 / 1000) *
          it->second->bitrate_kbps / time_diff.InSecondsF();
    }
    if (it->second->avg_delay_ms > 0)
      it->second->avg_delay_ms /= counts_[it->first];
  }
  return &frame_stats_;
}

const PacketStatsMap* LoggingStats::GetPacketStatsData() {
  PacketStatsMap::iterator it;
  for (it = packet_stats_.begin(); it != packet_stats_.end(); ++it) {
    if (counts_[it->first] == 0) continue;
    base::TimeDelta time_diff = clock_->NowTicks() - start_time_[it->first];
    it->second = (8 / 1000) * it->second / time_diff.InSecondsF();
  }
  return &packet_stats_;
}

const GenericStatsMap* LoggingStats::GetGenericStatsData() {
  // Compute averages.
  GenericStatsMap::iterator it;
  for (it = generic_stats_.begin(); it != generic_stats_.end(); ++it) {
    it->second /= counts_[ it->first];
  }
  return &generic_stats_;
}

}  // namespace cast
}  // namespace media