stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "video/stream_synchronization.h" |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 | [diff] [blame] | 12 | |
stefan@webrtc.org | 47fadba | 2013-11-25 12:03:56 | [diff] [blame] | 13 | #include <stdlib.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 14 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 | [diff] [blame] | 15 | #include <algorithm> |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 17 | #include "rtc_base/logging.h" |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 | [diff] [blame] | 21 | static const int kMaxChangeMs = 80; |
| 22 | static const int kMaxDeltaDelayMs = 10000; |
| 23 | static const int kFilterLength = 4; |
| 24 | // Minimum difference between audio and video to warrant a change. |
| 25 | static const int kMinDeltaMs = 30; |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 26 | |
Åsa Persson | 74d2b1d | 2020-02-10 15:33:29 | [diff] [blame] | 27 | StreamSynchronization::StreamSynchronization(uint32_t video_stream_id, |
| 28 | uint32_t audio_stream_id) |
solenberg | 3ebbcb5 | 2017-01-31 11:58:40 | [diff] [blame] | 29 | : video_stream_id_(video_stream_id), |
| 30 | audio_stream_id_(audio_stream_id), |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 | [diff] [blame] | 31 | base_target_delay_ms_(0), |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 32 | avg_diff_ms_(0) {} |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 33 | |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 | [diff] [blame] | 34 | bool StreamSynchronization::ComputeRelativeDelay( |
| 35 | const Measurements& audio_measurement, |
| 36 | const Measurements& video_measurement, |
| 37 | int* relative_delay_ms) { |
Danil Chapovalov | ae4fb61 | 2022-03-14 11:31:46 | [diff] [blame] | 38 | NtpTime audio_last_capture_time = |
| 39 | audio_measurement.rtp_to_ntp.Estimate(audio_measurement.latest_timestamp); |
| 40 | if (!audio_last_capture_time.Valid()) { |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 | [diff] [blame] | 41 | return false; |
| 42 | } |
Danil Chapovalov | ae4fb61 | 2022-03-14 11:31:46 | [diff] [blame] | 43 | NtpTime video_last_capture_time = |
| 44 | video_measurement.rtp_to_ntp.Estimate(video_measurement.latest_timestamp); |
| 45 | if (!video_last_capture_time.Valid()) { |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 | [diff] [blame] | 46 | return false; |
| 47 | } |
Danil Chapovalov | ae4fb61 | 2022-03-14 11:31:46 | [diff] [blame] | 48 | int64_t audio_last_capture_time_ms = audio_last_capture_time.ToMs(); |
| 49 | int64_t video_last_capture_time_ms = video_last_capture_time.ToMs(); |
| 50 | |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 | [diff] [blame] | 51 | // Positive diff means that video_measurement is behind audio_measurement. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 52 | *relative_delay_ms = |
| 53 | video_measurement.latest_receive_time_ms - |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 | [diff] [blame] | 54 | audio_measurement.latest_receive_time_ms - |
| 55 | (video_last_capture_time_ms - audio_last_capture_time_ms); |
Åsa Persson | 74d2b1d | 2020-02-10 15:33:29 | [diff] [blame] | 56 | |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 | [diff] [blame] | 57 | if (*relative_delay_ms > kMaxDeltaDelayMs || |
| 58 | *relative_delay_ms < -kMaxDeltaDelayMs) { |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 | [diff] [blame] | 59 | return false; |
| 60 | } |
| 61 | return true; |
| 62 | } |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 63 | |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 | [diff] [blame] | 64 | bool StreamSynchronization::ComputeDelays(int relative_delay_ms, |
| 65 | int current_audio_delay_ms, |
turaj@webrtc.org | e46c8d3 | 2013-05-22 20:39:43 | [diff] [blame] | 66 | int* total_audio_delay_target_ms, |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 | [diff] [blame] | 67 | int* total_video_delay_target_ms) { |
pwestin@webrtc.org | d35964a | 2013-04-30 16:06:10 | [diff] [blame] | 68 | int current_video_delay_ms = *total_video_delay_target_ms; |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 69 | |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 70 | RTC_LOG(LS_VERBOSE) << "Audio delay: " << current_audio_delay_ms |
| 71 | << " current diff: " << relative_delay_ms |
| 72 | << " for stream " << audio_stream_id_; |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 73 | |
| 74 | // Calculate the difference between the lowest possible video delay and the |
| 75 | // current audio delay. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 76 | int current_diff_ms = |
| 77 | current_video_delay_ms - current_audio_delay_ms + relative_delay_ms; |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 78 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 79 | avg_diff_ms_ = |
| 80 | ((kFilterLength - 1) * avg_diff_ms_ + current_diff_ms) / kFilterLength; |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 | [diff] [blame] | 81 | if (abs(avg_diff_ms_) < kMinDeltaMs) { |
| 82 | // Don't adjust if the diff is within our margin. |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | // Make sure we don't move too fast. |
| 87 | int diff_ms = avg_diff_ms_ / 2; |
| 88 | diff_ms = std::min(diff_ms, kMaxChangeMs); |
| 89 | diff_ms = std::max(diff_ms, -kMaxChangeMs); |
| 90 | |
pwestin@webrtc.org | d35964a | 2013-04-30 16:06:10 | [diff] [blame] | 91 | // Reset the average after a move to prevent overshooting reaction. |
| 92 | avg_diff_ms_ = 0; |
| 93 | |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 | [diff] [blame] | 94 | if (diff_ms > 0) { |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 95 | // The minimum video delay is longer than the current audio delay. |
pwestin@webrtc.org | d35964a | 2013-04-30 16:06:10 | [diff] [blame] | 96 | // We need to decrease extra video delay, or add extra audio delay. |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 97 | if (video_delay_.extra_ms > base_target_delay_ms_) { |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 98 | // We have extra delay added to ViE. Reduce this delay before adding |
| 99 | // extra delay to VoE. |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 100 | video_delay_.extra_ms -= diff_ms; |
| 101 | audio_delay_.extra_ms = base_target_delay_ms_; |
| 102 | } else { // video_delay_.extra_ms > 0 |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 103 | // We have no extra video delay to remove, increase the audio delay. |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 104 | audio_delay_.extra_ms += diff_ms; |
| 105 | video_delay_.extra_ms = base_target_delay_ms_; |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 106 | } |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 | [diff] [blame] | 107 | } else { // if (diff_ms > 0) |
pwestin@webrtc.org | d35964a | 2013-04-30 16:06:10 | [diff] [blame] | 108 | // The video delay is lower than the current audio delay. |
| 109 | // We need to decrease extra audio delay, or add extra video delay. |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 110 | if (audio_delay_.extra_ms > base_target_delay_ms_) { |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 | [diff] [blame] | 111 | // We have extra delay in VoiceEngine. |
| 112 | // Start with decreasing the voice delay. |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 | [diff] [blame] | 113 | // Note: diff_ms is negative; add the negative difference. |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 114 | audio_delay_.extra_ms += diff_ms; |
| 115 | video_delay_.extra_ms = base_target_delay_ms_; |
| 116 | } else { // audio_delay_.extra_ms > base_target_delay_ms_ |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 117 | // We have no extra delay in VoiceEngine, increase the video delay. |
pwestin@webrtc.org | d35964a | 2013-04-30 16:06:10 | [diff] [blame] | 118 | // Note: diff_ms is negative; subtract the negative difference. |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 119 | video_delay_.extra_ms -= diff_ms; // X - (-Y) = X + Y. |
| 120 | audio_delay_.extra_ms = base_target_delay_ms_; |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 121 | } |
| 122 | } |
pwestin@webrtc.org | d35964a | 2013-04-30 16:06:10 | [diff] [blame] | 123 | |
| 124 | // Make sure that video is never below our target. |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 125 | video_delay_.extra_ms = |
| 126 | std::max(video_delay_.extra_ms, base_target_delay_ms_); |
pwestin@webrtc.org | d35964a | 2013-04-30 16:06:10 | [diff] [blame] | 127 | |
| 128 | int new_video_delay_ms; |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 129 | if (video_delay_.extra_ms > base_target_delay_ms_) { |
| 130 | new_video_delay_ms = video_delay_.extra_ms; |
pwestin@webrtc.org | d35964a | 2013-04-30 16:06:10 | [diff] [blame] | 131 | } else { |
| 132 | // No change to the extra video delay. We are changing audio and we only |
| 133 | // allow to change one at the time. |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 134 | new_video_delay_ms = video_delay_.last_ms; |
pwestin@webrtc.org | d35964a | 2013-04-30 16:06:10 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // Make sure that we don't go below the extra video delay. |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 138 | new_video_delay_ms = std::max(new_video_delay_ms, video_delay_.extra_ms); |
pwestin@webrtc.org | d35964a | 2013-04-30 16:06:10 | [diff] [blame] | 139 | |
| 140 | // Verify we don't go above the maximum allowed video delay. |
| 141 | new_video_delay_ms = |
| 142 | std::min(new_video_delay_ms, base_target_delay_ms_ + kMaxDeltaDelayMs); |
| 143 | |
turaj@webrtc.org | e46c8d3 | 2013-05-22 20:39:43 | [diff] [blame] | 144 | int new_audio_delay_ms; |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 145 | if (audio_delay_.extra_ms > base_target_delay_ms_) { |
| 146 | new_audio_delay_ms = audio_delay_.extra_ms; |
turaj@webrtc.org | e46c8d3 | 2013-05-22 20:39:43 | [diff] [blame] | 147 | } else { |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 148 | // No change to the audio delay. We are changing video and we only allow to |
| 149 | // change one at the time. |
| 150 | new_audio_delay_ms = audio_delay_.last_ms; |
turaj@webrtc.org | e46c8d3 | 2013-05-22 20:39:43 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // Make sure that we don't go below the extra audio delay. |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 154 | new_audio_delay_ms = std::max(new_audio_delay_ms, audio_delay_.extra_ms); |
pwestin@webrtc.org | d35964a | 2013-04-30 16:06:10 | [diff] [blame] | 155 | |
| 156 | // Verify we don't go above the maximum allowed audio delay. |
turaj@webrtc.org | e46c8d3 | 2013-05-22 20:39:43 | [diff] [blame] | 157 | new_audio_delay_ms = |
| 158 | std::min(new_audio_delay_ms, base_target_delay_ms_ + kMaxDeltaDelayMs); |
pwestin@webrtc.org | d35964a | 2013-04-30 16:06:10 | [diff] [blame] | 159 | |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 160 | video_delay_.last_ms = new_video_delay_ms; |
| 161 | audio_delay_.last_ms = new_audio_delay_ms; |
pwestin@webrtc.org | d35964a | 2013-04-30 16:06:10 | [diff] [blame] | 162 | |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 163 | RTC_LOG(LS_VERBOSE) << "Sync video delay " << new_video_delay_ms |
| 164 | << " for video stream " << video_stream_id_ |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 165 | << " and audio delay " << audio_delay_.extra_ms |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 166 | << " for audio stream " << audio_stream_id_; |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 167 | |
pwestin@webrtc.org | d35964a | 2013-04-30 16:06:10 | [diff] [blame] | 168 | *total_video_delay_target_ms = new_video_delay_ms; |
turaj@webrtc.org | e46c8d3 | 2013-05-22 20:39:43 | [diff] [blame] | 169 | *total_audio_delay_target_ms = new_audio_delay_ms; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 | [diff] [blame] | 170 | return true; |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 171 | } |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 | [diff] [blame] | 172 | |
| 173 | void StreamSynchronization::SetTargetBufferingDelay(int target_delay_ms) { |
mikhal@webrtc.org | 0d8d010 | 2013-02-22 19:30:44 | [diff] [blame] | 174 | // Initial extra delay for audio (accounting for existing extra delay). |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 175 | audio_delay_.extra_ms += target_delay_ms - base_target_delay_ms_; |
| 176 | audio_delay_.last_ms += target_delay_ms - base_target_delay_ms_; |
pwestin@webrtc.org | d35964a | 2013-04-30 16:06:10 | [diff] [blame] | 177 | |
mikhal@webrtc.org | 0d8d010 | 2013-02-22 19:30:44 | [diff] [blame] | 178 | // The video delay is compared to the last value (and how much we can update |
| 179 | // is limited by that as well). |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 180 | video_delay_.last_ms += target_delay_ms - base_target_delay_ms_; |
Åsa Persson | 4fc52c8 | 2019-12-09 11:41:56 | [diff] [blame] | 181 | video_delay_.extra_ms += target_delay_ms - base_target_delay_ms_; |
pwestin@webrtc.org | d35964a | 2013-04-30 16:06:10 | [diff] [blame] | 182 | |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 | [diff] [blame] | 183 | // Video is already delayed by the desired amount. |
| 184 | base_target_delay_ms_ = target_delay_ms; |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 | [diff] [blame] | 185 | } |
| 186 | |
Ivo Creusen | bef7b05 | 2020-09-08 14:30:25 | [diff] [blame] | 187 | void StreamSynchronization::ReduceAudioDelay() { |
| 188 | audio_delay_.extra_ms *= 0.9f; |
| 189 | } |
| 190 | |
| 191 | void StreamSynchronization::ReduceVideoDelay() { |
| 192 | video_delay_.extra_ms *= 0.9f; |
| 193 | } |
| 194 | |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 | [diff] [blame] | 195 | } // namespace webrtc |