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 | |
pbos@webrtc.org | f5d4cb1 | 2013-05-17 13:44:48 +0000 | [diff] [blame] | 11 | #include "webrtc/video_engine/vie_sync_module.h" |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 12 | |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 13 | #include "webrtc/modules/rtp_rtcp/interface/rtp_receiver.h" |
pbos@webrtc.org | f5d4cb1 | 2013-05-17 13:44:48 +0000 | [diff] [blame] | 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" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 25 | enum { kSyncInterval = 1000}; |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 26 | |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 27 | int UpdateMeasurements(StreamSynchronization::Measurements* stream, |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 28 | const RtpRtcp& rtp_rtcp, const RtpReceiver& receiver) { |
stefan@webrtc.org | 48df381 | 2013-11-08 15:18:52 +0000 | [diff] [blame] | 29 | if (!receiver.Timestamp(&stream->latest_timestamp)) |
| 30 | return -1; |
| 31 | if (!receiver.LastReceivedTimeMs(&stream->latest_receive_time_ms)) |
| 32 | return -1; |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 33 | |
| 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, |
| 38 | &ntp_frac, |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 39 | NULL, |
| 40 | NULL, |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 41 | &rtp_timestamp)) { |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 42 | return -1; |
| 43 | } |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 44 | |
| 45 | bool new_rtcp_sr = false; |
wu@webrtc.org | 66773a0 | 2014-05-07 17:09:44 +0000 | [diff] [blame^] | 46 | if (!UpdateRtcpList( |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 47 | ntp_secs, ntp_frac, rtp_timestamp, &stream->rtcp, &new_rtcp_sr)) { |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 48 | return -1; |
| 49 | } |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 50 | |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | ViESyncModule::ViESyncModule(VideoCodingModule* vcm, |
| 55 | ViEChannel* vie_channel) |
mflodman@webrtc.org | d32c447 | 2011-12-22 14:17:53 +0000 | [diff] [blame] | 56 | : data_cs_(CriticalSectionWrapper::CreateCriticalSection()), |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 57 | vcm_(vcm), |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 58 | vie_channel_(vie_channel), |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 59 | video_receiver_(NULL), |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 60 | video_rtp_rtcp_(NULL), |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 61 | voe_channel_id_(-1), |
| 62 | voe_sync_interface_(NULL), |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 63 | last_sync_time_(TickTime::Now()), |
| 64 | sync_() { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 65 | } |
| 66 | |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 67 | ViESyncModule::~ViESyncModule() { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | } |
| 69 | |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 70 | int ViESyncModule::ConfigureSync(int voe_channel_id, |
| 71 | VoEVideoSync* voe_sync_interface, |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 72 | RtpRtcp* video_rtcp_module, |
| 73 | RtpReceiver* video_receiver) { |
mflodman@webrtc.org | d32c447 | 2011-12-22 14:17:53 +0000 | [diff] [blame] | 74 | CriticalSectionScoped cs(data_cs_.get()); |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 75 | voe_channel_id_ = voe_channel_id; |
| 76 | voe_sync_interface_ = voe_sync_interface; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 77 | video_receiver_ = video_receiver; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 78 | video_rtp_rtcp_ = video_rtcp_module; |
| 79 | sync_.reset(new StreamSynchronization(voe_channel_id, vie_channel_->Id())); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 80 | |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 81 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | } |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 87 | return 0; |
| 88 | } |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 89 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | } |
| 91 | |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 92 | int ViESyncModule::VoiceChannel() { |
| 93 | return voe_channel_id_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | } |
| 95 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 96 | int32_t ViESyncModule::TimeUntilNextProcess() { |
| 97 | return static_cast<int32_t>(kSyncInterval - |
| 98 | (TickTime::Now() - last_sync_time_).Milliseconds()); |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 99 | } |
| 100 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 101 | int32_t ViESyncModule::Process() { |
mflodman@webrtc.org | d32c447 | 2011-12-22 14:17:53 +0000 | [diff] [blame] | 102 | CriticalSectionScoped cs(data_cs_.get()); |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 103 | last_sync_time_ = TickTime::Now(); |
| 104 | |
hclam@chromium.org | 9b23ecb | 2013-06-14 23:30:58 +0000 | [diff] [blame] | 105 | const int current_video_delay_ms = vcm_->Delay(); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 106 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, vie_channel_->Id(), |
hclam@chromium.org | 9b23ecb | 2013-06-14 23:30:58 +0000 | [diff] [blame] | 107 | "Video delay (JB + decoder) is %d ms", current_video_delay_ms); |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 108 | |
| 109 | if (voe_channel_id_ == -1) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 110 | return 0; |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 111 | } |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 112 | assert(video_rtp_rtcp_ && voe_sync_interface_); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 113 | assert(sync_.get()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | |
pwestin@webrtc.org | 1de0135 | 2013-04-11 20:23:35 +0000 | [diff] [blame] | 115 | int audio_jitter_buffer_delay_ms = 0; |
| 116 | int playout_buffer_delay_ms = 0; |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 117 | if (voe_sync_interface_->GetDelayEstimate(voe_channel_id_, |
pwestin@webrtc.org | 1de0135 | 2013-04-11 20:23:35 +0000 | [diff] [blame] | 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. |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 122 | WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceVideo, vie_channel_->Id(), |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 123 | "%s: VE_GetDelayEstimate error for voice_channel %d", |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 124 | __FUNCTION__, voe_channel_id_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 125 | return 0; |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 126 | } |
hclam@chromium.org | 9b23ecb | 2013-06-14 23:30:58 +0000 | [diff] [blame] | 127 | const int current_audio_delay_ms = audio_jitter_buffer_delay_ms + |
| 128 | playout_buffer_delay_ms; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 129 | |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 130 | RtpRtcp* voice_rtp_rtcp = NULL; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 131 | RtpReceiver* voice_receiver = NULL; |
| 132 | if (0 != voe_sync_interface_->GetRtpRtcp(voe_channel_id_, &voice_rtp_rtcp, |
| 133 | &voice_receiver)) { |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 134 | return 0; |
| 135 | } |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 136 | assert(voice_rtp_rtcp); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 137 | assert(voice_receiver); |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 138 | |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 139 | if (UpdateMeasurements(&video_measurement_, *video_rtp_rtcp_, |
| 140 | *video_receiver_) != 0) { |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 141 | return 0; |
| 142 | } |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 143 | |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 144 | if (UpdateMeasurements(&audio_measurement_, *voice_rtp_rtcp, |
| 145 | *voice_receiver) != 0) { |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 146 | return 0; |
| 147 | } |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 148 | |
| 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)) { |
| 153 | return 0; |
| 154 | } |
| 155 | |
hclam@chromium.org | 9b23ecb | 2013-06-14 23:30:58 +0000 | [diff] [blame] | 156 | TRACE_COUNTER1("webrtc", "SyncCurrentVideoDelay", current_video_delay_ms); |
| 157 | TRACE_COUNTER1("webrtc", "SyncCurrentAudioDelay", current_audio_delay_ms); |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 +0000 | [diff] [blame] | 158 | TRACE_COUNTER1("webrtc", "SyncRelativeDelay", relative_delay_ms); |
hclam@chromium.org | 9b23ecb | 2013-06-14 23:30:58 +0000 | [diff] [blame] | 159 | int target_audio_delay_ms = 0; |
hclam@chromium.org | 7262ad1 | 2013-06-15 06:51:27 +0000 | [diff] [blame] | 160 | int target_video_delay_ms = current_video_delay_ms; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 161 | // Calculate the necessary extra audio delay and desired total video |
| 162 | // delay to get the streams in sync. |
stefan@webrtc.org | 8d18526 | 2012-11-12 18:51:52 +0000 | [diff] [blame] | 163 | if (!sync_->ComputeDelays(relative_delay_ms, |
hclam@chromium.org | 9b23ecb | 2013-06-14 23:30:58 +0000 | [diff] [blame] | 164 | current_audio_delay_ms, |
| 165 | &target_audio_delay_ms, |
| 166 | &target_video_delay_ms)) { |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 167 | return 0; |
| 168 | } |
edjee@google.com | 79b0289 | 2013-04-04 19:43:34 +0000 | [diff] [blame] | 169 | |
hclam@chromium.org | 9b23ecb | 2013-06-14 23:30:58 +0000 | [diff] [blame] | 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, |
| 173 | relative_delay_ms, |
| 174 | target_audio_delay_ms, target_video_delay_ms); |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 175 | if (voe_sync_interface_->SetMinimumPlayoutDelay( |
hclam@chromium.org | 9b23ecb | 2013-06-14 23:30:58 +0000 | [diff] [blame] | 176 | voe_channel_id_, target_audio_delay_ms) == -1) { |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 177 | WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, vie_channel_->Id(), |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 178 | "Error setting voice delay"); |
| 179 | } |
hclam@chromium.org | 9b23ecb | 2013-06-14 23:30:58 +0000 | [diff] [blame] | 180 | vcm_->SetMinimumPlayoutDelay(target_video_delay_ms); |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 181 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 182 | } |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 183 | |
mikhal@webrtc.org | efe4edb | 2013-03-06 23:29:33 +0000 | [diff] [blame] | 184 | int ViESyncModule::SetTargetBufferingDelay(int target_delay_ms) { |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 185 | CriticalSectionScoped cs(data_cs_.get()); |
mikhal@webrtc.org | efe4edb | 2013-03-06 23:29:33 +0000 | [diff] [blame] | 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."); |
| 189 | return -1; |
| 190 | } |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 191 | sync_->SetTargetBufferingDelay(target_delay_ms); |
| 192 | // Setting initial playout delay to voice engine (video engine is updated via |
| 193 | // the VCM interface). |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 194 | voe_sync_interface_->SetInitialPlayoutDelay(voe_channel_id_, |
| 195 | target_delay_ms); |
mikhal@webrtc.org | efe4edb | 2013-03-06 23:29:33 +0000 | [diff] [blame] | 196 | return 0; |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 197 | } |
| 198 | |
mflodman@webrtc.org | 511f82e | 2011-11-30 18:31:36 +0000 | [diff] [blame] | 199 | } // namespace webrtc |