2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/video_engine/vie_sync_module.h"
13 #include "webrtc/modules/rtp_rtcp/interface/rtp_receiver.h"
14 #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"
25 enum { kSyncInterval = 1000};
27 int UpdateMeasurements(StreamSynchronization::Measurements* stream,
28 const RtpRtcp& rtp_rtcp, const RtpReceiver& receiver) {
29 if (!receiver.Timestamp(&stream->latest_timestamp))
31 if (!receiver.LastReceivedTimeMs(&stream->latest_receive_time_ms))
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,
45 bool new_rtcp_sr = false;
47 ntp_secs, ntp_frac, rtp_timestamp, &stream->rtcp, &new_rtcp_sr)) {
54 ViESyncModule::ViESyncModule(VideoCodingModule* vcm,
55 ViEChannel* vie_channel)
56 : data_cs_(CriticalSectionWrapper::CreateCriticalSection()),
58 vie_channel_(vie_channel),
59 video_receiver_(NULL),
60 video_rtp_rtcp_(NULL),
62 voe_sync_interface_(NULL),
63 last_sync_time_(TickTime::Now()),
67 ViESyncModule::~ViESyncModule() {
70 int ViESyncModule::ConfigureSync(int voe_channel_id,
71 VoEVideoSync* voe_sync_interface,
72 RtpRtcp* video_rtcp_module,
73 RtpReceiver* video_receiver) {
74 CriticalSectionScoped cs(data_cs_.get());
75 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;
79 sync_.reset(new StreamSynchronization(voe_channel_id, vie_channel_->Id()));
81 if (!voe_sync_interface) {
83 if (voe_channel_id >= 0) {
84 // Trying to set a voice channel but no interface exist.
92 int ViESyncModule::VoiceChannel() {
93 return voe_channel_id_;
96 int32_t ViESyncModule::TimeUntilNextProcess() {
97 return static_cast<int32_t>(kSyncInterval -
98 (TickTime::Now() - last_sync_time_).Milliseconds());
101 int32_t ViESyncModule::Process() {
102 CriticalSectionScoped cs(data_cs_.get());
103 last_sync_time_ = TickTime::Now();
105 const int current_video_delay_ms = vcm_->Delay();
106 WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, vie_channel_->Id(),
107 "Video delay (JB + decoder) is %d ms", current_video_delay_ms);
109 if (voe_channel_id_ == -1) {
112 assert(video_rtp_rtcp_ && voe_sync_interface_);
115 int audio_jitter_buffer_delay_ms = 0;
116 int playout_buffer_delay_ms = 0;
117 if (voe_sync_interface_->GetDelayEstimate(voe_channel_id_,
118 &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.
122 WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceVideo, vie_channel_->Id(),
123 "%s: VE_GetDelayEstimate error for voice_channel %d",
124 __FUNCTION__, voe_channel_id_);
127 const int current_audio_delay_ms = audio_jitter_buffer_delay_ms +
128 playout_buffer_delay_ms;
130 RtpRtcp* voice_rtp_rtcp = NULL;
131 RtpReceiver* voice_receiver = NULL;
132 if (0 != voe_sync_interface_->GetRtpRtcp(voe_channel_id_, &voice_rtp_rtcp,
136 assert(voice_rtp_rtcp);
137 assert(voice_receiver);
139 if (UpdateMeasurements(&video_measurement_, *video_rtp_rtcp_,
140 *video_receiver_) != 0) {
144 if (UpdateMeasurements(&audio_measurement_, *voice_rtp_rtcp,
145 *voice_receiver) != 0) {
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)) {
156 TRACE_COUNTER1("webrtc", "SyncCurrentVideoDelay", current_video_delay_ms);
157 TRACE_COUNTER1("webrtc", "SyncCurrentAudioDelay", current_audio_delay_ms);
158 TRACE_COUNTER1("webrtc", "SyncRelativeDelay", relative_delay_ms);
159 int target_audio_delay_ms = 0;
160 int target_video_delay_ms = current_video_delay_ms;
161 // Calculate the necessary extra audio delay and desired total video
162 // delay to get the streams in sync.
163 if (!sync_->ComputeDelays(relative_delay_ms,
164 current_audio_delay_ms,
165 &target_audio_delay_ms,
166 &target_video_delay_ms)) {
170 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,
174 target_audio_delay_ms, target_video_delay_ms);
175 if (voe_sync_interface_->SetMinimumPlayoutDelay(
176 voe_channel_id_, target_audio_delay_ms) == -1) {
177 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, vie_channel_->Id(),
178 "Error setting voice delay");
180 vcm_->SetMinimumPlayoutDelay(target_video_delay_ms);
184 int ViESyncModule::SetTargetBufferingDelay(int target_delay_ms) {
185 CriticalSectionScoped cs(data_cs_.get());
186 if (!voe_sync_interface_) {
187 WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, vie_channel_->Id(),
188 "voe_sync_interface_ NULL, can't set playout delay.");
191 sync_->SetTargetBufferingDelay(target_delay_ms);
192 // Setting initial playout delay to voice engine (video engine is updated via
193 // the VCM interface).
194 voe_sync_interface_->SetInitialPlayoutDelay(voe_channel_id_,
199 } // namespace webrtc