Line data Source code
1 : /*
2 : * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 : *
4 : * Use of this source code is governed by a BSD-style license
5 : * that can be found in the LICENSE file in the root of the source
6 : * tree. An additional intellectual property rights grant can be found
7 : * in the file PATENTS. All contributing project authors may
8 : * be found in the AUTHORS file in the root of the source tree.
9 : */
10 :
11 : #include "webrtc/modules/rtp_rtcp/include/remote_ntp_time_estimator.h"
12 :
13 : #include "webrtc/base/logging.h"
14 : #include "webrtc/system_wrappers/include/clock.h"
15 : #include "webrtc/system_wrappers/include/timestamp_extrapolator.h"
16 :
17 : namespace webrtc {
18 :
19 : static const int kTimingLogIntervalMs = 10000;
20 :
21 : // TODO(wu): Refactor this class so that it can be shared with
22 : // vie_sync_module.cc.
23 0 : RemoteNtpTimeEstimator::RemoteNtpTimeEstimator(Clock* clock)
24 : : clock_(clock),
25 0 : ts_extrapolator_(new TimestampExtrapolator(clock_->TimeInMilliseconds())),
26 0 : last_timing_log_ms_(-1) {
27 0 : }
28 :
29 0 : RemoteNtpTimeEstimator::~RemoteNtpTimeEstimator() {}
30 :
31 0 : bool RemoteNtpTimeEstimator::UpdateRtcpTimestamp(int64_t rtt,
32 : uint32_t ntp_secs,
33 : uint32_t ntp_frac,
34 : uint32_t rtcp_timestamp) {
35 0 : bool new_rtcp_sr = false;
36 0 : if (!rtp_to_ntp_.UpdateMeasurements(ntp_secs, ntp_frac, rtcp_timestamp,
37 : &new_rtcp_sr)) {
38 0 : return false;
39 : }
40 0 : if (!new_rtcp_sr) {
41 : // No new RTCP SR since last time this function was called.
42 0 : return true;
43 : }
44 : // Update extrapolator with the new arrival time.
45 : // The extrapolator assumes the TimeInMilliseconds time.
46 0 : int64_t receiver_arrival_time_ms = clock_->TimeInMilliseconds();
47 0 : int64_t sender_send_time_ms = Clock::NtpToMs(ntp_secs, ntp_frac);
48 0 : int64_t sender_arrival_time_90k = (sender_send_time_ms + rtt / 2) * 90;
49 0 : ts_extrapolator_->Update(receiver_arrival_time_ms, sender_arrival_time_90k);
50 0 : return true;
51 : }
52 :
53 0 : int64_t RemoteNtpTimeEstimator::Estimate(uint32_t rtp_timestamp) {
54 0 : int64_t sender_capture_ntp_ms = 0;
55 0 : if (!rtp_to_ntp_.Estimate(rtp_timestamp, &sender_capture_ntp_ms)) {
56 0 : return -1;
57 : }
58 0 : uint32_t timestamp = sender_capture_ntp_ms * 90;
59 : int64_t receiver_capture_ms =
60 0 : ts_extrapolator_->ExtrapolateLocalTime(timestamp);
61 : int64_t ntp_offset =
62 0 : clock_->CurrentNtpInMilliseconds() - clock_->TimeInMilliseconds();
63 0 : int64_t receiver_capture_ntp_ms = receiver_capture_ms + ntp_offset;
64 0 : int64_t now_ms = clock_->TimeInMilliseconds();
65 0 : if (now_ms - last_timing_log_ms_ > kTimingLogIntervalMs) {
66 0 : LOG(LS_INFO) << "RTP timestamp: " << rtp_timestamp
67 0 : << " in NTP clock: " << sender_capture_ntp_ms
68 0 : << " estimated time in receiver clock: " << receiver_capture_ms
69 0 : << " converted to NTP clock: " << receiver_capture_ntp_ms;
70 0 : last_timing_log_ms_ = now_ms;
71 : }
72 0 : return receiver_capture_ntp_ms;
73 : }
74 :
75 : } // namespace webrtc
|