Ignore title parameter for navigator.registerProtocolHandler
[chromium-blink-merge.git] / media / cast / logging / receiver_time_offset_estimator_impl.cc
blob44d5eb0b3d731e954ecb32d7f393de374704c784
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 <algorithm>
6 #include <utility>
8 #include "base/logging.h"
9 #include "media/cast/logging/receiver_time_offset_estimator_impl.h"
11 namespace media {
12 namespace cast {
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()
19 : bounded_(false) {}
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)
30 return;
32 CastLoggingEvent event = frame_event.type;
33 if (event != FRAME_ENCODED && event != FRAME_ACK_SENT &&
34 event != FRAME_ACK_RECEIVED)
35 return;
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,
41 event_times)).first;
43 switch (event) {
44 case FRAME_ENCODED:
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;
49 } else {
50 event_times_map_.erase(it);
51 return;
53 break;
54 case FRAME_ACK_SENT:
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);
61 return;
63 break;
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;
69 } else {
70 it->second.event_c_time =
71 std::min(frame_event.timestamp, it->second.event_c_time);
73 break;
74 default:
75 NOTREACHED();
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) {
93 if (!bounded_)
94 return false;
96 *lower_bound = offset_lower_bound_;
97 *upper_bound = offset_upper_bound_;
98 return true;
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;
112 if (bounded_) {
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() << "].";
120 return;
123 offset_lower_bound_ = lower_bound;
124 offset_upper_bound_ = upper_bound;
125 bounded_ = true;
128 } // namespace cast
129 } // namespace media