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.

153 lines
4.9 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/rtp_streams_synchronizer.h"
  11. #include "absl/types/optional.h"
  12. #include "call/syncable.h"
  13. #include "rtc_base/checks.h"
  14. #include "rtc_base/time_utils.h"
  15. #include "rtc_base/trace_event.h"
  16. #include "system_wrappers/include/rtp_to_ntp_estimator.h"
  17. namespace webrtc {
  18. namespace {
  19. bool UpdateMeasurements(StreamSynchronization::Measurements* stream,
  20. const Syncable::Info& info) {
  21. RTC_DCHECK(stream);
  22. stream->latest_timestamp = info.latest_received_capture_timestamp;
  23. stream->latest_receive_time_ms = info.latest_receive_time_ms;
  24. bool new_rtcp_sr = false;
  25. if (!stream->rtp_to_ntp.UpdateMeasurements(
  26. info.capture_time_ntp_secs, info.capture_time_ntp_frac,
  27. info.capture_time_source_clock, &new_rtcp_sr)) {
  28. return false;
  29. }
  30. return true;
  31. }
  32. } // namespace
  33. RtpStreamsSynchronizer::RtpStreamsSynchronizer(Syncable* syncable_video)
  34. : syncable_video_(syncable_video),
  35. syncable_audio_(nullptr),
  36. sync_(),
  37. last_sync_time_(rtc::TimeNanos()) {
  38. RTC_DCHECK(syncable_video);
  39. process_thread_checker_.Detach();
  40. }
  41. RtpStreamsSynchronizer::~RtpStreamsSynchronizer() = default;
  42. void RtpStreamsSynchronizer::ConfigureSync(Syncable* syncable_audio) {
  43. rtc::CritScope lock(&crit_);
  44. if (syncable_audio == syncable_audio_) {
  45. // This prevents expensive no-ops.
  46. return;
  47. }
  48. syncable_audio_ = syncable_audio;
  49. sync_.reset(nullptr);
  50. if (syncable_audio_) {
  51. sync_.reset(new StreamSynchronization(syncable_video_->id(),
  52. syncable_audio_->id()));
  53. }
  54. }
  55. int64_t RtpStreamsSynchronizer::TimeUntilNextProcess() {
  56. RTC_DCHECK_RUN_ON(&process_thread_checker_);
  57. const int64_t kSyncIntervalMs = 1000;
  58. return kSyncIntervalMs -
  59. (rtc::TimeNanos() - last_sync_time_) / rtc::kNumNanosecsPerMillisec;
  60. }
  61. void RtpStreamsSynchronizer::Process() {
  62. RTC_DCHECK_RUN_ON(&process_thread_checker_);
  63. last_sync_time_ = rtc::TimeNanos();
  64. rtc::CritScope lock(&crit_);
  65. if (!syncable_audio_) {
  66. return;
  67. }
  68. RTC_DCHECK(sync_.get());
  69. absl::optional<Syncable::Info> audio_info = syncable_audio_->GetInfo();
  70. if (!audio_info || !UpdateMeasurements(&audio_measurement_, *audio_info)) {
  71. return;
  72. }
  73. int64_t last_video_receive_ms = video_measurement_.latest_receive_time_ms;
  74. absl::optional<Syncable::Info> video_info = syncable_video_->GetInfo();
  75. if (!video_info || !UpdateMeasurements(&video_measurement_, *video_info)) {
  76. return;
  77. }
  78. if (last_video_receive_ms == video_measurement_.latest_receive_time_ms) {
  79. // No new video packet has been received since last update.
  80. return;
  81. }
  82. int relative_delay_ms;
  83. // Calculate how much later or earlier the audio stream is compared to video.
  84. if (!sync_->ComputeRelativeDelay(audio_measurement_, video_measurement_,
  85. &relative_delay_ms)) {
  86. return;
  87. }
  88. TRACE_COUNTER1("webrtc", "SyncCurrentVideoDelay",
  89. video_info->current_delay_ms);
  90. TRACE_COUNTER1("webrtc", "SyncCurrentAudioDelay",
  91. audio_info->current_delay_ms);
  92. TRACE_COUNTER1("webrtc", "SyncRelativeDelay", relative_delay_ms);
  93. int target_audio_delay_ms = 0;
  94. int target_video_delay_ms = video_info->current_delay_ms;
  95. // Calculate the necessary extra audio delay and desired total video
  96. // delay to get the streams in sync.
  97. if (!sync_->ComputeDelays(relative_delay_ms, audio_info->current_delay_ms,
  98. &target_audio_delay_ms, &target_video_delay_ms)) {
  99. return;
  100. }
  101. syncable_audio_->SetMinimumPlayoutDelay(target_audio_delay_ms);
  102. syncable_video_->SetMinimumPlayoutDelay(target_video_delay_ms);
  103. }
  104. bool RtpStreamsSynchronizer::GetStreamSyncOffsetInMs(
  105. uint32_t timestamp,
  106. int64_t render_time_ms,
  107. int64_t* stream_offset_ms,
  108. double* estimated_freq_khz) const {
  109. rtc::CritScope lock(&crit_);
  110. if (!syncable_audio_) {
  111. return false;
  112. }
  113. uint32_t playout_timestamp = syncable_audio_->GetPlayoutTimestamp();
  114. int64_t latest_audio_ntp;
  115. if (!audio_measurement_.rtp_to_ntp.Estimate(playout_timestamp,
  116. &latest_audio_ntp)) {
  117. return false;
  118. }
  119. int64_t latest_video_ntp;
  120. if (!video_measurement_.rtp_to_ntp.Estimate(timestamp, &latest_video_ntp)) {
  121. return false;
  122. }
  123. int64_t time_to_render_ms = render_time_ms - rtc::TimeMillis();
  124. if (time_to_render_ms > 0)
  125. latest_video_ntp += time_to_render_ms;
  126. *stream_offset_ms = latest_audio_ntp - latest_video_ntp;
  127. *estimated_freq_khz = video_measurement_.rtp_to_ntp.params()->frequency_khz;
  128. return true;
  129. }
  130. } // namespace webrtc