niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
leozwang@webrtc.org | 39e9659 | 2012-03-01 18:22:48 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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.org | ab2610f | 2012-06-29 10:05:28 +0000 | [diff] [blame] | 11 | #include "video_engine/vie_sync_module.h" |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 12 | |
mflodman@webrtc.org | ab2610f | 2012-06-29 10:05:28 +0000 | [diff] [blame] | 13 | #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.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 17 | #include "video_engine/stream_synchronization.h" |
andrew@webrtc.org | 6f8db36 | 2012-07-27 21:49:28 +0000 | [diff] [blame^] | 18 | #include "voice_engine/include/voe_video_sync.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 22 | enum { kSyncInterval = 1000}; |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 23 | |
| 24 | ViESyncModule::ViESyncModule(const int32_t channel_id, VideoCodingModule* vcm) |
mflodman@webrtc.org | d32c447 | 2011-12-22 14:17:53 +0000 | [diff] [blame] | 25 | : data_cs_(CriticalSectionWrapper::CreateCriticalSection()), |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 26 | channel_id_(channel_id), |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 27 | vcm_(vcm), |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 28 | video_rtcp_module_(NULL), |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 29 | voe_channel_id_(-1), |
| 30 | voe_sync_interface_(NULL), |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 31 | last_sync_time_(TickTime::Now()), |
| 32 | sync_() { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | } |
| 34 | |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 35 | ViESyncModule::~ViESyncModule() { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 36 | } |
| 37 | |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 38 | int ViESyncModule::ConfigureSync(int voe_channel_id, |
| 39 | VoEVideoSync* voe_sync_interface, |
| 40 | RtpRtcp* video_rtcp_module) { |
mflodman@webrtc.org | d32c447 | 2011-12-22 14:17:53 +0000 | [diff] [blame] | 41 | CriticalSectionScoped cs(data_cs_.get()); |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 42 | voe_channel_id_ = voe_channel_id; |
| 43 | voe_sync_interface_ = voe_sync_interface; |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 44 | video_rtcp_module_ = video_rtcp_module; |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 45 | sync_.reset(new StreamSynchronization(voe_channel_id, channel_id_)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 47 | if (!voe_sync_interface) { |
| 48 | voe_channel_id_ = -1; |
| 49 | if (voe_channel_id >= 0) { |
| 50 | // Trying to set a voice channel but no interface exist. |
| 51 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 52 | } |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 53 | return 0; |
| 54 | } |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 55 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 56 | } |
| 57 | |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 58 | int ViESyncModule::VoiceChannel() { |
| 59 | return voe_channel_id_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 60 | } |
| 61 | |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 62 | WebRtc_Word32 ViESyncModule::TimeUntilNextProcess() { |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 63 | return static_cast<WebRtc_Word32>(kSyncInterval - |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 64 | (TickTime::Now() - last_sync_time_).Milliseconds()); |
| 65 | } |
| 66 | |
| 67 | WebRtc_Word32 ViESyncModule::Process() { |
mflodman@webrtc.org | d32c447 | 2011-12-22 14:17:53 +0000 | [diff] [blame] | 68 | CriticalSectionScoped cs(data_cs_.get()); |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 69 | last_sync_time_ = TickTime::Now(); |
| 70 | |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 71 | int total_video_delay_target_ms = vcm_->Delay(); |
| 72 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, channel_id_, |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 73 | "Video delay (JB + decoder) is %d ms", |
| 74 | total_video_delay_target_ms); |
| 75 | |
| 76 | if (voe_channel_id_ == -1) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 77 | return 0; |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 78 | } |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 79 | assert(video_rtcp_module_ && voe_sync_interface_); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 80 | assert(sync_.get()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 82 | int current_audio_delay_ms = 0; |
| 83 | if (voe_sync_interface_->GetDelayEstimate(voe_channel_id_, |
| 84 | current_audio_delay_ms) != 0) { |
| 85 | // Could not get VoE delay value, probably not a valid channel Id. |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 86 | WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceVideo, channel_id_, |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 87 | "%s: VE_GetDelayEstimate error for voice_channel %d", |
| 88 | __FUNCTION__, total_video_delay_target_ms, voe_channel_id_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 89 | return 0; |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 90 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 91 | |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 92 | // VoiceEngine report delay estimates even when not started, ignore if the |
| 93 | // reported value is lower than 40 ms. |
| 94 | if (current_audio_delay_ms < 40) { |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 95 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, channel_id_, |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 96 | "A/V Sync: Audio delay < 40, skipping."); |
| 97 | return 0; |
| 98 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 99 | |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 100 | RtpRtcp* voice_rtcp_module = NULL; |
| 101 | if (0 != voe_sync_interface_->GetRtpRtcp(voe_channel_id_, |
| 102 | voice_rtcp_module)) { |
| 103 | return 0; |
| 104 | } |
| 105 | assert(voice_rtcp_module); |
| 106 | |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 107 | StreamSynchronization::Measurements video; |
| 108 | if (0 != video_rtcp_module_->RemoteNTP(&video.received_ntp_secs, |
| 109 | &video.received_ntp_frac, |
| 110 | &video.rtcp_arrivaltime_secs, |
| 111 | &video.rtcp_arrivaltime_frac)) { |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 112 | // Failed to get video NTP. |
| 113 | return 0; |
| 114 | } |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 115 | |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 116 | StreamSynchronization::Measurements audio; |
| 117 | if (0 != voice_rtcp_module->RemoteNTP(&audio.received_ntp_secs, |
| 118 | &audio.received_ntp_frac, |
| 119 | &audio.rtcp_arrivaltime_secs, |
| 120 | &audio.rtcp_arrivaltime_frac)) { |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 121 | // Failed to get audio NTP. |
| 122 | return 0; |
| 123 | } |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 124 | int extra_audio_delay_ms = 0; |
| 125 | if (sync_->ComputeDelays(audio, current_audio_delay_ms, &extra_audio_delay_ms, |
| 126 | video, &total_video_delay_target_ms) != 0) { |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 127 | return 0; |
| 128 | } |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 129 | // Set the extra audio delay.synchronization |
| 130 | if (voe_sync_interface_->SetMinimumPlayoutDelay( |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 131 | voe_channel_id_, extra_audio_delay_ms) == -1) { |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 132 | WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, channel_id_, |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 133 | "Error setting voice delay"); |
| 134 | } |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 135 | vcm_->SetMinimumPlayoutDelay(total_video_delay_target_ms); |
| 136 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, channel_id_, |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 137 | "New Video delay target is: %d", total_video_delay_target_ms); |
| 138 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 139 | } |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 140 | |
| 141 | } // namespace webrtc |