blob: 493ed33b1db18a957c05a7f92e0bfb2dd3a55204 [file] [log] [blame]
stefan@webrtc.org5f284982012-06-28 07:51:16 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "video/stream_synchronization.h"
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000012
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000013#include <assert.h>
stefan@webrtc.org47fadba2013-11-25 12:03:56 +000014#include <stdlib.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000016#include <algorithm>
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/logging.h"
stefan@webrtc.org5f284982012-06-28 07:51:16 +000019
20namespace webrtc {
21
pwestin@webrtc.org63117332013-04-22 18:57:14 +000022static const int kMaxChangeMs = 80;
23static const int kMaxDeltaDelayMs = 10000;
24static const int kFilterLength = 4;
25// Minimum difference between audio and video to warrant a change.
26static const int kMinDeltaMs = 30;
stefan@webrtc.org5f284982012-06-28 07:51:16 +000027
solenberg3ebbcb52017-01-31 03:58:40 -080028StreamSynchronization::StreamSynchronization(int video_stream_id,
29 int audio_stream_id)
30 : video_stream_id_(video_stream_id),
31 audio_stream_id_(audio_stream_id),
pwestin@webrtc.org63117332013-04-22 18:57:14 +000032 base_target_delay_ms_(0),
Yves Gerey665174f2018-06-19 15:03:05 +020033 avg_diff_ms_(0) {}
stefan@webrtc.org5f284982012-06-28 07:51:16 +000034
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000035bool StreamSynchronization::ComputeRelativeDelay(
36 const Measurements& audio_measurement,
37 const Measurements& video_measurement,
38 int* relative_delay_ms) {
39 assert(relative_delay_ms);
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000040 int64_t audio_last_capture_time_ms;
asaperssonfe50b4d2016-12-22 07:53:51 -080041 if (!audio_measurement.rtp_to_ntp.Estimate(audio_measurement.latest_timestamp,
42 &audio_last_capture_time_ms)) {
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000043 return false;
44 }
45 int64_t video_last_capture_time_ms;
asaperssonfe50b4d2016-12-22 07:53:51 -080046 if (!video_measurement.rtp_to_ntp.Estimate(video_measurement.latest_timestamp,
47 &video_last_capture_time_ms)) {
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000048 return false;
49 }
50 if (video_last_capture_time_ms < 0) {
51 return false;
52 }
53 // Positive diff means that video_measurement is behind audio_measurement.
Yves Gerey665174f2018-06-19 15:03:05 +020054 *relative_delay_ms =
55 video_measurement.latest_receive_time_ms -
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000056 audio_measurement.latest_receive_time_ms -
57 (video_last_capture_time_ms - audio_last_capture_time_ms);
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +000058 if (*relative_delay_ms > kMaxDeltaDelayMs ||
59 *relative_delay_ms < -kMaxDeltaDelayMs) {
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000060 return false;
61 }
62 return true;
63}
stefan@webrtc.org5f284982012-06-28 07:51:16 +000064
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000065bool StreamSynchronization::ComputeDelays(int relative_delay_ms,
66 int current_audio_delay_ms,
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +000067 int* total_audio_delay_target_ms,
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000068 int* total_video_delay_target_ms) {
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +000069 assert(total_audio_delay_target_ms && total_video_delay_target_ms);
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +000070
71 int current_video_delay_ms = *total_video_delay_target_ms;
Mirko Bonadei675513b2017-11-09 11:09:25 +010072 RTC_LOG(LS_VERBOSE) << "Audio delay: " << current_audio_delay_ms
73 << " current diff: " << relative_delay_ms
74 << " for stream " << audio_stream_id_;
stefan@webrtc.org5f284982012-06-28 07:51:16 +000075 // Calculate the difference between the lowest possible video delay and
76 // the current audio delay.
Yves Gerey665174f2018-06-19 15:03:05 +020077 int current_diff_ms =
78 current_video_delay_ms - current_audio_delay_ms + relative_delay_ms;
stefan@webrtc.org5f284982012-06-28 07:51:16 +000079
Yves Gerey665174f2018-06-19 15:03:05 +020080 avg_diff_ms_ =
81 ((kFilterLength - 1) * avg_diff_ms_ + current_diff_ms) / kFilterLength;
pwestin@webrtc.org63117332013-04-22 18:57:14 +000082 if (abs(avg_diff_ms_) < kMinDeltaMs) {
83 // Don't adjust if the diff is within our margin.
84 return false;
85 }
86
87 // Make sure we don't move too fast.
88 int diff_ms = avg_diff_ms_ / 2;
89 diff_ms = std::min(diff_ms, kMaxChangeMs);
90 diff_ms = std::max(diff_ms, -kMaxChangeMs);
91
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +000092 // Reset the average after a move to prevent overshooting reaction.
93 avg_diff_ms_ = 0;
94
pwestin@webrtc.org63117332013-04-22 18:57:14 +000095 if (diff_ms > 0) {
stefan@webrtc.org5f284982012-06-28 07:51:16 +000096 // The minimum video delay is longer than the current audio delay.
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +000097 // We need to decrease extra video delay, or add extra audio delay.
mflodman4cd27902016-08-05 06:28:45 -070098 if (channel_delay_.extra_video_delay_ms > base_target_delay_ms_) {
stefan@webrtc.org5f284982012-06-28 07:51:16 +000099 // We have extra delay added to ViE. Reduce this delay before adding
100 // extra delay to VoE.
mflodman4cd27902016-08-05 06:28:45 -0700101 channel_delay_.extra_video_delay_ms -= diff_ms;
102 channel_delay_.extra_audio_delay_ms = base_target_delay_ms_;
103 } else { // channel_delay_.extra_video_delay_ms > 0
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000104 // We have no extra video delay to remove, increase the audio delay.
mflodman4cd27902016-08-05 06:28:45 -0700105 channel_delay_.extra_audio_delay_ms += diff_ms;
106 channel_delay_.extra_video_delay_ms = base_target_delay_ms_;
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000107 }
pwestin@webrtc.org63117332013-04-22 18:57:14 +0000108 } else { // if (diff_ms > 0)
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000109 // The video delay is lower than the current audio delay.
110 // We need to decrease extra audio delay, or add extra video delay.
mflodman4cd27902016-08-05 06:28:45 -0700111 if (channel_delay_.extra_audio_delay_ms > base_target_delay_ms_) {
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000112 // We have extra delay in VoiceEngine.
113 // Start with decreasing the voice delay.
pwestin@webrtc.org63117332013-04-22 18:57:14 +0000114 // Note: diff_ms is negative; add the negative difference.
mflodman4cd27902016-08-05 06:28:45 -0700115 channel_delay_.extra_audio_delay_ms += diff_ms;
116 channel_delay_.extra_video_delay_ms = base_target_delay_ms_;
117 } else { // channel_delay_.extra_audio_delay_ms > base_target_delay_ms_
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000118 // We have no extra delay in VoiceEngine, increase the video delay.
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000119 // Note: diff_ms is negative; subtract the negative difference.
mflodman4cd27902016-08-05 06:28:45 -0700120 channel_delay_.extra_video_delay_ms -= diff_ms; // X - (-Y) = X + Y.
121 channel_delay_.extra_audio_delay_ms = base_target_delay_ms_;
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000122 }
123 }
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000124
125 // Make sure that video is never below our target.
Yves Gerey665174f2018-06-19 15:03:05 +0200126 channel_delay_.extra_video_delay_ms =
127 std::max(channel_delay_.extra_video_delay_ms, base_target_delay_ms_);
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000128
129 int new_video_delay_ms;
mflodman4cd27902016-08-05 06:28:45 -0700130 if (channel_delay_.extra_video_delay_ms > base_target_delay_ms_) {
131 new_video_delay_ms = channel_delay_.extra_video_delay_ms;
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000132 } else {
133 // No change to the extra video delay. We are changing audio and we only
134 // allow to change one at the time.
mflodman4cd27902016-08-05 06:28:45 -0700135 new_video_delay_ms = channel_delay_.last_video_delay_ms;
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000136 }
137
138 // Make sure that we don't go below the extra video delay.
Yves Gerey665174f2018-06-19 15:03:05 +0200139 new_video_delay_ms =
140 std::max(new_video_delay_ms, channel_delay_.extra_video_delay_ms);
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000141
142 // Verify we don't go above the maximum allowed video delay.
143 new_video_delay_ms =
144 std::min(new_video_delay_ms, base_target_delay_ms_ + kMaxDeltaDelayMs);
145
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000146 int new_audio_delay_ms;
mflodman4cd27902016-08-05 06:28:45 -0700147 if (channel_delay_.extra_audio_delay_ms > base_target_delay_ms_) {
148 new_audio_delay_ms = channel_delay_.extra_audio_delay_ms;
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000149 } else {
150 // No change to the audio delay. We are changing video and we only
151 // allow to change one at the time.
mflodman4cd27902016-08-05 06:28:45 -0700152 new_audio_delay_ms = channel_delay_.last_audio_delay_ms;
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000153 }
154
155 // Make sure that we don't go below the extra audio delay.
Yves Gerey665174f2018-06-19 15:03:05 +0200156 new_audio_delay_ms =
157 std::max(new_audio_delay_ms, channel_delay_.extra_audio_delay_ms);
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000158
159 // Verify we don't go above the maximum allowed audio delay.
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000160 new_audio_delay_ms =
161 std::min(new_audio_delay_ms, base_target_delay_ms_ + kMaxDeltaDelayMs);
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000162
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000163 // Remember our last audio and video delays.
mflodman4cd27902016-08-05 06:28:45 -0700164 channel_delay_.last_video_delay_ms = new_video_delay_ms;
165 channel_delay_.last_audio_delay_ms = new_audio_delay_ms;
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000166
Mirko Bonadei675513b2017-11-09 11:09:25 +0100167 RTC_LOG(LS_VERBOSE) << "Sync video delay " << new_video_delay_ms
168 << " for video stream " << video_stream_id_
169 << " and audio delay "
170 << channel_delay_.extra_audio_delay_ms
171 << " for audio stream " << audio_stream_id_;
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000172
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000173 // Return values.
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000174 *total_video_delay_target_ms = new_video_delay_ms;
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000175 *total_audio_delay_target_ms = new_audio_delay_ms;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000176 return true;
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000177}
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000178
179void StreamSynchronization::SetTargetBufferingDelay(int target_delay_ms) {
mikhal@webrtc.org0d8d0102013-02-22 19:30:44 +0000180 // Initial extra delay for audio (accounting for existing extra delay).
mflodman4cd27902016-08-05 06:28:45 -0700181 channel_delay_.extra_audio_delay_ms +=
mikhal@webrtc.org0d8d0102013-02-22 19:30:44 +0000182 target_delay_ms - base_target_delay_ms_;
Yves Gerey665174f2018-06-19 15:03:05 +0200183 channel_delay_.last_audio_delay_ms += target_delay_ms - base_target_delay_ms_;
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000184
mikhal@webrtc.org0d8d0102013-02-22 19:30:44 +0000185 // The video delay is compared to the last value (and how much we can update
186 // is limited by that as well).
Yves Gerey665174f2018-06-19 15:03:05 +0200187 channel_delay_.last_video_delay_ms += target_delay_ms - base_target_delay_ms_;
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000188
mflodman4cd27902016-08-05 06:28:45 -0700189 channel_delay_.extra_video_delay_ms +=
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000190 target_delay_ms - base_target_delay_ms_;
191
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000192 // Video is already delayed by the desired amount.
193 base_target_delay_ms_ = target_delay_ms;
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000194}
195
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000196} // namespace webrtc