niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
leozwang@webrtc.org | 39e9659 | 2012-03-01 18:22:48 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "video/rtp_streams_synchronizer.h" |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "call/syncable.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 16 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/trace_event.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 18 | #include "system_wrappers/include/rtp_to_ntp_estimator.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
asapersson | f8cdd18 | 2016-03-15 01:00:47 -0700 | [diff] [blame] | 21 | namespace { |
asapersson | b7e7b49 | 2016-11-17 02:27:14 -0800 | [diff] [blame] | 22 | bool UpdateMeasurements(StreamSynchronization::Measurements* stream, |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 23 | const Syncable::Info& info) { |
| 24 | RTC_DCHECK(stream); |
| 25 | stream->latest_timestamp = info.latest_received_capture_timestamp; |
| 26 | stream->latest_receive_time_ms = info.latest_receive_time_ms; |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 27 | bool new_rtcp_sr = false; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 28 | if (!stream->rtp_to_ntp.UpdateMeasurements( |
| 29 | info.capture_time_ntp_secs, info.capture_time_ntp_frac, |
| 30 | info.capture_time_source_clock, &new_rtcp_sr)) { |
asapersson | b7e7b49 | 2016-11-17 02:27:14 -0800 | [diff] [blame] | 31 | return false; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 32 | } |
asapersson | b7e7b49 | 2016-11-17 02:27:14 -0800 | [diff] [blame] | 33 | return true; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 34 | } |
asapersson | f8cdd18 | 2016-03-15 01:00:47 -0700 | [diff] [blame] | 35 | } // namespace |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 36 | |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 37 | RtpStreamsSynchronizer::RtpStreamsSynchronizer(Syncable* syncable_video) |
| 38 | : syncable_video_(syncable_video), |
| 39 | syncable_audio_(nullptr), |
mflodman | 4cd2790 | 2016-08-05 06:28:45 -0700 | [diff] [blame] | 40 | sync_(), |
| 41 | last_sync_time_(rtc::TimeNanos()) { |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 42 | RTC_DCHECK(syncable_video); |
mflodman | 4cd2790 | 2016-08-05 06:28:45 -0700 | [diff] [blame] | 43 | process_thread_checker_.DetachFromThread(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Mirko Bonadei | 8fdcac3 | 2018-08-28 16:30:18 +0200 | [diff] [blame] | 46 | RtpStreamsSynchronizer::~RtpStreamsSynchronizer() = default; |
| 47 | |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 48 | void RtpStreamsSynchronizer::ConfigureSync(Syncable* syncable_audio) { |
mflodman | 4cd2790 | 2016-08-05 06:28:45 -0700 | [diff] [blame] | 49 | rtc::CritScope lock(&crit_); |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 50 | if (syncable_audio == syncable_audio_) { |
mflodman | 4cd2790 | 2016-08-05 06:28:45 -0700 | [diff] [blame] | 51 | // This prevents expensive no-ops. |
Peter Boström | 1794b26 | 2016-02-16 14:12:02 +0100 | [diff] [blame] | 52 | return; |
pbos | 8fc7fa7 | 2015-07-15 08:02:58 -0700 | [diff] [blame] | 53 | } |
mflodman | 4cd2790 | 2016-08-05 06:28:45 -0700 | [diff] [blame] | 54 | |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 55 | syncable_audio_ = syncable_audio; |
mflodman | 4cd2790 | 2016-08-05 06:28:45 -0700 | [diff] [blame] | 56 | sync_.reset(nullptr); |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 57 | if (syncable_audio_) { |
| 58 | sync_.reset(new StreamSynchronization(syncable_video_->id(), |
| 59 | syncable_audio_->id())); |
mflodman | 4cd2790 | 2016-08-05 06:28:45 -0700 | [diff] [blame] | 60 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 61 | } |
| 62 | |
mflodman | 4cd2790 | 2016-08-05 06:28:45 -0700 | [diff] [blame] | 63 | int64_t RtpStreamsSynchronizer::TimeUntilNextProcess() { |
| 64 | RTC_DCHECK_RUN_ON(&process_thread_checker_); |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 65 | const int64_t kSyncIntervalMs = 1000; |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 66 | return kSyncIntervalMs - |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 67 | (rtc::TimeNanos() - last_sync_time_) / rtc::kNumNanosecsPerMillisec; |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 68 | } |
| 69 | |
mflodman | 4cd2790 | 2016-08-05 06:28:45 -0700 | [diff] [blame] | 70 | void RtpStreamsSynchronizer::Process() { |
| 71 | RTC_DCHECK_RUN_ON(&process_thread_checker_); |
mflodman | 4cd2790 | 2016-08-05 06:28:45 -0700 | [diff] [blame] | 72 | last_sync_time_ = rtc::TimeNanos(); |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 73 | |
mflodman | 4cd2790 | 2016-08-05 06:28:45 -0700 | [diff] [blame] | 74 | rtc::CritScope lock(&crit_); |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 75 | if (!syncable_audio_) { |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 76 | return; |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 77 | } |
mflodman | 4cd2790 | 2016-08-05 06:28:45 -0700 | [diff] [blame] | 78 | RTC_DCHECK(sync_.get()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 79 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 80 | absl::optional<Syncable::Info> audio_info = syncable_audio_->GetInfo(); |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 81 | if (!audio_info || !UpdateMeasurements(&audio_measurement_, *audio_info)) { |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 82 | return; |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 83 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | |
asapersson | b0c1b4e | 2016-09-17 01:00:01 -0700 | [diff] [blame] | 85 | int64_t last_video_receive_ms = video_measurement_.latest_receive_time_ms; |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 86 | absl::optional<Syncable::Info> video_info = syncable_video_->GetInfo(); |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 87 | if (!video_info || !UpdateMeasurements(&video_measurement_, *video_info)) { |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 88 | return; |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 89 | } |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 90 | |
asapersson | b0c1b4e | 2016-09-17 01:00:01 -0700 | [diff] [blame] | 91 | if (last_video_receive_ms == video_measurement_.latest_receive_time_ms) { |
| 92 | // No new video packet has been received since last update. |
| 93 | return; |
| 94 | } |
| 95 | |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 96 | int relative_delay_ms; |
| 97 | // Calculate how much later or earlier the audio stream is compared to video. |
| 98 | if (!sync_->ComputeRelativeDelay(audio_measurement_, video_measurement_, |
| 99 | &relative_delay_ms)) { |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 100 | return; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 101 | } |
| 102 | |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 103 | TRACE_COUNTER1("webrtc", "SyncCurrentVideoDelay", |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 104 | video_info->current_delay_ms); |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 105 | TRACE_COUNTER1("webrtc", "SyncCurrentAudioDelay", |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 106 | audio_info->current_delay_ms); |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 +0000 | [diff] [blame] | 107 | TRACE_COUNTER1("webrtc", "SyncRelativeDelay", relative_delay_ms); |
hclam@chromium.org | 9b23ecb | 2013-06-14 23:30:58 +0000 | [diff] [blame] | 108 | int target_audio_delay_ms = 0; |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 109 | int target_video_delay_ms = video_info->current_delay_ms; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 110 | // Calculate the necessary extra audio delay and desired total video |
| 111 | // delay to get the streams in sync. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 112 | if (!sync_->ComputeDelays(relative_delay_ms, audio_info->current_delay_ms, |
| 113 | &target_audio_delay_ms, &target_video_delay_ms)) { |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 114 | return; |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 115 | } |
edjee@google.com | 79b0289 | 2013-04-04 19:43:34 +0000 | [diff] [blame] | 116 | |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 117 | syncable_audio_->SetMinimumPlayoutDelay(target_audio_delay_ms); |
| 118 | syncable_video_->SetMinimumPlayoutDelay(target_video_delay_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 119 | } |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 120 | |
mflodman | 4cd2790 | 2016-08-05 06:28:45 -0700 | [diff] [blame] | 121 | bool RtpStreamsSynchronizer::GetStreamSyncOffsetInMs( |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 122 | uint32_t timestamp, |
| 123 | int64_t render_time_ms, |
asapersson | de9e5ff | 2016-11-02 07:14:03 -0700 | [diff] [blame] | 124 | int64_t* stream_offset_ms, |
| 125 | double* estimated_freq_khz) const { |
mflodman | 4cd2790 | 2016-08-05 06:28:45 -0700 | [diff] [blame] | 126 | rtc::CritScope lock(&crit_); |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 127 | if (!syncable_audio_) { |
asapersson | f8cdd18 | 2016-03-15 01:00:47 -0700 | [diff] [blame] | 128 | return false; |
| 129 | } |
| 130 | |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 131 | uint32_t playout_timestamp = syncable_audio_->GetPlayoutTimestamp(); |
| 132 | |
asapersson | f8cdd18 | 2016-03-15 01:00:47 -0700 | [diff] [blame] | 133 | int64_t latest_audio_ntp; |
asapersson | fe50b4d | 2016-12-22 07:53:51 -0800 | [diff] [blame] | 134 | if (!audio_measurement_.rtp_to_ntp.Estimate(playout_timestamp, |
| 135 | &latest_audio_ntp)) { |
asapersson | f8cdd18 | 2016-03-15 01:00:47 -0700 | [diff] [blame] | 136 | return false; |
| 137 | } |
| 138 | |
| 139 | int64_t latest_video_ntp; |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 140 | if (!video_measurement_.rtp_to_ntp.Estimate(timestamp, &latest_video_ntp)) { |
asapersson | f8cdd18 | 2016-03-15 01:00:47 -0700 | [diff] [blame] | 141 | return false; |
| 142 | } |
| 143 | |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 144 | int64_t time_to_render_ms = render_time_ms - rtc::TimeMillis(); |
asapersson | f8cdd18 | 2016-03-15 01:00:47 -0700 | [diff] [blame] | 145 | if (time_to_render_ms > 0) |
| 146 | latest_video_ntp += time_to_render_ms; |
| 147 | |
| 148 | *stream_offset_ms = latest_audio_ntp - latest_video_ntp; |
Ilya Nikolaevskiy | 558cabf | 2017-11-14 10:32:15 +0100 | [diff] [blame] | 149 | *estimated_freq_khz = video_measurement_.rtp_to_ntp.params()->frequency_khz; |
asapersson | f8cdd18 | 2016-03-15 01:00:47 -0700 | [diff] [blame] | 150 | return true; |
| 151 | } |
| 152 | |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 153 | } // namespace webrtc |