blob: 31a7a6d49c33724620e5e1c8f8a5f3f6734d0666 [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
wu@webrtc.org822fbd82013-08-15 23:38:54 +000013#include "webrtc/modules/rtp_rtcp/interface/rtp_receiver.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000014#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
15#include "webrtc/modules/video_coding/main/interface/video_coding.h"
16#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
17#include "webrtc/system_wrappers/interface/trace.h"
18#include "webrtc/system_wrappers/interface/trace_event.h"
19#include "webrtc/video_engine/stream_synchronization.h"
20#include "webrtc/video_engine/vie_channel.h"
21#include "webrtc/voice_engine/include/voe_video_sync.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
23namespace webrtc {
24
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000025enum { kSyncInterval = 1000};
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000026
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000027int UpdateMeasurements(StreamSynchronization::Measurements* stream,
wu@webrtc.org822fbd82013-08-15 23:38:54 +000028 const RtpRtcp& rtp_rtcp, const RtpReceiver& receiver) {
stefan@webrtc.org48df3812013-11-08 15:18:52 +000029 if (!receiver.Timestamp(&stream->latest_timestamp))
30 return -1;
31 if (!receiver.LastReceivedTimeMs(&stream->latest_receive_time_ms))
32 return -1;
wu@webrtc.orgcd701192014-04-24 22:10:24 +000033
34 uint32_t ntp_secs = 0;
35 uint32_t ntp_frac = 0;
36 uint32_t rtp_timestamp = 0;
37 if (0 != rtp_rtcp.RemoteNTP(&ntp_secs,
38 &ntp_frac,
wu@webrtc.org822fbd82013-08-15 23:38:54 +000039 NULL,
40 NULL,
wu@webrtc.orgcd701192014-04-24 22:10:24 +000041 &rtp_timestamp)) {
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000042 return -1;
43 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +000044
45 bool new_rtcp_sr = false;
wu@webrtc.org66773a02014-05-07 17:09:44 +000046 if (!UpdateRtcpList(
wu@webrtc.orgcd701192014-04-24 22:10:24 +000047 ntp_secs, ntp_frac, rtp_timestamp, &stream->rtcp, &new_rtcp_sr)) {
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000048 return -1;
49 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +000050
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000051 return 0;
52}
53
54ViESyncModule::ViESyncModule(VideoCodingModule* vcm,
55 ViEChannel* vie_channel)
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +000056 : data_cs_(CriticalSectionWrapper::CreateCriticalSection()),
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000057 vcm_(vcm),
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000058 vie_channel_(vie_channel),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000059 video_receiver_(NULL),
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000060 video_rtp_rtcp_(NULL),
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000061 voe_channel_id_(-1),
62 voe_sync_interface_(NULL),
stefan@webrtc.org5f284982012-06-28 07:51:16 +000063 last_sync_time_(TickTime::Now()),
64 sync_() {
niklase@google.com470e71d2011-07-07 08:21:25 +000065}
66
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000067ViESyncModule::~ViESyncModule() {
niklase@google.com470e71d2011-07-07 08:21:25 +000068}
69
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000070int ViESyncModule::ConfigureSync(int voe_channel_id,
71 VoEVideoSync* voe_sync_interface,
wu@webrtc.org822fbd82013-08-15 23:38:54 +000072 RtpRtcp* video_rtcp_module,
73 RtpReceiver* video_receiver) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +000074 CriticalSectionScoped cs(data_cs_.get());
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000075 voe_channel_id_ = voe_channel_id;
76 voe_sync_interface_ = voe_sync_interface;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000077 video_receiver_ = video_receiver;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000078 video_rtp_rtcp_ = video_rtcp_module;
79 sync_.reset(new StreamSynchronization(voe_channel_id, vie_channel_->Id()));
niklase@google.com470e71d2011-07-07 08:21:25 +000080
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000081 if (!voe_sync_interface) {
82 voe_channel_id_ = -1;
83 if (voe_channel_id >= 0) {
84 // Trying to set a voice channel but no interface exist.
85 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +000086 }
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000087 return 0;
88 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000089 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000090}
91
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000092int ViESyncModule::VoiceChannel() {
93 return voe_channel_id_;
niklase@google.com470e71d2011-07-07 08:21:25 +000094}
95
pbos@webrtc.orgb238d122013-04-09 13:41:51 +000096int32_t ViESyncModule::TimeUntilNextProcess() {
97 return static_cast<int32_t>(kSyncInterval -
98 (TickTime::Now() - last_sync_time_).Milliseconds());
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000099}
100
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000101int32_t ViESyncModule::Process() {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000102 CriticalSectionScoped cs(data_cs_.get());
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000103 last_sync_time_ = TickTime::Now();
104
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000105 const int current_video_delay_ms = vcm_->Delay();
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000106 WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, vie_channel_->Id(),
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000107 "Video delay (JB + decoder) is %d ms", current_video_delay_ms);
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000108
109 if (voe_channel_id_ == -1) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000110 return 0;
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000111 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000112 assert(video_rtp_rtcp_ && voe_sync_interface_);
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000113 assert(sync_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000114
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000115 int audio_jitter_buffer_delay_ms = 0;
116 int playout_buffer_delay_ms = 0;
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000117 if (voe_sync_interface_->GetDelayEstimate(voe_channel_id_,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000118 &audio_jitter_buffer_delay_ms,
119 &playout_buffer_delay_ms) != 0) {
120 // Could not get VoE delay value, probably not a valid channel Id or
121 // the channel have not received enough packets.
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000122 WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceVideo, vie_channel_->Id(),
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000123 "%s: VE_GetDelayEstimate error for voice_channel %d",
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000124 __FUNCTION__, voe_channel_id_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000125 return 0;
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000126 }
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000127 const int current_audio_delay_ms = audio_jitter_buffer_delay_ms +
128 playout_buffer_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000129
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000130 RtpRtcp* voice_rtp_rtcp = NULL;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000131 RtpReceiver* voice_receiver = NULL;
132 if (0 != voe_sync_interface_->GetRtpRtcp(voe_channel_id_, &voice_rtp_rtcp,
133 &voice_receiver)) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000134 return 0;
135 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000136 assert(voice_rtp_rtcp);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000137 assert(voice_receiver);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000138
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000139 if (UpdateMeasurements(&video_measurement_, *video_rtp_rtcp_,
140 *video_receiver_) != 0) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000141 return 0;
142 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000143
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000144 if (UpdateMeasurements(&audio_measurement_, *voice_rtp_rtcp,
145 *voice_receiver) != 0) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000146 return 0;
147 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000148
149 int relative_delay_ms;
150 // Calculate how much later or earlier the audio stream is compared to video.
151 if (!sync_->ComputeRelativeDelay(audio_measurement_, video_measurement_,
152 &relative_delay_ms)) {
153 return 0;
154 }
155
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000156 TRACE_COUNTER1("webrtc", "SyncCurrentVideoDelay", current_video_delay_ms);
157 TRACE_COUNTER1("webrtc", "SyncCurrentAudioDelay", current_audio_delay_ms);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000158 TRACE_COUNTER1("webrtc", "SyncRelativeDelay", relative_delay_ms);
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000159 int target_audio_delay_ms = 0;
hclam@chromium.org7262ad12013-06-15 06:51:27 +0000160 int target_video_delay_ms = current_video_delay_ms;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000161 // Calculate the necessary extra audio delay and desired total video
162 // delay to get the streams in sync.
stefan@webrtc.org8d185262012-11-12 18:51:52 +0000163 if (!sync_->ComputeDelays(relative_delay_ms,
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000164 current_audio_delay_ms,
165 &target_audio_delay_ms,
166 &target_video_delay_ms)) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000167 return 0;
168 }
edjee@google.com79b02892013-04-04 19:43:34 +0000169
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000170 WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, vie_channel_->Id(),
171 "Set delay current(a=%d v=%d rel=%d) target(a=%d v=%d)",
172 current_audio_delay_ms, current_video_delay_ms,
173 relative_delay_ms,
174 target_audio_delay_ms, target_video_delay_ms);
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000175 if (voe_sync_interface_->SetMinimumPlayoutDelay(
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000176 voe_channel_id_, target_audio_delay_ms) == -1) {
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000177 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, vie_channel_->Id(),
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000178 "Error setting voice delay");
179 }
hclam@chromium.org9b23ecb2013-06-14 23:30:58 +0000180 vcm_->SetMinimumPlayoutDelay(target_video_delay_ms);
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000181 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000182}
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000183
mikhal@webrtc.orgefe4edb2013-03-06 23:29:33 +0000184int ViESyncModule::SetTargetBufferingDelay(int target_delay_ms) {
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000185 CriticalSectionScoped cs(data_cs_.get());
mikhal@webrtc.orgefe4edb2013-03-06 23:29:33 +0000186 if (!voe_sync_interface_) {
187 WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, vie_channel_->Id(),
188 "voe_sync_interface_ NULL, can't set playout delay.");
189 return -1;
190 }
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000191 sync_->SetTargetBufferingDelay(target_delay_ms);
192 // Setting initial playout delay to voice engine (video engine is updated via
193 // the VCM interface).
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000194 voe_sync_interface_->SetInitialPlayoutDelay(voe_channel_id_,
195 target_delay_ms);
mikhal@webrtc.orgefe4edb2013-03-06 23:29:33 +0000196 return 0;
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000197}
198
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000199} // namespace webrtc