| 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 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 virtual void OnNetworkChanged(uint32_t bitrate_bps, | 98 virtual void OnNetworkChanged(uint32_t bitrate_bps, |
| 99 uint8_t fraction_lost, | 99 uint8_t fraction_lost, |
| 100 int64_t rtt) { | 100 int64_t rtt) { |
| 101 owner_->OnNetworkChanged(bitrate_bps, fraction_lost, rtt); | 101 owner_->OnNetworkChanged(bitrate_bps, fraction_lost, rtt); |
| 102 } | 102 } |
| 103 private: | 103 private: |
| 104 ViEEncoder* owner_; | 104 ViEEncoder* owner_; |
| 105 }; | 105 }; |
| 106 | 106 |
| 107 ViEEncoder::ViEEncoder(uint32_t number_of_cores, | 107 ViEEncoder::ViEEncoder(uint32_t number_of_cores, |
| 108 const std::vector<uint32_t>& ssrcs, |
| 108 ProcessThread* module_process_thread, | 109 ProcessThread* module_process_thread, |
| 109 SendStatisticsProxy* stats_proxy, | 110 SendStatisticsProxy* stats_proxy, |
| 110 I420FrameCallback* pre_encode_callback, | 111 I420FrameCallback* pre_encode_callback, |
| 111 OveruseFrameDetector* overuse_detector, | 112 OveruseFrameDetector* overuse_detector, |
| 112 PacedSender* pacer, | 113 PacedSender* pacer, |
| 113 PayloadRouter* payload_router, | 114 PayloadRouter* payload_router, |
| 114 BitrateAllocator* bitrate_allocator) | 115 BitrateAllocator* bitrate_allocator) |
| 115 : number_of_cores_(number_of_cores), | 116 : number_of_cores_(number_of_cores), |
| 117 ssrcs_(ssrcs), |
| 116 vp_(VideoProcessing::Create()), | 118 vp_(VideoProcessing::Create()), |
| 117 qm_callback_(new QMVideoSettingsCallback(vp_.get())), | 119 qm_callback_(new QMVideoSettingsCallback(vp_.get())), |
| 118 vcm_(VideoCodingModule::Create(Clock::GetRealTimeClock(), | 120 vcm_(VideoCodingModule::Create(Clock::GetRealTimeClock(), |
| 119 this, | 121 this, |
| 120 qm_callback_.get())), | 122 qm_callback_.get())), |
| 121 stats_proxy_(stats_proxy), | 123 stats_proxy_(stats_proxy), |
| 122 pre_encode_callback_(pre_encode_callback), | 124 pre_encode_callback_(pre_encode_callback), |
| 123 overuse_detector_(overuse_detector), | 125 overuse_detector_(overuse_detector), |
| 124 pacer_(pacer), | 126 pacer_(pacer), |
| 125 send_payload_router_(payload_router), | 127 send_payload_router_(payload_router), |
| 126 bitrate_allocator_(bitrate_allocator), | 128 bitrate_allocator_(bitrate_allocator), |
| 127 time_of_last_frame_activity_ms_(0), | 129 time_of_last_frame_activity_ms_(0), |
| 128 encoder_config_(), | 130 encoder_config_(), |
| 129 min_transmit_bitrate_kbps_(0), | 131 min_transmit_bitrate_kbps_(0), |
| 130 last_observed_bitrate_bps_(0), | 132 last_observed_bitrate_bps_(0), |
| 131 target_delay_ms_(0), | 133 target_delay_ms_(0), |
| 132 network_is_transmitting_(true), | 134 network_is_transmitting_(true), |
| 133 encoder_paused_(false), | 135 encoder_paused_(false), |
| 134 encoder_paused_and_dropped_frame_(false), | 136 encoder_paused_and_dropped_frame_(false), |
| 137 time_last_intra_request_ms_(ssrcs.size(), -1), |
| 135 module_process_thread_(module_process_thread), | 138 module_process_thread_(module_process_thread), |
| 136 has_received_sli_(false), | 139 has_received_sli_(false), |
| 137 picture_id_sli_(0), | 140 picture_id_sli_(0), |
| 138 has_received_rpsi_(false), | 141 has_received_rpsi_(false), |
| 139 picture_id_rpsi_(0), | 142 picture_id_rpsi_(0), |
| 140 video_suspended_(false) { | 143 video_suspended_(false) { |
| 141 bitrate_observer_.reset(new ViEBitrateObserver(this)); | 144 bitrate_observer_.reset(new ViEBitrateObserver(this)); |
| 142 module_process_thread_->RegisterModule(vcm_.get()); | 145 module_process_thread_->RegisterModule(vcm_.get()); |
| 143 } | 146 } |
| 144 | 147 |
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 uint64_t picture_id) { | 491 uint64_t picture_id) { |
| 489 rtc::CritScope lock(&data_cs_); | 492 rtc::CritScope lock(&data_cs_); |
| 490 picture_id_rpsi_ = picture_id; | 493 picture_id_rpsi_ = picture_id; |
| 491 has_received_rpsi_ = true; | 494 has_received_rpsi_ = true; |
| 492 } | 495 } |
| 493 | 496 |
| 494 void ViEEncoder::OnReceivedIntraFrameRequest(uint32_t ssrc) { | 497 void ViEEncoder::OnReceivedIntraFrameRequest(uint32_t ssrc) { |
| 495 // Key frame request from remote side, signal to VCM. | 498 // Key frame request from remote side, signal to VCM. |
| 496 TRACE_EVENT0("webrtc", "OnKeyFrameRequest"); | 499 TRACE_EVENT0("webrtc", "OnKeyFrameRequest"); |
| 497 | 500 |
| 498 int idx = 0; | 501 for (size_t i = 0; i < ssrcs_.size(); ++i) { |
| 499 { | 502 if (ssrcs_[i] != ssrc) |
| 500 rtc::CritScope lock(&data_cs_); | 503 continue; |
| 501 auto stream_it = ssrc_streams_.find(ssrc); | 504 int64_t now_ms = TickTime::MillisecondTimestamp(); |
| 502 if (stream_it == ssrc_streams_.end()) { | 505 { |
| 503 LOG_F(LS_WARNING) << "ssrc not found: " << ssrc << ", map size " | 506 rtc::CritScope lock(&data_cs_); |
| 504 << ssrc_streams_.size(); | 507 if (time_last_intra_request_ms_[i] + kMinKeyFrameRequestIntervalMs > |
| 505 return; | 508 now_ms) { |
| 509 return; |
| 510 } |
| 511 time_last_intra_request_ms_[i] = now_ms; |
| 506 } | 512 } |
| 507 std::map<unsigned int, int64_t>::iterator time_it = | 513 vcm_->IntraFrameRequest(static_cast<int>(i)); |
| 508 time_last_intra_request_ms_.find(ssrc); | |
| 509 if (time_it == time_last_intra_request_ms_.end()) { | |
| 510 time_last_intra_request_ms_[ssrc] = 0; | |
| 511 } | |
| 512 | |
| 513 int64_t now = TickTime::MillisecondTimestamp(); | |
| 514 if (time_last_intra_request_ms_[ssrc] + kMinKeyFrameRequestIntervalMs | |
| 515 > now) { | |
| 516 return; | |
| 517 } | |
| 518 time_last_intra_request_ms_[ssrc] = now; | |
| 519 idx = stream_it->second; | |
| 520 } | |
| 521 // Release the critsect before triggering key frame. | |
| 522 vcm_->IntraFrameRequest(idx); | |
| 523 } | |
| 524 | |
| 525 void ViEEncoder::OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc) { | |
| 526 rtc::CritScope lock(&data_cs_); | |
| 527 std::map<unsigned int, int>::iterator it = ssrc_streams_.find(old_ssrc); | |
| 528 if (it == ssrc_streams_.end()) { | |
| 529 return; | 514 return; |
| 530 } | 515 } |
| 531 | 516 RTC_NOTREACHED() << "Should not receive keyframe requests on unknown SSRCs."; |
| 532 ssrc_streams_[new_ssrc] = it->second; | |
| 533 ssrc_streams_.erase(it); | |
| 534 | |
| 535 std::map<unsigned int, int64_t>::iterator time_it = | |
| 536 time_last_intra_request_ms_.find(old_ssrc); | |
| 537 int64_t last_intra_request_ms = 0; | |
| 538 if (time_it != time_last_intra_request_ms_.end()) { | |
| 539 last_intra_request_ms = time_it->second; | |
| 540 time_last_intra_request_ms_.erase(time_it); | |
| 541 } | |
| 542 time_last_intra_request_ms_[new_ssrc] = last_intra_request_ms; | |
| 543 } | |
| 544 | |
| 545 void ViEEncoder::SetSsrcs(const std::vector<uint32_t>& ssrcs) { | |
| 546 rtc::CritScope lock(&data_cs_); | |
| 547 ssrc_streams_.clear(); | |
| 548 time_last_intra_request_ms_.clear(); | |
| 549 int idx = 0; | |
| 550 for (uint32_t ssrc : ssrcs) { | |
| 551 ssrc_streams_[ssrc] = idx++; | |
| 552 } | |
| 553 } | 517 } |
| 554 | 518 |
| 555 void ViEEncoder::SetMinTransmitBitrate(int min_transmit_bitrate_kbps) { | 519 void ViEEncoder::SetMinTransmitBitrate(int min_transmit_bitrate_kbps) { |
| 556 assert(min_transmit_bitrate_kbps >= 0); | 520 assert(min_transmit_bitrate_kbps >= 0); |
| 557 rtc::CritScope lock(&data_cs_); | 521 rtc::CritScope lock(&data_cs_); |
| 558 min_transmit_bitrate_kbps_ = min_transmit_bitrate_kbps; | 522 min_transmit_bitrate_kbps_ = min_transmit_bitrate_kbps; |
| 559 } | 523 } |
| 560 | 524 |
| 561 // Called from ViEBitrateObserver. | 525 // Called from ViEBitrateObserver. |
| 562 void ViEEncoder::OnNetworkChanged(uint32_t bitrate_bps, | 526 void ViEEncoder::OnNetworkChanged(uint32_t bitrate_bps, |
| 563 uint8_t fraction_lost, | 527 uint8_t fraction_lost, |
| 564 int64_t round_trip_time_ms) { | 528 int64_t round_trip_time_ms) { |
| 565 LOG(LS_VERBOSE) << "OnNetworkChanged, bitrate" << bitrate_bps | 529 LOG(LS_VERBOSE) << "OnNetworkChanged, bitrate" << bitrate_bps |
| 566 << " packet loss " << static_cast<int>(fraction_lost) | 530 << " packet loss " << static_cast<int>(fraction_lost) |
| 567 << " rtt " << round_trip_time_ms; | 531 << " rtt " << round_trip_time_ms; |
| 568 RTC_DCHECK(send_payload_router_ != NULL); | 532 RTC_DCHECK(send_payload_router_ != NULL); |
| 569 vcm_->SetChannelParameters(bitrate_bps, fraction_lost, round_trip_time_ms); | 533 vcm_->SetChannelParameters(bitrate_bps, fraction_lost, round_trip_time_ms); |
| 570 bool video_is_suspended = vcm_->VideoSuspended(); | 534 bool video_is_suspended = vcm_->VideoSuspended(); |
| 571 bool video_suspension_changed; | 535 bool video_suspension_changed; |
| 572 VideoCodec send_codec; | 536 VideoCodec send_codec; |
| 573 uint32_t first_ssrc; | |
| 574 { | 537 { |
| 575 rtc::CritScope lock(&data_cs_); | 538 rtc::CritScope lock(&data_cs_); |
| 576 last_observed_bitrate_bps_ = bitrate_bps; | 539 last_observed_bitrate_bps_ = bitrate_bps; |
| 577 video_suspension_changed = video_suspended_ != video_is_suspended; | 540 video_suspension_changed = video_suspended_ != video_is_suspended; |
| 578 video_suspended_ = video_is_suspended; | 541 video_suspended_ = video_is_suspended; |
| 579 send_codec = encoder_config_; | 542 send_codec = encoder_config_; |
| 580 first_ssrc = ssrc_streams_.begin()->first; | |
| 581 } | 543 } |
| 582 | 544 |
| 583 SimulcastStream* stream_configs = send_codec.simulcastStream; | 545 SimulcastStream* stream_configs = send_codec.simulcastStream; |
| 584 // Allocate the bandwidth between the streams. | 546 // Allocate the bandwidth between the streams. |
| 585 std::vector<uint32_t> stream_bitrates = AllocateStreamBitrates( | 547 std::vector<uint32_t> stream_bitrates = AllocateStreamBitrates( |
| 586 bitrate_bps, stream_configs, send_codec.numberOfSimulcastStreams); | 548 bitrate_bps, stream_configs, send_codec.numberOfSimulcastStreams); |
| 587 send_payload_router_->SetTargetSendBitrates(stream_bitrates); | 549 send_payload_router_->SetTargetSendBitrates(stream_bitrates); |
| 588 | 550 |
| 589 if (!video_suspension_changed) | 551 if (!video_suspension_changed) |
| 590 return; | 552 return; |
| 591 // Video suspend-state changed, inform codec observer. | 553 // Video suspend-state changed, inform codec observer. |
| 592 LOG(LS_INFO) << "Video suspend state changed " << video_is_suspended | 554 LOG(LS_INFO) << "Video suspend state changed " << video_is_suspended |
| 593 << " for ssrc " << first_ssrc; | 555 << " for ssrc " << ssrcs_[0]; |
| 594 if (stats_proxy_) | 556 if (stats_proxy_) |
| 595 stats_proxy_->OnSuspendChange(video_is_suspended); | 557 stats_proxy_->OnSuspendChange(video_is_suspended); |
| 596 } | 558 } |
| 597 | 559 |
| 598 void ViEEncoder::SuspendBelowMinBitrate() { | 560 void ViEEncoder::SuspendBelowMinBitrate() { |
| 599 vcm_->SuspendBelowMinBitrate(); | 561 vcm_->SuspendBelowMinBitrate(); |
| 600 bitrate_allocator_->EnforceMinBitrate(false); | 562 bitrate_allocator_->EnforceMinBitrate(false); |
| 601 } | 563 } |
| 602 | 564 |
| 603 void ViEEncoder::RegisterPostEncodeImageCallback( | 565 void ViEEncoder::RegisterPostEncodeImageCallback( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 617 const uint32_t width, | 579 const uint32_t width, |
| 618 const uint32_t height) { | 580 const uint32_t height) { |
| 619 return vp_->SetTargetResolution(width, height, frame_rate); | 581 return vp_->SetTargetResolution(width, height, frame_rate); |
| 620 } | 582 } |
| 621 | 583 |
| 622 void QMVideoSettingsCallback::SetTargetFramerate(int frame_rate) { | 584 void QMVideoSettingsCallback::SetTargetFramerate(int frame_rate) { |
| 623 vp_->SetTargetFramerate(frame_rate); | 585 vp_->SetTargetFramerate(frame_rate); |
| 624 } | 586 } |
| 625 | 587 |
| 626 } // namespace webrtc | 588 } // namespace webrtc |
| OLD | NEW |