OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_CAST_LOGGING_ENCODING_EVENT_SUBSCRIBER_H_ | 5 #ifndef MEDIA_CAST_LOGGING_ENCODING_EVENT_SUBSCRIBER_H_ |
6 #define MEDIA_CAST_LOGGING_ENCODING_EVENT_SUBSCRIBER_H_ | 6 #define MEDIA_CAST_LOGGING_ENCODING_EVENT_SUBSCRIBER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/memory/linked_ptr.h" | 10 #include "base/memory/linked_ptr.h" |
11 #include "base/threading/thread_checker.h" | 11 #include "base/threading/thread_checker.h" |
12 #include "media/cast/logging/logging_defines.h" | 12 #include "media/cast/logging/logging_defines.h" |
13 #include "media/cast/logging/proto/raw_events.pb.h" | 13 #include "media/cast/logging/proto/raw_events.pb.h" |
14 #include "media/cast/logging/raw_event_subscriber.h" | 14 #include "media/cast/logging/raw_event_subscriber.h" |
15 | 15 |
16 namespace media { | 16 namespace media { |
17 namespace cast { | 17 namespace cast { |
18 | 18 |
19 typedef std::map<RtpTimestamp, | 19 // Number of packets per frame recorded by the subscriber. |
20 linked_ptr<media::cast::proto::AggregatedFrameEvent> > | 20 // Once the max number of packets has been reached, a new aggregated proto |
21 FrameEventMap; | 21 // will be created. |
22 typedef std::map<RtpTimestamp, | 22 static const int kMaxPacketsPerFrame = 64; |
23 linked_ptr<media::cast::proto::AggregatedPacketEvent> > | 23 // Number of events per proto recorded by the subscriber. |
24 PacketEventMap; | 24 // Once the max number of events has been reached, a new aggregated proto |
| 25 // will be created. |
| 26 static const int kMaxEventsPerProto = 16; |
| 27 |
| 28 typedef std::vector<linked_ptr<media::cast::proto::AggregatedFrameEvent> > |
| 29 FrameEventList; |
| 30 typedef std::vector<linked_ptr<media::cast::proto::AggregatedPacketEvent> > |
| 31 PacketEventList; |
25 | 32 |
26 // A RawEventSubscriber implementation that subscribes to events, | 33 // A RawEventSubscriber implementation that subscribes to events, |
27 // encodes them in protocol buffer format, and aggregates them into a more | 34 // encodes them in protocol buffer format, and aggregates them into a more |
28 // compact structure. | 35 // compact structure. Aggregation is per-frame, and uses a map with RTP |
| 36 // timestamp as key. Periodically, old entries in the map will be transferred |
| 37 // to a storage vector. This helps keep the size of the map small and |
| 38 // lookup times fast. The storage itself is a circular buffer that will |
| 39 // overwrite old entries once it has reached the size configured by user. |
29 class EncodingEventSubscriber : public RawEventSubscriber { | 40 class EncodingEventSubscriber : public RawEventSubscriber { |
30 public: | 41 public: |
31 // |event_media_type|: The subscriber will only process events that | 42 // |event_media_type|: The subscriber will only process events that |
32 // corresponds to this type. | 43 // corresponds to this type. |
33 // |max_frames|: How many events to keep in the frame / packet map. | 44 // |max_frames|: How many events to keep in the frame / packet storage. |
34 // This helps keep memory usage bounded. | 45 // This helps keep memory usage bounded. |
35 // Every time one of |OnReceive[Frame,Packet]Event()| is | 46 // Every time one of |OnReceive[Frame,Packet]Event()| is |
36 // called, it will check if the respective map size has exceeded |max_frames|. | 47 // called, it will check if the respective map size has exceeded |max_frames|. |
37 // If so, it will remove the oldest aggregated entry (ordered by RTP | 48 // If so, it will remove the oldest aggregated entry (ordered by RTP |
38 // timestamp). | 49 // timestamp). |
39 EncodingEventSubscriber(EventMediaType event_media_type, size_t max_frames); | 50 EncodingEventSubscriber(EventMediaType event_media_type, size_t max_frames); |
40 | 51 |
41 virtual ~EncodingEventSubscriber(); | 52 virtual ~EncodingEventSubscriber(); |
42 | 53 |
43 // RawReventSubscriber implementations. | 54 // RawReventSubscriber implementations. |
44 virtual void OnReceiveFrameEvent(const FrameEvent& frame_event) OVERRIDE; | 55 virtual void OnReceiveFrameEvent(const FrameEvent& frame_event) OVERRIDE; |
45 virtual void OnReceivePacketEvent(const PacketEvent& packet_event) OVERRIDE; | 56 virtual void OnReceivePacketEvent(const PacketEvent& packet_event) OVERRIDE; |
46 virtual void OnReceiveGenericEvent(const GenericEvent& generic_event) | 57 virtual void OnReceiveGenericEvent(const GenericEvent& generic_event) |
47 OVERRIDE; | 58 OVERRIDE; |
48 | 59 |
49 // Assigns frame events and packet events received so far to |frame_events| | 60 // Assigns frame events and packet events received so far to |frame_events| |
50 // and |packet_events| and resets the internal state. | 61 // and |packet_events| and resets the internal state. |
51 // In addition, assign metadata associated with these events to |metadata|. | 62 // In addition, assign metadata associated with these events to |metadata|. |
| 63 // The protos in |frame_events| and |packets_events| are sorted in |
| 64 // ascending RTP timestamp order. |
52 void GetEventsAndReset(media::cast::proto::LogMetadata* metadata, | 65 void GetEventsAndReset(media::cast::proto::LogMetadata* metadata, |
53 FrameEventMap* frame_events, | 66 FrameEventList* frame_events, |
54 PacketEventMap* packet_events); | 67 PacketEventList* packet_events); |
55 | 68 |
56 private: | 69 private: |
| 70 typedef std::map<RtpTimestamp, |
| 71 linked_ptr<media::cast::proto::AggregatedFrameEvent> > |
| 72 FrameEventMap; |
| 73 typedef std::map<RtpTimestamp, |
| 74 linked_ptr<media::cast::proto::AggregatedPacketEvent> > |
| 75 PacketEventMap; |
| 76 |
57 bool ShouldProcessEvent(CastLoggingEvent event); | 77 bool ShouldProcessEvent(CastLoggingEvent event); |
58 | 78 |
59 // Removes oldest entry from |frame_event_map_| (ordered by RTP timestamp). | 79 // Transfer |num_entries| smallest entries from |frame_event_map_| to |
60 void TruncateFrameEventMapIfNeeded(); | 80 // |frame_event_storage_|. This helps keep size of |frame_event_map_| small |
| 81 // and lookup speed fast. |
| 82 void TransferFrameEvents(size_t num_entries); |
| 83 // See above. |
| 84 void TransferPacketEvents(size_t num_entries); |
61 | 85 |
62 // Removes oldest entry from |packet_event_map_| (ordered by RTP timestamp). | 86 void AddFrameEventToStorage( |
63 void TruncatePacketEventMapIfNeeded(); | 87 const linked_ptr<media::cast::proto::AggregatedFrameEvent>& |
| 88 frame_event_proto); |
| 89 void AddPacketEventToStorage( |
| 90 const linked_ptr<media::cast::proto::AggregatedPacketEvent>& |
| 91 packet_event_proto); |
64 | 92 |
65 // Returns the difference between |rtp_timestamp| and |first_rtp_timestamp_|. | 93 // Returns the difference between |rtp_timestamp| and |first_rtp_timestamp_|. |
66 // Sets |first_rtp_timestamp_| if it is not already set. | 94 // Sets |first_rtp_timestamp_| if it is not already set. |
67 RtpTimestamp GetRelativeRtpTimestamp(RtpTimestamp rtp_timestamp); | 95 RtpTimestamp GetRelativeRtpTimestamp(RtpTimestamp rtp_timestamp); |
68 | 96 |
69 // Clears the maps and first RTP timestamp seen. | 97 // Clears the maps and first RTP timestamp seen. |
70 void Reset(); | 98 void Reset(); |
71 | 99 |
72 const EventMediaType event_media_type_; | 100 const EventMediaType event_media_type_; |
73 const size_t max_frames_; | 101 const size_t max_frames_; |
74 | 102 |
75 FrameEventMap frame_event_map_; | 103 FrameEventMap frame_event_map_; |
| 104 FrameEventList frame_event_storage_; |
| 105 int frame_event_storage_index_; |
| 106 |
76 PacketEventMap packet_event_map_; | 107 PacketEventMap packet_event_map_; |
| 108 PacketEventList packet_event_storage_; |
| 109 int packet_event_storage_index_; |
77 | 110 |
78 // All functions must be called on the main thread. | 111 // All functions must be called on the main thread. |
79 base::ThreadChecker thread_checker_; | 112 base::ThreadChecker thread_checker_; |
80 | 113 |
81 // Set to true on first event encountered after a |Reset()|. | 114 // Set to true on first event encountered after a |Reset()|. |
82 bool seen_first_rtp_timestamp_; | 115 bool seen_first_rtp_timestamp_; |
83 | 116 |
84 // Set to RTP timestamp of first event encountered after a |Reset()|. | 117 // Set to RTP timestamp of first event encountered after a |Reset()|. |
85 RtpTimestamp first_rtp_timestamp_; | 118 RtpTimestamp first_rtp_timestamp_; |
86 | 119 |
87 DISALLOW_COPY_AND_ASSIGN(EncodingEventSubscriber); | 120 DISALLOW_COPY_AND_ASSIGN(EncodingEventSubscriber); |
88 }; | 121 }; |
89 | 122 |
90 } // namespace cast | 123 } // namespace cast |
91 } // namespace media | 124 } // namespace media |
92 | 125 |
93 #endif // MEDIA_CAST_LOGGING_ENCODING_EVENT_SUBSCRIBER_H_ | 126 #endif // MEDIA_CAST_LOGGING_ENCODING_EVENT_SUBSCRIBER_H_ |
OLD | NEW |