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 "video/encoder_rtcp_feedback.h"
12 
13 #include "rtc_base/checks.h"
14 #include "video/video_stream_encoder.h"
15 
16 static const int kMinKeyFrameRequestIntervalMs = 300;
17 
18 namespace webrtc {
19 
EncoderRtcpFeedback(Clock * clock,const std::vector<uint32_t> & ssrcs,VideoStreamEncoder * encoder)20 EncoderRtcpFeedback::EncoderRtcpFeedback(Clock* clock,
21                                          const std::vector<uint32_t>& ssrcs,
22                                          VideoStreamEncoder* encoder)
23     : clock_(clock),
24       ssrcs_(ssrcs),
25       video_stream_encoder_(encoder),
26       time_last_intra_request_ms_(ssrcs.size(), -1) {
27   RTC_DCHECK(!ssrcs.empty());
28 }
29 
HasSsrc(uint32_t ssrc)30 bool EncoderRtcpFeedback::HasSsrc(uint32_t ssrc) {
31   for (uint32_t registered_ssrc : ssrcs_) {
32     if (registered_ssrc == ssrc) {
33       return true;
34     }
35   }
36   return false;
37 }
38 
GetStreamIndex(uint32_t ssrc)39 size_t EncoderRtcpFeedback::GetStreamIndex(uint32_t ssrc) {
40   for (size_t i = 0; i < ssrcs_.size(); ++i) {
41     if (ssrcs_[i] == ssrc)
42       return i;
43   }
44   RTC_NOTREACHED() << "Unknown ssrc " << ssrc;
45   return 0;
46 }
47 
OnReceivedIntraFrameRequest(uint32_t ssrc)48 void EncoderRtcpFeedback::OnReceivedIntraFrameRequest(uint32_t ssrc) {
49   RTC_DCHECK(HasSsrc(ssrc));
50   size_t index = GetStreamIndex(ssrc);
51   {
52     // TODO(mflodman): Move to VideoStreamEncoder after some more changes making
53     // it easier to test there.
54     int64_t now_ms = clock_->TimeInMilliseconds();
55     rtc::CritScope lock(&crit_);
56     if (time_last_intra_request_ms_[index] + kMinKeyFrameRequestIntervalMs >
57         now_ms) {
58       return;
59     }
60     time_last_intra_request_ms_[index] = now_ms;
61   }
62 
63   video_stream_encoder_->OnReceivedIntraFrameRequest(index);
64 }
65 
66 }  // namespace webrtc
67