blob: 9feace79b930e189969d6fd4c4efd2b48866e86c [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
Peter Boström7623ce42015-12-09 12:13:30 +010011#include "webrtc/video/vie_sync_module.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"
tommie4f96502015-10-20 23:00:48 -070015#include "webrtc/base/trace_event.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010016#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
17#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010018#include "webrtc/modules/video_coding/include/video_coding.h"
asaperssonf8cdd182016-03-15 01:00:47 -070019#include "webrtc/system_wrappers/include/clock.h"
Peter Boström7623ce42015-12-09 12:13:30 +010020#include "webrtc/video/stream_synchronization.h"
asaperssonf8cdd182016-03-15 01:00:47 -070021#include "webrtc/video_frame.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000022#include "webrtc/voice_engine/include/voe_video_sync.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023
24namespace webrtc {
asaperssonf8cdd182016-03-15 01:00:47 -070025namespace {
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000026int UpdateMeasurements(StreamSynchronization::Measurements* stream,
wu@webrtc.org822fbd82013-08-15 23:38:54 +000027 const RtpRtcp& rtp_rtcp, const RtpReceiver& receiver) {
stefan@webrtc.org48df3812013-11-08 15:18:52 +000028 if (!receiver.Timestamp(&stream->latest_timestamp))
29 return -1;
30 if (!receiver.LastReceivedTimeMs(&stream->latest_receive_time_ms))
31 return -1;
wu@webrtc.orgcd701192014-04-24 22:10:24 +000032
33 uint32_t ntp_secs = 0;
34 uint32_t ntp_frac = 0;
35 uint32_t rtp_timestamp = 0;
36 if (0 != rtp_rtcp.RemoteNTP(&ntp_secs,
37 &ntp_frac,
wu@webrtc.org822fbd82013-08-15 23:38:54 +000038 NULL,
39 NULL,
wu@webrtc.orgcd701192014-04-24 22:10:24 +000040 &rtp_timestamp)) {
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000041 return -1;
42 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +000043
44 bool new_rtcp_sr = false;
wu@webrtc.org66773a02014-05-07 17:09:44 +000045 if (!UpdateRtcpList(
wu@webrtc.orgcd701192014-04-24 22:10:24 +000046 ntp_secs, ntp_frac, rtp_timestamp, &stream->rtcp, &new_rtcp_sr)) {
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000047 return -1;
48 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +000049
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000050 return 0;
51}
asaperssonf8cdd182016-03-15 01:00:47 -070052} // namespace
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000053
Peter Boström36a14382015-05-21 17:00:24 +020054ViESyncModule::ViESyncModule(VideoCodingModule* vcm)
Tommi97888bd2016-01-21 23:24:59 +010055 : vcm_(vcm),
asaperssonf8cdd182016-03-15 01:00:47 -070056 clock_(Clock::GetRealTimeClock()),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000057 video_receiver_(NULL),
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000058 video_rtp_rtcp_(NULL),
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000059 voe_channel_id_(-1),
60 voe_sync_interface_(NULL),
stefan@webrtc.org5f284982012-06-28 07:51:16 +000061 last_sync_time_(TickTime::Now()),
asaperssonf8cdd182016-03-15 01:00:47 -070062 sync_() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000063
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000064ViESyncModule::~ViESyncModule() {
niklase@google.com470e71d2011-07-07 08:21:25 +000065}
66
Peter Boström1794b262016-02-16 14:12:02 +010067void ViESyncModule::ConfigureSync(int voe_channel_id,
68 VoEVideoSync* voe_sync_interface,
69 RtpRtcp* video_rtcp_module,
70 RtpReceiver* video_receiver) {
71 if (voe_channel_id != -1)
72 RTC_DCHECK(voe_sync_interface);
Tommi97888bd2016-01-21 23:24:59 +010073 rtc::CritScope lock(&data_cs_);
pbos8fc7fa72015-07-15 08:02:58 -070074 // Prevent expensive no-ops.
75 if (voe_channel_id_ == voe_channel_id &&
76 voe_sync_interface_ == voe_sync_interface &&
77 video_receiver_ == video_receiver &&
78 video_rtp_rtcp_ == video_rtcp_module) {
Peter Boström1794b262016-02-16 14:12:02 +010079 return;
pbos8fc7fa72015-07-15 08:02:58 -070080 }
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000081 voe_channel_id_ = voe_channel_id;
82 voe_sync_interface_ = voe_sync_interface;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000083 video_receiver_ = video_receiver;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000084 video_rtp_rtcp_ = video_rtcp_module;
Peter Boström36a14382015-05-21 17:00:24 +020085 sync_.reset(
86 new StreamSynchronization(video_rtp_rtcp_->SSRC(), voe_channel_id));
niklase@google.com470e71d2011-07-07 08:21:25 +000087}
88
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000089int64_t ViESyncModule::TimeUntilNextProcess() {
90 const int64_t kSyncIntervalMs = 1000;
91 return kSyncIntervalMs - (TickTime::Now() - last_sync_time_).Milliseconds();
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000092}
93
pbosa26ac922016-02-25 04:50:01 -080094void ViESyncModule::Process() {
Tommi97888bd2016-01-21 23:24:59 +010095 rtc::CritScope lock(&data_cs_);
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000096 last_sync_time_ = TickTime::Now();
97
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +000098 const int current_video_delay_ms = vcm_->Delay();
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000099
100 if (voe_channel_id_ == -1) {
pbosa26ac922016-02-25 04:50:01 -0800101 return;
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000102 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000103 assert(video_rtp_rtcp_ && voe_sync_interface_);
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000104 assert(sync_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000105
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000106 int audio_jitter_buffer_delay_ms = 0;
107 int playout_buffer_delay_ms = 0;
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000108 if (voe_sync_interface_->GetDelayEstimate(voe_channel_id_,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000109 &audio_jitter_buffer_delay_ms,
110 &playout_buffer_delay_ms) != 0) {
pbosa26ac922016-02-25 04:50:01 -0800111 return;
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000112 }
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000113 const int current_audio_delay_ms = audio_jitter_buffer_delay_ms +
114 playout_buffer_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000115
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000116 RtpRtcp* voice_rtp_rtcp = NULL;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000117 RtpReceiver* voice_receiver = NULL;
118 if (0 != voe_sync_interface_->GetRtpRtcp(voe_channel_id_, &voice_rtp_rtcp,
119 &voice_receiver)) {
pbosa26ac922016-02-25 04:50:01 -0800120 return;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000121 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000122 assert(voice_rtp_rtcp);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000123 assert(voice_receiver);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000124
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000125 if (UpdateMeasurements(&video_measurement_, *video_rtp_rtcp_,
126 *video_receiver_) != 0) {
pbosa26ac922016-02-25 04:50:01 -0800127 return;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000128 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000129
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000130 if (UpdateMeasurements(&audio_measurement_, *voice_rtp_rtcp,
131 *voice_receiver) != 0) {
pbosa26ac922016-02-25 04:50:01 -0800132 return;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000133 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000134
135 int relative_delay_ms;
136 // Calculate how much later or earlier the audio stream is compared to video.
137 if (!sync_->ComputeRelativeDelay(audio_measurement_, video_measurement_,
138 &relative_delay_ms)) {
pbosa26ac922016-02-25 04:50:01 -0800139 return;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000140 }
141
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000142 TRACE_COUNTER1("webrtc", "SyncCurrentVideoDelay", current_video_delay_ms);
143 TRACE_COUNTER1("webrtc", "SyncCurrentAudioDelay", current_audio_delay_ms);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000144 TRACE_COUNTER1("webrtc", "SyncRelativeDelay", relative_delay_ms);
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000145 int target_audio_delay_ms = 0;
hclam@chromium.org7262ad12013-06-15 06:51:27 +0000146 int target_video_delay_ms = current_video_delay_ms;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000147 // Calculate the necessary extra audio delay and desired total video
148 // delay to get the streams in sync.
stefan@webrtc.org8d185262012-11-12 18:51:52 +0000149 if (!sync_->ComputeDelays(relative_delay_ms,
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000150 current_audio_delay_ms,
151 &target_audio_delay_ms,
152 &target_video_delay_ms)) {
pbosa26ac922016-02-25 04:50:01 -0800153 return;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000154 }
edjee@google.com79b02892013-04-04 19:43:34 +0000155
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000156 if (voe_sync_interface_->SetMinimumPlayoutDelay(
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000157 voe_channel_id_, target_audio_delay_ms) == -1) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000158 LOG(LS_ERROR) << "Error setting voice delay.";
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000159 }
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000160 vcm_->SetMinimumPlayoutDelay(target_video_delay_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000161}
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000162
asaperssonf8cdd182016-03-15 01:00:47 -0700163bool ViESyncModule::GetStreamSyncOffsetInMs(const VideoFrame& frame,
164 int64_t* stream_offset_ms) const {
165 rtc::CritScope lock(&data_cs_);
166 if (voe_channel_id_ == -1)
167 return false;
168
169 uint32_t playout_timestamp = 0;
170 if (voe_sync_interface_->GetPlayoutTimestamp(voe_channel_id_,
171 playout_timestamp) != 0) {
172 return false;
173 }
174
175 int64_t latest_audio_ntp;
176 if (!RtpToNtpMs(playout_timestamp, audio_measurement_.rtcp,
177 &latest_audio_ntp)) {
178 return false;
179 }
180
181 int64_t latest_video_ntp;
182 if (!RtpToNtpMs(frame.timestamp(), video_measurement_.rtcp,
183 &latest_video_ntp)) {
184 return false;
185 }
186
187 int64_t time_to_render_ms =
188 frame.render_time_ms() - clock_->TimeInMilliseconds();
189 if (time_to_render_ms > 0)
190 latest_video_ntp += time_to_render_ms;
191
192 *stream_offset_ms = latest_audio_ntp - latest_video_ntp;
193 return true;
194}
195
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000196} // namespace webrtc