| 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 |
| 21 | namespace webrtc { |
| 22 | namespace internal { |
| 23 | namespace { |
| 24 | // Time interval for logging stats. |
| 25 | constexpr int64_t kStatsLogIntervalMs = 10000; |
| 26 | constexpr TimeDelta kSyncInterval = TimeDelta::Millis(1000); |
| 27 |
| 28 | bool 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 |
| 38 | } // namespace |
| 39 |
| 40 | RtpStreamsSynchronizer::RtpStreamsSynchronizer(TaskQueueBase* main_queue, |
| 41 | Syncable* syncable_video) |
| 42 | : task_queue_(main_queue), |
| 43 | syncable_video_(syncable_video), |
| 44 | last_stats_log_ms_(rtc::TimeMillis()) { |
| 45 | RTC_DCHECK(syncable_video); |
| 46 | } |
| 47 |
| 48 | RtpStreamsSynchronizer::~RtpStreamsSynchronizer() { |
| 49 | RTC_DCHECK_RUN_ON(&main_checker_); |
| 50 | repeating_task_.Stop(); |
| 51 | } |
| 52 |
| 53 | void 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 | repeating_task_.Stop(); |
| 64 | return; |
| 65 | } |
| 66 |
| 67 | sync_.reset( |
| 68 | new StreamSynchronization(syncable_video_->id(), syncable_audio_->id())); |
| 69 |
| 70 | if (repeating_task_.Running()) |
| 71 | return; |
| 72 |
| 73 | repeating_task_ = |
| 74 | RepeatingTaskHandle::DelayedStart(task_queue_, kSyncInterval, [this]() { |
| 75 | UpdateDelay(); |
| 76 | return kSyncInterval; |
| 77 | }); |
| 78 | } |
| 79 |
| 80 | void RtpStreamsSynchronizer::UpdateDelay() { |
| 81 | RTC_DCHECK_RUN_ON(&main_checker_); |
| 82 |
| 83 | if (!syncable_audio_) |
| 84 | return; |
| 85 |
| 86 | RTC_DCHECK(sync_.get()); |
| 87 |
| 88 | bool log_stats = false; |
| 89 | const int64_t now_ms = rtc::TimeMillis(); |
| 90 | if (now_ms - last_stats_log_ms_ > kStatsLogIntervalMs) { |
| 91 | last_stats_log_ms_ = now_ms; |
| 92 | log_stats = true; |
| 93 | } |
| 94 |
| 95 | absl::optional<Syncable::Info> audio_info = syncable_audio_->GetInfo(); |
| 96 | if (!audio_info || !UpdateMeasurements(&audio_measurement_, *audio_info)) { |
| 97 | return; |
| 98 | } |
| 99 |
| 100 | int64_t last_video_receive_ms = video_measurement_.latest_receive_time_ms; |
| 101 | absl::optional<Syncable::Info> video_info = syncable_video_->GetInfo(); |
| 102 | if (!video_info || !UpdateMeasurements(&video_measurement_, *video_info)) { |
| 103 | return; |
| 104 | } |
| 105 |
| 106 | if (last_video_receive_ms == video_measurement_.latest_receive_time_ms) { |
| 107 | // No new video packet has been received since last update. |
| 108 | return; |
| 109 | } |
| 110 |
| 111 | int relative_delay_ms; |
| 112 | // Calculate how much later or earlier the audio stream is compared to video. |
| 113 | if (!sync_->ComputeRelativeDelay(audio_measurement_, video_measurement_, |
| 114 | &relative_delay_ms)) { |
| 115 | return; |
| 116 | } |
| 117 |
| 118 | if (log_stats) { |
| 119 | RTC_LOG(LS_INFO) << "Sync info stats: " << now_ms |
| 120 | << ", {ssrc: " << sync_->audio_stream_id() << ", " |
| 121 | << "cur_delay_ms: " << audio_info->current_delay_ms |
| 122 | << "} {ssrc: " << sync_->video_stream_id() << ", " |
| 123 | << "cur_delay_ms: " << video_info->current_delay_ms |
| 124 | << "} {relative_delay_ms: " << relative_delay_ms << "} "; |
| 125 | } |
| 126 |
| 127 | TRACE_COUNTER1("webrtc", "SyncCurrentVideoDelay", |
| 128 | video_info->current_delay_ms); |
| 129 | TRACE_COUNTER1("webrtc", "SyncCurrentAudioDelay", |
| 130 | audio_info->current_delay_ms); |
| 131 | TRACE_COUNTER1("webrtc", "SyncRelativeDelay", relative_delay_ms); |
| 132 |
| 133 | int target_audio_delay_ms = 0; |
| 134 | int target_video_delay_ms = video_info->current_delay_ms; |
| 135 | // Calculate the necessary extra audio delay and desired total video |
| 136 | // delay to get the streams in sync. |
| 137 | if (!sync_->ComputeDelays(relative_delay_ms, audio_info->current_delay_ms, |
| 138 | &target_audio_delay_ms, &target_video_delay_ms)) { |
| 139 | return; |
| 140 | } |
| 141 |
| 142 | if (log_stats) { |
| 143 | RTC_LOG(LS_INFO) << "Sync delay stats: " << now_ms |
| 144 | << ", {ssrc: " << sync_->audio_stream_id() << ", " |
| 145 | << "target_delay_ms: " << target_audio_delay_ms |
| 146 | << "} {ssrc: " << sync_->video_stream_id() << ", " |
| 147 | << "target_delay_ms: " << target_video_delay_ms << "} "; |
| 148 | } |
| 149 |
| 150 | syncable_audio_->SetMinimumPlayoutDelay(target_audio_delay_ms); |
| 151 | syncable_video_->SetMinimumPlayoutDelay(target_video_delay_ms); |
| 152 | } |
| 153 |
| 154 | // TODO(https://bugs.webrtc.org/7065): Move RtpToNtpEstimator out of |
| 155 | // RtpStreamsSynchronizer and into respective receive stream to always populate |
| 156 | // the estimated playout timestamp. |
| 157 | bool RtpStreamsSynchronizer::GetStreamSyncOffsetInMs( |
| 158 | uint32_t rtp_timestamp, |
| 159 | int64_t render_time_ms, |
| 160 | int64_t* video_playout_ntp_ms, |
| 161 | int64_t* stream_offset_ms, |
| 162 | double* estimated_freq_khz) const { |
| 163 | RTC_DCHECK_RUN_ON(&main_checker_); |
| 164 |
| 165 | if (!syncable_audio_) |
| 166 | return false; |
| 167 |
| 168 | uint32_t audio_rtp_timestamp; |
| 169 | int64_t time_ms; |
| 170 | if (!syncable_audio_->GetPlayoutRtpTimestamp(&audio_rtp_timestamp, |
| 171 | &time_ms)) { |
| 172 | return false; |
| 173 | } |
| 174 |
| 175 | int64_t latest_audio_ntp; |
| 176 | if (!audio_measurement_.rtp_to_ntp.Estimate(audio_rtp_timestamp, |
| 177 | &latest_audio_ntp)) { |
| 178 | return false; |
| 179 | } |
| 180 |
| 181 | syncable_audio_->SetEstimatedPlayoutNtpTimestampMs(latest_audio_ntp, time_ms); |
| 182 |
| 183 | int64_t latest_video_ntp; |
| 184 | if (!video_measurement_.rtp_to_ntp.Estimate(rtp_timestamp, |
| 185 | &latest_video_ntp)) { |
| 186 | return false; |
| 187 | } |
| 188 |
| 189 | // Current audio ntp. |
| 190 | int64_t now_ms = rtc::TimeMillis(); |
| 191 | latest_audio_ntp += (now_ms - time_ms); |
| 192 |
| 193 | // Remove video playout delay. |
| 194 | int64_t time_to_render_ms = render_time_ms - now_ms; |
| 195 | if (time_to_render_ms > 0) |
| 196 | latest_video_ntp -= time_to_render_ms; |
| 197 |
| 198 | *video_playout_ntp_ms = latest_video_ntp; |
| 199 | *stream_offset_ms = latest_audio_ntp - latest_video_ntp; |
| 200 | *estimated_freq_khz = video_measurement_.rtp_to_ntp.params()->frequency_khz; |
| 201 | return true; |
| 202 | } |
| 203 |
| 204 | } // namespace internal |
| 205 | } // namespace webrtc |
| 206 |