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 #include "media/cast/rtcp/sender_rtcp_event_subscriber.h"
9 #include "base/logging.h"
10 #include "media/cast/rtcp/rtcp_defines.h"
15 SenderRtcpEventSubscriber::SenderRtcpEventSubscriber(
16 const size_t max_size_to_retain
)
17 : max_size_to_retain_(max_size_to_retain
) {
18 DCHECK(max_size_to_retain_
> 0u);
21 SenderRtcpEventSubscriber::~SenderRtcpEventSubscriber() {
22 DCHECK(thread_checker_
.CalledOnValidThread());
25 void SenderRtcpEventSubscriber::OnReceiveFrameEvent(
26 const FrameEvent
& frame_event
) {
27 DCHECK(thread_checker_
.CalledOnValidThread());
28 if (frame_event
.type
!= kVideoFrameCaptured
&&
29 frame_event
.type
!= kVideoFrameSentToEncoder
&&
30 frame_event
.type
!= kVideoFrameEncoded
) {
31 // Not interested in other events.
35 RtcpEventMap::iterator it
= rtcp_events_
.find(frame_event
.rtp_timestamp
);
36 if (it
== rtcp_events_
.end()) {
37 // We have not stored this frame (RTP timestamp) in our map.
39 rtcp_event
.type
= frame_event
.type
;
40 rtcp_event
.timestamp
= frame_event
.timestamp
;
42 // Do not need to fill out rtcp_event.delay_delta or rtcp_event.packet_id
43 // as they are not set in frame events we are interested in.
44 rtcp_events_
.insert(std::make_pair(frame_event
.rtp_timestamp
, rtcp_event
));
46 TruncateMapIfNeeded();
48 // We already have this frame (RTP timestamp) in our map.
49 // Only update events that are later in the chain.
50 // This is due to that events can be reordered on the wire.
51 if (frame_event
.type
== kVideoFrameCaptured
) {
52 return; // First event in chain can not be late by definition.
55 if (it
->second
.type
== kVideoFrameEncoded
) {
56 return; // Last event in chain should not be updated.
59 // Update existing entry.
60 it
->second
.type
= frame_event
.type
;
63 DCHECK(rtcp_events_
.size() <= max_size_to_retain_
);
66 void SenderRtcpEventSubscriber::OnReceivePacketEvent(
67 const PacketEvent
& packet_event
) {
68 DCHECK(thread_checker_
.CalledOnValidThread());
69 // Do nothing as RTP sender is not interested in packet events for RTCP.
72 void SenderRtcpEventSubscriber::OnReceiveGenericEvent(
73 const GenericEvent
& generic_event
) {
74 DCHECK(thread_checker_
.CalledOnValidThread());
75 // Do nothing as RTP sender is not interested in generic events for RTCP.
78 void SenderRtcpEventSubscriber::GetRtcpEventsAndReset(
79 RtcpEventMap
* rtcp_events
) {
80 DCHECK(thread_checker_
.CalledOnValidThread());
81 rtcp_events
->swap(rtcp_events_
);
85 void SenderRtcpEventSubscriber::TruncateMapIfNeeded() {
86 // If map size has exceeded |max_size_to_retain_|, remove entry with
87 // the smallest RTP timestamp.
88 if (rtcp_events_
.size() > max_size_to_retain_
) {
89 DVLOG(2) << "RTCP event map exceeded size limit; "
90 << "removing oldest entry";
91 // This is fine since we only insert elements one at a time.
92 rtcp_events_
.erase(rtcp_events_
.begin());