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