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_ENCODING_EVENT_SUBSCRIBER_H_
6 #define MEDIA_CAST_LOGGING_ENCODING_EVENT_SUBSCRIBER_H_
10 #include "base/memory/linked_ptr.h"
11 #include "base/threading/thread_checker.h"
12 #include "media/cast/logging/logging_defines.h"
13 #include "media/cast/logging/proto/raw_events.pb.h"
14 #include "media/cast/logging/raw_event_subscriber.h"
19 // Number of packets per frame recorded by the subscriber.
20 // Once the max number of packets has been reached, a new aggregated proto
22 static const int kMaxPacketsPerFrame = 64;
23 // Number of events per proto recorded by the subscriber.
24 // Once the max number of events has been reached, a new aggregated proto
26 static const int kMaxEventsPerProto = 16;
28 typedef std::vector<linked_ptr<media::cast::proto::AggregatedFrameEvent> >
30 typedef std::vector<linked_ptr<media::cast::proto::AggregatedPacketEvent> >
33 // A RawEventSubscriber implementation that subscribes to events,
34 // encodes them in protocol buffer format, and aggregates them into a more
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.
40 class EncodingEventSubscriber : public RawEventSubscriber {
42 // |event_media_type|: The subscriber will only process events that
43 // corresponds to this type.
44 // |max_frames|: How many events to keep in the frame / packet storage.
45 // This helps keep memory usage bounded.
46 // Every time one of |OnReceive[Frame,Packet]Event()| is
47 // called, it will check if the respective map size has exceeded |max_frames|.
48 // If so, it will remove the oldest aggregated entry (ordered by RTP
50 EncodingEventSubscriber(EventMediaType event_media_type, size_t max_frames);
52 virtual ~EncodingEventSubscriber();
54 // RawReventSubscriber implementations.
55 virtual void OnReceiveFrameEvent(const FrameEvent& frame_event) OVERRIDE;
56 virtual void OnReceivePacketEvent(const PacketEvent& packet_event) OVERRIDE;
58 // Assigns frame events and packet events received so far to |frame_events|
59 // and |packet_events| and resets the internal state.
60 // In addition, assign metadata associated with these events to |metadata|.
61 // The protos in |frame_events| and |packets_events| are sorted in
62 // ascending RTP timestamp order.
63 void GetEventsAndReset(media::cast::proto::LogMetadata* metadata,
64 FrameEventList* frame_events,
65 PacketEventList* packet_events);
68 typedef std::map<RtpTimestamp,
69 linked_ptr<media::cast::proto::AggregatedFrameEvent> >
71 typedef std::map<RtpTimestamp,
72 linked_ptr<media::cast::proto::AggregatedPacketEvent> >
75 bool ShouldProcessEvent(CastLoggingEvent event);
77 // Transfer up to |max_num_entries| smallest entries from |frame_event_map_|
78 // to |frame_event_storage_|. This helps keep size of |frame_event_map_| small
79 // and lookup speed fast.
80 void TransferFrameEvents(size_t max_num_entries);
82 void TransferPacketEvents(size_t max_num_entries);
84 void AddFrameEventToStorage(
85 const linked_ptr<media::cast::proto::AggregatedFrameEvent>&
87 void AddPacketEventToStorage(
88 const linked_ptr<media::cast::proto::AggregatedPacketEvent>&
91 // Returns the difference between |rtp_timestamp| and |first_rtp_timestamp_|.
92 // Sets |first_rtp_timestamp_| if it is not already set.
93 RtpTimestamp GetRelativeRtpTimestamp(RtpTimestamp rtp_timestamp);
95 // Clears the maps and first RTP timestamp seen.
98 const EventMediaType event_media_type_;
99 const size_t max_frames_;
101 FrameEventMap frame_event_map_;
102 FrameEventList frame_event_storage_;
103 int frame_event_storage_index_;
105 PacketEventMap packet_event_map_;
106 PacketEventList packet_event_storage_;
107 int packet_event_storage_index_;
109 // All functions must be called on the main thread.
110 base::ThreadChecker thread_checker_;
112 // Set to true on first event encountered after a |Reset()|.
113 bool seen_first_rtp_timestamp_;
115 // Set to RTP timestamp of first event encountered after a |Reset()|.
116 RtpTimestamp first_rtp_timestamp_;
118 DISALLOW_COPY_AND_ASSIGN(EncodingEventSubscriber);
124 #endif // MEDIA_CAST_LOGGING_ENCODING_EVENT_SUBSCRIBER_H_