2011-07-07 08:21:25 +00:00
|
|
|
/*
|
2012-03-01 18:22:48 +00:00
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-07-07 08:21:25 +00:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "vie_sync_module.h"
|
2011-11-30 18:31:36 +00:00
|
|
|
|
2011-07-07 08:21:25 +00:00
|
|
|
#include "critical_section_wrapper.h"
|
|
|
|
|
#include "rtp_rtcp.h"
|
|
|
|
|
#include "trace.h"
|
|
|
|
|
#include "video_coding.h"
|
2011-11-30 18:31:36 +00:00
|
|
|
#include "voe_video_sync.h"
|
2012-06-28 07:51:16 +00:00
|
|
|
#include "video_engine/stream_synchronization.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2011-11-30 18:31:36 +00:00
|
|
|
enum { kSyncInterval = 1000};
|
2012-05-11 11:08:54 +00:00
|
|
|
|
|
|
|
|
ViESyncModule::ViESyncModule(const int32_t channel_id, VideoCodingModule* vcm)
|
2011-12-22 14:17:53 +00:00
|
|
|
: data_cs_(CriticalSectionWrapper::CreateCriticalSection()),
|
2012-05-11 11:08:54 +00:00
|
|
|
channel_id_(channel_id),
|
2011-11-30 18:31:36 +00:00
|
|
|
vcm_(vcm),
|
2012-05-11 11:08:54 +00:00
|
|
|
video_rtcp_module_(NULL),
|
2011-11-30 18:31:36 +00:00
|
|
|
voe_channel_id_(-1),
|
|
|
|
|
voe_sync_interface_(NULL),
|
2012-06-28 07:51:16 +00:00
|
|
|
last_sync_time_(TickTime::Now()),
|
|
|
|
|
sync_() {
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-30 18:31:36 +00:00
|
|
|
ViESyncModule::~ViESyncModule() {
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-11 11:08:54 +00:00
|
|
|
int ViESyncModule::ConfigureSync(int voe_channel_id,
|
|
|
|
|
VoEVideoSync* voe_sync_interface,
|
|
|
|
|
RtpRtcp* video_rtcp_module) {
|
2011-12-22 14:17:53 +00:00
|
|
|
CriticalSectionScoped cs(data_cs_.get());
|
2011-11-30 18:31:36 +00:00
|
|
|
voe_channel_id_ = voe_channel_id;
|
|
|
|
|
voe_sync_interface_ = voe_sync_interface;
|
2012-05-11 11:08:54 +00:00
|
|
|
video_rtcp_module_ = video_rtcp_module;
|
2012-06-28 07:51:16 +00:00
|
|
|
sync_.reset(new StreamSynchronization(voe_channel_id, channel_id_));
|
2011-11-30 18:31:36 +00:00
|
|
|
|
|
|
|
|
if (!voe_sync_interface) {
|
|
|
|
|
voe_channel_id_ = -1;
|
|
|
|
|
if (voe_channel_id >= 0) {
|
|
|
|
|
// Trying to set a voice channel but no interface exist.
|
|
|
|
|
return -1;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2011-11-30 18:31:36 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2012-05-11 11:08:54 +00:00
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-30 18:31:36 +00:00
|
|
|
int ViESyncModule::VoiceChannel() {
|
|
|
|
|
return voe_channel_id_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-30 18:31:36 +00:00
|
|
|
WebRtc_Word32 ViESyncModule::TimeUntilNextProcess() {
|
2012-06-28 07:51:16 +00:00
|
|
|
return static_cast<WebRtc_Word32>(kSyncInterval -
|
2011-11-30 18:31:36 +00:00
|
|
|
(TickTime::Now() - last_sync_time_).Milliseconds());
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-30 18:31:36 +00:00
|
|
|
WebRtc_Word32 ViESyncModule::Process() {
|
2011-12-22 14:17:53 +00:00
|
|
|
CriticalSectionScoped cs(data_cs_.get());
|
2011-11-30 18:31:36 +00:00
|
|
|
last_sync_time_ = TickTime::Now();
|
|
|
|
|
|
2012-05-11 11:08:54 +00:00
|
|
|
int total_video_delay_target_ms = vcm_->Delay();
|
|
|
|
|
WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, channel_id_,
|
2011-11-30 18:31:36 +00:00
|
|
|
"Video delay (JB + decoder) is %d ms",
|
|
|
|
|
total_video_delay_target_ms);
|
|
|
|
|
|
|
|
|
|
if (voe_channel_id_ == -1) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2012-05-11 11:08:54 +00:00
|
|
|
assert(video_rtcp_module_ && voe_sync_interface_);
|
2012-06-28 07:51:16 +00:00
|
|
|
assert(sync_.get());
|
2011-11-30 18:31:36 +00:00
|
|
|
|
|
|
|
|
int current_audio_delay_ms = 0;
|
|
|
|
|
if (voe_sync_interface_->GetDelayEstimate(voe_channel_id_,
|
|
|
|
|
current_audio_delay_ms) != 0) {
|
|
|
|
|
// Could not get VoE delay value, probably not a valid channel Id.
|
2012-05-11 11:08:54 +00:00
|
|
|
WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceVideo, channel_id_,
|
2011-11-30 18:31:36 +00:00
|
|
|
"%s: VE_GetDelayEstimate error for voice_channel %d",
|
|
|
|
|
__FUNCTION__, total_video_delay_target_ms, voe_channel_id_);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VoiceEngine report delay estimates even when not started, ignore if the
|
|
|
|
|
// reported value is lower than 40 ms.
|
|
|
|
|
if (current_audio_delay_ms < 40) {
|
2012-05-11 11:08:54 +00:00
|
|
|
WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, channel_id_,
|
2011-11-30 18:31:36 +00:00
|
|
|
"A/V Sync: Audio delay < 40, skipping.");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-11 11:08:54 +00:00
|
|
|
RtpRtcp* voice_rtcp_module = NULL;
|
|
|
|
|
if (0 != voe_sync_interface_->GetRtpRtcp(voe_channel_id_,
|
|
|
|
|
voice_rtcp_module)) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
assert(voice_rtcp_module);
|
|
|
|
|
|
2012-06-28 07:51:16 +00:00
|
|
|
StreamSynchronization::Measurements video;
|
|
|
|
|
if (0 != video_rtcp_module_->RemoteNTP(&video.received_ntp_secs,
|
|
|
|
|
&video.received_ntp_frac,
|
|
|
|
|
&video.rtcp_arrivaltime_secs,
|
|
|
|
|
&video.rtcp_arrivaltime_frac)) {
|
2012-05-11 11:08:54 +00:00
|
|
|
// Failed to get video NTP.
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-28 07:51:16 +00:00
|
|
|
StreamSynchronization::Measurements audio;
|
|
|
|
|
if (0 != voice_rtcp_module->RemoteNTP(&audio.received_ntp_secs,
|
|
|
|
|
&audio.received_ntp_frac,
|
|
|
|
|
&audio.rtcp_arrivaltime_secs,
|
|
|
|
|
&audio.rtcp_arrivaltime_frac)) {
|
2012-05-11 11:08:54 +00:00
|
|
|
// Failed to get audio NTP.
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2012-06-28 07:51:16 +00:00
|
|
|
int extra_audio_delay_ms = 0;
|
|
|
|
|
if (sync_->ComputeDelays(audio, current_audio_delay_ms, &extra_audio_delay_ms,
|
|
|
|
|
video, &total_video_delay_target_ms) != 0) {
|
2012-05-11 11:08:54 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2011-11-30 18:31:36 +00:00
|
|
|
// Set the extra audio delay.synchronization
|
|
|
|
|
if (voe_sync_interface_->SetMinimumPlayoutDelay(
|
2012-06-28 07:51:16 +00:00
|
|
|
voe_channel_id_, extra_audio_delay_ms) == -1) {
|
2012-05-11 11:08:54 +00:00
|
|
|
WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, channel_id_,
|
2011-11-30 18:31:36 +00:00
|
|
|
"Error setting voice delay");
|
|
|
|
|
}
|
2012-05-11 11:08:54 +00:00
|
|
|
vcm_->SetMinimumPlayoutDelay(total_video_delay_target_ms);
|
|
|
|
|
WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, channel_id_,
|
2011-11-30 18:31:36 +00:00
|
|
|
"New Video delay target is: %d", total_video_delay_target_ms);
|
|
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2011-11-30 18:31:36 +00:00
|
|
|
|
|
|
|
|
} // namespace webrtc
|