You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
7.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "video/stream_synchronization.h"
  11. #include <assert.h>
  12. #include <stdlib.h>
  13. #include <algorithm>
  14. #include "rtc_base/logging.h"
  15. namespace webrtc {
  16. static const int kMaxChangeMs = 80;
  17. static const int kMaxDeltaDelayMs = 10000;
  18. static const int kFilterLength = 4;
  19. // Minimum difference between audio and video to warrant a change.
  20. static const int kMinDeltaMs = 30;
  21. StreamSynchronization::StreamSynchronization(int video_stream_id,
  22. int audio_stream_id)
  23. : video_stream_id_(video_stream_id),
  24. audio_stream_id_(audio_stream_id),
  25. base_target_delay_ms_(0),
  26. avg_diff_ms_(0) {}
  27. bool StreamSynchronization::ComputeRelativeDelay(
  28. const Measurements& audio_measurement,
  29. const Measurements& video_measurement,
  30. int* relative_delay_ms) {
  31. assert(relative_delay_ms);
  32. int64_t audio_last_capture_time_ms;
  33. if (!audio_measurement.rtp_to_ntp.Estimate(audio_measurement.latest_timestamp,
  34. &audio_last_capture_time_ms)) {
  35. return false;
  36. }
  37. int64_t video_last_capture_time_ms;
  38. if (!video_measurement.rtp_to_ntp.Estimate(video_measurement.latest_timestamp,
  39. &video_last_capture_time_ms)) {
  40. return false;
  41. }
  42. if (video_last_capture_time_ms < 0) {
  43. return false;
  44. }
  45. // Positive diff means that video_measurement is behind audio_measurement.
  46. *relative_delay_ms =
  47. video_measurement.latest_receive_time_ms -
  48. audio_measurement.latest_receive_time_ms -
  49. (video_last_capture_time_ms - audio_last_capture_time_ms);
  50. if (*relative_delay_ms > kMaxDeltaDelayMs ||
  51. *relative_delay_ms < -kMaxDeltaDelayMs) {
  52. return false;
  53. }
  54. return true;
  55. }
  56. bool StreamSynchronization::ComputeDelays(int relative_delay_ms,
  57. int current_audio_delay_ms,
  58. int* total_audio_delay_target_ms,
  59. int* total_video_delay_target_ms) {
  60. assert(total_audio_delay_target_ms && total_video_delay_target_ms);
  61. int current_video_delay_ms = *total_video_delay_target_ms;
  62. RTC_LOG(LS_VERBOSE) << "Audio delay: " << current_audio_delay_ms
  63. << " current diff: " << relative_delay_ms
  64. << " for stream " << audio_stream_id_;
  65. // Calculate the difference between the lowest possible video delay and
  66. // the current audio delay.
  67. int current_diff_ms =
  68. current_video_delay_ms - current_audio_delay_ms + relative_delay_ms;
  69. avg_diff_ms_ =
  70. ((kFilterLength - 1) * avg_diff_ms_ + current_diff_ms) / kFilterLength;
  71. if (abs(avg_diff_ms_) < kMinDeltaMs) {
  72. // Don't adjust if the diff is within our margin.
  73. return false;
  74. }
  75. // Make sure we don't move too fast.
  76. int diff_ms = avg_diff_ms_ / 2;
  77. diff_ms = std::min(diff_ms, kMaxChangeMs);
  78. diff_ms = std::max(diff_ms, -kMaxChangeMs);
  79. // Reset the average after a move to prevent overshooting reaction.
  80. avg_diff_ms_ = 0;
  81. if (diff_ms > 0) {
  82. // The minimum video delay is longer than the current audio delay.
  83. // We need to decrease extra video delay, or add extra audio delay.
  84. if (channel_delay_.extra_video_delay_ms > base_target_delay_ms_) {
  85. // We have extra delay added to ViE. Reduce this delay before adding
  86. // extra delay to VoE.
  87. channel_delay_.extra_video_delay_ms -= diff_ms;
  88. channel_delay_.extra_audio_delay_ms = base_target_delay_ms_;
  89. } else { // channel_delay_.extra_video_delay_ms > 0
  90. // We have no extra video delay to remove, increase the audio delay.
  91. channel_delay_.extra_audio_delay_ms += diff_ms;
  92. channel_delay_.extra_video_delay_ms = base_target_delay_ms_;
  93. }
  94. } else { // if (diff_ms > 0)
  95. // The video delay is lower than the current audio delay.
  96. // We need to decrease extra audio delay, or add extra video delay.
  97. if (channel_delay_.extra_audio_delay_ms > base_target_delay_ms_) {
  98. // We have extra delay in VoiceEngine.
  99. // Start with decreasing the voice delay.
  100. // Note: diff_ms is negative; add the negative difference.
  101. channel_delay_.extra_audio_delay_ms += diff_ms;
  102. channel_delay_.extra_video_delay_ms = base_target_delay_ms_;
  103. } else { // channel_delay_.extra_audio_delay_ms > base_target_delay_ms_
  104. // We have no extra delay in VoiceEngine, increase the video delay.
  105. // Note: diff_ms is negative; subtract the negative difference.
  106. channel_delay_.extra_video_delay_ms -= diff_ms; // X - (-Y) = X + Y.
  107. channel_delay_.extra_audio_delay_ms = base_target_delay_ms_;
  108. }
  109. }
  110. // Make sure that video is never below our target.
  111. channel_delay_.extra_video_delay_ms =
  112. std::max(channel_delay_.extra_video_delay_ms, base_target_delay_ms_);
  113. int new_video_delay_ms;
  114. if (channel_delay_.extra_video_delay_ms > base_target_delay_ms_) {
  115. new_video_delay_ms = channel_delay_.extra_video_delay_ms;
  116. } else {
  117. // No change to the extra video delay. We are changing audio and we only
  118. // allow to change one at the time.
  119. new_video_delay_ms = channel_delay_.last_video_delay_ms;
  120. }
  121. // Make sure that we don't go below the extra video delay.
  122. new_video_delay_ms =
  123. std::max(new_video_delay_ms, channel_delay_.extra_video_delay_ms);
  124. // Verify we don't go above the maximum allowed video delay.
  125. new_video_delay_ms =
  126. std::min(new_video_delay_ms, base_target_delay_ms_ + kMaxDeltaDelayMs);
  127. int new_audio_delay_ms;
  128. if (channel_delay_.extra_audio_delay_ms > base_target_delay_ms_) {
  129. new_audio_delay_ms = channel_delay_.extra_audio_delay_ms;
  130. } else {
  131. // No change to the audio delay. We are changing video and we only
  132. // allow to change one at the time.
  133. new_audio_delay_ms = channel_delay_.last_audio_delay_ms;
  134. }
  135. // Make sure that we don't go below the extra audio delay.
  136. new_audio_delay_ms =
  137. std::max(new_audio_delay_ms, channel_delay_.extra_audio_delay_ms);
  138. // Verify we don't go above the maximum allowed audio delay.
  139. new_audio_delay_ms =
  140. std::min(new_audio_delay_ms, base_target_delay_ms_ + kMaxDeltaDelayMs);
  141. // Remember our last audio and video delays.
  142. channel_delay_.last_video_delay_ms = new_video_delay_ms;
  143. channel_delay_.last_audio_delay_ms = new_audio_delay_ms;
  144. RTC_LOG(LS_VERBOSE) << "Sync video delay " << new_video_delay_ms
  145. << " for video stream " << video_stream_id_
  146. << " and audio delay "
  147. << channel_delay_.extra_audio_delay_ms
  148. << " for audio stream " << audio_stream_id_;
  149. // Return values.
  150. *total_video_delay_target_ms = new_video_delay_ms;
  151. *total_audio_delay_target_ms = new_audio_delay_ms;
  152. return true;
  153. }
  154. void StreamSynchronization::SetTargetBufferingDelay(int target_delay_ms) {
  155. // Initial extra delay for audio (accounting for existing extra delay).
  156. channel_delay_.extra_audio_delay_ms +=
  157. target_delay_ms - base_target_delay_ms_;
  158. channel_delay_.last_audio_delay_ms += target_delay_ms - base_target_delay_ms_;
  159. // The video delay is compared to the last value (and how much we can update
  160. // is limited by that as well).
  161. channel_delay_.last_video_delay_ms += target_delay_ms - base_target_delay_ms_;
  162. channel_delay_.extra_video_delay_ms +=
  163. target_delay_ms - base_target_delay_ms_;
  164. // Video is already delayed by the desired amount.
  165. base_target_delay_ms_ = target_delay_ms;
  166. }
  167. } // namespace webrtc