Home | History | Annotate | Download | only in logging
      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_ENCODING_EVENT_SUBSCRIBER_H_
      6 #define MEDIA_CAST_LOGGING_ENCODING_EVENT_SUBSCRIBER_H_
      7 
      8 #include <map>
      9 
     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"
     15 
     16 namespace media {
     17 namespace cast {
     18 
     19 // Number of packets per frame recorded by the subscriber.
     20 // Once the max number of packets has been reached, a new aggregated proto
     21 // will be created.
     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
     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;
     32 
     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 {
     41  public:
     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
     49   // timestamp).
     50   EncodingEventSubscriber(EventMediaType event_media_type, size_t max_frames);
     51 
     52   virtual ~EncodingEventSubscriber();
     53 
     54   // RawReventSubscriber implementations.
     55   virtual void OnReceiveFrameEvent(const FrameEvent& frame_event) OVERRIDE;
     56   virtual void OnReceivePacketEvent(const PacketEvent& packet_event) OVERRIDE;
     57 
     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);
     66 
     67  private:
     68   typedef std::map<RtpTimestamp,
     69                    linked_ptr<media::cast::proto::AggregatedFrameEvent> >
     70       FrameEventMap;
     71   typedef std::map<RtpTimestamp,
     72                    linked_ptr<media::cast::proto::AggregatedPacketEvent> >
     73       PacketEventMap;
     74 
     75   // Transfer up to |max_num_entries| smallest entries from |frame_event_map_|
     76   // to |frame_event_storage_|. This helps keep size of |frame_event_map_| small
     77   // and lookup speed fast.
     78   void TransferFrameEvents(size_t max_num_entries);
     79   // See above.
     80   void TransferPacketEvents(size_t max_num_entries);
     81 
     82   void AddFrameEventToStorage(
     83       const linked_ptr<media::cast::proto::AggregatedFrameEvent>&
     84           frame_event_proto);
     85   void AddPacketEventToStorage(
     86       const linked_ptr<media::cast::proto::AggregatedPacketEvent>&
     87           packet_event_proto);
     88 
     89   // Returns the difference between |rtp_timestamp| and |first_rtp_timestamp_|.
     90   // Sets |first_rtp_timestamp_| if it is not already set.
     91   RtpTimestamp GetRelativeRtpTimestamp(RtpTimestamp rtp_timestamp);
     92 
     93   // Clears the maps and first RTP timestamp seen.
     94   void Reset();
     95 
     96   const EventMediaType event_media_type_;
     97   const size_t max_frames_;
     98 
     99   FrameEventMap frame_event_map_;
    100   FrameEventList frame_event_storage_;
    101   int frame_event_storage_index_;
    102 
    103   PacketEventMap packet_event_map_;
    104   PacketEventList packet_event_storage_;
    105   int packet_event_storage_index_;
    106 
    107   // All functions must be called on the main thread.
    108   base::ThreadChecker thread_checker_;
    109 
    110   // Set to true on first event encountered after a |Reset()|.
    111   bool seen_first_rtp_timestamp_;
    112 
    113   // Set to RTP timestamp of first event encountered after a |Reset()|.
    114   RtpTimestamp first_rtp_timestamp_;
    115 
    116   DISALLOW_COPY_AND_ASSIGN(EncodingEventSubscriber);
    117 };
    118 
    119 }  // namespace cast
    120 }  // namespace media
    121 
    122 #endif  // MEDIA_CAST_LOGGING_ENCODING_EVENT_SUBSCRIBER_H_
    123