blob: e973898681ce63c874fe40cd742b68676114f950 [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
mflodman@webrtc.orgab2610f2012-06-29 10:05:28 +000011#include "video_engine/vie_sync_module.h"
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000012
mflodman@webrtc.orgab2610f2012-06-29 10:05:28 +000013#include "modules/rtp_rtcp/interface/rtp_rtcp.h"
14#include "modules/video_coding/main/interface/video_coding.h"
15#include "system_wrappers/interface/critical_section_wrapper.h"
16#include "system_wrappers/interface/trace.h"
stefan@webrtc.org5f284982012-06-28 07:51:16 +000017#include "video_engine/stream_synchronization.h"
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000018#include "video_engine/vie_channel.h"
andrew@webrtc.org6f8db362012-07-27 21:49:28 +000019#include "voice_engine/include/voe_video_sync.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
21namespace webrtc {
22
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000023enum { kSyncInterval = 1000};
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000024
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000025int UpdateMeasurements(StreamSynchronization::Measurements* stream,
26 const RtpRtcp* rtp_rtcp) {
27 stream->latest_timestamp = rtp_rtcp->RemoteTimestamp();
28 stream->latest_receive_time_ms = rtp_rtcp->LocalTimeOfRemoteTimeStamp();
29 synchronization::RtcpMeasurement measurement;
30 if (0 != rtp_rtcp->RemoteNTP(&measurement.ntp_secs,
31 &measurement.ntp_frac,
32 NULL,
33 NULL,
34 &measurement.rtp_timestamp)) {
35 return -1;
36 }
37 if (measurement.ntp_secs == 0 && measurement.ntp_frac == 0) {
38 return -1;
39 }
40 for (synchronization::RtcpList::iterator it = stream->rtcp.begin();
41 it != stream->rtcp.end(); ++it) {
42 if (measurement.ntp_secs == (*it).ntp_secs &&
43 measurement.ntp_frac == (*it).ntp_frac) {
44 // This RTCP has already been added to the list.
45 return 0;
46 }
47 }
48 // We need two RTCP SR reports to map between RTP and NTP. More than two will
49 // not improve the mapping.
50 if (stream->rtcp.size() == 2) {
51 stream->rtcp.pop_back();
52 }
53 stream->rtcp.push_front(measurement);
54 return 0;
55}
56
57ViESyncModule::ViESyncModule(VideoCodingModule* vcm,
58 ViEChannel* vie_channel)
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +000059 : data_cs_(CriticalSectionWrapper::CreateCriticalSection()),
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000060 vcm_(vcm),
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000061 vie_channel_(vie_channel),
62 video_rtp_rtcp_(NULL),
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000063 voe_channel_id_(-1),
64 voe_sync_interface_(NULL),
stefan@webrtc.org5f284982012-06-28 07:51:16 +000065 last_sync_time_(TickTime::Now()),
66 sync_() {
niklase@google.com470e71d2011-07-07 08:21:25 +000067}
68
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000069ViESyncModule::~ViESyncModule() {
niklase@google.com470e71d2011-07-07 08:21:25 +000070}
71
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000072int ViESyncModule::ConfigureSync(int voe_channel_id,
73 VoEVideoSync* voe_sync_interface,
74 RtpRtcp* video_rtcp_module) {
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +000075 CriticalSectionScoped cs(data_cs_.get());
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000076 voe_channel_id_ = voe_channel_id;
77 voe_sync_interface_ = voe_sync_interface;
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
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000096WebRtc_Word32 ViESyncModule::TimeUntilNextProcess() {
stefan@webrtc.org5f284982012-06-28 07:51:16 +000097 return static_cast<WebRtc_Word32>(kSyncInterval -
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +000098 (TickTime::Now() - last_sync_time_).Milliseconds());
99}
100
101WebRtc_Word32 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
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000105 int total_video_delay_target_ms = vcm_->Delay();
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000106 WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, vie_channel_->Id(),
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000107 "Video delay (JB + decoder) is %d ms",
108 total_video_delay_target_ms);
109
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
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000116 int current_audio_delay_ms = 0;
117 if (voe_sync_interface_->GetDelayEstimate(voe_channel_id_,
118 current_audio_delay_ms) != 0) {
119 // Could not get VoE delay value, probably not a valid channel Id.
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000120 WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceVideo, vie_channel_->Id(),
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000121 "%s: VE_GetDelayEstimate error for voice_channel %d",
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000122 __FUNCTION__, voe_channel_id_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000123 return 0;
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000124 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000125
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000126 // VoiceEngine report delay estimates even when not started, ignore if the
127 // reported value is lower than 40 ms.
128 if (current_audio_delay_ms < 40) {
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000129 WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, vie_channel_->Id(),
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000130 "A/V Sync: Audio delay < 40, skipping.");
131 return 0;
132 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000133
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000134 RtpRtcp* voice_rtp_rtcp = NULL;
135 if (0 != voe_sync_interface_->GetRtpRtcp(voe_channel_id_, voice_rtp_rtcp)) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000136 return 0;
137 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000138 assert(voice_rtp_rtcp);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000139
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000140 if (UpdateMeasurements(&video_measurement_, video_rtp_rtcp_) != 0) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000141 return 0;
142 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000143
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000144 if (UpdateMeasurements(&audio_measurement_, voice_rtp_rtcp) != 0) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000145 return 0;
146 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000147
148 int relative_delay_ms;
149 // Calculate how much later or earlier the audio stream is compared to video.
150 if (!sync_->ComputeRelativeDelay(audio_measurement_, video_measurement_,
151 &relative_delay_ms)) {
152 return 0;
153 }
154
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000155 int extra_audio_delay_ms = 0;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000156 // Calculate the necessary extra audio delay and desired total video
157 // delay to get the streams in sync.
stefan@webrtc.org8d185262012-11-12 18:51:52 +0000158 if (!sync_->ComputeDelays(relative_delay_ms,
159 current_audio_delay_ms,
160 &extra_audio_delay_ms,
161 &total_video_delay_target_ms)) {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000162 return 0;
163 }
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000164 if (voe_sync_interface_->SetMinimumPlayoutDelay(
stefan@webrtc.org5f284982012-06-28 07:51:16 +0000165 voe_channel_id_, extra_audio_delay_ms) == -1) {
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000166 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, vie_channel_->Id(),
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000167 "Error setting voice delay");
168 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000169 vcm_->SetMinimumPlayoutDelay(total_video_delay_target_ms);
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000170 WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, vie_channel_->Id(),
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000171 "New Video delay target is: %d", total_video_delay_target_ms);
172 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000173}
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000174
mikhal@webrtc.orgefe4edb2013-03-06 23:29:33 +0000175int ViESyncModule::SetTargetBufferingDelay(int target_delay_ms) {
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000176 CriticalSectionScoped cs(data_cs_.get());
mikhal@webrtc.orgefe4edb2013-03-06 23:29:33 +0000177 if (!voe_sync_interface_) {
178 WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, vie_channel_->Id(),
179 "voe_sync_interface_ NULL, can't set playout delay.");
180 return -1;
181 }
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000182 sync_->SetTargetBufferingDelay(target_delay_ms);
183 // Setting initial playout delay to voice engine (video engine is updated via
184 // the VCM interface).
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000185 voe_sync_interface_->SetInitialPlayoutDelay(voe_channel_id_,
186 target_delay_ms);
mikhal@webrtc.orgefe4edb2013-03-06 23:29:33 +0000187 return 0;
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000188}
189
mflodman@webrtc.org511f82e2011-11-30 18:31:36 +0000190} // namespace webrtc