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 #ifndef MEDIA_CAST_LOGGING_RECEIVER_TIME_OFFSET_ESTIMATOR_IMPL_H_ | 5 #ifndef MEDIA_CAST_LOGGING_RECEIVER_TIME_OFFSET_ESTIMATOR_IMPL_H_ |
6 #define MEDIA_CAST_LOGGING_RECEIVER_TIME_OFFSET_ESTIMATOR_IMPL_H_ | 6 #define MEDIA_CAST_LOGGING_RECEIVER_TIME_OFFSET_ESTIMATOR_IMPL_H_ |
7 | 7 |
| 8 #include <map> |
| 9 |
8 #include "base/time/time.h" | 10 #include "base/time/time.h" |
9 #include "base/threading/thread_checker.h" | 11 #include "base/threading/thread_checker.h" |
| 12 #include "media/cast/common/mod_util.h" |
10 #include "media/cast/logging/logging_defines.h" | 13 #include "media/cast/logging/logging_defines.h" |
11 #include "media/cast/logging/receiver_time_offset_estimator.h" | 14 #include "media/cast/logging/receiver_time_offset_estimator.h" |
12 | 15 |
13 namespace base { | |
14 class TickClock; | |
15 } | |
16 | |
17 namespace media { | 16 namespace media { |
18 namespace cast { | 17 namespace cast { |
19 | 18 |
20 // This implementation listens to three types of video events: | 19 |
21 // 1. FRAME_ENCODED (sender side) | 20 // This should be large enough so that we can collect all 3 events before |
22 // 2. FRAME_ACK_SENT (receiver side) | 21 // the entry gets removed from the map. |
23 // 3. FRAME_ACK_RECEIVED (sender side) | 22 const size_t kMaxEventTimesMapSize = 500; |
| 23 |
| 24 // The lower, this is, the faster we adjust to clock drift. |
| 25 // (But with more jitter.) |
| 26 const size_t kClockDriftSpeed = 500; |
| 27 |
| 28 |
| 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) |
24 // There is a causal relationship between these events in that these events | 32 // There is a causal relationship between these events in that these events |
25 // must happen in order. This class obtains the lower and upper bounds for | 33 // must happen in order. This class obtains the lower and upper bounds for |
26 // the offset by taking the difference of timestamps (2) - (1) and (2) - (3), | 34 // the offset by taking the difference of timestamps. |
27 // respectively. | |
28 // The bound will become better as the latency between the events decreases. | |
29 class ReceiverTimeOffsetEstimatorImpl : public ReceiverTimeOffsetEstimator { | 35 class ReceiverTimeOffsetEstimatorImpl : public ReceiverTimeOffsetEstimator { |
30 public: | 36 public: |
31 ReceiverTimeOffsetEstimatorImpl(base::TickClock* clock); | 37 ReceiverTimeOffsetEstimatorImpl(); |
32 | 38 |
33 virtual ~ReceiverTimeOffsetEstimatorImpl(); | 39 virtual ~ReceiverTimeOffsetEstimatorImpl(); |
34 | 40 |
35 // RawEventSubscriber implementations. | 41 // RawEventSubscriber implementations. |
36 virtual void OnReceiveFrameEvent(const FrameEvent& frame_event) OVERRIDE; | 42 virtual void OnReceiveFrameEvent(const FrameEvent& frame_event) OVERRIDE; |
37 virtual void OnReceivePacketEvent(const PacketEvent& packet_event) OVERRIDE; | 43 virtual void OnReceivePacketEvent(const PacketEvent& packet_event) OVERRIDE; |
38 | 44 |
39 // ReceiverTimeOffsetEstimator implementation. | 45 // ReceiverTimeOffsetEstimator implementation. |
40 virtual bool GetReceiverOffsetBounds(base::TimeDelta* lower_bound, | 46 virtual bool GetReceiverOffsetBounds(base::TimeDelta* lower_bound, |
41 base::TimeDelta* upper_bound) OVERRIDE; | 47 base::TimeDelta* upper_bound) OVERRIDE; |
42 | 48 |
43 private: | 49 private: |
44 struct EventTimes { | 50 // This helper uses the difference between sent and recived event |
45 base::TimeTicks event_a_time; | 51 // to calculate an upper bound on the difference between the clocks |
46 base::TimeTicks event_b_time; | 52 // on the sender and receiver. Note that this difference can take |
47 base::TimeTicks event_c_time; | 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 |
| 57 // relationship. |
| 58 class BoundCalculator { |
| 59 public: |
| 60 typedef std::pair<base::TimeTicks, base::TimeTicks> TimeTickPair; |
| 61 typedef std::map<uint64, TimeTickPair> EventMap; |
| 62 |
| 63 BoundCalculator(); |
| 64 ~BoundCalculator(); |
| 65 bool has_bound() const { return has_bound_; } |
| 66 base::TimeDelta bound() const { return bound_; } |
| 67 |
| 68 void SetSent(uint32 rtp, |
| 69 uint32 packet_id, |
| 70 bool audio, |
| 71 base::TimeTicks t); |
| 72 |
| 73 void SetReceived(uint32 rtp, |
| 74 uint16 packet_id, |
| 75 bool audio, |
| 76 base::TimeTicks t); |
| 77 |
| 78 private: |
| 79 void UpdateBound(base::TimeTicks a, base::TimeTicks b); |
| 80 void CheckUpdate(uint64 key); |
| 81 |
| 82 private: |
| 83 EventMap events_; |
| 84 bool has_bound_; |
| 85 base::TimeDelta bound_; |
48 }; | 86 }; |
49 | 87 |
50 typedef std::map<RtpTimestamp, EventTimes> EventTimesMap; | |
51 | |
52 void UpdateOffsetBounds(const EventTimes& event); | |
53 | |
54 // Fixed size storage to store event times for recent frames. | 88 // Fixed size storage to store event times for recent frames. |
55 EventTimesMap event_times_map_; | 89 BoundCalculator upper_bound_; |
56 | 90 BoundCalculator lower_bound_; |
57 bool bounded_; | |
58 base::TickClock* clock_; // Not owned by this class. | |
59 | |
60 bool offset_bounds_valid_; | |
61 base::TimeDelta offset_lower_bound_; | |
62 base::TimeDelta offset_upper_bound_; | |
63 base::TimeDelta prev_offset_lower_bound_; | |
64 base::TimeDelta prev_offset_upper_bound_; | |
65 base::TimeTicks last_reset_time_; | |
66 | 91 |
67 base::ThreadChecker thread_checker_; | 92 base::ThreadChecker thread_checker_; |
68 DISALLOW_COPY_AND_ASSIGN(ReceiverTimeOffsetEstimatorImpl); | 93 DISALLOW_COPY_AND_ASSIGN(ReceiverTimeOffsetEstimatorImpl); |
69 }; | 94 }; |
70 | 95 |
71 } // namespace cast | 96 } // namespace cast |
72 } // namespace media | 97 } // namespace media |
73 | 98 |
74 #endif // MEDIA_CAST_LOGGING_RECEIVER_TIME_OFFSET_ESTIMATOR_IMPL_H_ | 99 #endif // MEDIA_CAST_LOGGING_RECEIVER_TIME_OFFSET_ESTIMATOR_IMPL_H_ |
OLD | NEW |