Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / media / cast / logging / stats_event_subscriber.h
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.
4
5 #ifndef MEDIA_CAST_LOGGING_STATS_EVENT_SUBSCRIBER_H_
6 #define MEDIA_CAST_LOGGING_STATS_EVENT_SUBSCRIBER_H_
7
8 #include "base/gtest_prod_util.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/threading/thread_checker.h"
11 #include "base/time/tick_clock.h"
12 #include "media/cast/logging/logging_defines.h"
13 #include "media/cast/logging/raw_event_subscriber.h"
14 #include "media/cast/logging/receiver_time_offset_estimator.h"
15
16 namespace base {
17 class DictionaryValue;
18 }
19
20 namespace media {
21 namespace cast {
22
23 class StatsEventSubscriberTest;
24
25 // A RawEventSubscriber implementation that subscribes to events,
26 // and aggregates them into stats.
27 class StatsEventSubscriber : public RawEventSubscriber {
28  public:
29   StatsEventSubscriber(EventMediaType event_media_type,
30                        base::TickClock* clock,
31                        ReceiverTimeOffsetEstimator* offset_estimator);
32
33   virtual ~StatsEventSubscriber();
34
35   // RawReventSubscriber implementations.
36   virtual void OnReceiveFrameEvent(const FrameEvent& frame_event) OVERRIDE;
37   virtual void OnReceivePacketEvent(const PacketEvent& packet_event) OVERRIDE;
38
39   // Returns stats as a DictionaryValue. The dictionary contains one entry -
40   // "audio" or "video" pointing to an inner dictionary.
41   // The inner dictionary consists of string - double entries, where the string
42   // describes the name of the stat, and the double describes
43   // the value of the stat. See CastStat and StatsMap below.
44   scoped_ptr<base::DictionaryValue> GetStats() const;
45
46   // Resets stats in this object.
47   void Reset();
48
49  private:
50   friend class StatsEventSubscriberTest;
51   FRIEND_TEST_ALL_PREFIXES(StatsEventSubscriberTest, EmptyStats);
52   FRIEND_TEST_ALL_PREFIXES(StatsEventSubscriberTest, CaptureEncode);
53   FRIEND_TEST_ALL_PREFIXES(StatsEventSubscriberTest, Encode);
54   FRIEND_TEST_ALL_PREFIXES(StatsEventSubscriberTest, Decode);
55   FRIEND_TEST_ALL_PREFIXES(StatsEventSubscriberTest, PlayoutDelay);
56   FRIEND_TEST_ALL_PREFIXES(StatsEventSubscriberTest, E2ELatency);
57   FRIEND_TEST_ALL_PREFIXES(StatsEventSubscriberTest, Packets);
58
59   static const size_t kMaxFrameInfoMapSize = 100;
60
61   // Generic statistics given the raw data. More specific data (e.g. frame rate
62   // and bit rate) can be computed given the basic metrics.
63   // Some of the metrics will only be set when applicable, e.g. delay and size.
64   struct FrameLogStats {
65     FrameLogStats();
66     ~FrameLogStats();
67     int event_counter;
68     size_t sum_size;
69     base::TimeDelta sum_delay;
70   };
71
72   struct PacketLogStats {
73     PacketLogStats();
74     ~PacketLogStats();
75     int event_counter;
76     size_t sum_size;
77   };
78
79   enum CastStat {
80     // Capture frame rate.
81     CAPTURE_FPS,
82     // Encode frame rate.
83     ENCODE_FPS,
84     // Decode frame rate.
85     DECODE_FPS,
86     // Average encode duration in milliseconds.
87     // TODO(imcheng): This stat is not populated yet because we do not have
88     // the time when encode started. Record it in FRAME_ENCODED event.
89     AVG_ENCODE_TIME_MS,
90     // Average playout delay in milliseconds.
91     AVG_PLAYOUT_DELAY_MS,
92     // Duration from when a packet is transmitted to when it is received.
93     // This measures latency from sender to receiver.
94     AVG_NETWORK_LATENCY_MS,
95     // Duration from when a frame is captured to when it should be played out.
96     AVG_E2E_LATENCY_MS,
97     // Encode bitrate in kbps.
98     ENCODE_KBPS,
99     // Packet transmission bitrate in kbps.
100     TRANSMISSION_KBPS,
101     // Packet retransmission bitrate in kbps.
102     RETRANSMISSION_KBPS,
103     // Fraction of packet loss.
104     PACKET_LOSS_FRACTION,
105     // Duration in milliseconds since last receiver response.
106     MS_SINCE_LAST_RECEIVER_RESPONSE,
107     // Number of frames captured.
108     NUM_FRAMES_CAPTURED,
109     // Number of frames dropped by encoder.
110     NUM_FRAMES_DROPPED_BY_ENCODER,
111     // Number of late frames.
112     NUM_FRAMES_LATE,
113     // Number of packets that were sent (not retransmitted).
114     NUM_PACKETS_SENT,
115     // Number of packets that were retransmitted.
116     NUM_PACKETS_RETRANSMITTED,
117     // Number of packets that had their retransmission cancelled.
118     NUM_PACKETS_RTX_REJECTED,
119   };
120
121   struct FrameInfo {
122     explicit FrameInfo(base::TimeTicks capture_time);
123     ~FrameInfo();
124
125     base::TimeTicks capture_time;
126     bool encoded;
127   };
128
129   typedef std::map<CastStat, double> StatsMap;
130   typedef std::map<RtpTimestamp, FrameInfo> FrameInfoMap;
131   typedef std::map<
132       std::pair<RtpTimestamp, uint16>,
133       std::pair<base::TimeTicks, CastLoggingEvent> >
134       PacketEventTimeMap;
135   typedef std::map<CastLoggingEvent, FrameLogStats> FrameStatsMap;
136   typedef std::map<CastLoggingEvent, PacketLogStats> PacketStatsMap;
137
138   static const char* CastStatToString(CastStat stat);
139
140   // Assigns |stats_map| with stats data. Used for testing.
141   void GetStatsInternal(StatsMap* stats_map) const;
142
143   bool GetReceiverOffset(base::TimeDelta* offset);
144   void RecordFrameCaptureTime(const FrameEvent& frame_event);
145   void MarkAsEncoded(RtpTimestamp rtp_timestamp);
146   void RecordE2ELatency(const FrameEvent& frame_event);
147   void RecordPacketSentTime(const PacketEvent& packet_event);
148   void ErasePacketSentTime(const PacketEvent& packet_event);
149   void RecordNetworkLatency(const PacketEvent& packet_event);
150   void UpdateLastResponseTime(base::TimeTicks receiver_time);
151
152   void PopulateFpsStat(base::TimeTicks now,
153                        CastLoggingEvent event,
154                        CastStat stat,
155                        StatsMap* stats_map) const;
156   void PopulateFrameCountStat(CastLoggingEvent event,
157                               CastStat stat,
158                               StatsMap* stats_map) const;
159   void PopulatePacketCountStat(CastLoggingEvent event,
160                                CastStat stat,
161                                StatsMap* stats_map) const;
162   void PopulatePlayoutDelayStat(StatsMap* stats_map) const;
163   void PopulateFrameBitrateStat(base::TimeTicks now, StatsMap* stats_map) const;
164   void PopulatePacketBitrateStat(base::TimeTicks now,
165                                  CastLoggingEvent event,
166                                  CastStat stat,
167                                  StatsMap* stats_map) const;
168   void PopulatePacketLossPercentageStat(StatsMap* stats_map) const;
169
170   const EventMediaType event_media_type_;
171
172   // Not owned by this class.
173   base::TickClock* const clock_;
174
175   // Not owned by this class.
176   ReceiverTimeOffsetEstimator* const offset_estimator_;
177
178   FrameStatsMap frame_stats_;
179   PacketStatsMap packet_stats_;
180
181   base::TimeDelta total_network_latency_;
182   int network_latency_datapoints_;
183   base::TimeDelta total_e2e_latency_;
184   int e2e_latency_datapoints_;
185
186   base::TimeTicks last_response_received_time_;
187
188   int num_frames_dropped_by_encoder_;
189   int num_frames_late_;
190
191   // Fixed size map to record when recent frames were captured and other info.
192   FrameInfoMap recent_captured_frames_;
193
194   // Fixed size map to record when recent packets were sent.
195   PacketEventTimeMap packet_sent_times_;
196
197   // Sender time assigned on creation and |Reset()|.
198   base::TimeTicks start_time_;
199
200   base::ThreadChecker thread_checker_;
201   DISALLOW_COPY_AND_ASSIGN(StatsEventSubscriber);
202 };
203
204 }  // namespace cast
205 }  // namespace media
206
207 #endif  // MEDIA_CAST_LOGGING_STATS_EVENT_SUBSCRIBER_H_