OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 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 <string.h> | 11 #include <string.h> |
12 | |
13 #include <algorithm> | 12 #include <algorithm> |
14 #include <map> | 13 #include <map> |
15 #include <memory> | 14 #include <memory> |
16 #include <vector> | 15 #include <vector> |
17 | 16 |
18 #include "webrtc/audio/audio_receive_stream.h" | 17 #include "webrtc/audio/audio_receive_stream.h" |
19 #include "webrtc/audio/audio_send_stream.h" | 18 #include "webrtc/audio/audio_send_stream.h" |
20 #include "webrtc/audio/audio_state.h" | 19 #include "webrtc/audio/audio_state.h" |
21 #include "webrtc/audio/scoped_voe_interface.h" | 20 #include "webrtc/audio/scoped_voe_interface.h" |
22 #include "webrtc/base/checks.h" | 21 #include "webrtc/base/checks.h" |
23 #include "webrtc/base/constructormagic.h" | 22 #include "webrtc/base/constructormagic.h" |
24 #include "webrtc/base/logging.h" | 23 #include "webrtc/base/logging.h" |
24 #include "webrtc/base/task_queue.h" | |
25 #include "webrtc/base/thread_annotations.h" | 25 #include "webrtc/base/thread_annotations.h" |
26 #include "webrtc/base/thread_checker.h" | 26 #include "webrtc/base/thread_checker.h" |
27 #include "webrtc/base/trace_event.h" | 27 #include "webrtc/base/trace_event.h" |
28 #include "webrtc/call.h" | 28 #include "webrtc/call.h" |
29 #include "webrtc/call/bitrate_allocator.h" | 29 #include "webrtc/call/bitrate_allocator.h" |
30 #include "webrtc/call/rtc_event_log.h" | 30 #include "webrtc/call/rtc_event_log.h" |
31 #include "webrtc/config.h" | 31 #include "webrtc/config.h" |
32 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" | 32 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" |
33 #include "webrtc/modules/congestion_controller/include/congestion_controller.h" | 33 #include "webrtc/modules/congestion_controller/include/congestion_controller.h" |
34 #include "webrtc/modules/pacing/paced_sender.h" | 34 #include "webrtc/modules/pacing/paced_sender.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 webrtc::AudioSendStream* CreateAudioSendStream( | 67 webrtc::AudioSendStream* CreateAudioSendStream( |
68 const webrtc::AudioSendStream::Config& config) override; | 68 const webrtc::AudioSendStream::Config& config) override; |
69 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override; | 69 void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override; |
70 | 70 |
71 webrtc::AudioReceiveStream* CreateAudioReceiveStream( | 71 webrtc::AudioReceiveStream* CreateAudioReceiveStream( |
72 const webrtc::AudioReceiveStream::Config& config) override; | 72 const webrtc::AudioReceiveStream::Config& config) override; |
73 void DestroyAudioReceiveStream( | 73 void DestroyAudioReceiveStream( |
74 webrtc::AudioReceiveStream* receive_stream) override; | 74 webrtc::AudioReceiveStream* receive_stream) override; |
75 | 75 |
76 webrtc::VideoSendStream* CreateVideoSendStream( | 76 webrtc::VideoSendStream* CreateVideoSendStream( |
77 const webrtc::VideoSendStream::Config& config, | 77 webrtc::VideoSendStream::Config config, |
78 const VideoEncoderConfig& encoder_config) override; | 78 VideoEncoderConfig encoder_config) override; |
79 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override; | 79 void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override; |
80 | 80 |
81 webrtc::VideoReceiveStream* CreateVideoReceiveStream( | 81 webrtc::VideoReceiveStream* CreateVideoReceiveStream( |
82 webrtc::VideoReceiveStream::Config configuration) override; | 82 webrtc::VideoReceiveStream::Config configuration) override; |
83 void DestroyVideoReceiveStream( | 83 void DestroyVideoReceiveStream( |
84 webrtc::VideoReceiveStream* receive_stream) override; | 84 webrtc::VideoReceiveStream* receive_stream) override; |
85 | 85 |
86 Stats GetStats() const override; | 86 Stats GetStats() const override; |
87 | 87 |
88 DeliveryStatus DeliverPacket(MediaType media_type, | 88 DeliveryStatus DeliverPacket(MediaType media_type, |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
188 int64_t estimated_send_bitrate_sum_kbits_ GUARDED_BY(&bitrate_crit_); | 188 int64_t estimated_send_bitrate_sum_kbits_ GUARDED_BY(&bitrate_crit_); |
189 int64_t pacer_bitrate_sum_kbits_ GUARDED_BY(&bitrate_crit_); | 189 int64_t pacer_bitrate_sum_kbits_ GUARDED_BY(&bitrate_crit_); |
190 uint32_t min_allocated_send_bitrate_bps_ GUARDED_BY(&bitrate_crit_); | 190 uint32_t min_allocated_send_bitrate_bps_ GUARDED_BY(&bitrate_crit_); |
191 int64_t num_bitrate_updates_ GUARDED_BY(&bitrate_crit_); | 191 int64_t num_bitrate_updates_ GUARDED_BY(&bitrate_crit_); |
192 | 192 |
193 std::map<std::string, rtc::NetworkRoute> network_routes_; | 193 std::map<std::string, rtc::NetworkRoute> network_routes_; |
194 | 194 |
195 VieRemb remb_; | 195 VieRemb remb_; |
196 const std::unique_ptr<CongestionController> congestion_controller_; | 196 const std::unique_ptr<CongestionController> congestion_controller_; |
197 const std::unique_ptr<SendDelayStats> video_send_delay_stats_; | 197 const std::unique_ptr<SendDelayStats> video_send_delay_stats_; |
198 // TODO(perkj): |worker_queue_| is supposed to replace | |
199 // |module_process_thread_|. | |
200 // |worker_queue| is defined last to ensure all pending tasks are | |
201 // deleted before any other members. | |
tommi
2016/07/06 11:15:01
pending tasks may actually not run when the worker
perkj_webrtc
2016/07/06 13:08:53
updated comment. That is what I want. I don't care
| |
202 rtc::TaskQueue worker_queue_; | |
198 | 203 |
199 RTC_DISALLOW_COPY_AND_ASSIGN(Call); | 204 RTC_DISALLOW_COPY_AND_ASSIGN(Call); |
200 }; | 205 }; |
201 } // namespace internal | 206 } // namespace internal |
202 | 207 |
203 Call* Call::Create(const Call::Config& config) { | 208 Call* Call::Create(const Call::Config& config) { |
204 return new internal::Call(config); | 209 return new internal::Call(config); |
205 } | 210 } |
206 | 211 |
207 namespace internal { | 212 namespace internal { |
(...skipping 17 matching lines...) Expand all Loading... | |
225 first_rtp_packet_received_ms_(-1), | 230 first_rtp_packet_received_ms_(-1), |
226 last_rtp_packet_received_ms_(-1), | 231 last_rtp_packet_received_ms_(-1), |
227 first_packet_sent_ms_(-1), | 232 first_packet_sent_ms_(-1), |
228 estimated_send_bitrate_sum_kbits_(0), | 233 estimated_send_bitrate_sum_kbits_(0), |
229 pacer_bitrate_sum_kbits_(0), | 234 pacer_bitrate_sum_kbits_(0), |
230 min_allocated_send_bitrate_bps_(0), | 235 min_allocated_send_bitrate_bps_(0), |
231 num_bitrate_updates_(0), | 236 num_bitrate_updates_(0), |
232 remb_(clock_), | 237 remb_(clock_), |
233 congestion_controller_( | 238 congestion_controller_( |
234 new CongestionController(clock_, this, &remb_, event_log_.get())), | 239 new CongestionController(clock_, this, &remb_, event_log_.get())), |
235 video_send_delay_stats_(new SendDelayStats(clock_)) { | 240 video_send_delay_stats_(new SendDelayStats(clock_)), |
241 worker_queue_("worker_queue") { | |
236 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread()); | 242 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread()); |
237 RTC_DCHECK_GE(config.bitrate_config.min_bitrate_bps, 0); | 243 RTC_DCHECK_GE(config.bitrate_config.min_bitrate_bps, 0); |
238 RTC_DCHECK_GE(config.bitrate_config.start_bitrate_bps, | 244 RTC_DCHECK_GE(config.bitrate_config.start_bitrate_bps, |
239 config.bitrate_config.min_bitrate_bps); | 245 config.bitrate_config.min_bitrate_bps); |
240 if (config.bitrate_config.max_bitrate_bps != -1) { | 246 if (config.bitrate_config.max_bitrate_bps != -1) { |
241 RTC_DCHECK_GE(config.bitrate_config.max_bitrate_bps, | 247 RTC_DCHECK_GE(config.bitrate_config.max_bitrate_bps, |
242 config.bitrate_config.start_bitrate_bps); | 248 config.bitrate_config.start_bitrate_bps); |
243 } | 249 } |
244 | 250 |
245 Trace::CreateTrace(); | 251 Trace::CreateTrace(); |
246 call_stats_->RegisterStatsObserver(congestion_controller_.get()); | 252 call_stats_->RegisterStatsObserver(congestion_controller_.get()); |
247 | 253 |
248 congestion_controller_->SetBweBitrates( | 254 congestion_controller_->SetBweBitrates( |
249 config_.bitrate_config.min_bitrate_bps, | 255 config_.bitrate_config.min_bitrate_bps, |
250 config_.bitrate_config.start_bitrate_bps, | 256 config_.bitrate_config.start_bitrate_bps, |
251 config_.bitrate_config.max_bitrate_bps); | 257 config_.bitrate_config.max_bitrate_bps); |
252 | 258 |
253 module_process_thread_->Start(); | 259 module_process_thread_->Start(); |
254 module_process_thread_->RegisterModule(call_stats_.get()); | 260 module_process_thread_->RegisterModule(call_stats_.get()); |
255 module_process_thread_->RegisterModule(congestion_controller_.get()); | 261 module_process_thread_->RegisterModule(congestion_controller_.get()); |
256 pacer_thread_->RegisterModule(congestion_controller_->pacer()); | 262 pacer_thread_->RegisterModule(congestion_controller_->pacer()); |
257 pacer_thread_->RegisterModule( | 263 pacer_thread_->RegisterModule( |
258 congestion_controller_->GetRemoteBitrateEstimator(true)); | 264 congestion_controller_->GetRemoteBitrateEstimator(true)); |
259 pacer_thread_->Start(); | 265 pacer_thread_->Start(); |
260 } | 266 } |
261 | 267 |
262 Call::~Call() { | 268 Call::~Call() { |
263 RTC_DCHECK(!remb_.InUse()); | 269 RTC_DCHECK(!remb_.InUse()); |
264 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread()); | 270 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread()); |
265 UpdateSendHistograms(); | 271 { |
272 rtc::CritScope lock(&bitrate_crit_); | |
273 UpdateSendHistograms(); | |
274 } | |
266 UpdateReceiveHistograms(); | 275 UpdateReceiveHistograms(); |
267 RTC_CHECK(audio_send_ssrcs_.empty()); | 276 RTC_CHECK(audio_send_ssrcs_.empty()); |
268 RTC_CHECK(video_send_ssrcs_.empty()); | 277 RTC_CHECK(video_send_ssrcs_.empty()); |
269 RTC_CHECK(video_send_streams_.empty()); | 278 RTC_CHECK(video_send_streams_.empty()); |
270 RTC_CHECK(audio_receive_ssrcs_.empty()); | 279 RTC_CHECK(audio_receive_ssrcs_.empty()); |
271 RTC_CHECK(video_receive_ssrcs_.empty()); | 280 RTC_CHECK(video_receive_ssrcs_.empty()); |
272 RTC_CHECK(video_receive_streams_.empty()); | 281 RTC_CHECK(video_receive_streams_.empty()); |
273 | 282 |
274 pacer_thread_->Stop(); | 283 pacer_thread_->Stop(); |
275 pacer_thread_->DeRegisterModule(congestion_controller_->pacer()); | 284 pacer_thread_->DeRegisterModule(congestion_controller_->pacer()); |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
409 it->second == audio_receive_stream) { | 418 it->second == audio_receive_stream) { |
410 sync_stream_mapping_.erase(it); | 419 sync_stream_mapping_.erase(it); |
411 ConfigureSync(sync_group); | 420 ConfigureSync(sync_group); |
412 } | 421 } |
413 } | 422 } |
414 UpdateAggregateNetworkState(); | 423 UpdateAggregateNetworkState(); |
415 delete audio_receive_stream; | 424 delete audio_receive_stream; |
416 } | 425 } |
417 | 426 |
418 webrtc::VideoSendStream* Call::CreateVideoSendStream( | 427 webrtc::VideoSendStream* Call::CreateVideoSendStream( |
419 const webrtc::VideoSendStream::Config& config, | 428 webrtc::VideoSendStream::Config config, |
420 const VideoEncoderConfig& encoder_config) { | 429 VideoEncoderConfig encoder_config) { |
421 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream"); | 430 TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream"); |
422 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread()); | 431 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread()); |
423 | 432 |
424 video_send_delay_stats_->AddSsrcs(config); | 433 video_send_delay_stats_->AddSsrcs(config); |
434 event_log_->LogVideoSendStreamConfig(config); | |
435 | |
425 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if | 436 // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if |
426 // the call has already started. | 437 // the call has already started. |
438 std::vector<uint32_t> ssrcs = config.rtp.ssrcs; | |
tommi
2016/07/06 11:15:01
do we need this array? It looks like we create a
perkj_webrtc
2016/07/06 13:08:53
Done.
| |
427 VideoSendStream* send_stream = new VideoSendStream( | 439 VideoSendStream* send_stream = new VideoSendStream( |
428 num_cpu_cores_, module_process_thread_.get(), call_stats_.get(), | 440 num_cpu_cores_, module_process_thread_.get(), &worker_queue_, |
429 congestion_controller_.get(), bitrate_allocator_.get(), | 441 call_stats_.get(), congestion_controller_.get(), bitrate_allocator_.get(), |
430 video_send_delay_stats_.get(), &remb_, event_log_.get(), config, | 442 video_send_delay_stats_.get(), &remb_, event_log_.get(), |
431 encoder_config, suspended_video_send_ssrcs_); | 443 std::move(config), std::move(encoder_config), |
444 suspended_video_send_ssrcs_); | |
445 | |
432 { | 446 { |
433 WriteLockScoped write_lock(*send_crit_); | 447 WriteLockScoped write_lock(*send_crit_); |
434 for (uint32_t ssrc : config.rtp.ssrcs) { | 448 for (uint32_t ssrc : ssrcs) { |
435 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end()); | 449 RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end()); |
436 video_send_ssrcs_[ssrc] = send_stream; | 450 video_send_ssrcs_[ssrc] = send_stream; |
437 } | 451 } |
438 video_send_streams_.insert(send_stream); | 452 video_send_streams_.insert(send_stream); |
439 } | 453 } |
440 send_stream->SignalNetworkState(video_network_state_); | 454 send_stream->SignalNetworkState(video_network_state_); |
441 UpdateAggregateNetworkState(); | 455 UpdateAggregateNetworkState(); |
442 event_log_->LogVideoSendStreamConfig(config); | 456 |
443 return send_stream; | 457 return send_stream; |
444 } | 458 } |
445 | 459 |
446 void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) { | 460 void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) { |
447 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream"); | 461 TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream"); |
448 RTC_DCHECK(send_stream != nullptr); | 462 RTC_DCHECK(send_stream != nullptr); |
449 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread()); | 463 RTC_DCHECK(configuration_thread_checker_.CalledOnValidThread()); |
450 | 464 |
451 send_stream->Stop(); | 465 send_stream->Stop(); |
452 | 466 |
453 VideoSendStream* send_stream_impl = nullptr; | 467 VideoSendStream* send_stream_impl = nullptr; |
454 { | 468 { |
455 WriteLockScoped write_lock(*send_crit_); | 469 WriteLockScoped write_lock(*send_crit_); |
456 auto it = video_send_ssrcs_.begin(); | 470 auto it = video_send_ssrcs_.begin(); |
457 while (it != video_send_ssrcs_.end()) { | 471 while (it != video_send_ssrcs_.end()) { |
458 if (it->second == static_cast<VideoSendStream*>(send_stream)) { | 472 if (it->second == static_cast<VideoSendStream*>(send_stream)) { |
459 send_stream_impl = it->second; | 473 send_stream_impl = it->second; |
460 video_send_ssrcs_.erase(it++); | 474 video_send_ssrcs_.erase(it++); |
461 } else { | 475 } else { |
462 ++it; | 476 ++it; |
463 } | 477 } |
464 } | 478 } |
465 video_send_streams_.erase(send_stream_impl); | 479 video_send_streams_.erase(send_stream_impl); |
466 } | 480 } |
467 RTC_CHECK(send_stream_impl != nullptr); | 481 RTC_CHECK(send_stream_impl != nullptr); |
468 | 482 |
469 VideoSendStream::RtpStateMap rtp_state = send_stream_impl->GetRtpStates(); | 483 VideoSendStream::RtpStateMap rtp_state = |
484 send_stream_impl->StopPermanentlyAndGetRtpStates(); | |
470 | 485 |
471 for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin(); | 486 for (VideoSendStream::RtpStateMap::iterator it = rtp_state.begin(); |
472 it != rtp_state.end(); | 487 it != rtp_state.end(); ++it) { |
473 ++it) { | |
474 suspended_video_send_ssrcs_[it->first] = it->second; | 488 suspended_video_send_ssrcs_[it->first] = it->second; |
475 } | 489 } |
476 | 490 |
477 UpdateAggregateNetworkState(); | 491 UpdateAggregateNetworkState(); |
478 delete send_stream_impl; | 492 delete send_stream_impl; |
479 } | 493 } |
480 | 494 |
481 webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream( | 495 webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream( |
482 webrtc::VideoReceiveStream::Config configuration) { | 496 webrtc::VideoReceiveStream::Config configuration) { |
483 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream"); | 497 TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream"); |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
684 void Call::OnSentPacket(const rtc::SentPacket& sent_packet) { | 698 void Call::OnSentPacket(const rtc::SentPacket& sent_packet) { |
685 if (first_packet_sent_ms_ == -1) | 699 if (first_packet_sent_ms_ == -1) |
686 first_packet_sent_ms_ = clock_->TimeInMilliseconds(); | 700 first_packet_sent_ms_ = clock_->TimeInMilliseconds(); |
687 video_send_delay_stats_->OnSentPacket(sent_packet.packet_id, | 701 video_send_delay_stats_->OnSentPacket(sent_packet.packet_id, |
688 clock_->TimeInMilliseconds()); | 702 clock_->TimeInMilliseconds()); |
689 congestion_controller_->OnSentPacket(sent_packet); | 703 congestion_controller_->OnSentPacket(sent_packet); |
690 } | 704 } |
691 | 705 |
692 void Call::OnNetworkChanged(uint32_t target_bitrate_bps, uint8_t fraction_loss, | 706 void Call::OnNetworkChanged(uint32_t target_bitrate_bps, uint8_t fraction_loss, |
693 int64_t rtt_ms) { | 707 int64_t rtt_ms) { |
708 // TODO(perkj): Consider making sure CongestionController operates on | |
709 // |worker_queue_|. | |
710 if (!worker_queue_.IsCurrent()) { | |
711 worker_queue_.PostTask([this, target_bitrate_bps, fraction_loss, rtt_ms] { | |
712 OnNetworkChanged(target_bitrate_bps, fraction_loss, rtt_ms); | |
713 }); | |
714 return; | |
715 } | |
716 RTC_DCHECK_RUN_ON(&worker_queue_); | |
694 bitrate_allocator_->OnNetworkChanged(target_bitrate_bps, fraction_loss, | 717 bitrate_allocator_->OnNetworkChanged(target_bitrate_bps, fraction_loss, |
695 rtt_ms); | 718 rtt_ms); |
696 | 719 |
697 { | 720 { |
698 rtc::CritScope lock(&bitrate_crit_); | 721 rtc::CritScope lock(&bitrate_crit_); |
699 // We only update these stats if we have send streams, and assume that | 722 // We only update these stats if we have send streams, and assume that |
700 // OnNetworkChanged is called roughly with a fixed frequency. | 723 // OnNetworkChanged is called roughly with a fixed frequency. |
701 estimated_send_bitrate_sum_kbits_ += target_bitrate_bps / 1000; | 724 estimated_send_bitrate_sum_kbits_ += target_bitrate_bps / 1000; |
702 // Pacer bitrate might be higher than bitrate estimate if enforcing min | 725 // Pacer bitrate might be higher than bitrate estimate if enforcing min |
703 // bitrate. | 726 // bitrate. |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
860 // thread. Then this check can be enabled. | 883 // thread. Then this check can be enabled. |
861 // RTC_DCHECK(!configuration_thread_checker_.CalledOnValidThread()); | 884 // RTC_DCHECK(!configuration_thread_checker_.CalledOnValidThread()); |
862 if (RtpHeaderParser::IsRtcp(packet, length)) | 885 if (RtpHeaderParser::IsRtcp(packet, length)) |
863 return DeliverRtcp(media_type, packet, length); | 886 return DeliverRtcp(media_type, packet, length); |
864 | 887 |
865 return DeliverRtp(media_type, packet, length, packet_time); | 888 return DeliverRtp(media_type, packet, length, packet_time); |
866 } | 889 } |
867 | 890 |
868 } // namespace internal | 891 } // namespace internal |
869 } // namespace webrtc | 892 } // namespace webrtc |
OLD | NEW |