Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / cast / net / rtcp / receiver_rtcp_event_subscriber.h
blob248f07a74ea5a626613c2cbaf2bdf9a024f38b3d
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_RTCP_RECEIVER_RTCP_EVENT_SUBSCRIBER_H_
6 #define MEDIA_CAST_RTCP_RECEIVER_RTCP_EVENT_SUBSCRIBER_H_
8 #include <deque>
9 #include <vector>
11 #include "base/threading/thread_checker.h"
12 #include "media/cast/logging/logging_defines.h"
13 #include "media/cast/logging/raw_event_subscriber.h"
14 #include "media/cast/net/rtcp/rtcp_defines.h"
16 namespace media {
17 namespace cast {
19 static const size_t kNumResends = 3;
20 static const size_t kResendDelay = 10;
21 static const size_t kMaxEventsPerRTCP = 20;
23 // A RawEventSubscriber implementation with the following properties:
24 // - Only processes raw event types that are relevant for sending from cast
25 // receiver to cast sender via RTCP.
26 // - Captures information to be sent over to RTCP from raw event logs into the
27 // more compact RtcpEvent struct.
28 // - Orders events by RTP timestamp with a multimap.
29 // - Internally, the map is capped at a maximum size configurable by the caller.
30 // The subscriber only keeps the most recent events (determined by RTP
31 // timestamp) up to the size limit.
32 class ReceiverRtcpEventSubscriber : public RawEventSubscriber {
33 public:
34 typedef std::pair<RtpTimestamp, RtcpEvent> RtcpEventPair;
35 typedef std::vector<std::pair<RtpTimestamp, RtcpEvent> > RtcpEvents;
37 // |max_size_to_retain|: The object will keep up to |max_size_to_retain|
38 // events
39 // in the map. Once threshold has been reached, an event with the smallest
40 // RTP timestamp will be removed.
41 // |type|: Determines whether the subscriber will process only audio or video
42 // events.
43 ReceiverRtcpEventSubscriber(const size_t max_size_to_retain,
44 EventMediaType type);
46 ~ReceiverRtcpEventSubscriber() final;
48 // RawEventSubscriber implementation.
49 void OnReceiveFrameEvent(const FrameEvent& frame_event) final;
50 void OnReceivePacketEvent(const PacketEvent& packet_event) final;
52 // Assigns events collected to |rtcp_events|. If there is space, some
53 // older events will be added for redundancy as well.
54 void GetRtcpEventsWithRedundancy(RtcpEvents* rtcp_events);
56 private:
57 // If |rtcp_events_.size()| exceeds |max_size_to_retain_|, remove an oldest
58 // entry (determined by RTP timestamp) so its size no greater than
59 // |max_size_to_retain_|.
60 void TruncateMapIfNeeded();
62 // Returns |true| if events of |event_type| and |media_type|
63 // should be processed.
64 bool ShouldProcessEvent(CastLoggingEvent event_type,
65 EventMediaType media_type);
67 const size_t max_size_to_retain_;
68 EventMediaType type_;
70 // The key should really be something more than just a RTP timestamp in order
71 // to differentiate between video and audio frames, but since the
72 // implementation doesn't mix audio and video frame events, RTP timestamp
73 // only as key is fine.
74 std::deque<RtcpEventPair> rtcp_events_;
76 // Counts how many events have been removed from rtcp_events_.
77 uint64 popped_events_;
79 // Events greater than send_ptrs_[0] have not been sent yet.
80 // Events greater than send_ptrs_[1] have been transmit once.
81 // Note that these counters use absolute numbers, so you need
82 // to subtract popped_events_ before looking up the events in
83 // rtcp_events_.
84 uint64 send_ptrs_[kNumResends];
86 // For each frame, we push how many events have been added to
87 // rtcp_events_ so far. We use this to make sure that
88 // send_ptrs_[N+1] is always at least kResendDelay frames behind
89 // send_ptrs_[N]. Old information is removed so that information
90 // for (kNumResends + 1) * kResendDelay frames remain.
91 std::deque<uint64> event_levels_for_past_frames_;
93 // Ensures methods are only called on the main thread.
94 base::ThreadChecker thread_checker_;
96 DISALLOW_COPY_AND_ASSIGN(ReceiverRtcpEventSubscriber);
99 } // namespace cast
100 } // namespace media
102 #endif // MEDIA_CAST_RTCP_RECEIVER_RTCP_EVENT_SUBSCRIBER_H_