blob: 8a51039b20fa0154db9a793356f2ff6db8e2c772 [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>
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000015#include <algorithm>
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/logging.h"
stefan@webrtc.org5f284982012-06-28 07:51:16 +000018
19namespace webrtc {
20
pwestin@webrtc.org63117332013-04-22 18:57:14 +000021static const int kMaxChangeMs = 80;
22static const int kMaxDeltaDelayMs = 10000;
23static const int kFilterLength = 4;
24// Minimum difference between audio and video to warrant a change.
25static const int kMinDeltaMs = 30;
stefan@webrtc.org5f284982012-06-28 07:51:16 +000026
solenberg3ebbcb52017-01-31 03:58:40 -080027StreamSynchronization::StreamSynchronization(int video_stream_id,
28 int audio_stream_id)
29 : video_stream_id_(video_stream_id),
30 audio_stream_id_(audio_stream_id),
pwestin@webrtc.org63117332013-04-22 18:57:14 +000031 base_target_delay_ms_(0),
Yves Gerey665174f2018-06-19 15:03:05 +020032 avg_diff_ms_(0) {}
stefan@webrtc.org5f284982012-06-28 07:51:16 +000033
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000034bool StreamSynchronization::ComputeRelativeDelay(
35 const Measurements& audio_measurement,
36 const Measurements& video_measurement,
37 int* relative_delay_ms) {
38 assert(relative_delay_ms);
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000039 int64_t audio_last_capture_time_ms;
asaperssonfe50b4d2016-12-22 07:53:51 -080040 if (!audio_measurement.rtp_to_ntp.Estimate(audio_measurement.latest_timestamp,
41 &audio_last_capture_time_ms)) {
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000042 return false;
43 }
44 int64_t video_last_capture_time_ms;
asaperssonfe50b4d2016-12-22 07:53:51 -080045 if (!video_measurement.rtp_to_ntp.Estimate(video_measurement.latest_timestamp,
46 &video_last_capture_time_ms)) {
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000047 return false;
48 }
49 if (video_last_capture_time_ms < 0) {
50 return false;
51 }
52 // Positive diff means that video_measurement is behind audio_measurement.
Yves Gerey665174f2018-06-19 15:03:05 +020053 *relative_delay_ms =
54 video_measurement.latest_receive_time_ms -
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000055 audio_measurement.latest_receive_time_ms -
56 (video_last_capture_time_ms - audio_last_capture_time_ms);
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +000057 if (*relative_delay_ms > kMaxDeltaDelayMs ||
58 *relative_delay_ms < -kMaxDeltaDelayMs) {
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000059 return false;
60 }
61 return true;
62}
stefan@webrtc.org5f284982012-06-28 07:51:16 +000063
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000064bool StreamSynchronization::ComputeDelays(int relative_delay_ms,
65 int current_audio_delay_ms,
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +000066 int* total_audio_delay_target_ms,
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000067 int* total_video_delay_target_ms) {
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +000068 assert(total_audio_delay_target_ms && total_video_delay_target_ms);
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +000069
70 int current_video_delay_ms = *total_video_delay_target_ms;
Mirko Bonadei675513b2017-11-09 11:09:25 +010071 RTC_LOG(LS_VERBOSE) << "Audio delay: " << current_audio_delay_ms
72 << " current diff: " << relative_delay_ms
73 << " for stream " << audio_stream_id_;
stefan@webrtc.org5f284982012-06-28 07:51:16 +000074 // Calculate the difference between the lowest possible video delay and
75 // the current audio delay.
Yves Gerey665174f2018-06-19 15:03:05 +020076 int current_diff_ms =
77 current_video_delay_ms - current_audio_delay_ms + relative_delay_ms;
stefan@webrtc.org5f284982012-06-28 07:51:16 +000078
Yves Gerey665174f2018-06-19 15:03:05 +020079 avg_diff_ms_ =
80 ((kFilterLength - 1) * avg_diff_ms_ + current_diff_ms) / kFilterLength;
pwestin@webrtc.org63117332013-04-22 18:57:14 +000081 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.orgd35964a2013-04-30 16:06:10 +000091 // Reset the average after a move to prevent overshooting reaction.
92 avg_diff_ms_ = 0;
93
pwestin@webrtc.org63117332013-04-22 18:57:14 +000094 if (diff_ms > 0) {
stefan@webrtc.org5f284982012-06-28 07:51:16 +000095 // The minimum video delay is longer than the current audio delay.
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +000096 // We need to decrease extra video delay, or add extra audio delay.
mflodman4cd27902016-08-05 06:28:45 -070097 if (channel_delay_.extra_video_delay_ms > base_target_delay_ms_) {
stefan@webrtc.org5f284982012-06-28 07:51:16 +000098 // We have extra delay added to ViE. Reduce this delay before adding
99 // extra delay to VoE.
mflodman4cd27902016-08-05 06:28:45 -0700100 channel_delay_.extra_video_delay_ms -= diff_ms;
101 channel_delay_.extra_audio_delay_ms = base_target_delay_ms_;
102 } else { // channel_delay_.extra_video_delay_ms > 0
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000103 // We have no extra video delay to remove, increase the audio delay.
mflodman4cd27902016-08-05 06:28:45 -0700104 channel_delay_.extra_audio_delay_ms += diff_ms;
105 channel_delay_.extra_video_delay_ms = base_target_delay_ms_;
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000106 }
pwestin@webrtc.org63117332013-04-22 18:57:14 +0000107 } else { // if (diff_ms > 0)
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000108 // The video delay is lower than the current audio delay.
109 // We need to decrease extra audio delay, or add extra video delay.
mflodman4cd27902016-08-05 06:28:45 -0700110 if (channel_delay_.extra_audio_delay_ms > base_target_delay_ms_) {
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000111 // We have extra delay in VoiceEngine.
112 // Start with decreasing the voice delay.
pwestin@webrtc.org63117332013-04-22 18:57:14 +0000113 // Note: diff_ms is negative; add the negative difference.
mflodman4cd27902016-08-05 06:28:45 -0700114 channel_delay_.extra_audio_delay_ms += diff_ms;
115 channel_delay_.extra_video_delay_ms = base_target_delay_ms_;
116 } else { // channel_delay_.extra_audio_delay_ms > base_target_delay_ms_
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000117 // We have no extra delay in VoiceEngine, increase the video delay.
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000118 // Note: diff_ms is negative; subtract the negative difference.
mflodman4cd27902016-08-05 06:28:45 -0700119 channel_delay_.extra_video_delay_ms -= diff_ms; // X - (-Y) = X + Y.
120 channel_delay_.extra_audio_delay_ms = base_target_delay_ms_;
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000121 }
122 }
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000123
124 // Make sure that video is never below our target.
Yves Gerey665174f2018-06-19 15:03:05 +0200125 channel_delay_.extra_video_delay_ms =
126 std::max(channel_delay_.extra_video_delay_ms, base_target_delay_ms_);
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000127
128 int new_video_delay_ms;
mflodman4cd27902016-08-05 06:28:45 -0700129 if (channel_delay_.extra_video_delay_ms > base_target_delay_ms_) {
130 new_video_delay_ms = channel_delay_.extra_video_delay_ms;
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000131 } else {
132 // No change to the extra video delay. We are changing audio and we only
133 // allow to change one at the time.
mflodman4cd27902016-08-05 06:28:45 -0700134 new_video_delay_ms = channel_delay_.last_video_delay_ms;
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000135 }
136
137 // Make sure that we don't go below the extra video delay.
Yves Gerey665174f2018-06-19 15:03:05 +0200138 new_video_delay_ms =
139 std::max(new_video_delay_ms, channel_delay_.extra_video_delay_ms);
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000140
141 // Verify we don't go above the maximum allowed video delay.
142 new_video_delay_ms =
143 std::min(new_video_delay_ms, base_target_delay_ms_ + kMaxDeltaDelayMs);
144
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000145 int new_audio_delay_ms;
mflodman4cd27902016-08-05 06:28:45 -0700146 if (channel_delay_.extra_audio_delay_ms > base_target_delay_ms_) {
147 new_audio_delay_ms = channel_delay_.extra_audio_delay_ms;
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000148 } else {
149 // No change to the audio delay. We are changing video and we only
150 // allow to change one at the time.
mflodman4cd27902016-08-05 06:28:45 -0700151 new_audio_delay_ms = channel_delay_.last_audio_delay_ms;
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000152 }
153
154 // Make sure that we don't go below the extra audio delay.
Yves Gerey665174f2018-06-19 15:03:05 +0200155 new_audio_delay_ms =
156 std::max(new_audio_delay_ms, channel_delay_.extra_audio_delay_ms);
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000157
158 // Verify we don't go above the maximum allowed audio delay.
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000159 new_audio_delay_ms =
160 std::min(new_audio_delay_ms, base_target_delay_ms_ + kMaxDeltaDelayMs);
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000161
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000162 // Remember our last audio and video delays.
mflodman4cd27902016-08-05 06:28:45 -0700163 channel_delay_.last_video_delay_ms = new_video_delay_ms;
164 channel_delay_.last_audio_delay_ms = new_audio_delay_ms;
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000165
Mirko Bonadei675513b2017-11-09 11:09:25 +0100166 RTC_LOG(LS_VERBOSE) << "Sync video delay " << new_video_delay_ms
167 << " for video stream " << video_stream_id_
168 << " and audio delay "
169 << channel_delay_.extra_audio_delay_ms
170 << " for audio stream " << audio_stream_id_;
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000171
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000172 // Return values.
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000173 *total_video_delay_target_ms = new_video_delay_ms;
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000174 *total_audio_delay_target_ms = new_audio_delay_ms;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000175 return true;
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000176}
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000177
178void StreamSynchronization::SetTargetBufferingDelay(int target_delay_ms) {
mikhal@webrtc.org0d8d0102013-02-22 19:30:44 +0000179 // Initial extra delay for audio (accounting for existing extra delay).
mflodman4cd27902016-08-05 06:28:45 -0700180 channel_delay_.extra_audio_delay_ms +=
mikhal@webrtc.org0d8d0102013-02-22 19:30:44 +0000181 target_delay_ms - base_target_delay_ms_;
Yves Gerey665174f2018-06-19 15:03:05 +0200182 channel_delay_.last_audio_delay_ms += target_delay_ms - base_target_delay_ms_;
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000183
mikhal@webrtc.org0d8d0102013-02-22 19:30:44 +0000184 // The video delay is compared to the last value (and how much we can update
185 // is limited by that as well).
Yves Gerey665174f2018-06-19 15:03:05 +0200186 channel_delay_.last_video_delay_ms += target_delay_ms - base_target_delay_ms_;
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000187
mflodman4cd27902016-08-05 06:28:45 -0700188 channel_delay_.extra_video_delay_ms +=
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000189 target_delay_ms - base_target_delay_ms_;
190
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000191 // Video is already delayed by the desired amount.
192 base_target_delay_ms_ = target_delay_ms;
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000193}
194
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000195} // namespace webrtc