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 #ifndef MEDIA_CAST_LOGGING_RECEIVER_TIME_OFFSET_ESTIMATOR_IMPL_H_
6 #define MEDIA_CAST_LOGGING_RECEIVER_TIME_OFFSET_ESTIMATOR_IMPL_H_
10 #include "base/time/time.h"
11 #include "base/threading/thread_checker.h"
12 #include "media/cast/common/mod_util.h"
13 #include "media/cast/logging/logging_defines.h"
14 #include "media/cast/logging/receiver_time_offset_estimator.h"
20 // This should be large enough so that we can collect all 3 events before
21 // the entry gets removed from the map.
22 const size_t kMaxEventTimesMapSize
= 500;
24 // The lower, this is, the faster we adjust to clock drift.
25 // (But with more jitter.)
26 const size_t kClockDriftSpeed
= 500;
29 // This implementation listens to two pair of events
30 // 1. FRAME_ACK_SENT / FRAME_ACK_RECEIVED (receiver->sender)
31 // 2. PACKET_SENT_TO_NETWORK / PACKET_RECEIVED (sender->receiver)
32 // There is a causal relationship between these events in that these events
33 // must happen in order. This class obtains the lower and upper bounds for
34 // the offset by taking the difference of timestamps.
35 class ReceiverTimeOffsetEstimatorImpl
: public ReceiverTimeOffsetEstimator
{
37 ReceiverTimeOffsetEstimatorImpl();
39 ~ReceiverTimeOffsetEstimatorImpl() final
;
41 // RawEventSubscriber implementations.
42 void OnReceiveFrameEvent(const FrameEvent
& frame_event
) final
;
43 void OnReceivePacketEvent(const PacketEvent
& packet_event
) final
;
45 // ReceiverTimeOffsetEstimator implementation.
46 bool GetReceiverOffsetBounds(base::TimeDelta
* lower_bound
,
47 base::TimeDelta
* upper_bound
) final
;
50 // This helper uses the difference between sent and recived event
51 // to calculate an upper bound on the difference between the clocks
52 // on the sender and receiver. Note that this difference can take
53 // very large positive or negative values, but the smaller value is
54 // always the better estimate, since a receive event cannot possibly
55 // happen before a send event. Note that we use this to calculate
56 // both upper and lower bounds by reversing the sender/receiver
58 class BoundCalculator
{
60 typedef std::pair
<base::TimeTicks
, base::TimeTicks
> TimeTickPair
;
61 typedef std::map
<uint64
, TimeTickPair
> EventMap
;
65 bool has_bound() const { return has_bound_
; }
66 base::TimeDelta
bound() const { return bound_
; }
68 void SetSent(uint32 rtp
,
73 void SetReceived(uint32 rtp
,
79 void UpdateBound(base::TimeTicks a
, base::TimeTicks b
);
80 void CheckUpdate(uint64 key
);
85 base::TimeDelta bound_
;
88 // Fixed size storage to store event times for recent frames.
89 BoundCalculator upper_bound_
;
90 BoundCalculator lower_bound_
;
92 base::ThreadChecker thread_checker_
;
93 DISALLOW_COPY_AND_ASSIGN(ReceiverTimeOffsetEstimatorImpl
);
99 #endif // MEDIA_CAST_LOGGING_RECEIVER_TIME_OFFSET_ESTIMATOR_IMPL_H_