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 adjusts 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 class BoundCalculator { |
45 base::TimeTicks event_a_time; | 51 public: |
46 base::TimeTicks event_b_time; | 52 typedef std::pair<base::TimeTicks, base::TimeTicks> TimeTickPair; |
imcheng
2014/09/09 01:04:23
would it be possible to define such that the first
hubbe
2014/09/09 19:06:15
I find it far less confusing to have the events or
| |
47 base::TimeTicks event_c_time; | 53 typedef std::map<uint64, TimeTickPair> EventMap; |
54 | |
55 BoundCalculator(); | |
56 ~BoundCalculator(); | |
57 bool has_bound() const { return has_bound_; } | |
58 base::TimeDelta bound() const { return bound_; } | |
59 | |
60 void SetSent(uint32 rtp, | |
61 uint32 packet_id, | |
62 bool audio, | |
63 base::TimeTicks t); | |
64 | |
65 void SetReceived(uint32 rtp, | |
66 uint16 packet_id, | |
67 bool audio, | |
68 base::TimeTicks t); | |
69 | |
70 private: | |
71 void UpdateBound(base::TimeTicks a, base::TimeTicks b); | |
72 void CheckUpdate(uint64 key); | |
73 | |
74 private: | |
75 EventMap events_; | |
76 bool has_bound_; | |
77 base::TimeDelta bound_; | |
48 }; | 78 }; |
49 | 79 |
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. | 80 // Fixed size storage to store event times for recent frames. |
55 EventTimesMap event_times_map_; | 81 BoundCalculator upper_bound_; |
56 | 82 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 | 83 |
67 base::ThreadChecker thread_checker_; | 84 base::ThreadChecker thread_checker_; |
68 DISALLOW_COPY_AND_ASSIGN(ReceiverTimeOffsetEstimatorImpl); | 85 DISALLOW_COPY_AND_ASSIGN(ReceiverTimeOffsetEstimatorImpl); |
69 }; | 86 }; |
70 | 87 |
71 } // namespace cast | 88 } // namespace cast |
72 } // namespace media | 89 } // namespace media |
73 | 90 |
74 #endif // MEDIA_CAST_LOGGING_RECEIVER_TIME_OFFSET_ESTIMATOR_IMPL_H_ | 91 #endif // MEDIA_CAST_LOGGING_RECEIVER_TIME_OFFSET_ESTIMATOR_IMPL_H_ |
OLD | NEW |