summaryrefslogtreecommitdiffstats
path: root/media/cast/logging/receiver_time_offset_estimator_impl.cc
blob: 4a8459744ee8772e73d2f024e5f822f585f40f54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <algorithm>
#include <utility>

#include "base/logging.h"
#include "base/time/tick_clock.h"
#include "media/cast/logging/receiver_time_offset_estimator_impl.h"

namespace media {
namespace cast {

namespace {

// Bitwise merging of values to produce an ordered key for entries in the
// BoundCalculator::events_ map.
uint64_t MakeEventKey(RtpTimeTicks rtp, uint16_t packet_id, bool audio) {
  return (static_cast<uint64_t>(rtp.lower_32_bits()) << 32) |
         (static_cast<uint64_t>(packet_id) << 1) |
         (audio ? UINT64_C(1) : UINT64_C(0));
}

}  // namespace

ReceiverTimeOffsetEstimatorImpl::BoundCalculator::BoundCalculator()
    : has_bound_(false) {}
ReceiverTimeOffsetEstimatorImpl::BoundCalculator::~BoundCalculator() {}

void ReceiverTimeOffsetEstimatorImpl::BoundCalculator::SetSent(
    RtpTimeTicks rtp,
    uint16_t packet_id,
    bool audio,
    base::TimeTicks t) {
  const uint64_t key = MakeEventKey(rtp, packet_id, audio);
  events_[key].first = t;
  CheckUpdate(key);
}

void ReceiverTimeOffsetEstimatorImpl::BoundCalculator::SetReceived(
    RtpTimeTicks rtp,
    uint16_t packet_id,
    bool audio,
    base::TimeTicks t) {
  const uint64_t key = MakeEventKey(rtp, packet_id, audio);
  events_[key].second = t;
  CheckUpdate(key);
}

void ReceiverTimeOffsetEstimatorImpl::BoundCalculator::UpdateBound(
    base::TimeTicks sent, base::TimeTicks received) {
    base::TimeDelta delta = received - sent;
    if (has_bound_) {
      if (delta < bound_) {
        bound_ = delta;
      } else {
        bound_ += (delta - bound_) / kClockDriftSpeed;
      }
    } else {
      bound_ = delta;
    }
    has_bound_ = true;
  }

  void ReceiverTimeOffsetEstimatorImpl::BoundCalculator::CheckUpdate(
      uint64_t key) {
  const TimeTickPair& ticks = events_[key];
  if (!ticks.first.is_null() && !ticks.second.is_null()) {
    UpdateBound(ticks.first, ticks.second);
    events_.erase(key);
    return;
  }

  if (events_.size() > kMaxEventTimesMapSize) {
    EventMap::iterator i = ModMapOldest(&events_);
    if (i != events_.end()) {
      events_.erase(i);
    }
  }
}

ReceiverTimeOffsetEstimatorImpl::ReceiverTimeOffsetEstimatorImpl() {
}

ReceiverTimeOffsetEstimatorImpl::~ReceiverTimeOffsetEstimatorImpl() {
  DCHECK(thread_checker_.CalledOnValidThread());
}


void ReceiverTimeOffsetEstimatorImpl::OnReceiveFrameEvent(
    const FrameEvent& frame_event) {
  DCHECK(thread_checker_.CalledOnValidThread());
  switch (frame_event.type) {
    case FRAME_ACK_SENT:
      lower_bound_.SetSent(frame_event.rtp_timestamp,
                           0,
                           frame_event.media_type == AUDIO_EVENT,
                           frame_event.timestamp);
      break;
    case FRAME_ACK_RECEIVED:
      lower_bound_.SetReceived(frame_event.rtp_timestamp,
                               0,
                               frame_event.media_type == AUDIO_EVENT,
                               frame_event.timestamp);
      break;
    default:
      // Ignored
      break;
  }
}

bool ReceiverTimeOffsetEstimatorImpl::GetReceiverOffsetBounds(
    base::TimeDelta* lower_bound,
    base::TimeDelta* upper_bound) {
  if (!lower_bound_.has_bound() || !upper_bound_.has_bound())
    return false;

  *lower_bound = -lower_bound_.bound();
  *upper_bound = upper_bound_.bound();

  // Sanitize the output, we don't want the upper bound to be
  // lower than the lower bound, make them the same.
  if (upper_bound < lower_bound) {
    lower_bound += (lower_bound - upper_bound) / 2;
    upper_bound = lower_bound;
  }
  return true;
}

void ReceiverTimeOffsetEstimatorImpl::OnReceivePacketEvent(
    const PacketEvent& packet_event) {
  DCHECK(thread_checker_.CalledOnValidThread());
  switch (packet_event.type) {
    case PACKET_SENT_TO_NETWORK:
      upper_bound_.SetSent(packet_event.rtp_timestamp,
                           packet_event.packet_id,
                           packet_event.media_type == AUDIO_EVENT,
                           packet_event.timestamp);
      break;
    case PACKET_RECEIVED:
      upper_bound_.SetReceived(packet_event.rtp_timestamp,
                               packet_event.packet_id,
                               packet_event.media_type == AUDIO_EVENT,
                               packet_event.timestamp);
      break;
    default:
      // Ignored
      break;
  }
}


}  // namespace cast
}  // namespace media