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.
5 #ifndef MEDIA_CAST_LOGGING_STATS_EVENT_SUBSCRIBER_H_
6 #define MEDIA_CAST_LOGGING_STATS_EVENT_SUBSCRIBER_H_
8 #include "base/gtest_prod_util.h"
9 #include "base/memory/linked_ptr.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/threading/thread_checker.h"
12 #include "base/time/tick_clock.h"
13 #include "media/cast/logging/logging_defines.h"
14 #include "media/cast/logging/raw_event_subscriber.h"
15 #include "media/cast/logging/receiver_time_offset_estimator.h"
18 class DictionaryValue
;
25 class StatsEventSubscriberTest
;
27 // A RawEventSubscriber implementation that subscribes to events,
28 // and aggregates them into stats.
29 class StatsEventSubscriber
: public RawEventSubscriber
{
31 StatsEventSubscriber(EventMediaType event_media_type
,
32 base::TickClock
* clock
,
33 ReceiverTimeOffsetEstimator
* offset_estimator
);
35 ~StatsEventSubscriber() final
;
37 // RawReventSubscriber implementations.
38 void OnReceiveFrameEvent(const FrameEvent
& frame_event
) final
;
39 void OnReceivePacketEvent(const PacketEvent
& packet_event
) final
;
41 // Returns stats as a DictionaryValue. The dictionary contains one entry -
42 // "audio" or "video" pointing to an inner dictionary.
43 // The inner dictionary consists of string - double entries, where the string
44 // describes the name of the stat, and the double describes
45 // the value of the stat. See CastStat and StatsMap below.
46 scoped_ptr
<base::DictionaryValue
> GetStats() const;
48 // Resets stats in this object.
52 friend class StatsEventSubscriberTest
;
53 FRIEND_TEST_ALL_PREFIXES(StatsEventSubscriberTest
, EmptyStats
);
54 FRIEND_TEST_ALL_PREFIXES(StatsEventSubscriberTest
, CaptureEncode
);
55 FRIEND_TEST_ALL_PREFIXES(StatsEventSubscriberTest
, Encode
);
56 FRIEND_TEST_ALL_PREFIXES(StatsEventSubscriberTest
, Decode
);
57 FRIEND_TEST_ALL_PREFIXES(StatsEventSubscriberTest
, PlayoutDelay
);
58 FRIEND_TEST_ALL_PREFIXES(StatsEventSubscriberTest
, E2ELatency
);
59 FRIEND_TEST_ALL_PREFIXES(StatsEventSubscriberTest
, Packets
);
60 FRIEND_TEST_ALL_PREFIXES(StatsEventSubscriberTest
, Histograms
);
62 static const size_t kMaxFrameInfoMapSize
= 100;
64 // Generic statistics given the raw data. More specific data (e.g. frame rate
65 // and bit rate) can be computed given the basic metrics.
66 // Some of the metrics will only be set when applicable, e.g. delay and size.
67 struct FrameLogStats
{
72 base::TimeDelta sum_delay
;
75 struct PacketLogStats
{
82 class SimpleHistogram
{
84 // This will create N+2 buckets where N = (max - min) / width:
85 // Underflow bucket: < min
86 // Bucket 0: [min, min + width - 1]
87 // Bucket 1: [min + width, min + 2 * width - 1]
89 // Bucket N-1: [max - width, max - 1]
90 // Overflow bucket: >= max
91 // |min| must be less than |max|.
92 // |width| must divide |max - min| evenly.
93 SimpleHistogram(int64 min
, int64 max
, int64 width
);
97 void Add(int64 sample
);
101 scoped_ptr
<base::ListValue
> GetHistogram() const;
107 std::vector
<int> buckets_
;
111 // Capture frame rate.
113 // Encode frame rate.
115 // Decode frame rate.
117 // Average capture latency in milliseconds.
118 AVG_CAPTURE_LATENCY_MS
,
119 // Average encode duration in milliseconds.
121 // Duration from when a frame is encoded to when the packet is first
123 AVG_QUEUEING_LATENCY_MS
,
124 // Duration from when a packet is transmitted to when it is received.
125 // This measures latency from sender to receiver.
126 AVG_NETWORK_LATENCY_MS
,
127 // Duration from when a frame is encoded to when the packet is first
129 AVG_PACKET_LATENCY_MS
,
130 // Average latency between frame encoded and the moment when the frame
131 // is fully received.
132 AVG_FRAME_LATENCY_MS
,
133 // Duration from when a frame is captured to when it should be played out.
135 // Encode bitrate in kbps.
137 // Packet transmission bitrate in kbps.
139 // Packet retransmission bitrate in kbps.
141 // Duration in milliseconds since last receiver response.
142 MS_SINCE_LAST_RECEIVER_RESPONSE
,
143 // Number of frames captured.
145 // Number of frames dropped by encoder.
146 NUM_FRAMES_DROPPED_BY_ENCODER
,
147 // Number of late frames.
149 // Number of packets that were sent (not retransmitted).
151 // Number of packets that were retransmitted.
152 NUM_PACKETS_RETRANSMITTED
,
153 // Number of packets that were received by receiver.
154 NUM_PACKETS_RECEIVED
,
155 // Number of packets that had their retransmission cancelled.
156 NUM_PACKETS_RTX_REJECTED
,
157 // Unix time in milliseconds of first event since reset.
159 // Unix time in milliseconds of last event since reset.
163 CAPTURE_LATENCY_MS_HISTO
,
164 ENCODE_TIME_MS_HISTO
,
165 QUEUEING_LATENCY_MS_HISTO
,
166 NETWORK_LATENCY_MS_HISTO
,
167 PACKET_LATENCY_MS_HISTO
,
168 FRAME_LATENCY_MS_HISTO
,
169 E2E_LATENCY_MS_HISTO
,
177 base::TimeTicks capture_time
;
178 base::TimeTicks capture_end_time
;
179 base::TimeTicks encode_end_time
;
183 typedef std::map
<CastStat
, double> StatsMap
;
184 typedef std::map
<CastStat
, linked_ptr
<SimpleHistogram
> > HistogramMap
;
185 typedef std::map
<RtpTimestamp
, FrameInfo
> FrameInfoMap
;
187 std::pair
<RtpTimestamp
, uint16
>,
188 std::pair
<base::TimeTicks
, CastLoggingEvent
> >
190 typedef std::map
<CastLoggingEvent
, FrameLogStats
> FrameStatsMap
;
191 typedef std::map
<CastLoggingEvent
, PacketLogStats
> PacketStatsMap
;
193 static const char* CastStatToString(CastStat stat
);
195 void InitHistograms();
197 // Assigns |stats_map| with stats data. Used for testing.
198 void GetStatsInternal(StatsMap
* stats_map
) const;
200 // Return a histogram of the type specified.
201 SimpleHistogram
* GetHistogramForTesting(CastStat stats
) const;
203 void UpdateFirstLastEventTime(base::TimeTicks timestamp
,
204 bool is_receiver_event
);
205 bool GetReceiverOffset(base::TimeDelta
* offset
);
206 void MaybeInsertFrameInfo(RtpTimestamp rtp_timestamp
,
207 const FrameInfo
& frame_info
);
208 void RecordFrameCaptureTime(const FrameEvent
& frame_event
);
209 void RecordCaptureLatency(const FrameEvent
& frame_event
);
210 void RecordEncodeLatency(const FrameEvent
& frame_event
);
211 void RecordFrameTxLatency(const FrameEvent
& frame_event
);
212 void RecordE2ELatency(const FrameEvent
& frame_event
);
213 void RecordPacketSentTime(const PacketEvent
& packet_event
);
214 void ErasePacketSentTime(const PacketEvent
& packet_event
);
215 void RecordPacketRelatedLatencies(const PacketEvent
& packet_event
);
216 void UpdateLastResponseTime(base::TimeTicks receiver_time
);
218 void PopulateFpsStat(base::TimeTicks now
,
219 CastLoggingEvent event
,
221 StatsMap
* stats_map
) const;
222 void PopulateFrameCountStat(CastLoggingEvent event
,
224 StatsMap
* stats_map
) const;
225 void PopulatePacketCountStat(CastLoggingEvent event
,
227 StatsMap
* stats_map
) const;
228 void PopulateFrameBitrateStat(base::TimeTicks now
, StatsMap
* stats_map
) const;
229 void PopulatePacketBitrateStat(base::TimeTicks now
,
230 CastLoggingEvent event
,
232 StatsMap
* stats_map
) const;
234 const EventMediaType event_media_type_
;
236 // Not owned by this class.
237 base::TickClock
* const clock_
;
239 // Not owned by this class.
240 ReceiverTimeOffsetEstimator
* const offset_estimator_
;
242 FrameStatsMap frame_stats_
;
243 PacketStatsMap packet_stats_
;
245 base::TimeDelta total_capture_latency_
;
246 int capture_latency_datapoints_
;
247 base::TimeDelta total_encode_time_
;
248 int encode_time_datapoints_
;
249 base::TimeDelta total_queueing_latency_
;
250 int queueing_latency_datapoints_
;
251 base::TimeDelta total_network_latency_
;
252 int network_latency_datapoints_
;
253 base::TimeDelta total_packet_latency_
;
254 int packet_latency_datapoints_
;
255 base::TimeDelta total_frame_latency_
;
256 int frame_latency_datapoints_
;
257 base::TimeDelta total_e2e_latency_
;
258 int e2e_latency_datapoints_
;
260 base::TimeTicks last_response_received_time_
;
262 int num_frames_dropped_by_encoder_
;
263 int num_frames_late_
;
265 // Fixed size map to record when recent frames were captured and other info.
266 FrameInfoMap recent_frame_infos_
;
268 // Fixed size map to record when recent packets were sent.
269 PacketEventTimeMap packet_sent_times_
;
271 // Sender time assigned on creation and |Reset()|.
272 base::TimeTicks start_time_
;
273 base::TimeTicks first_event_time_
;
274 base::TimeTicks last_event_time_
;
276 HistogramMap histograms_
;
278 base::ThreadChecker thread_checker_
;
279 DISALLOW_COPY_AND_ASSIGN(StatsEventSubscriber
);
285 #endif // MEDIA_CAST_LOGGING_STATS_EVENT_SUBSCRIBER_H_