OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <algorithm> | 5 #include <algorithm> |
6 #include <utility> | 6 #include <utility> |
7 | 7 |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/time/tick_clock.h" |
9 #include "media/cast/logging/receiver_time_offset_estimator_impl.h" | 10 #include "media/cast/logging/receiver_time_offset_estimator_impl.h" |
10 | 11 |
11 namespace media { | 12 namespace media { |
12 namespace cast { | 13 namespace cast { |
13 | 14 |
14 // This should be large enough so that we can collect all 3 events before | 15 // This should be large enough so that we can collect all 3 events before |
15 // the entry gets removed from the map. | 16 // the entry gets removed from the map. |
16 const size_t kMaxEventTimesMapSize = 100; | 17 const size_t kMaxEventTimesMapSize = 100; |
17 | 18 |
18 ReceiverTimeOffsetEstimatorImpl::ReceiverTimeOffsetEstimatorImpl() | 19 ReceiverTimeOffsetEstimatorImpl::ReceiverTimeOffsetEstimatorImpl( |
19 : bounded_(false) {} | 20 base::TickClock* clock) |
| 21 : bounded_(false), |
| 22 clock_(clock), |
| 23 offset_bounds_valid_(false), |
| 24 last_reset_time_(clock_->NowTicks()) { |
| 25 } |
20 | 26 |
21 ReceiverTimeOffsetEstimatorImpl::~ReceiverTimeOffsetEstimatorImpl() { | 27 ReceiverTimeOffsetEstimatorImpl::~ReceiverTimeOffsetEstimatorImpl() { |
22 DCHECK(thread_checker_.CalledOnValidThread()); | 28 DCHECK(thread_checker_.CalledOnValidThread()); |
23 } | 29 } |
24 | 30 |
25 void ReceiverTimeOffsetEstimatorImpl::OnReceiveFrameEvent( | 31 void ReceiverTimeOffsetEstimatorImpl::OnReceiveFrameEvent( |
26 const FrameEvent& frame_event) { | 32 const FrameEvent& frame_event) { |
27 DCHECK(thread_checker_.CalledOnValidThread()); | 33 DCHECK(thread_checker_.CalledOnValidThread()); |
28 | 34 |
29 if (frame_event.media_type != VIDEO_EVENT) | 35 if (frame_event.media_type != VIDEO_EVENT) |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 if (event_times_map_.size() > kMaxEventTimesMapSize) | 92 if (event_times_map_.size() > kMaxEventTimesMapSize) |
87 event_times_map_.erase(event_times_map_.begin()); | 93 event_times_map_.erase(event_times_map_.begin()); |
88 } | 94 } |
89 | 95 |
90 bool ReceiverTimeOffsetEstimatorImpl::GetReceiverOffsetBounds( | 96 bool ReceiverTimeOffsetEstimatorImpl::GetReceiverOffsetBounds( |
91 base::TimeDelta* lower_bound, | 97 base::TimeDelta* lower_bound, |
92 base::TimeDelta* upper_bound) { | 98 base::TimeDelta* upper_bound) { |
93 if (!bounded_) | 99 if (!bounded_) |
94 return false; | 100 return false; |
95 | 101 |
96 *lower_bound = offset_lower_bound_; | 102 *lower_bound = prev_offset_lower_bound_; |
97 *upper_bound = offset_upper_bound_; | 103 *upper_bound = prev_offset_upper_bound_; |
98 return true; | 104 return true; |
99 } | 105 } |
100 | 106 |
101 void ReceiverTimeOffsetEstimatorImpl::OnReceivePacketEvent( | 107 void ReceiverTimeOffsetEstimatorImpl::OnReceivePacketEvent( |
102 const PacketEvent& packet_event) { | 108 const PacketEvent& packet_event) { |
103 // Not interested in packet events. | 109 // Not interested in packet events. |
104 DCHECK(thread_checker_.CalledOnValidThread()); | 110 DCHECK(thread_checker_.CalledOnValidThread()); |
105 } | 111 } |
106 | 112 |
107 void ReceiverTimeOffsetEstimatorImpl::UpdateOffsetBounds( | 113 void ReceiverTimeOffsetEstimatorImpl::UpdateOffsetBounds( |
108 const EventTimes& event) { | 114 const EventTimes& event) { |
109 base::TimeDelta lower_bound = event.event_b_time - event.event_c_time; | 115 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; | 116 base::TimeDelta upper_bound = event.event_b_time - event.event_a_time; |
111 | 117 |
112 if (bounded_) { | 118 if (offset_bounds_valid_) { |
113 lower_bound = std::max(lower_bound, offset_lower_bound_); | 119 lower_bound = std::max(lower_bound, offset_lower_bound_); |
114 upper_bound = std::min(upper_bound, offset_upper_bound_); | 120 upper_bound = std::min(upper_bound, offset_upper_bound_); |
115 } | 121 } |
116 | 122 |
117 if (lower_bound > upper_bound) { | 123 if (lower_bound > upper_bound) { |
118 VLOG(2) << "Got bogus offset bound values [" << lower_bound.InMilliseconds() | 124 VLOG(2) << "Got bogus offset bound values [" << lower_bound.InMilliseconds() |
119 << ", " << upper_bound.InMilliseconds() << "]."; | 125 << ", " << upper_bound.InMilliseconds() << "]."; |
120 return; | 126 return; |
121 } | 127 } |
122 | 128 |
123 offset_lower_bound_ = lower_bound; | 129 offset_lower_bound_ = lower_bound; |
124 offset_upper_bound_ = upper_bound; | 130 offset_upper_bound_ = upper_bound; |
| 131 offset_bounds_valid_ = true; |
| 132 if (!bounded_ || |
| 133 offset_upper_bound_ - offset_lower_bound_ < |
| 134 base::TimeDelta::FromMilliseconds(20)) { |
| 135 prev_offset_lower_bound_ = offset_lower_bound_; |
| 136 prev_offset_upper_bound_ = offset_upper_bound_; |
| 137 } |
| 138 |
| 139 base::TimeTicks now = clock_->NowTicks(); |
| 140 if (now - last_reset_time_ > base::TimeDelta::FromSeconds(20)) { |
| 141 last_reset_time_ = now; |
| 142 offset_lower_bound_ = base::TimeDelta(); |
| 143 offset_upper_bound_ = base::TimeDelta(); |
| 144 offset_bounds_valid_ = false; |
| 145 } |
| 146 |
125 bounded_ = true; | 147 bounded_ = true; |
126 } | 148 } |
127 | 149 |
128 } // namespace cast | 150 } // namespace cast |
129 } // namespace media | 151 } // namespace media |
OLD | NEW |