Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/video/vie_sync_module.h" | 11 #include "webrtc/video/vie_sync_module.h" |
| 12 | 12 |
| 13 #include <algorithm> | |
| 14 | |
| 13 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
| 14 #include "webrtc/base/logging.h" | 16 #include "webrtc/base/logging.h" |
| 15 #include "webrtc/base/trace_event.h" | 17 #include "webrtc/base/trace_event.h" |
| 16 #include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h" | 18 #include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h" |
| 17 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" | 19 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" |
| 18 #include "webrtc/modules/video_coding/include/video_coding.h" | 20 #include "webrtc/modules/video_coding/include/video_coding.h" |
| 21 #include "webrtc/system_wrappers/include/clock.h" | |
| 19 #include "webrtc/video/stream_synchronization.h" | 22 #include "webrtc/video/stream_synchronization.h" |
| 23 #include "webrtc/video_frame.h" | |
| 20 #include "webrtc/voice_engine/include/voe_video_sync.h" | 24 #include "webrtc/voice_engine/include/voe_video_sync.h" |
| 21 | 25 |
| 22 namespace webrtc { | 26 namespace webrtc { |
| 27 namespace { | |
| 28 bool RtpTimestampToNtp(uint32_t timestamp, | |
| 29 const RtcpList& rtcp_list, | |
| 30 int64_t* timestamp_ms) { | |
| 31 if (rtcp_list.size() != 2) | |
| 32 return false; | |
|
stefan-webrtc
2016/03/04 14:32:17
Maybe we should simply move this check into RtpToN
åsapersson
2016/03/09 15:44:35
Done.
| |
| 33 | |
| 34 if (!RtpToNtpMs(timestamp, rtcp_list, timestamp_ms)) | |
| 35 return false; | |
| 36 | |
| 37 return true; | |
| 38 } | |
| 23 | 39 |
| 24 int UpdateMeasurements(StreamSynchronization::Measurements* stream, | 40 int UpdateMeasurements(StreamSynchronization::Measurements* stream, |
| 25 const RtpRtcp& rtp_rtcp, const RtpReceiver& receiver) { | 41 const RtpRtcp& rtp_rtcp, const RtpReceiver& receiver) { |
| 26 if (!receiver.Timestamp(&stream->latest_timestamp)) | 42 if (!receiver.Timestamp(&stream->latest_timestamp)) |
| 27 return -1; | 43 return -1; |
| 28 if (!receiver.LastReceivedTimeMs(&stream->latest_receive_time_ms)) | 44 if (!receiver.LastReceivedTimeMs(&stream->latest_receive_time_ms)) |
| 29 return -1; | 45 return -1; |
| 30 | 46 |
| 31 uint32_t ntp_secs = 0; | 47 uint32_t ntp_secs = 0; |
| 32 uint32_t ntp_frac = 0; | 48 uint32_t ntp_frac = 0; |
| 33 uint32_t rtp_timestamp = 0; | 49 uint32_t rtp_timestamp = 0; |
| 34 if (0 != rtp_rtcp.RemoteNTP(&ntp_secs, | 50 if (0 != rtp_rtcp.RemoteNTP(&ntp_secs, |
| 35 &ntp_frac, | 51 &ntp_frac, |
| 36 NULL, | 52 NULL, |
| 37 NULL, | 53 NULL, |
| 38 &rtp_timestamp)) { | 54 &rtp_timestamp)) { |
| 39 return -1; | 55 return -1; |
| 40 } | 56 } |
| 41 | 57 |
| 42 bool new_rtcp_sr = false; | 58 bool new_rtcp_sr = false; |
| 43 if (!UpdateRtcpList( | 59 if (!UpdateRtcpList( |
| 44 ntp_secs, ntp_frac, rtp_timestamp, &stream->rtcp, &new_rtcp_sr)) { | 60 ntp_secs, ntp_frac, rtp_timestamp, &stream->rtcp, &new_rtcp_sr)) { |
| 45 return -1; | 61 return -1; |
| 46 } | 62 } |
| 47 | 63 |
| 48 return 0; | 64 return 0; |
| 49 } | 65 } |
| 66 } // namespace | |
| 50 | 67 |
| 51 ViESyncModule::ViESyncModule(VideoCodingModule* vcm) | 68 ViESyncModule::ViESyncModule(VideoCodingModule* vcm) |
| 52 : vcm_(vcm), | 69 : vcm_(vcm), |
| 70 clock_(Clock::GetRealTimeClock()), | |
| 53 video_receiver_(NULL), | 71 video_receiver_(NULL), |
| 54 video_rtp_rtcp_(NULL), | 72 video_rtp_rtcp_(NULL), |
| 55 voe_channel_id_(-1), | 73 voe_channel_id_(-1), |
| 56 voe_sync_interface_(NULL), | 74 voe_sync_interface_(NULL), |
| 57 last_sync_time_(TickTime::Now()), | 75 last_sync_time_(TickTime::Now()), |
| 58 sync_() { | 76 sync_() {} |
| 59 } | |
| 60 | 77 |
| 61 ViESyncModule::~ViESyncModule() { | 78 ViESyncModule::~ViESyncModule() { |
| 62 } | 79 } |
| 63 | 80 |
| 64 void ViESyncModule::ConfigureSync(int voe_channel_id, | 81 void ViESyncModule::ConfigureSync(int voe_channel_id, |
| 65 VoEVideoSync* voe_sync_interface, | 82 VoEVideoSync* voe_sync_interface, |
| 66 RtpRtcp* video_rtcp_module, | 83 RtpRtcp* video_rtcp_module, |
| 67 RtpReceiver* video_receiver) { | 84 RtpReceiver* video_receiver) { |
| 68 if (voe_channel_id != -1) | 85 if (voe_channel_id != -1) |
| 69 RTC_DCHECK(voe_sync_interface); | 86 RTC_DCHECK(voe_sync_interface); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 return; | 146 return; |
| 130 } | 147 } |
| 131 | 148 |
| 132 int relative_delay_ms; | 149 int relative_delay_ms; |
| 133 // Calculate how much later or earlier the audio stream is compared to video. | 150 // Calculate how much later or earlier the audio stream is compared to video. |
| 134 if (!sync_->ComputeRelativeDelay(audio_measurement_, video_measurement_, | 151 if (!sync_->ComputeRelativeDelay(audio_measurement_, video_measurement_, |
| 135 &relative_delay_ms)) { | 152 &relative_delay_ms)) { |
| 136 return; | 153 return; |
| 137 } | 154 } |
| 138 | 155 |
| 139 TRACE_COUNTER1("webrtc", "SyncCurrentVideoDelay", current_video_delay_ms); | 156 TRACE_COUNTER1("webrtc", "SyncCurrentVideoDelay", current_video_delay_ms); |
|
pbos-webrtc
2016/03/04 14:34:27
Can we store and use something based on these thre
stefan-webrtc
2016/03/04 14:38:55
This is only called periodically, once per second.
åsapersson
2016/03/09 15:44:35
Acknowledged.
| |
| 140 TRACE_COUNTER1("webrtc", "SyncCurrentAudioDelay", current_audio_delay_ms); | 157 TRACE_COUNTER1("webrtc", "SyncCurrentAudioDelay", current_audio_delay_ms); |
| 141 TRACE_COUNTER1("webrtc", "SyncRelativeDelay", relative_delay_ms); | 158 TRACE_COUNTER1("webrtc", "SyncRelativeDelay", relative_delay_ms); |
| 142 int target_audio_delay_ms = 0; | 159 int target_audio_delay_ms = 0; |
| 143 int target_video_delay_ms = current_video_delay_ms; | 160 int target_video_delay_ms = current_video_delay_ms; |
| 144 // Calculate the necessary extra audio delay and desired total video | 161 // Calculate the necessary extra audio delay and desired total video |
| 145 // delay to get the streams in sync. | 162 // delay to get the streams in sync. |
| 146 if (!sync_->ComputeDelays(relative_delay_ms, | 163 if (!sync_->ComputeDelays(relative_delay_ms, |
| 147 current_audio_delay_ms, | 164 current_audio_delay_ms, |
| 148 &target_audio_delay_ms, | 165 &target_audio_delay_ms, |
| 149 &target_video_delay_ms)) { | 166 &target_video_delay_ms)) { |
| 150 return; | 167 return; |
| 151 } | 168 } |
| 152 | 169 |
| 153 if (voe_sync_interface_->SetMinimumPlayoutDelay( | 170 if (voe_sync_interface_->SetMinimumPlayoutDelay( |
| 154 voe_channel_id_, target_audio_delay_ms) == -1) { | 171 voe_channel_id_, target_audio_delay_ms) == -1) { |
| 155 LOG(LS_ERROR) << "Error setting voice delay."; | 172 LOG(LS_ERROR) << "Error setting voice delay."; |
| 156 } | 173 } |
| 157 vcm_->SetMinimumPlayoutDelay(target_video_delay_ms); | 174 vcm_->SetMinimumPlayoutDelay(target_video_delay_ms); |
| 158 } | 175 } |
| 159 | 176 |
| 177 int64_t ViESyncModule::GetStreamSyncOffsetInMs(const VideoFrame& frame) { | |
| 178 rtc::CritScope lock(&data_cs_); | |
| 179 if (voe_channel_id_ == -1) | |
| 180 return -1; | |
| 181 | |
| 182 uint32_t playout_timestamp = 0; | |
| 183 if (voe_sync_interface_->GetPlayoutTimestamp(voe_channel_id_, | |
| 184 playout_timestamp) != 0) { | |
| 185 return -1; | |
| 186 } | |
| 187 int64_t latest_audio_ntp; | |
| 188 if (!RtpTimestampToNtp(playout_timestamp, audio_measurement_.rtcp, | |
| 189 &latest_audio_ntp)) { | |
| 190 return -1; | |
| 191 } | |
| 192 int64_t latest_video_ntp; | |
| 193 if (!RtpTimestampToNtp(frame.timestamp(), video_measurement_.rtcp, | |
| 194 &latest_video_ntp)) { | |
| 195 return -1; | |
| 196 } | |
| 197 int64_t time_to_render_ms = | |
| 198 frame.render_time_ms() - clock_->TimeInMilliseconds(); | |
| 199 if (time_to_render_ms > 0) | |
| 200 latest_video_ntp += time_to_render_ms; | |
| 201 | |
| 202 return std::abs(latest_audio_ntp - latest_video_ntp); | |
| 203 } | |
| 204 | |
| 160 } // namespace webrtc | 205 } // namespace webrtc |
| OLD | NEW |