blob: d0617d6a0771471397968df88053d581273d7610 [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,
27 const RtpRtcp* rtp_rtcp) {
28 stream->latest_timestamp = rtp_rtcp->RemoteTimestamp();
29 stream->latest_receive_time_ms = rtp_rtcp->LocalTimeOfRemoteTimeStamp();
30 synchronization::RtcpMeasurement measurement;
31 if (0 != rtp_rtcp->RemoteNTP(&measurement.ntp_secs,
32 &measurement.ntp_frac,
33 NULL,
34 NULL,
35 &measurement.rtp_timestamp)) {
36 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,
75 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
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000106 int total_video_delay_target_ms = vcm_->Delay();
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000107 WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, vie_channel_->Id(),
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000108 "Video delay (JB + decoder) is %d ms",
109 total_video_delay_target_ms);
110
111 if (voe_channel_id_ == -1) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000112 return 0;
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000113 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000114 assert(video_rtp_rtcp_ && voe_sync_interface_);
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000115 assert(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) {
122 // Could not get VoE delay value, probably not a valid channel Id or
123 // the channel have not received enough packets.
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000124 WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceVideo, vie_channel_->Id(),
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000125 "%s: VE_GetDelayEstimate error for voice_channel %d",
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000126 __FUNCTION__, voe_channel_id_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000127 return 0;
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000128 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000129
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000130 RtpRtcp* voice_rtp_rtcp = NULL;
131 if (0 != voe_sync_interface_->GetRtpRtcp(voe_channel_id_, voice_rtp_rtcp)) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000132 return 0;
133 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000134 assert(voice_rtp_rtcp);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000135
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000136 if (UpdateMeasurements(&video_measurement_, video_rtp_rtcp_) != 0) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000137 return 0;
138 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000139
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000140 if (UpdateMeasurements(&audio_measurement_, voice_rtp_rtcp) != 0) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000141 return 0;
142 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000143
144 int relative_delay_ms;
145 // Calculate how much later or earlier the audio stream is compared to video.
146 if (!sync_->ComputeRelativeDelay(audio_measurement_, video_measurement_,
147 &relative_delay_ms)) {
148 return 0;
149 }
150
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000151 TRACE_COUNTER1("webrtc", "SyncCurrentVideoDelay",
152 total_video_delay_target_ms);
153 TRACE_COUNTER1("webrtc", "SyncCurrentAudioDelay",
154 audio_jitter_buffer_delay_ms);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000155 TRACE_COUNTER1("webrtc", "SyncRelativeDelay", relative_delay_ms);
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000156 int extra_audio_delay_ms = 0;
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,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000160 audio_jitter_buffer_delay_ms,
stefan@webrtc.org8d185262012-11-12 18:51:52 +0000161 &extra_audio_delay_ms,
162 &total_video_delay_target_ms)) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000163 return 0;
164 }
edjee@google.com79b02892013-04-04 19:43:34 +0000165
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000166 TRACE_COUNTER1("webrtc", "SyncExtraAudioDelayTarget", extra_audio_delay_ms);
167 TRACE_COUNTER1("webrtc", "SyncTotalVideoDelayTarget",
edjee@google.com79b02892013-04-04 19:43:34 +0000168 total_video_delay_target_ms);
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000169 if (voe_sync_interface_->SetMinimumPlayoutDelay(
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000170 voe_channel_id_, extra_audio_delay_ms) == -1) {
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000171 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, vie_channel_->Id(),
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000172 "Error setting voice delay");
173 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000174 vcm_->SetMinimumPlayoutDelay(total_video_delay_target_ms);
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000175 WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, vie_channel_->Id(),
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000176 "New Video delay target is: %d", total_video_delay_target_ms);
177 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