blob: 0d026b310a492d0463f6d9ac12ff0d4f46bec1d3 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
leozwang@webrtc.org39e96592012-03-01 18:22:48 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
mflodman4cd27902016-08-05 06:28:45 -070011#include "webrtc/video/rtp_streams_synchronizer.h"
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000012
Peter Boström1794b262016-02-16 14:12:02 +010013#include "webrtc/base/checks.h"
Peter Boström415d2cd2015-10-26 11:35:17 +010014#include "webrtc/base/logging.h"
Niels Möllerd28db7f2016-05-10 16:31:47 +020015#include "webrtc/base/timeutils.h"
tommie4f96502015-10-20 23:00:48 -070016#include "webrtc/base/trace_event.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010017#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
18#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
Peter Boström0b250722016-04-22 18:23:15 +020019#include "webrtc/modules/video_coding/video_coding_impl.h"
asaperssonf8cdd182016-03-15 01:00:47 -070020#include "webrtc/system_wrappers/include/clock.h"
Peter Boström7623ce42015-12-09 12:13:30 +010021#include "webrtc/video/stream_synchronization.h"
asaperssonf8cdd182016-03-15 01:00:47 -070022#include "webrtc/video_frame.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000023#include "webrtc/voice_engine/include/voe_video_sync.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000024
25namespace webrtc {
asaperssonf8cdd182016-03-15 01:00:47 -070026namespace {
asaperssonb7e7b492016-11-17 02:27:14 -080027bool UpdateMeasurements(StreamSynchronization::Measurements* stream,
28 RtpRtcp* rtp_rtcp,
29 RtpReceiver* receiver) {
mflodman4cd27902016-08-05 06:28:45 -070030 if (!receiver->Timestamp(&stream->latest_timestamp))
asaperssonb7e7b492016-11-17 02:27:14 -080031 return false;
mflodman4cd27902016-08-05 06:28:45 -070032 if (!receiver->LastReceivedTimeMs(&stream->latest_receive_time_ms))
asaperssonb7e7b492016-11-17 02:27:14 -080033 return false;
wu@webrtc.orgcd701192014-04-24 22:10:24 +000034
35 uint32_t ntp_secs = 0;
36 uint32_t ntp_frac = 0;
37 uint32_t rtp_timestamp = 0;
mflodman4cd27902016-08-05 06:28:45 -070038 if (rtp_rtcp->RemoteNTP(&ntp_secs, &ntp_frac, nullptr, nullptr,
39 &rtp_timestamp) != 0) {
asaperssonb7e7b492016-11-17 02:27:14 -080040 return false;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000041 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +000042
43 bool new_rtcp_sr = false;
asaperssonfe50b4d2016-12-22 07:53:51 -080044 if (!stream->rtp_to_ntp.UpdateMeasurements(ntp_secs, ntp_frac, rtp_timestamp,
45 &new_rtcp_sr)) {
asaperssonb7e7b492016-11-17 02:27:14 -080046 return false;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000047 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +000048
asaperssonb7e7b492016-11-17 02:27:14 -080049 return true;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000050}
asaperssonf8cdd182016-03-15 01:00:47 -070051} // namespace
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000052
mflodman4cd27902016-08-05 06:28:45 -070053RtpStreamsSynchronizer::RtpStreamsSynchronizer(
54 vcm::VideoReceiver* video_receiver,
55 RtpStreamReceiver* rtp_stream_receiver)
56 : clock_(Clock::GetRealTimeClock()),
57 video_receiver_(video_receiver),
58 video_rtp_receiver_(rtp_stream_receiver->GetRtpReceiver()),
59 video_rtp_rtcp_(rtp_stream_receiver->rtp_rtcp()),
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000060 voe_channel_id_(-1),
Peter Boström74f6e9e2016-04-04 17:56:10 +020061 voe_sync_interface_(nullptr),
mflodman4cd27902016-08-05 06:28:45 -070062 audio_rtp_receiver_(nullptr),
63 audio_rtp_rtcp_(nullptr),
64 sync_(),
65 last_sync_time_(rtc::TimeNanos()) {
66 process_thread_checker_.DetachFromThread();
niklase@google.com470e71d2011-07-07 08:21:25 +000067}
68
mflodman4cd27902016-08-05 06:28:45 -070069void RtpStreamsSynchronizer::ConfigureSync(int voe_channel_id,
70 VoEVideoSync* voe_sync_interface) {
Peter Boström1794b262016-02-16 14:12:02 +010071 if (voe_channel_id != -1)
72 RTC_DCHECK(voe_sync_interface);
mflodman4cd27902016-08-05 06:28:45 -070073
74 rtc::CritScope lock(&crit_);
pbos8fc7fa72015-07-15 08:02:58 -070075 if (voe_channel_id_ == voe_channel_id &&
mflodman4cd27902016-08-05 06:28:45 -070076 voe_sync_interface_ == voe_sync_interface) {
77 // This prevents expensive no-ops.
Peter Boström1794b262016-02-16 14:12:02 +010078 return;
pbos8fc7fa72015-07-15 08:02:58 -070079 }
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000080 voe_channel_id_ = voe_channel_id;
81 voe_sync_interface_ = voe_sync_interface;
mflodman4cd27902016-08-05 06:28:45 -070082
83 audio_rtp_rtcp_ = nullptr;
84 audio_rtp_receiver_ = nullptr;
85 sync_.reset(nullptr);
86
87 if (voe_channel_id_ != -1) {
88 voe_sync_interface_->GetRtpRtcp(voe_channel_id_, &audio_rtp_rtcp_,
89 &audio_rtp_receiver_);
90 RTC_DCHECK(audio_rtp_rtcp_);
91 RTC_DCHECK(audio_rtp_receiver_);
92 sync_.reset(new StreamSynchronization(video_rtp_rtcp_->SSRC(),
93 voe_channel_id_));
94 }
niklase@google.com470e71d2011-07-07 08:21:25 +000095}
96
mflodman4cd27902016-08-05 06:28:45 -070097int64_t RtpStreamsSynchronizer::TimeUntilNextProcess() {
98 RTC_DCHECK_RUN_ON(&process_thread_checker_);
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000099 const int64_t kSyncIntervalMs = 1000;
Niels Möllerd28db7f2016-05-10 16:31:47 +0200100 return kSyncIntervalMs -
101 (rtc::TimeNanos() - last_sync_time_) / rtc::kNumNanosecsPerMillisec;
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000102}
103
mflodman4cd27902016-08-05 06:28:45 -0700104void RtpStreamsSynchronizer::Process() {
105 RTC_DCHECK_RUN_ON(&process_thread_checker_);
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000106
Peter Boström0b250722016-04-22 18:23:15 +0200107 const int current_video_delay_ms = video_receiver_->Delay();
mflodman4cd27902016-08-05 06:28:45 -0700108 last_sync_time_ = rtc::TimeNanos();
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000109
mflodman4cd27902016-08-05 06:28:45 -0700110 rtc::CritScope lock(&crit_);
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000111 if (voe_channel_id_ == -1) {
pbosa26ac922016-02-25 04:50:01 -0800112 return;
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000113 }
mflodman4cd27902016-08-05 06:28:45 -0700114 RTC_DCHECK(voe_sync_interface_);
115 RTC_DCHECK(sync_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000116
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000117 int audio_jitter_buffer_delay_ms = 0;
118 int playout_buffer_delay_ms = 0;
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000119 if (voe_sync_interface_->GetDelayEstimate(voe_channel_id_,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000120 &audio_jitter_buffer_delay_ms,
121 &playout_buffer_delay_ms) != 0) {
pbosa26ac922016-02-25 04:50:01 -0800122 return;
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000123 }
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000124 const int current_audio_delay_ms = audio_jitter_buffer_delay_ms +
125 playout_buffer_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
asaperssonb0c1b4e2016-09-17 01:00:01 -0700127 int64_t last_video_receive_ms = video_measurement_.latest_receive_time_ms;
asaperssonb7e7b492016-11-17 02:27:14 -0800128 if (!UpdateMeasurements(&video_measurement_, video_rtp_rtcp_,
129 video_rtp_receiver_)) {
pbosa26ac922016-02-25 04:50:01 -0800130 return;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000131 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000132
asaperssonb7e7b492016-11-17 02:27:14 -0800133 if (!UpdateMeasurements(&audio_measurement_, audio_rtp_rtcp_,
134 audio_rtp_receiver_)) {
pbosa26ac922016-02-25 04:50:01 -0800135 return;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000136 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000137
asaperssonb0c1b4e2016-09-17 01:00:01 -0700138 if (last_video_receive_ms == video_measurement_.latest_receive_time_ms) {
139 // No new video packet has been received since last update.
140 return;
141 }
142
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000143 int relative_delay_ms;
144 // Calculate how much later or earlier the audio stream is compared to video.
145 if (!sync_->ComputeRelativeDelay(audio_measurement_, video_measurement_,
146 &relative_delay_ms)) {
pbosa26ac922016-02-25 04:50:01 -0800147 return;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000148 }
149
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000150 TRACE_COUNTER1("webrtc", "SyncCurrentVideoDelay", current_video_delay_ms);
151 TRACE_COUNTER1("webrtc", "SyncCurrentAudioDelay", current_audio_delay_ms);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000152 TRACE_COUNTER1("webrtc", "SyncRelativeDelay", relative_delay_ms);
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000153 int target_audio_delay_ms = 0;
hclam@chromium.org7262ad12013-06-15 06:51:27 +0000154 int target_video_delay_ms = current_video_delay_ms;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000155 // Calculate the necessary extra audio delay and desired total video
156 // delay to get the streams in sync.
stefan@webrtc.org8d185262012-11-12 18:51:52 +0000157 if (!sync_->ComputeDelays(relative_delay_ms,
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000158 current_audio_delay_ms,
159 &target_audio_delay_ms,
160 &target_video_delay_ms)) {
pbosa26ac922016-02-25 04:50:01 -0800161 return;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000162 }
edjee@google.com79b02892013-04-04 19:43:34 +0000163
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000164 if (voe_sync_interface_->SetMinimumPlayoutDelay(
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000165 voe_channel_id_, target_audio_delay_ms) == -1) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000166 LOG(LS_ERROR) << "Error setting voice delay.";
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000167 }
Peter Boström0b250722016-04-22 18:23:15 +0200168 video_receiver_->SetMinimumPlayoutDelay(target_video_delay_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000169}
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000170
mflodman4cd27902016-08-05 06:28:45 -0700171bool RtpStreamsSynchronizer::GetStreamSyncOffsetInMs(
asaperssonde9e5ff2016-11-02 07:14:03 -0700172 const VideoFrame& frame,
173 int64_t* stream_offset_ms,
174 double* estimated_freq_khz) const {
mflodman4cd27902016-08-05 06:28:45 -0700175 rtc::CritScope lock(&crit_);
asaperssonf8cdd182016-03-15 01:00:47 -0700176 if (voe_channel_id_ == -1)
177 return false;
178
179 uint32_t playout_timestamp = 0;
180 if (voe_sync_interface_->GetPlayoutTimestamp(voe_channel_id_,
181 playout_timestamp) != 0) {
182 return false;
183 }
184
185 int64_t latest_audio_ntp;
asaperssonfe50b4d2016-12-22 07:53:51 -0800186 if (!audio_measurement_.rtp_to_ntp.Estimate(playout_timestamp,
187 &latest_audio_ntp)) {
asaperssonf8cdd182016-03-15 01:00:47 -0700188 return false;
189 }
190
191 int64_t latest_video_ntp;
asaperssonfe50b4d2016-12-22 07:53:51 -0800192 if (!video_measurement_.rtp_to_ntp.Estimate(frame.timestamp(),
193 &latest_video_ntp)) {
asaperssonf8cdd182016-03-15 01:00:47 -0700194 return false;
195 }
196
197 int64_t time_to_render_ms =
198 frame.render_time_ms() - clock_->TimeInMilliseconds();
199 if (time_to_render_ms > 0)
200 latest_video_ntp += time_to_render_ms;
201
202 *stream_offset_ms = latest_audio_ntp - latest_video_ntp;
asaperssonfe50b4d2016-12-22 07:53:51 -0800203 *estimated_freq_khz = video_measurement_.rtp_to_ntp.params().frequency_khz;
asaperssonf8cdd182016-03-15 01:00:47 -0700204 return true;
205}
206
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000207} // namespace webrtc