| 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/encoder_state_feedback.h" | 11 #include "webrtc/video/encoder_state_feedback.h" |
| 12 | 12 |
| 13 #include "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" |
| 14 #include "webrtc/video/vie_encoder.h" | 14 #include "webrtc/video/vie_encoder.h" |
| 15 | 15 |
| 16 static const int kMinKeyFrameRequestIntervalMs = 300; |
| 17 |
| 16 namespace webrtc { | 18 namespace webrtc { |
| 17 | 19 |
| 18 EncoderStateFeedback::EncoderStateFeedback() : vie_encoder_(nullptr) {} | 20 EncoderStateFeedback::EncoderStateFeedback(Clock* clock, |
| 19 | 21 const std::vector<uint32_t>& ssrcs, |
| 20 void EncoderStateFeedback::Init(const std::vector<uint32_t>& ssrcs, | 22 ViEEncoder* encoder) |
| 21 ViEEncoder* encoder) { | 23 : clock_(clock), |
| 24 ssrcs_(ssrcs), |
| 25 vie_encoder_(encoder), |
| 26 time_last_intra_request_ms_(ssrcs.size(), -1) { |
| 22 RTC_DCHECK(!ssrcs.empty()); | 27 RTC_DCHECK(!ssrcs.empty()); |
| 23 rtc::CritScope lock(&crit_); | |
| 24 ssrcs_ = ssrcs; | |
| 25 vie_encoder_ = encoder; | |
| 26 } | 28 } |
| 27 | 29 |
| 28 bool EncoderStateFeedback::HasSsrc(uint32_t ssrc) { | 30 bool EncoderStateFeedback::HasSsrc(uint32_t ssrc) { |
| 29 for (uint32_t registered_ssrc : ssrcs_) { | 31 for (uint32_t registered_ssrc : ssrcs_) { |
| 30 if (registered_ssrc == ssrc) | 32 if (registered_ssrc == ssrc) |
| 31 return true; | 33 return true; |
| 32 } | 34 } |
| 33 return false; | 35 return false; |
| 34 } | 36 } |
| 35 | 37 |
| 38 size_t EncoderStateFeedback::GetStreamIndex(uint32_t ssrc) { |
| 39 for (size_t i = 0; i < ssrcs_.size(); ++i) { |
| 40 if (ssrcs_[i] == ssrc) |
| 41 return i; |
| 42 } |
| 43 RTC_NOTREACHED() << "Unknown ssrc " << ssrc; |
| 44 return 0; |
| 45 } |
| 46 |
| 36 void EncoderStateFeedback::OnReceivedIntraFrameRequest(uint32_t ssrc) { | 47 void EncoderStateFeedback::OnReceivedIntraFrameRequest(uint32_t ssrc) { |
| 37 rtc::CritScope lock(&crit_); | |
| 38 if (!HasSsrc(ssrc)) | 48 if (!HasSsrc(ssrc)) |
| 39 return; | 49 return; |
| 40 RTC_DCHECK(vie_encoder_); | |
| 41 | 50 |
| 42 vie_encoder_->OnReceivedIntraFrameRequest(ssrc); | 51 size_t index = GetStreamIndex(ssrc); |
| 52 { |
| 53 int64_t now_ms = clock_->TimeInMilliseconds(); |
| 54 rtc::CritScope lock(&crit_); |
| 55 if (time_last_intra_request_ms_[index] + kMinKeyFrameRequestIntervalMs > |
| 56 now_ms) { |
| 57 return; |
| 58 } |
| 59 time_last_intra_request_ms_[index] = now_ms; |
| 60 } |
| 61 |
| 62 vie_encoder_->OnReceivedIntraFrameRequest(index); |
| 43 } | 63 } |
| 44 | 64 |
| 45 void EncoderStateFeedback::OnReceivedSLI(uint32_t ssrc, uint8_t picture_id) { | 65 void EncoderStateFeedback::OnReceivedSLI(uint32_t ssrc, uint8_t picture_id) { |
| 46 rtc::CritScope lock(&crit_); | |
| 47 if (!HasSsrc(ssrc)) | 66 if (!HasSsrc(ssrc)) |
| 48 return; | 67 return; |
| 49 RTC_DCHECK(vie_encoder_); | |
| 50 | 68 |
| 51 vie_encoder_->OnReceivedSLI(ssrc, picture_id); | 69 vie_encoder_->OnReceivedSLI(picture_id); |
| 52 } | 70 } |
| 53 | 71 |
| 54 void EncoderStateFeedback::OnReceivedRPSI(uint32_t ssrc, uint64_t picture_id) { | 72 void EncoderStateFeedback::OnReceivedRPSI(uint32_t ssrc, uint64_t picture_id) { |
| 55 rtc::CritScope lock(&crit_); | |
| 56 if (!HasSsrc(ssrc)) | 73 if (!HasSsrc(ssrc)) |
| 57 return; | 74 return; |
| 58 RTC_DCHECK(vie_encoder_); | |
| 59 | 75 |
| 60 vie_encoder_->OnReceivedRPSI(ssrc, picture_id); | 76 vie_encoder_->OnReceivedRPSI(picture_id); |
| 61 } | 77 } |
| 62 | 78 |
| 63 // Sending SSRCs for this encoder should never change since they are configured | 79 // Sending SSRCs for this encoder should never change since they are configured |
| 64 // once and not reconfigured. | 80 // once and not reconfigured, however, OnLocalSsrcChanged is called when the |
| 81 // RtpModules are created with a different SSRC than what will be used in the |
| 82 // end. |
| 83 // TODO(perkj): Can we make sure the RTP module is created with the right SSRC |
| 84 // from the beginning so this method is not triggered during creation ? |
| 65 void EncoderStateFeedback::OnLocalSsrcChanged(uint32_t old_ssrc, | 85 void EncoderStateFeedback::OnLocalSsrcChanged(uint32_t old_ssrc, |
| 66 uint32_t new_ssrc) { | 86 uint32_t new_ssrc) { |
| 67 if (!RTC_DCHECK_IS_ON) | 87 if (!RTC_DCHECK_IS_ON) |
| 68 return; | 88 return; |
| 69 rtc::CritScope lock(&crit_); | 89 |
| 70 if (ssrcs_.empty()) // Encoder not yet attached (or detached for teardown). | 90 if (old_ssrc == 0) // old_ssrc == 0 during creation. |
| 71 return; | 91 return; |
| 72 // SSRC shouldn't change to something we haven't already registered with the | 92 // SSRC shouldn't change to something we haven't already registered with the |
| 73 // encoder. | 93 // encoder. |
| 74 RTC_DCHECK(HasSsrc(new_ssrc)); | 94 RTC_DCHECK(HasSsrc(new_ssrc)); |
| 75 } | 95 } |
| 76 | 96 |
| 77 } // namespace webrtc | 97 } // namespace webrtc |
| OLD | NEW |