blob: aecfa2a4261b5ac298379580c207ba06990ebecc [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
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000011#include "webrtc/video_engine/vie_sync_module.h"
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000012
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000013#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
14#include "webrtc/modules/video_coding/main/interface/video_coding.h"
15#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
16#include "webrtc/system_wrappers/interface/trace.h"
17#include "webrtc/system_wrappers/interface/trace_event.h"
18#include "webrtc/video_engine/stream_synchronization.h"
19#include "webrtc/video_engine/vie_channel.h"
20#include "webrtc/voice_engine/include/voe_video_sync.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
22namespace webrtc {
23
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000024enum { kSyncInterval = 1000};
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000025
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000026int UpdateMeasurements(StreamSynchronization::Measurements* stream,
tnakamura@webrtc.orgaa4d96a2013-07-16 19:25:04 +000027 const RtpRtcp* rtp_rtcp) {
28 stream->latest_timestamp = rtp_rtcp->RemoteTimestamp();
29 stream->latest_receive_time_ms = rtp_rtcp->LocalTimeOfRemoteTimeStamp();
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000030 synchronization::RtcpMeasurement measurement;
tnakamura@webrtc.orgaa4d96a2013-07-16 19:25:04 +000031 if (0 != rtp_rtcp->RemoteNTP(&measurement.ntp_secs,
32 &measurement.ntp_frac,
33 NULL,
34 NULL,
35 &measurement.rtp_timestamp)) {
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000036 return -1;
37 }
38 if (measurement.ntp_secs == 0 && measurement.ntp_frac == 0) {
39 return -1;
40 }
41 for (synchronization::RtcpList::iterator it = stream->rtcp.begin();
42 it != stream->rtcp.end(); ++it) {
43 if (measurement.ntp_secs == (*it).ntp_secs &&
44 measurement.ntp_frac == (*it).ntp_frac) {
45 // This RTCP has already been added to the list.
46 return 0;
47 }
48 }
49 // We need two RTCP SR reports to map between RTP and NTP. More than two will
50 // not improve the mapping.
51 if (stream->rtcp.size() == 2) {
52 stream->rtcp.pop_back();
53 }
54 stream->rtcp.push_front(measurement);
55 return 0;
56}
57
58ViESyncModule::ViESyncModule(VideoCodingModule* vcm,
59 ViEChannel* vie_channel)
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +000060 : data_cs_(CriticalSectionWrapper::CreateCriticalSection()),
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000061 vcm_(vcm),
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000062 vie_channel_(vie_channel),
63 video_rtp_rtcp_(NULL),
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000064 voe_channel_id_(-1),
65 voe_sync_interface_(NULL),
stefan@webrtc.org5f284982012-06-28 07:51:16 +000066 last_sync_time_(TickTime::Now()),
67 sync_() {
niklase@google.com470e71d2011-07-07 08:21:25 +000068}
69
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000070ViESyncModule::~ViESyncModule() {
niklase@google.com470e71d2011-07-07 08:21:25 +000071}
72
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000073int ViESyncModule::ConfigureSync(int voe_channel_id,
74 VoEVideoSync* voe_sync_interface,
tnakamura@webrtc.orgaa4d96a2013-07-16 19:25:04 +000075 RtpRtcp* video_rtcp_module) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +000076 CriticalSectionScoped cs(data_cs_.get());
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000077 voe_channel_id_ = voe_channel_id;
78 voe_sync_interface_ = voe_sync_interface;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000079 video_rtp_rtcp_ = video_rtcp_module;
80 sync_.reset(new StreamSynchronization(voe_channel_id, vie_channel_->Id()));
niklase@google.com470e71d2011-07-07 08:21:25 +000081
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000082 if (!voe_sync_interface) {
83 voe_channel_id_ = -1;
84 if (voe_channel_id >= 0) {
85 // Trying to set a voice channel but no interface exist.
86 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +000087 }
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000088 return 0;
89 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000090 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000091}
92
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000093int ViESyncModule::VoiceChannel() {
94 return voe_channel_id_;
niklase@google.com470e71d2011-07-07 08:21:25 +000095}
96
pbos@webrtc.orgb238d122013-04-09 13:41:51 +000097int32_t ViESyncModule::TimeUntilNextProcess() {
98 return static_cast<int32_t>(kSyncInterval -
99 (TickTime::Now() - last_sync_time_).Milliseconds());
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000100}
101
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000102int32_t ViESyncModule::Process() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000103 CriticalSectionScoped cs(data_cs_.get());
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000104 last_sync_time_ = TickTime::Now();
105
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000106 const int current_video_delay_ms = vcm_->Delay();
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000107 WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, vie_channel_->Id(),
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000108 "Video delay (JB + decoder) is %d ms", current_video_delay_ms);
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000109
110 if (voe_channel_id_ == -1) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000111 return 0;
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000112 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000113 assert(video_rtp_rtcp_ && voe_sync_interface_);
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000114 assert(sync_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000115
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000116 int audio_jitter_buffer_delay_ms = 0;
117 int playout_buffer_delay_ms = 0;
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000118 if (voe_sync_interface_->GetDelayEstimate(voe_channel_id_,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000119 &audio_jitter_buffer_delay_ms,
120 &playout_buffer_delay_ms) != 0) {
121 // Could not get VoE delay value, probably not a valid channel Id or
122 // the channel have not received enough packets.
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000123 WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceVideo, vie_channel_->Id(),
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000124 "%s: VE_GetDelayEstimate error for voice_channel %d",
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000125 __FUNCTION__, voe_channel_id_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000126 return 0;
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000127 }
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000128 const int current_audio_delay_ms = audio_jitter_buffer_delay_ms +
129 playout_buffer_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000130
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000131 RtpRtcp* voice_rtp_rtcp = NULL;
tnakamura@webrtc.orgaa4d96a2013-07-16 19:25:04 +0000132 if (0 != voe_sync_interface_->GetRtpRtcp(voe_channel_id_, voice_rtp_rtcp)) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000133 return 0;
134 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000135 assert(voice_rtp_rtcp);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000136
tnakamura@webrtc.orgaa4d96a2013-07-16 19:25:04 +0000137 if (UpdateMeasurements(&video_measurement_, video_rtp_rtcp_) != 0) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000138 return 0;
139 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000140
tnakamura@webrtc.orgaa4d96a2013-07-16 19:25:04 +0000141 if (UpdateMeasurements(&audio_measurement_, voice_rtp_rtcp) != 0) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000142 return 0;
143 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000144
145 int relative_delay_ms;
146 // Calculate how much later or earlier the audio stream is compared to video.
147 if (!sync_->ComputeRelativeDelay(audio_measurement_, video_measurement_,
148 &relative_delay_ms)) {
149 return 0;
150 }
151
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000152 TRACE_COUNTER1("webrtc", "SyncCurrentVideoDelay", current_video_delay_ms);
153 TRACE_COUNTER1("webrtc", "SyncCurrentAudioDelay", current_audio_delay_ms);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000154 TRACE_COUNTER1("webrtc", "SyncRelativeDelay", relative_delay_ms);
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000155 int target_audio_delay_ms = 0;
hclam@chromium.org7262ad12013-06-15 06:51:27 +0000156 int target_video_delay_ms = current_video_delay_ms;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000157 // Calculate the necessary extra audio delay and desired total video
158 // delay to get the streams in sync.
stefan@webrtc.org8d185262012-11-12 18:51:52 +0000159 if (!sync_->ComputeDelays(relative_delay_ms,
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000160 current_audio_delay_ms,
161 &target_audio_delay_ms,
162 &target_video_delay_ms)) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000163 return 0;
164 }
edjee@google.com79b02892013-04-04 19:43:34 +0000165
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000166 WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, vie_channel_->Id(),
167 "Set delay current(a=%d v=%d rel=%d) target(a=%d v=%d)",
168 current_audio_delay_ms, current_video_delay_ms,
169 relative_delay_ms,
170 target_audio_delay_ms, target_video_delay_ms);
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000171 if (voe_sync_interface_->SetMinimumPlayoutDelay(
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000172 voe_channel_id_, target_audio_delay_ms) == -1) {
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000173 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, vie_channel_->Id(),
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000174 "Error setting voice delay");
175 }
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000176 vcm_->SetMinimumPlayoutDelay(target_video_delay_ms);
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000177 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000178}
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000179
mikhal@webrtc.orgefe4edb2013-03-06 23:29:33 +0000180int ViESyncModule::SetTargetBufferingDelay(int target_delay_ms) {
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000181 CriticalSectionScoped cs(data_cs_.get());
mikhal@webrtc.orgefe4edb2013-03-06 23:29:33 +0000182 if (!voe_sync_interface_) {
183 WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, vie_channel_->Id(),
184 "voe_sync_interface_ NULL, can't set playout delay.");
185 return -1;
186 }
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000187 sync_->SetTargetBufferingDelay(target_delay_ms);
188 // Setting initial playout delay to voice engine (video engine is updated via
189 // the VCM interface).
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000190 voe_sync_interface_->SetInitialPlayoutDelay(voe_channel_id_,
191 target_delay_ms);
mikhal@webrtc.orgefe4edb2013-03-06 23:29:33 +0000192 return 0;
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000193}
194
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000195} // namespace webrtc