| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | |
| 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 | |
| 11 #include "webrtc/video_engine/vie_channel_group.h" | |
| 12 | |
| 13 #include "webrtc/base/checks.h" | |
| 14 #include "webrtc/base/thread_annotations.h" | |
| 15 #include "webrtc/common.h" | |
| 16 #include "webrtc/modules/pacing/include/paced_sender.h" | |
| 17 #include "webrtc/modules/pacing/include/packet_router.h" | |
| 18 #include "webrtc/modules/remote_bitrate_estimator/include/send_time_history.h" | |
| 19 #include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_s
end_time.h" | |
| 20 #include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_singl
e_stream.h" | |
| 21 #include "webrtc/modules/remote_bitrate_estimator/remote_estimator_proxy.h" | |
| 22 #include "webrtc/modules/remote_bitrate_estimator/transport_feedback_adapter.h" | |
| 23 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h" | |
| 24 #include "webrtc/modules/utility/interface/process_thread.h" | |
| 25 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" | |
| 26 #include "webrtc/system_wrappers/interface/logging.h" | |
| 27 #include "webrtc/video_engine/call_stats.h" | |
| 28 #include "webrtc/video_engine/payload_router.h" | |
| 29 #include "webrtc/video_engine/vie_encoder.h" | |
| 30 #include "webrtc/video_engine/vie_remb.h" | |
| 31 #include "webrtc/voice_engine/include/voe_video_sync.h" | |
| 32 | |
| 33 namespace webrtc { | |
| 34 namespace { | |
| 35 | |
| 36 static const uint32_t kTimeOffsetSwitchThreshold = 30; | |
| 37 | |
| 38 class WrappingBitrateEstimator : public RemoteBitrateEstimator { | |
| 39 public: | |
| 40 WrappingBitrateEstimator(RemoteBitrateObserver* observer, Clock* clock) | |
| 41 : observer_(observer), | |
| 42 clock_(clock), | |
| 43 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), | |
| 44 rbe_(new RemoteBitrateEstimatorSingleStream(observer_, clock_)), | |
| 45 using_absolute_send_time_(false), | |
| 46 packets_since_absolute_send_time_(0), | |
| 47 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps) {} | |
| 48 | |
| 49 virtual ~WrappingBitrateEstimator() {} | |
| 50 | |
| 51 void IncomingPacket(int64_t arrival_time_ms, | |
| 52 size_t payload_size, | |
| 53 const RTPHeader& header, | |
| 54 bool was_paced) override { | |
| 55 CriticalSectionScoped cs(crit_sect_.get()); | |
| 56 PickEstimatorFromHeader(header); | |
| 57 rbe_->IncomingPacket(arrival_time_ms, payload_size, header, was_paced); | |
| 58 } | |
| 59 | |
| 60 int32_t Process() override { | |
| 61 CriticalSectionScoped cs(crit_sect_.get()); | |
| 62 return rbe_->Process(); | |
| 63 } | |
| 64 | |
| 65 int64_t TimeUntilNextProcess() override { | |
| 66 CriticalSectionScoped cs(crit_sect_.get()); | |
| 67 return rbe_->TimeUntilNextProcess(); | |
| 68 } | |
| 69 | |
| 70 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override { | |
| 71 CriticalSectionScoped cs(crit_sect_.get()); | |
| 72 rbe_->OnRttUpdate(avg_rtt_ms, max_rtt_ms); | |
| 73 } | |
| 74 | |
| 75 void RemoveStream(unsigned int ssrc) override { | |
| 76 CriticalSectionScoped cs(crit_sect_.get()); | |
| 77 rbe_->RemoveStream(ssrc); | |
| 78 } | |
| 79 | |
| 80 bool LatestEstimate(std::vector<unsigned int>* ssrcs, | |
| 81 unsigned int* bitrate_bps) const override { | |
| 82 CriticalSectionScoped cs(crit_sect_.get()); | |
| 83 return rbe_->LatestEstimate(ssrcs, bitrate_bps); | |
| 84 } | |
| 85 | |
| 86 bool GetStats(ReceiveBandwidthEstimatorStats* output) const override { | |
| 87 CriticalSectionScoped cs(crit_sect_.get()); | |
| 88 return rbe_->GetStats(output); | |
| 89 } | |
| 90 | |
| 91 void SetMinBitrate(int min_bitrate_bps) { | |
| 92 CriticalSectionScoped cs(crit_sect_.get()); | |
| 93 rbe_->SetMinBitrate(min_bitrate_bps); | |
| 94 min_bitrate_bps_ = min_bitrate_bps; | |
| 95 } | |
| 96 | |
| 97 private: | |
| 98 void PickEstimatorFromHeader(const RTPHeader& header) | |
| 99 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get()) { | |
| 100 if (header.extension.hasAbsoluteSendTime) { | |
| 101 // If we see AST in header, switch RBE strategy immediately. | |
| 102 if (!using_absolute_send_time_) { | |
| 103 LOG(LS_INFO) << | |
| 104 "WrappingBitrateEstimator: Switching to absolute send time RBE."; | |
| 105 using_absolute_send_time_ = true; | |
| 106 PickEstimator(); | |
| 107 } | |
| 108 packets_since_absolute_send_time_ = 0; | |
| 109 } else { | |
| 110 // When we don't see AST, wait for a few packets before going back to TOF. | |
| 111 if (using_absolute_send_time_) { | |
| 112 ++packets_since_absolute_send_time_; | |
| 113 if (packets_since_absolute_send_time_ >= kTimeOffsetSwitchThreshold) { | |
| 114 LOG(LS_INFO) << "WrappingBitrateEstimator: Switching to transmission " | |
| 115 << "time offset RBE."; | |
| 116 using_absolute_send_time_ = false; | |
| 117 PickEstimator(); | |
| 118 } | |
| 119 } | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 // Instantiate RBE for Time Offset or Absolute Send Time extensions. | |
| 124 void PickEstimator() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get()) { | |
| 125 if (using_absolute_send_time_) { | |
| 126 rbe_.reset(new RemoteBitrateEstimatorAbsSendTime(observer_, clock_)); | |
| 127 } else { | |
| 128 rbe_.reset(new RemoteBitrateEstimatorSingleStream(observer_, clock_)); | |
| 129 } | |
| 130 rbe_->SetMinBitrate(min_bitrate_bps_); | |
| 131 } | |
| 132 | |
| 133 RemoteBitrateObserver* observer_; | |
| 134 Clock* clock_; | |
| 135 rtc::scoped_ptr<CriticalSectionWrapper> crit_sect_; | |
| 136 rtc::scoped_ptr<RemoteBitrateEstimator> rbe_; | |
| 137 bool using_absolute_send_time_; | |
| 138 uint32_t packets_since_absolute_send_time_; | |
| 139 int min_bitrate_bps_; | |
| 140 | |
| 141 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WrappingBitrateEstimator); | |
| 142 }; | |
| 143 | |
| 144 } // namespace | |
| 145 | |
| 146 ChannelGroup::ChannelGroup(ProcessThread* process_thread, | |
| 147 CallStats* call_stats) | |
| 148 : remb_(new VieRemb()), | |
| 149 bitrate_allocator_(new BitrateAllocator()), | |
| 150 packet_router_(new PacketRouter()), | |
| 151 pacer_(new PacedSender(Clock::GetRealTimeClock(), | |
| 152 packet_router_.get(), | |
| 153 BitrateController::kDefaultStartBitrateKbps, | |
| 154 PacedSender::kDefaultPaceMultiplier * | |
| 155 BitrateController::kDefaultStartBitrateKbps, | |
| 156 0)), | |
| 157 remote_bitrate_estimator_( | |
| 158 new WrappingBitrateEstimator(remb_.get(), Clock::GetRealTimeClock())), | |
| 159 remote_estimator_proxy_( | |
| 160 new RemoteEstimatorProxy(Clock::GetRealTimeClock(), | |
| 161 packet_router_.get())), | |
| 162 process_thread_(process_thread), | |
| 163 call_stats_(call_stats), | |
| 164 pacer_thread_(ProcessThread::Create("PacerThread")), | |
| 165 // Constructed last as this object calls the provided callback on | |
| 166 // construction. | |
| 167 bitrate_controller_( | |
| 168 BitrateController::CreateBitrateController(Clock::GetRealTimeClock(), | |
| 169 this)), | |
| 170 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps) { | |
| 171 call_stats_->RegisterStatsObserver(remote_bitrate_estimator_.get()); | |
| 172 | |
| 173 pacer_thread_->RegisterModule(pacer_.get()); | |
| 174 pacer_thread_->Start(); | |
| 175 | |
| 176 process_thread->RegisterModule(remote_estimator_proxy_.get()); | |
| 177 process_thread->RegisterModule(remote_bitrate_estimator_.get()); | |
| 178 process_thread->RegisterModule(bitrate_controller_.get()); | |
| 179 } | |
| 180 | |
| 181 ChannelGroup::~ChannelGroup() { | |
| 182 pacer_thread_->Stop(); | |
| 183 pacer_thread_->DeRegisterModule(pacer_.get()); | |
| 184 process_thread_->DeRegisterModule(bitrate_controller_.get()); | |
| 185 process_thread_->DeRegisterModule(remote_bitrate_estimator_.get()); | |
| 186 process_thread_->DeRegisterModule(remote_estimator_proxy_.get()); | |
| 187 call_stats_->DeregisterStatsObserver(remote_bitrate_estimator_.get()); | |
| 188 if (transport_feedback_adapter_.get()) | |
| 189 call_stats_->DeregisterStatsObserver(transport_feedback_adapter_.get()); | |
| 190 RTC_DCHECK(!remb_->InUse()); | |
| 191 RTC_DCHECK(encoders_.empty()); | |
| 192 } | |
| 193 | |
| 194 void ChannelGroup::AddEncoder(ViEEncoder* encoder) { | |
| 195 rtc::CritScope lock(&encoder_crit_); | |
| 196 encoders_.push_back(encoder); | |
| 197 } | |
| 198 | |
| 199 void ChannelGroup::RemoveEncoder(ViEEncoder* encoder) { | |
| 200 rtc::CritScope lock(&encoder_crit_); | |
| 201 for (auto it = encoders_.begin(); it != encoders_.end(); ++it) { | |
| 202 if (*it == encoder) { | |
| 203 encoders_.erase(it); | |
| 204 return; | |
| 205 } | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 void ChannelGroup::SetBweBitrates(int min_bitrate_bps, | |
| 210 int start_bitrate_bps, | |
| 211 int max_bitrate_bps) { | |
| 212 if (start_bitrate_bps > 0) | |
| 213 bitrate_controller_->SetStartBitrate(start_bitrate_bps); | |
| 214 bitrate_controller_->SetMinMaxBitrate(min_bitrate_bps, max_bitrate_bps); | |
| 215 if (remote_bitrate_estimator_.get()) | |
| 216 remote_bitrate_estimator_->SetMinBitrate(min_bitrate_bps); | |
| 217 if (transport_feedback_adapter_.get()) | |
| 218 transport_feedback_adapter_->GetBitrateEstimator()->SetMinBitrate( | |
| 219 min_bitrate_bps); | |
| 220 min_bitrate_bps_ = min_bitrate_bps; | |
| 221 } | |
| 222 | |
| 223 BitrateController* ChannelGroup::GetBitrateController() const { | |
| 224 return bitrate_controller_.get(); | |
| 225 } | |
| 226 | |
| 227 RemoteBitrateEstimator* ChannelGroup::GetRemoteBitrateEstimator( | |
| 228 bool send_side_bwe) const { | |
| 229 | |
| 230 if (send_side_bwe) | |
| 231 return remote_estimator_proxy_.get(); | |
| 232 else | |
| 233 return remote_bitrate_estimator_.get(); | |
| 234 } | |
| 235 | |
| 236 TransportFeedbackObserver* ChannelGroup::GetTransportFeedbackObserver() { | |
| 237 if (transport_feedback_adapter_.get() == nullptr) { | |
| 238 transport_feedback_adapter_.reset(new TransportFeedbackAdapter( | |
| 239 bitrate_controller_->CreateRtcpBandwidthObserver(), | |
| 240 Clock::GetRealTimeClock(), process_thread_)); | |
| 241 transport_feedback_adapter_->SetBitrateEstimator( | |
| 242 new RemoteBitrateEstimatorAbsSendTime( | |
| 243 transport_feedback_adapter_.get(), Clock::GetRealTimeClock())); | |
| 244 transport_feedback_adapter_->GetBitrateEstimator()->SetMinBitrate( | |
| 245 min_bitrate_bps_); | |
| 246 call_stats_->RegisterStatsObserver(transport_feedback_adapter_.get()); | |
| 247 } | |
| 248 return transport_feedback_adapter_.get(); | |
| 249 } | |
| 250 | |
| 251 int64_t ChannelGroup::GetPacerQueuingDelayMs() const { | |
| 252 return pacer_->QueueInMs(); | |
| 253 } | |
| 254 | |
| 255 // TODO(mflodman): Move out of this class. | |
| 256 void ChannelGroup::SetChannelRembStatus(bool sender, | |
| 257 bool receiver, | |
| 258 RtpRtcp* rtp_module) { | |
| 259 rtp_module->SetREMBStatus(sender || receiver); | |
| 260 if (sender) { | |
| 261 remb_->AddRembSender(rtp_module); | |
| 262 } else { | |
| 263 remb_->RemoveRembSender(rtp_module); | |
| 264 } | |
| 265 if (receiver) { | |
| 266 remb_->AddReceiveChannel(rtp_module); | |
| 267 } else { | |
| 268 remb_->RemoveReceiveChannel(rtp_module); | |
| 269 } | |
| 270 } | |
| 271 | |
| 272 void ChannelGroup::SignalNetworkState(NetworkState state) { | |
| 273 if (state == kNetworkUp) { | |
| 274 pacer_->Resume(); | |
| 275 } else { | |
| 276 pacer_->Pause(); | |
| 277 } | |
| 278 } | |
| 279 | |
| 280 // TODO(mflodman): Move this logic out from ChannelGroup. | |
| 281 void ChannelGroup::OnNetworkChanged(uint32_t target_bitrate_bps, | |
| 282 uint8_t fraction_loss, | |
| 283 int64_t rtt) { | |
| 284 bitrate_allocator_->OnNetworkChanged(target_bitrate_bps, fraction_loss, rtt); | |
| 285 int pad_up_to_bitrate_bps = 0; | |
| 286 { | |
| 287 rtc::CritScope lock(&encoder_crit_); | |
| 288 for (const auto& encoder : encoders_) | |
| 289 pad_up_to_bitrate_bps += encoder->GetPaddingNeededBps(); | |
| 290 } | |
| 291 pacer_->UpdateBitrate( | |
| 292 target_bitrate_bps / 1000, | |
| 293 PacedSender::kDefaultPaceMultiplier * target_bitrate_bps / 1000, | |
| 294 pad_up_to_bitrate_bps / 1000); | |
| 295 } | |
| 296 | |
| 297 void ChannelGroup::OnSentPacket(const rtc::SentPacket& sent_packet) { | |
| 298 if (transport_feedback_adapter_) { | |
| 299 transport_feedback_adapter_->UpdateSendTime(sent_packet.packet_id, | |
| 300 sent_packet.send_time_ms); | |
| 301 } | |
| 302 } | |
| 303 } // namespace webrtc | |
| OLD | NEW |