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/rtp_streams_synchronizer.h" | 11 #include "webrtc/video/rtp_streams_synchronizer.h" |
12 | 12 |
13 #include "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" |
14 #include "webrtc/base/logging.h" | 14 #include "webrtc/base/logging.h" |
15 #include "webrtc/base/timeutils.h" | 15 #include "webrtc/base/timeutils.h" |
16 #include "webrtc/base/trace_event.h" | 16 #include "webrtc/base/trace_event.h" |
17 #include "webrtc/call/syncable.h" | |
17 #include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h" | 18 #include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h" |
18 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" | 19 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" |
19 #include "webrtc/modules/video_coding/video_coding_impl.h" | 20 #include "webrtc/modules/video_coding/video_coding_impl.h" |
20 #include "webrtc/system_wrappers/include/clock.h" | 21 #include "webrtc/system_wrappers/include/clock.h" |
21 #include "webrtc/video/stream_synchronization.h" | 22 #include "webrtc/video/stream_synchronization.h" |
22 #include "webrtc/video_frame.h" | 23 #include "webrtc/video_frame.h" |
23 #include "webrtc/voice_engine/include/voe_video_sync.h" | |
24 | 24 |
25 namespace webrtc { | 25 namespace webrtc { |
26 namespace { | 26 namespace { |
27 bool UpdateMeasurements(StreamSynchronization::Measurements* stream, | 27 bool UpdateMeasurements(StreamSynchronization::Measurements* stream, |
28 RtpRtcp* rtp_rtcp, | 28 RtpRtcp* rtp_rtcp, |
29 RtpReceiver* receiver) { | 29 RtpReceiver* receiver) { |
30 if (!receiver->Timestamp(&stream->latest_timestamp)) | 30 if (!receiver->Timestamp(&stream->latest_timestamp)) |
31 return false; | 31 return false; |
32 if (!receiver->LastReceivedTimeMs(&stream->latest_receive_time_ms)) | 32 if (!receiver->LastReceivedTimeMs(&stream->latest_receive_time_ms)) |
33 return false; | 33 return false; |
(...skipping 16 matching lines...) Expand all Loading... | |
50 } | 50 } |
51 } // namespace | 51 } // namespace |
52 | 52 |
53 RtpStreamsSynchronizer::RtpStreamsSynchronizer( | 53 RtpStreamsSynchronizer::RtpStreamsSynchronizer( |
54 vcm::VideoReceiver* video_receiver, | 54 vcm::VideoReceiver* video_receiver, |
55 RtpStreamReceiver* rtp_stream_receiver) | 55 RtpStreamReceiver* rtp_stream_receiver) |
56 : clock_(Clock::GetRealTimeClock()), | 56 : clock_(Clock::GetRealTimeClock()), |
57 video_receiver_(video_receiver), | 57 video_receiver_(video_receiver), |
58 video_rtp_receiver_(rtp_stream_receiver->GetRtpReceiver()), | 58 video_rtp_receiver_(rtp_stream_receiver->GetRtpReceiver()), |
59 video_rtp_rtcp_(rtp_stream_receiver->rtp_rtcp()), | 59 video_rtp_rtcp_(rtp_stream_receiver->rtp_rtcp()), |
60 voe_channel_id_(-1), | 60 syncable_(nullptr), |
61 voe_sync_interface_(nullptr), | |
62 audio_rtp_receiver_(nullptr), | 61 audio_rtp_receiver_(nullptr), |
63 audio_rtp_rtcp_(nullptr), | 62 audio_rtp_rtcp_(nullptr), |
64 sync_(), | 63 sync_(), |
65 last_sync_time_(rtc::TimeNanos()) { | 64 last_sync_time_(rtc::TimeNanos()) { |
66 process_thread_checker_.DetachFromThread(); | 65 process_thread_checker_.DetachFromThread(); |
67 } | 66 } |
68 | 67 |
69 void RtpStreamsSynchronizer::ConfigureSync(int voe_channel_id, | 68 void RtpStreamsSynchronizer::ConfigureSync(Syncable* syncable) { |
70 VoEVideoSync* voe_sync_interface) { | |
71 if (voe_channel_id != -1) | |
72 RTC_DCHECK(voe_sync_interface); | |
73 | |
74 rtc::CritScope lock(&crit_); | 69 rtc::CritScope lock(&crit_); |
75 if (voe_channel_id_ == voe_channel_id && | 70 if (syncable == syncable_) { |
76 voe_sync_interface_ == voe_sync_interface) { | |
77 // This prevents expensive no-ops. | 71 // This prevents expensive no-ops. |
78 return; | 72 return; |
79 } | 73 } |
80 voe_channel_id_ = voe_channel_id; | 74 syncable_ = syncable; |
81 voe_sync_interface_ = voe_sync_interface; | |
82 | 75 |
83 audio_rtp_rtcp_ = nullptr; | 76 audio_rtp_rtcp_ = nullptr; |
84 audio_rtp_receiver_ = nullptr; | 77 audio_rtp_receiver_ = nullptr; |
85 sync_.reset(nullptr); | 78 sync_.reset(nullptr); |
86 | 79 |
87 if (voe_channel_id_ != -1) { | 80 if (syncable_) { |
88 voe_sync_interface_->GetRtpRtcp(voe_channel_id_, &audio_rtp_rtcp_, | 81 syncable_->GetRtpRtcp(&audio_rtp_rtcp_, &audio_rtp_receiver_); |
stefan-webrtc
2017/01/13 16:21:04
Maybe we take the opportunity to change the interf
the sun
2017/01/19 11:45:29
Yes, I agree it makes sense to not expose the RtpR
stefan-webrtc
2017/01/19 11:59:59
Yes, I think it would make a lot of sense to use t
| |
89 &audio_rtp_receiver_); | |
90 RTC_DCHECK(audio_rtp_rtcp_); | 82 RTC_DCHECK(audio_rtp_rtcp_); |
91 RTC_DCHECK(audio_rtp_receiver_); | 83 RTC_DCHECK(audio_rtp_receiver_); |
92 sync_.reset(new StreamSynchronization(video_rtp_rtcp_->SSRC(), | 84 sync_.reset(new StreamSynchronization(video_rtp_rtcp_->SSRC(), |
93 voe_channel_id_)); | 85 syncable_->id())); |
94 } | 86 } |
95 } | 87 } |
96 | 88 |
97 int64_t RtpStreamsSynchronizer::TimeUntilNextProcess() { | 89 int64_t RtpStreamsSynchronizer::TimeUntilNextProcess() { |
98 RTC_DCHECK_RUN_ON(&process_thread_checker_); | 90 RTC_DCHECK_RUN_ON(&process_thread_checker_); |
99 const int64_t kSyncIntervalMs = 1000; | 91 const int64_t kSyncIntervalMs = 1000; |
100 return kSyncIntervalMs - | 92 return kSyncIntervalMs - |
101 (rtc::TimeNanos() - last_sync_time_) / rtc::kNumNanosecsPerMillisec; | 93 (rtc::TimeNanos() - last_sync_time_) / rtc::kNumNanosecsPerMillisec; |
102 } | 94 } |
103 | 95 |
104 void RtpStreamsSynchronizer::Process() { | 96 void RtpStreamsSynchronizer::Process() { |
105 RTC_DCHECK_RUN_ON(&process_thread_checker_); | 97 RTC_DCHECK_RUN_ON(&process_thread_checker_); |
106 | 98 |
107 const int current_video_delay_ms = video_receiver_->Delay(); | 99 const int current_video_delay_ms = video_receiver_->Delay(); |
108 last_sync_time_ = rtc::TimeNanos(); | 100 last_sync_time_ = rtc::TimeNanos(); |
109 | 101 |
110 rtc::CritScope lock(&crit_); | 102 rtc::CritScope lock(&crit_); |
111 if (voe_channel_id_ == -1) { | 103 if (!syncable_) { |
112 return; | 104 return; |
113 } | 105 } |
114 RTC_DCHECK(voe_sync_interface_); | |
115 RTC_DCHECK(sync_.get()); | 106 RTC_DCHECK(sync_.get()); |
116 | 107 |
117 int audio_jitter_buffer_delay_ms = 0; | 108 int audio_jitter_buffer_delay_ms = 0; |
118 int playout_buffer_delay_ms = 0; | 109 int playout_buffer_delay_ms = 0; |
119 if (voe_sync_interface_->GetDelayEstimate(voe_channel_id_, | 110 syncable_->GetDelayEstimate(&audio_jitter_buffer_delay_ms, |
120 &audio_jitter_buffer_delay_ms, | 111 &playout_buffer_delay_ms); |
121 &playout_buffer_delay_ms) != 0) { | |
122 return; | |
123 } | |
124 const int current_audio_delay_ms = audio_jitter_buffer_delay_ms + | 112 const int current_audio_delay_ms = audio_jitter_buffer_delay_ms + |
125 playout_buffer_delay_ms; | 113 playout_buffer_delay_ms; |
126 | 114 |
127 int64_t last_video_receive_ms = video_measurement_.latest_receive_time_ms; | 115 int64_t last_video_receive_ms = video_measurement_.latest_receive_time_ms; |
128 if (!UpdateMeasurements(&video_measurement_, video_rtp_rtcp_, | 116 if (!UpdateMeasurements(&video_measurement_, video_rtp_rtcp_, |
129 video_rtp_receiver_)) { | 117 video_rtp_receiver_)) { |
130 return; | 118 return; |
131 } | 119 } |
132 | 120 |
133 if (!UpdateMeasurements(&audio_measurement_, audio_rtp_rtcp_, | 121 if (!UpdateMeasurements(&audio_measurement_, audio_rtp_rtcp_, |
(...skipping 20 matching lines...) Expand all Loading... | |
154 int target_video_delay_ms = current_video_delay_ms; | 142 int target_video_delay_ms = current_video_delay_ms; |
155 // Calculate the necessary extra audio delay and desired total video | 143 // Calculate the necessary extra audio delay and desired total video |
156 // delay to get the streams in sync. | 144 // delay to get the streams in sync. |
157 if (!sync_->ComputeDelays(relative_delay_ms, | 145 if (!sync_->ComputeDelays(relative_delay_ms, |
158 current_audio_delay_ms, | 146 current_audio_delay_ms, |
159 &target_audio_delay_ms, | 147 &target_audio_delay_ms, |
160 &target_video_delay_ms)) { | 148 &target_video_delay_ms)) { |
161 return; | 149 return; |
162 } | 150 } |
163 | 151 |
164 if (voe_sync_interface_->SetMinimumPlayoutDelay( | 152 syncable_->SetMinimumPlayoutDelay(target_audio_delay_ms); |
165 voe_channel_id_, target_audio_delay_ms) == -1) { | |
166 LOG(LS_ERROR) << "Error setting voice delay."; | |
167 } | |
168 video_receiver_->SetMinimumPlayoutDelay(target_video_delay_ms); | 153 video_receiver_->SetMinimumPlayoutDelay(target_video_delay_ms); |
169 } | 154 } |
170 | 155 |
171 bool RtpStreamsSynchronizer::GetStreamSyncOffsetInMs( | 156 bool RtpStreamsSynchronizer::GetStreamSyncOffsetInMs( |
172 const VideoFrame& frame, | 157 const VideoFrame& frame, |
173 int64_t* stream_offset_ms, | 158 int64_t* stream_offset_ms, |
174 double* estimated_freq_khz) const { | 159 double* estimated_freq_khz) const { |
175 rtc::CritScope lock(&crit_); | 160 rtc::CritScope lock(&crit_); |
176 if (voe_channel_id_ == -1) | 161 if (!syncable_) { |
177 return false; | |
178 | |
179 uint32_t playout_timestamp = 0; | |
180 if (voe_sync_interface_->GetPlayoutTimestamp(voe_channel_id_, | |
181 playout_timestamp) != 0) { | |
182 return false; | 162 return false; |
183 } | 163 } |
184 | 164 |
165 uint32_t playout_timestamp = syncable_->GetPlayoutTimestamp(); | |
166 | |
185 int64_t latest_audio_ntp; | 167 int64_t latest_audio_ntp; |
186 if (!audio_measurement_.rtp_to_ntp.Estimate(playout_timestamp, | 168 if (!audio_measurement_.rtp_to_ntp.Estimate(playout_timestamp, |
187 &latest_audio_ntp)) { | 169 &latest_audio_ntp)) { |
188 return false; | 170 return false; |
189 } | 171 } |
190 | 172 |
191 int64_t latest_video_ntp; | 173 int64_t latest_video_ntp; |
192 if (!video_measurement_.rtp_to_ntp.Estimate(frame.timestamp(), | 174 if (!video_measurement_.rtp_to_ntp.Estimate(frame.timestamp(), |
193 &latest_video_ntp)) { | 175 &latest_video_ntp)) { |
194 return false; | 176 return false; |
195 } | 177 } |
196 | 178 |
197 int64_t time_to_render_ms = | 179 int64_t time_to_render_ms = |
198 frame.render_time_ms() - clock_->TimeInMilliseconds(); | 180 frame.render_time_ms() - clock_->TimeInMilliseconds(); |
199 if (time_to_render_ms > 0) | 181 if (time_to_render_ms > 0) |
200 latest_video_ntp += time_to_render_ms; | 182 latest_video_ntp += time_to_render_ms; |
201 | 183 |
202 *stream_offset_ms = latest_audio_ntp - latest_video_ntp; | 184 *stream_offset_ms = latest_audio_ntp - latest_video_ntp; |
203 *estimated_freq_khz = video_measurement_.rtp_to_ntp.params().frequency_khz; | 185 *estimated_freq_khz = video_measurement_.rtp_to_ntp.params().frequency_khz; |
204 return true; | 186 return true; |
205 } | 187 } |
206 | 188 |
207 } // namespace webrtc | 189 } // namespace webrtc |
OLD | NEW |