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.
8 #include "base/logging.h"
9 #include "media/cast/logging/receiver_time_offset_estimator_impl.h"
14 // This should be large enough so that we can collect all 3 events before
15 // the entry gets removed from the map.
16 const size_t kMaxEventTimesMapSize
= 100;
18 ReceiverTimeOffsetEstimatorImpl::ReceiverTimeOffsetEstimatorImpl()
21 ReceiverTimeOffsetEstimatorImpl::~ReceiverTimeOffsetEstimatorImpl() {
22 DCHECK(thread_checker_
.CalledOnValidThread());
25 void ReceiverTimeOffsetEstimatorImpl::OnReceiveFrameEvent(
26 const FrameEvent
& frame_event
) {
27 DCHECK(thread_checker_
.CalledOnValidThread());
29 if (frame_event
.media_type
!= VIDEO_EVENT
)
32 CastLoggingEvent event
= frame_event
.type
;
33 if (event
!= FRAME_ENCODED
&& event
!= FRAME_ACK_SENT
&&
34 event
!= FRAME_ACK_RECEIVED
)
37 EventTimesMap::iterator it
= event_times_map_
.find(frame_event
.rtp_timestamp
);
38 if (it
== event_times_map_
.end()) {
39 EventTimes event_times
;
40 it
= event_times_map_
.insert(std::make_pair(frame_event
.rtp_timestamp
,
45 // Encode is supposed to happen only once. If we see duplicate event,
46 // throw away the entry.
47 if (it
->second
.event_a_time
.is_null()) {
48 it
->second
.event_a_time
= frame_event
.timestamp
;
50 event_times_map_
.erase(it
);
55 if (it
->second
.event_b_time
.is_null()) {
56 it
->second
.event_b_time
= frame_event
.timestamp
;
57 } else if (it
->second
.event_b_time
!= frame_event
.timestamp
) {
58 // Duplicate ack sent events are normal due to RTCP redundancy,
59 // but they must have the same event timestamp.
60 event_times_map_
.erase(it
);
64 case FRAME_ACK_RECEIVED
:
65 // If there are duplicate ack received events, pick the one with the
66 // smallest event timestamp so we can get a better bound.
67 if (it
->second
.event_c_time
.is_null()) {
68 it
->second
.event_c_time
= frame_event
.timestamp
;
70 it
->second
.event_c_time
=
71 std::min(frame_event
.timestamp
, it
->second
.event_c_time
);
78 if (!it
->second
.event_a_time
.is_null() &&
79 !it
->second
.event_b_time
.is_null() &&
80 !it
->second
.event_c_time
.is_null()) {
81 UpdateOffsetBounds(it
->second
);
82 event_times_map_
.erase(it
);
85 // Keep the map size at most |kMaxEventTimesMapSize|.
86 if (event_times_map_
.size() > kMaxEventTimesMapSize
)
87 event_times_map_
.erase(event_times_map_
.begin());
90 bool ReceiverTimeOffsetEstimatorImpl::GetReceiverOffsetBounds(
91 base::TimeDelta
* lower_bound
,
92 base::TimeDelta
* upper_bound
) {
96 *lower_bound
= offset_lower_bound_
;
97 *upper_bound
= offset_upper_bound_
;
101 void ReceiverTimeOffsetEstimatorImpl::OnReceivePacketEvent(
102 const PacketEvent
& packet_event
) {
103 // Not interested in packet events.
104 DCHECK(thread_checker_
.CalledOnValidThread());
107 void ReceiverTimeOffsetEstimatorImpl::UpdateOffsetBounds(
108 const EventTimes
& event
) {
109 base::TimeDelta lower_bound
= event
.event_b_time
- event
.event_c_time
;
110 base::TimeDelta upper_bound
= event
.event_b_time
- event
.event_a_time
;
113 lower_bound
= std::max(lower_bound
, offset_lower_bound_
);
114 upper_bound
= std::min(upper_bound
, offset_upper_bound_
);
117 if (lower_bound
> upper_bound
) {
118 VLOG(2) << "Got bogus offset bound values [" << lower_bound
.InMilliseconds()
119 << ", " << upper_bound
.InMilliseconds() << "].";
123 offset_lower_bound_
= lower_bound
;
124 offset_upper_bound_
= upper_bound
;