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/vie_channel.h" | |
12 | |
13 #include <algorithm> | |
14 #include <map> | |
15 #include <vector> | |
16 | |
17 #include "webrtc/base/checks.h" | |
18 #include "webrtc/base/logging.h" | |
19 #include "webrtc/common_video/include/frame_callback.h" | |
20 #include "webrtc/common_video/include/incoming_video_stream.h" | |
21 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" | |
22 #include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h" | |
23 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" | |
24 #include "webrtc/modules/video_coding/video_coding_impl.h" | |
25 #include "webrtc/modules/video_processing/include/video_processing.h" | |
26 #include "webrtc/system_wrappers/include/metrics.h" | |
27 #include "webrtc/video/call_stats.h" | |
28 #include "webrtc/video/payload_router.h" | |
29 #include "webrtc/video/receive_statistics_proxy.h" | |
30 | |
31 namespace webrtc { | |
32 | |
33 static const int kMaxPacketAgeToNack = 450; | |
34 static const int kMaxNackListSize = 250; | |
35 | |
36 // Helper class receiving statistics callbacks. | |
37 class ChannelStatsObserver : public CallStatsObserver { | |
38 public: | |
39 explicit ChannelStatsObserver(ViEChannel* owner) : owner_(owner) {} | |
40 virtual ~ChannelStatsObserver() {} | |
41 | |
42 // Implements StatsObserver. | |
43 virtual void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) { | |
44 owner_->OnRttUpdate(avg_rtt_ms, max_rtt_ms); | |
45 } | |
46 | |
47 private: | |
48 ViEChannel* const owner_; | |
49 }; | |
50 | |
51 ViEChannel::ViEChannel(vcm::VideoReceiver* video_receiver, | |
52 RtpStreamReceiver* rtp_stream_receiver) | |
53 : video_receiver_(video_receiver), | |
54 rtp_stream_receiver_(rtp_stream_receiver), | |
55 rtp_rtcp_(rtp_stream_receiver_->rtp_rtcp()), | |
56 stats_observer_(new ChannelStatsObserver(this)), | |
57 receive_stats_callback_(nullptr), | |
58 incoming_video_stream_(nullptr), | |
59 max_nack_reordering_threshold_(kMaxPacketAgeToNack), | |
60 pre_render_callback_(nullptr), | |
61 last_rtt_ms_(0) { | |
62 RTC_DCHECK(video_receiver_); | |
63 video_receiver_->SetNackSettings(kMaxNackListSize, | |
64 max_nack_reordering_threshold_, 0); | |
65 } | |
66 | |
67 ViEChannel::~ViEChannel() {} | |
68 | |
69 int32_t ViEChannel::Init() { | |
70 static const int kDefaultRenderDelayMs = 10; | |
71 | |
72 if (video_receiver_->RegisterReceiveCallback(this) != 0) { | |
73 return -1; | |
74 } | |
75 video_receiver_->RegisterFrameTypeCallback(this); | |
76 video_receiver_->RegisterReceiveStatisticsCallback(this); | |
77 video_receiver_->RegisterDecoderTimingCallback(this); | |
78 video_receiver_->SetRenderDelay(kDefaultRenderDelayMs); | |
79 | |
80 return 0; | |
81 } | |
82 | |
83 void ViEChannel::SetProtectionMode(bool enable_nack, | |
84 bool enable_fec, | |
85 int payload_type_red, | |
86 int payload_type_fec) { | |
87 // Validate payload types. If either RED or FEC payload types are set then | |
88 // both should be. If FEC is enabled then they both have to be set. | |
89 if (enable_fec || payload_type_red != -1 || payload_type_fec != -1) { | |
90 RTC_DCHECK_GE(payload_type_red, 0); | |
91 RTC_DCHECK_GE(payload_type_fec, 0); | |
92 RTC_DCHECK_LE(payload_type_red, 127); | |
93 RTC_DCHECK_LE(payload_type_fec, 127); | |
94 } else { | |
95 // Payload types unset. | |
96 RTC_DCHECK_EQ(payload_type_red, -1); | |
97 RTC_DCHECK_EQ(payload_type_fec, -1); | |
98 // Set to valid uint8_ts to be castable later without signed overflows. | |
99 payload_type_red = 0; | |
100 payload_type_fec = 0; | |
101 } | |
102 | |
103 VCMVideoProtection protection_method; | |
104 if (enable_nack) { | |
105 protection_method = enable_fec ? kProtectionNackFEC : kProtectionNack; | |
106 } else { | |
107 protection_method = kProtectionNone; | |
108 } | |
109 | |
110 video_receiver_->SetVideoProtection(protection_method, true); | |
111 | |
112 // Set NACK. | |
113 ProcessNACKRequest(enable_nack); | |
114 | |
115 // Set FEC. | |
116 rtp_rtcp_->SetGenericFECStatus(enable_fec, | |
117 static_cast<uint8_t>(payload_type_red), | |
118 static_cast<uint8_t>(payload_type_fec)); | |
119 } | |
120 | |
121 void ViEChannel::ProcessNACKRequest(const bool enable) { | |
122 if (enable) { | |
123 // Turn on NACK. | |
124 if (rtp_rtcp_->RTCP() == RtcpMode::kOff) | |
125 return; | |
126 rtp_stream_receiver_->SetNackStatus(true, max_nack_reordering_threshold_); | |
127 video_receiver_->RegisterPacketRequestCallback(this); | |
128 // Don't introduce errors when NACK is enabled. | |
129 video_receiver_->SetDecodeErrorMode(kNoErrors); | |
130 | |
131 } else { | |
132 video_receiver_->RegisterPacketRequestCallback(nullptr); | |
133 // When NACK is off, allow decoding with errors. Otherwise, the video | |
134 // will freeze, and will only recover with a complete key frame. | |
135 video_receiver_->SetDecodeErrorMode(kWithErrors); | |
136 rtp_stream_receiver_->SetNackStatus(false, max_nack_reordering_threshold_); | |
137 } | |
138 } | |
139 | |
140 int ViEChannel::GetRequiredNackListSize(int target_delay_ms) { | |
141 // The max size of the nack list should be large enough to accommodate the | |
142 // the number of packets (frames) resulting from the increased delay. | |
143 // Roughly estimating for ~40 packets per frame @ 30fps. | |
144 return target_delay_ms * 40 * 30 / 1000; | |
145 } | |
146 | |
147 RtpState ViEChannel::GetRtpStateForSsrc(uint32_t ssrc) const { | |
148 RTC_DCHECK(!rtp_rtcp_->Sending()); | |
149 RTC_DCHECK_EQ(ssrc, rtp_rtcp_->SSRC()); | |
150 return rtp_rtcp_->GetRtpState(); | |
151 } | |
152 | |
153 void ViEChannel::RegisterRtcpPacketTypeCounterObserver( | |
154 RtcpPacketTypeCounterObserver* observer) { | |
155 rtp_stream_receiver_->RegisterRtcpPacketTypeCounterObserver(observer); | |
156 } | |
157 | |
158 CallStatsObserver* ViEChannel::GetStatsObserver() { | |
159 return stats_observer_.get(); | |
160 } | |
161 | |
162 // Do not acquire the lock of |video_receiver_| in this function. Decode | |
163 // callback won't necessarily be called from the decoding thread. The decoding | |
164 // thread may have held the lock when calling VideoDecoder::Decode, Reset, or | |
165 // Release. Acquiring the same lock in the path of decode callback can deadlock. | |
166 int32_t ViEChannel::FrameToRender(VideoFrame& video_frame) { // NOLINT | |
167 rtc::CritScope lock(&crit_); | |
168 | |
169 if (pre_render_callback_) | |
170 pre_render_callback_->FrameCallback(&video_frame); | |
171 | |
172 incoming_video_stream_->OnFrame(video_frame); | |
173 return 0; | |
174 } | |
175 | |
176 int32_t ViEChannel::ReceivedDecodedReferenceFrame( | |
177 const uint64_t picture_id) { | |
178 return rtp_rtcp_->SendRTCPReferencePictureSelection(picture_id); | |
179 } | |
180 | |
181 void ViEChannel::OnIncomingPayloadType(int payload_type) { | |
182 rtc::CritScope lock(&crit_); | |
183 if (receive_stats_callback_) | |
184 receive_stats_callback_->OnIncomingPayloadType(payload_type); | |
185 } | |
186 | |
187 void ViEChannel::OnDecoderImplementationName(const char* implementation_name) { | |
188 rtc::CritScope lock(&crit_); | |
189 if (receive_stats_callback_) | |
190 receive_stats_callback_->OnDecoderImplementationName(implementation_name); | |
191 } | |
192 | |
193 void ViEChannel::OnReceiveRatesUpdated(uint32_t bit_rate, uint32_t frame_rate) { | |
194 rtc::CritScope lock(&crit_); | |
195 if (receive_stats_callback_) | |
196 receive_stats_callback_->OnIncomingRate(frame_rate, bit_rate); | |
197 } | |
198 | |
199 void ViEChannel::OnDiscardedPacketsUpdated(int discarded_packets) { | |
200 rtc::CritScope lock(&crit_); | |
201 if (receive_stats_callback_) | |
202 receive_stats_callback_->OnDiscardedPacketsUpdated(discarded_packets); | |
203 } | |
204 | |
205 void ViEChannel::OnFrameCountsUpdated(const FrameCounts& frame_counts) { | |
206 rtc::CritScope lock(&crit_); | |
207 receive_frame_counts_ = frame_counts; | |
208 if (receive_stats_callback_) | |
209 receive_stats_callback_->OnFrameCountsUpdated(frame_counts); | |
210 } | |
211 | |
212 void ViEChannel::OnDecoderTiming(int decode_ms, | |
213 int max_decode_ms, | |
214 int current_delay_ms, | |
215 int target_delay_ms, | |
216 int jitter_buffer_ms, | |
217 int min_playout_delay_ms, | |
218 int render_delay_ms) { | |
219 rtc::CritScope lock(&crit_); | |
220 if (!receive_stats_callback_) | |
221 return; | |
222 receive_stats_callback_->OnDecoderTiming( | |
223 decode_ms, max_decode_ms, current_delay_ms, target_delay_ms, | |
224 jitter_buffer_ms, min_playout_delay_ms, render_delay_ms, last_rtt_ms_); | |
225 } | |
226 | |
227 int32_t ViEChannel::RequestKeyFrame() { | |
228 return rtp_rtcp_->RequestKeyFrame(); | |
229 } | |
230 | |
231 int32_t ViEChannel::SliceLossIndicationRequest( | |
232 const uint64_t picture_id) { | |
233 return rtp_rtcp_->SendRTCPSliceLossIndication( | |
234 static_cast<uint8_t>(picture_id)); | |
235 } | |
236 | |
237 int32_t ViEChannel::ResendPackets(const uint16_t* sequence_numbers, | |
238 uint16_t length) { | |
239 return rtp_rtcp_->SendNACK(sequence_numbers, length); | |
240 } | |
241 | |
242 void ViEChannel::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) { | |
243 video_receiver_->SetReceiveChannelParameters(max_rtt_ms); | |
244 | |
245 rtc::CritScope lock(&crit_); | |
246 last_rtt_ms_ = avg_rtt_ms; | |
247 } | |
248 | |
249 void ViEChannel::RegisterPreRenderCallback( | |
250 I420FrameCallback* pre_render_callback) { | |
251 rtc::CritScope lock(&crit_); | |
252 pre_render_callback_ = pre_render_callback; | |
253 } | |
254 | |
255 void ViEChannel::RegisterReceiveStatisticsProxy( | |
256 ReceiveStatisticsProxy* receive_statistics_proxy) { | |
257 rtc::CritScope lock(&crit_); | |
258 receive_stats_callback_ = receive_statistics_proxy; | |
259 } | |
260 | |
261 void ViEChannel::SetIncomingVideoStream( | |
262 IncomingVideoStream* incoming_video_stream) { | |
263 rtc::CritScope lock(&crit_); | |
264 incoming_video_stream_ = incoming_video_stream; | |
265 } | |
266 } // namespace webrtc | |
OLD | NEW |