blob: 1a9a3e8026daad4d6303fe50050753a99db198ab [file] [log] [blame]
Tommiad84d022020-05-10 19:03:43 +02001/*
2 * Copyright (c) 2020 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 "video/rtp_streams_synchronizer2.h"
12
13#include "absl/types/optional.h"
14#include "call/syncable.h"
15#include "rtc_base/checks.h"
16#include "rtc_base/logging.h"
17#include "rtc_base/time_utils.h"
18#include "rtc_base/trace_event.h"
19#include "system_wrappers/include/rtp_to_ntp_estimator.h"
20
21namespace webrtc {
22namespace internal {
23namespace {
24// Time interval for logging stats.
25constexpr int64_t kStatsLogIntervalMs = 10000;
26constexpr uint32_t kSyncIntervalMs = 1000;
27
28bool UpdateMeasurements(StreamSynchronization::Measurements* stream,
29 const Syncable::Info& info) {
30 stream->latest_timestamp = info.latest_received_capture_timestamp;
31 stream->latest_receive_time_ms = info.latest_receive_time_ms;
32 bool new_rtcp_sr = false;
33 return stream->rtp_to_ntp.UpdateMeasurements(
34 info.capture_time_ntp_secs, info.capture_time_ntp_frac,
35 info.capture_time_source_clock, &new_rtcp_sr);
36}
37} // namespace
38
39RtpStreamsSynchronizer::RtpStreamsSynchronizer(TaskQueueBase* main_queue,
40 Syncable* syncable_video)
41 : task_queue_(main_queue),
42 syncable_video_(syncable_video),
43 last_sync_time_(rtc::TimeNanos()),
44 last_stats_log_ms_(rtc::TimeMillis()) {
45 RTC_DCHECK(syncable_video);
46}
47
48RtpStreamsSynchronizer::~RtpStreamsSynchronizer() {
49 RTC_DCHECK_RUN_ON(&main_checker_);
50 task_safety_flag_->SetNotAlive();
51}
52
53void RtpStreamsSynchronizer::ConfigureSync(Syncable* syncable_audio) {
54 RTC_DCHECK_RUN_ON(&main_checker_);
55
56 // Prevent expensive no-ops.
57 if (syncable_audio == syncable_audio_)
58 return;
59
60 syncable_audio_ = syncable_audio;
61 sync_.reset(nullptr);
62 if (!syncable_audio_)
63 return;
64
65 sync_.reset(
66 new StreamSynchronization(syncable_video_->id(), syncable_audio_->id()));
67 QueueTimer();
68}
69
70void RtpStreamsSynchronizer::QueueTimer() {
71 RTC_DCHECK_RUN_ON(&main_checker_);
72 if (timer_running_)
73 return;
74
75 timer_running_ = true;
76 uint32_t delay = kSyncIntervalMs - (rtc::TimeNanos() - last_sync_time_) /
77 rtc::kNumNanosecsPerMillisec;
78 RTC_DCHECK_LE(delay, kSyncIntervalMs);
79 task_queue_->PostDelayedTask(ToQueuedTask([this, safety = task_safety_flag_] {
80 if (!safety->alive())
81 return;
82 RTC_DCHECK_RUN_ON(&main_checker_);
83 timer_running_ = false;
84 UpdateDelay();
85 }),
86 delay);
87}
88
89void RtpStreamsSynchronizer::UpdateDelay() {
90 RTC_DCHECK_RUN_ON(&main_checker_);
91 last_sync_time_ = rtc::TimeNanos();
92
93 if (!syncable_audio_)
94 return;
95
96 RTC_DCHECK(sync_.get());
97
98 QueueTimer();
99
100 bool log_stats = false;
101 const int64_t now_ms = rtc::TimeMillis();
102 if (now_ms - last_stats_log_ms_ > kStatsLogIntervalMs) {
103 last_stats_log_ms_ = now_ms;
104 log_stats = true;
105 }
106
107 absl::optional<Syncable::Info> audio_info = syncable_audio_->GetInfo();
108 if (!audio_info || !UpdateMeasurements(&audio_measurement_, *audio_info)) {
109 return;
110 }
111
112 int64_t last_video_receive_ms = video_measurement_.latest_receive_time_ms;
113 absl::optional<Syncable::Info> video_info = syncable_video_->GetInfo();
114 if (!video_info || !UpdateMeasurements(&video_measurement_, *video_info)) {
115 return;
116 }
117
118 if (last_video_receive_ms == video_measurement_.latest_receive_time_ms) {
119 // No new video packet has been received since last update.
120 return;
121 }
122
123 int relative_delay_ms;
124 // Calculate how much later or earlier the audio stream is compared to video.
125 if (!sync_->ComputeRelativeDelay(audio_measurement_, video_measurement_,
126 &relative_delay_ms)) {
127 return;
128 }
129
130 if (log_stats) {
131 RTC_LOG(LS_INFO) << "Sync info stats: " << now_ms
132 << ", {ssrc: " << sync_->audio_stream_id() << ", "
133 << "cur_delay_ms: " << audio_info->current_delay_ms
134 << "} {ssrc: " << sync_->video_stream_id() << ", "
135 << "cur_delay_ms: " << video_info->current_delay_ms
136 << "} {relative_delay_ms: " << relative_delay_ms << "} ";
137 }
138
139 TRACE_COUNTER1("webrtc", "SyncCurrentVideoDelay",
140 video_info->current_delay_ms);
141 TRACE_COUNTER1("webrtc", "SyncCurrentAudioDelay",
142 audio_info->current_delay_ms);
143 TRACE_COUNTER1("webrtc", "SyncRelativeDelay", relative_delay_ms);
144
145 int target_audio_delay_ms = 0;
146 int target_video_delay_ms = video_info->current_delay_ms;
147 // Calculate the necessary extra audio delay and desired total video
148 // delay to get the streams in sync.
149 if (!sync_->ComputeDelays(relative_delay_ms, audio_info->current_delay_ms,
150 &target_audio_delay_ms, &target_video_delay_ms)) {
151 return;
152 }
153
154 if (log_stats) {
155 RTC_LOG(LS_INFO) << "Sync delay stats: " << now_ms
156 << ", {ssrc: " << sync_->audio_stream_id() << ", "
157 << "target_delay_ms: " << target_audio_delay_ms
158 << "} {ssrc: " << sync_->video_stream_id() << ", "
159 << "target_delay_ms: " << target_video_delay_ms << "} ";
160 }
161
162 syncable_audio_->SetMinimumPlayoutDelay(target_audio_delay_ms);
163 syncable_video_->SetMinimumPlayoutDelay(target_video_delay_ms);
164}
165
166// TODO(https://bugs.webrtc.org/7065): Move RtpToNtpEstimator out of
167// RtpStreamsSynchronizer and into respective receive stream to always populate
168// the estimated playout timestamp.
169bool RtpStreamsSynchronizer::GetStreamSyncOffsetInMs(
170 uint32_t rtp_timestamp,
171 int64_t render_time_ms,
172 int64_t* video_playout_ntp_ms,
173 int64_t* stream_offset_ms,
174 double* estimated_freq_khz) const {
175 RTC_DCHECK_RUN_ON(&main_checker_);
176
177 if (!syncable_audio_)
178 return false;
179
180 uint32_t audio_rtp_timestamp;
181 int64_t time_ms;
182 if (!syncable_audio_->GetPlayoutRtpTimestamp(&audio_rtp_timestamp,
183 &time_ms)) {
184 return false;
185 }
186
187 int64_t latest_audio_ntp;
188 if (!audio_measurement_.rtp_to_ntp.Estimate(audio_rtp_timestamp,
189 &latest_audio_ntp)) {
190 return false;
191 }
192
193 syncable_audio_->SetEstimatedPlayoutNtpTimestampMs(latest_audio_ntp, time_ms);
194
195 int64_t latest_video_ntp;
196 if (!video_measurement_.rtp_to_ntp.Estimate(rtp_timestamp,
197 &latest_video_ntp)) {
198 return false;
199 }
200
201 // Current audio ntp.
202 int64_t now_ms = rtc::TimeMillis();
203 latest_audio_ntp += (now_ms - time_ms);
204
205 // Remove video playout delay.
206 int64_t time_to_render_ms = render_time_ms - now_ms;
207 if (time_to_render_ms > 0)
208 latest_video_ntp -= time_to_render_ms;
209
210 *video_playout_ntp_ms = latest_video_ntp;
211 *stream_offset_ms = latest_audio_ntp - latest_video_ntp;
212 *estimated_freq_khz = video_measurement_.rtp_to_ntp.params()->frequency_khz;
213 return true;
214}
215
216} // namespace internal
217} // namespace webrtc