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