blob: 9490d101ff652e281a33501c04928dc1bd383e62 [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
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000011#include "webrtc/video_engine/stream_synchronization.h"
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000012
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000013#include <algorithm>
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000014#include <assert.h>
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000015#include <cmath>
16
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000017#include "webrtc/system_wrappers/interface/trace.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
stefan@webrtc.org5f284982012-06-28 07:51:16 +000027struct ViESyncDelay {
28 ViESyncDelay() {
29 extra_video_delay_ms = 0;
30 last_video_delay_ms = 0;
31 extra_audio_delay_ms = 0;
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +000032 last_audio_delay_ms = 0;
stefan@webrtc.org5f284982012-06-28 07:51:16 +000033 network_delay = 120;
34 }
35
36 int extra_video_delay_ms;
37 int last_video_delay_ms;
38 int extra_audio_delay_ms;
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +000039 int last_audio_delay_ms;
stefan@webrtc.org5f284982012-06-28 07:51:16 +000040 int network_delay;
41};
42
43StreamSynchronization::StreamSynchronization(int audio_channel_id,
44 int video_channel_id)
45 : channel_delay_(new ViESyncDelay),
46 audio_channel_id_(audio_channel_id),
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +000047 video_channel_id_(video_channel_id),
pwestin@webrtc.org63117332013-04-22 18:57:14 +000048 base_target_delay_ms_(0),
49 avg_diff_ms_(0) {}
stefan@webrtc.org5f284982012-06-28 07:51:16 +000050
51StreamSynchronization::~StreamSynchronization() {
52 delete channel_delay_;
53}
54
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000055bool StreamSynchronization::ComputeRelativeDelay(
56 const Measurements& audio_measurement,
57 const Measurements& video_measurement,
58 int* relative_delay_ms) {
59 assert(relative_delay_ms);
60 if (audio_measurement.rtcp.size() < 2 || video_measurement.rtcp.size() < 2) {
61 // We need two RTCP SR reports per stream to do synchronization.
62 return false;
stefan@webrtc.org5f284982012-06-28 07:51:16 +000063 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000064 int64_t audio_last_capture_time_ms;
65 if (!synchronization::RtpToNtpMs(audio_measurement.latest_timestamp,
66 audio_measurement.rtcp,
67 &audio_last_capture_time_ms)) {
68 return false;
69 }
70 int64_t video_last_capture_time_ms;
71 if (!synchronization::RtpToNtpMs(video_measurement.latest_timestamp,
72 video_measurement.rtcp,
73 &video_last_capture_time_ms)) {
74 return false;
75 }
76 if (video_last_capture_time_ms < 0) {
77 return false;
78 }
79 // Positive diff means that video_measurement is behind audio_measurement.
80 *relative_delay_ms = video_measurement.latest_receive_time_ms -
81 audio_measurement.latest_receive_time_ms -
82 (video_last_capture_time_ms - audio_last_capture_time_ms);
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +000083 if (*relative_delay_ms > kMaxDeltaDelayMs ||
84 *relative_delay_ms < -kMaxDeltaDelayMs) {
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000085 return false;
86 }
87 return true;
88}
stefan@webrtc.org5f284982012-06-28 07:51:16 +000089
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000090bool StreamSynchronization::ComputeDelays(int relative_delay_ms,
91 int current_audio_delay_ms,
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +000092 int* total_audio_delay_target_ms,
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000093 int* total_video_delay_target_ms) {
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +000094 assert(total_audio_delay_target_ms && total_video_delay_target_ms);
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +000095
96 int current_video_delay_ms = *total_video_delay_target_ms;
stefan@webrtc.org5f284982012-06-28 07:51:16 +000097 WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, video_channel_id_,
98 "Audio delay is: %d for voice channel: %d",
99 current_audio_delay_ms, audio_channel_id_);
100 WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, video_channel_id_,
101 "Network delay diff is: %d for voice channel: %d",
102 channel_delay_->network_delay, audio_channel_id_);
103 // Calculate the difference between the lowest possible video delay and
104 // the current audio delay.
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000105 WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, video_channel_id_,
106 "Current diff is: %d for audio channel: %d",
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000107 relative_delay_ms, audio_channel_id_);
pwestin@webrtc.org63117332013-04-22 18:57:14 +0000108
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000109 int current_diff_ms = current_video_delay_ms - current_audio_delay_ms +
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000110 relative_delay_ms;
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000111
pwestin@webrtc.org63117332013-04-22 18:57:14 +0000112 avg_diff_ms_ = ((kFilterLength - 1) * avg_diff_ms_ +
113 current_diff_ms) / kFilterLength;
114 if (abs(avg_diff_ms_) < kMinDeltaMs) {
115 // Don't adjust if the diff is within our margin.
116 return false;
117 }
118
119 // Make sure we don't move too fast.
120 int diff_ms = avg_diff_ms_ / 2;
121 diff_ms = std::min(diff_ms, kMaxChangeMs);
122 diff_ms = std::max(diff_ms, -kMaxChangeMs);
123
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000124 // Reset the average after a move to prevent overshooting reaction.
125 avg_diff_ms_ = 0;
126
pwestin@webrtc.org63117332013-04-22 18:57:14 +0000127 if (diff_ms > 0) {
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000128 // The minimum video delay is longer than the current audio delay.
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000129 // We need to decrease extra video delay, or add extra audio delay.
130 if (channel_delay_->extra_video_delay_ms > base_target_delay_ms_) {
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000131 // We have extra delay added to ViE. Reduce this delay before adding
132 // extra delay to VoE.
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000133 channel_delay_->extra_video_delay_ms -= diff_ms;
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000134 channel_delay_->extra_audio_delay_ms = base_target_delay_ms_;
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000135 } else { // channel_delay_->extra_video_delay_ms > 0
136 // We have no extra video delay to remove, increase the audio delay.
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000137 channel_delay_->extra_audio_delay_ms += diff_ms;
138 channel_delay_->extra_video_delay_ms = base_target_delay_ms_;
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000139 }
pwestin@webrtc.org63117332013-04-22 18:57:14 +0000140 } else { // if (diff_ms > 0)
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000141 // The video delay is lower than the current audio delay.
142 // We need to decrease extra audio delay, or add extra video delay.
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000143 if (channel_delay_->extra_audio_delay_ms > base_target_delay_ms_) {
144 // We have extra delay in VoiceEngine.
145 // Start with decreasing the voice delay.
pwestin@webrtc.org63117332013-04-22 18:57:14 +0000146 // Note: diff_ms is negative; add the negative difference.
147 channel_delay_->extra_audio_delay_ms += diff_ms;
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000148 channel_delay_->extra_video_delay_ms = base_target_delay_ms_;
149 } else { // channel_delay_->extra_audio_delay_ms > base_target_delay_ms_
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000150 // We have no extra delay in VoiceEngine, increase the video delay.
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000151 // Note: diff_ms is negative; subtract the negative difference.
152 channel_delay_->extra_video_delay_ms -= diff_ms; // X - (-Y) = X + Y.
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000153 channel_delay_->extra_audio_delay_ms = base_target_delay_ms_;
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000154 }
155 }
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000156
157 // Make sure that video is never below our target.
158 channel_delay_->extra_video_delay_ms = std::max(
159 channel_delay_->extra_video_delay_ms, base_target_delay_ms_);
160
161 int new_video_delay_ms;
162 if (channel_delay_->extra_video_delay_ms > base_target_delay_ms_) {
163 new_video_delay_ms = channel_delay_->extra_video_delay_ms;
164 } else {
165 // No change to the extra video delay. We are changing audio and we only
166 // allow to change one at the time.
167 new_video_delay_ms = channel_delay_->last_video_delay_ms;
168 }
169
170 // Make sure that we don't go below the extra video delay.
171 new_video_delay_ms = std::max(
172 new_video_delay_ms, channel_delay_->extra_video_delay_ms);
173
174 // Verify we don't go above the maximum allowed video delay.
175 new_video_delay_ms =
176 std::min(new_video_delay_ms, base_target_delay_ms_ + kMaxDeltaDelayMs);
177
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000178 int new_audio_delay_ms;
179 if (channel_delay_->extra_audio_delay_ms > base_target_delay_ms_) {
180 new_audio_delay_ms = channel_delay_->extra_audio_delay_ms;
181 } else {
182 // No change to the audio delay. We are changing video and we only
183 // allow to change one at the time.
184 new_audio_delay_ms = channel_delay_->last_audio_delay_ms;
185 }
186
187 // Make sure that we don't go below the extra audio delay.
188 new_audio_delay_ms = std::max(
189 new_audio_delay_ms, channel_delay_->extra_audio_delay_ms);
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000190
191 // Verify we don't go above the maximum allowed audio delay.
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000192 new_audio_delay_ms =
193 std::min(new_audio_delay_ms, base_target_delay_ms_ + kMaxDeltaDelayMs);
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000194
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000195 // Remember our last audio and video delays.
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000196 channel_delay_->last_video_delay_ms = new_video_delay_ms;
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000197 channel_delay_->last_audio_delay_ms = new_audio_delay_ms;
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000198
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000199 WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, video_channel_id_,
200 "Sync video delay %d ms for video channel and audio delay %d for audio "
201 "channel %d",
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000202 new_video_delay_ms, channel_delay_->extra_audio_delay_ms,
203 audio_channel_id_);
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000204
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000205 // Return values.
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000206 *total_video_delay_target_ms = new_video_delay_ms;
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000207 *total_audio_delay_target_ms = new_audio_delay_ms;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000208 return true;
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000209}
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000210
211void StreamSynchronization::SetTargetBufferingDelay(int target_delay_ms) {
mikhal@webrtc.org0d8d0102013-02-22 19:30:44 +0000212 // Initial extra delay for audio (accounting for existing extra delay).
213 channel_delay_->extra_audio_delay_ms +=
214 target_delay_ms - base_target_delay_ms_;
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000215 channel_delay_->last_audio_delay_ms +=
216 target_delay_ms - base_target_delay_ms_;
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000217
mikhal@webrtc.org0d8d0102013-02-22 19:30:44 +0000218 // The video delay is compared to the last value (and how much we can update
219 // is limited by that as well).
220 channel_delay_->last_video_delay_ms +=
221 target_delay_ms - base_target_delay_ms_;
pwestin@webrtc.orgd35964a2013-04-30 16:06:10 +0000222
223 channel_delay_->extra_video_delay_ms +=
224 target_delay_ms - base_target_delay_ms_;
225
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000226 // Video is already delayed by the desired amount.
227 base_target_delay_ms_ = target_delay_ms;
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000228}
229
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000230} // namespace webrtc