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/base/platform_thread.h"
20 #include "webrtc/common.h"
21 #include "webrtc/common_video/include/incoming_video_stream.h"
22 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
23 #include "webrtc/frame_callback.h"
24 #include "webrtc/modules/pacing/paced_sender.h"
25 #include "webrtc/modules/pacing/packet_router.h"
26 #include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
27 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
28 #include "webrtc/modules/utility/include/process_thread.h"
29 #include "webrtc/modules/video_coding/include/video_coding.h"
30 #include "webrtc/modules/video_processing/include/video_processing.h"
31 #include "webrtc/modules/video_render/video_render_defines.h"
32 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
33 #include "webrtc/system_wrappers/include/metrics.h"
34 #include "webrtc/video/call_stats.h"
35 #include "webrtc/video/payload_router.h"
36 #include "webrtc/video/receive_statistics_proxy.h"
37 #include "webrtc/video/report_block_stats.h"
38
39 namespace webrtc {
40
41 const int kMaxDecodeWaitTimeMs = 50;
42 static const int kMaxTargetDelayMs = 10000;
43 const int kMinSendSidePacketHistorySize = 600;
44 const int kMaxPacketAgeToNack = 450;
45 const int kMaxNackListSize = 250;
46
47 // Helper class receiving statistics callbacks.
48 class ChannelStatsObserver : public CallStatsObserver {
49 public:
ChannelStatsObserver(ViEChannel * owner)50 explicit ChannelStatsObserver(ViEChannel* owner) : owner_(owner) {}
~ChannelStatsObserver()51 virtual ~ChannelStatsObserver() {}
52
53 // Implements StatsObserver.
OnRttUpdate(int64_t avg_rtt_ms,int64_t max_rtt_ms)54 virtual void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) {
55 owner_->OnRttUpdate(avg_rtt_ms, max_rtt_ms);
56 }
57
58 private:
59 ViEChannel* const owner_;
60 };
61
62 class ViEChannelProtectionCallback : public VCMProtectionCallback {
63 public:
ViEChannelProtectionCallback(ViEChannel * owner)64 explicit ViEChannelProtectionCallback(ViEChannel* owner) : owner_(owner) {}
~ViEChannelProtectionCallback()65 ~ViEChannelProtectionCallback() {}
66
67
ProtectionRequest(const FecProtectionParams * delta_fec_params,const FecProtectionParams * key_fec_params,uint32_t * sent_video_rate_bps,uint32_t * sent_nack_rate_bps,uint32_t * sent_fec_rate_bps)68 int ProtectionRequest(
69 const FecProtectionParams* delta_fec_params,
70 const FecProtectionParams* key_fec_params,
71 uint32_t* sent_video_rate_bps,
72 uint32_t* sent_nack_rate_bps,
73 uint32_t* sent_fec_rate_bps) override {
74 return owner_->ProtectionRequest(delta_fec_params, key_fec_params,
75 sent_video_rate_bps, sent_nack_rate_bps,
76 sent_fec_rate_bps);
77 }
78 private:
79 ViEChannel* owner_;
80 };
81
ViEChannel(uint32_t number_of_cores,Transport * transport,ProcessThread * module_process_thread,RtcpIntraFrameObserver * intra_frame_observer,RtcpBandwidthObserver * bandwidth_observer,TransportFeedbackObserver * transport_feedback_observer,RemoteBitrateEstimator * remote_bitrate_estimator,RtcpRttStats * rtt_stats,PacedSender * paced_sender,PacketRouter * packet_router,size_t max_rtp_streams,bool sender)82 ViEChannel::ViEChannel(uint32_t number_of_cores,
83 Transport* transport,
84 ProcessThread* module_process_thread,
85 RtcpIntraFrameObserver* intra_frame_observer,
86 RtcpBandwidthObserver* bandwidth_observer,
87 TransportFeedbackObserver* transport_feedback_observer,
88 RemoteBitrateEstimator* remote_bitrate_estimator,
89 RtcpRttStats* rtt_stats,
90 PacedSender* paced_sender,
91 PacketRouter* packet_router,
92 size_t max_rtp_streams,
93 bool sender)
94 : number_of_cores_(number_of_cores),
95 sender_(sender),
96 module_process_thread_(module_process_thread),
97 crit_(CriticalSectionWrapper::CreateCriticalSection()),
98 send_payload_router_(new PayloadRouter()),
99 vcm_protection_callback_(new ViEChannelProtectionCallback(this)),
100 vcm_(VideoCodingModule::Create(Clock::GetRealTimeClock(),
101 nullptr,
102 nullptr)),
103 vie_receiver_(vcm_, remote_bitrate_estimator, this),
104 vie_sync_(vcm_),
105 stats_observer_(new ChannelStatsObserver(this)),
106 receive_stats_callback_(nullptr),
107 incoming_video_stream_(nullptr),
108 intra_frame_observer_(intra_frame_observer),
109 rtt_stats_(rtt_stats),
110 paced_sender_(paced_sender),
111 packet_router_(packet_router),
112 bandwidth_observer_(bandwidth_observer),
113 transport_feedback_observer_(transport_feedback_observer),
114 decode_thread_(ChannelDecodeThreadFunction, this, "DecodingThread"),
115 nack_history_size_sender_(kMinSendSidePacketHistorySize),
116 max_nack_reordering_threshold_(kMaxPacketAgeToNack),
117 pre_render_callback_(NULL),
118 report_block_stats_sender_(new ReportBlockStats()),
119 time_of_first_rtt_ms_(-1),
120 rtt_sum_ms_(0),
121 last_rtt_ms_(0),
122 num_rtts_(0),
123 rtp_rtcp_modules_(
124 CreateRtpRtcpModules(!sender,
125 vie_receiver_.GetReceiveStatistics(),
126 transport,
127 intra_frame_observer_,
128 bandwidth_observer_.get(),
129 transport_feedback_observer_,
130 rtt_stats_,
131 &rtcp_packet_type_counter_observer_,
132 remote_bitrate_estimator,
133 paced_sender_,
134 packet_router_,
135 &send_bitrate_observer_,
136 &send_frame_count_observer_,
137 &send_side_delay_observer_,
138 max_rtp_streams)),
139 num_active_rtp_rtcp_modules_(1) {
140 vie_receiver_.SetRtpRtcpModule(rtp_rtcp_modules_[0]);
141 vcm_->SetNackSettings(kMaxNackListSize, max_nack_reordering_threshold_, 0);
142 }
143
Init()144 int32_t ViEChannel::Init() {
145 static const int kDefaultRenderDelayMs = 10;
146 module_process_thread_->RegisterModule(vie_receiver_.GetReceiveStatistics());
147
148 // RTP/RTCP initialization.
149 module_process_thread_->RegisterModule(rtp_rtcp_modules_[0]);
150
151 rtp_rtcp_modules_[0]->SetKeyFrameRequestMethod(kKeyFrameReqPliRtcp);
152 if (paced_sender_) {
153 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_)
154 rtp_rtcp->SetStorePacketsStatus(true, nack_history_size_sender_);
155 }
156 packet_router_->AddRtpModule(rtp_rtcp_modules_[0]);
157 if (sender_) {
158 std::list<RtpRtcp*> send_rtp_modules(1, rtp_rtcp_modules_[0]);
159 send_payload_router_->SetSendingRtpModules(send_rtp_modules);
160 RTC_DCHECK(!send_payload_router_->active());
161 }
162 if (vcm_->RegisterReceiveCallback(this) != 0) {
163 return -1;
164 }
165 vcm_->RegisterFrameTypeCallback(this);
166 vcm_->RegisterReceiveStatisticsCallback(this);
167 vcm_->RegisterDecoderTimingCallback(this);
168 vcm_->SetRenderDelay(kDefaultRenderDelayMs);
169
170 module_process_thread_->RegisterModule(vcm_);
171 module_process_thread_->RegisterModule(&vie_sync_);
172
173 return 0;
174 }
175
~ViEChannel()176 ViEChannel::~ViEChannel() {
177 UpdateHistograms();
178 // Make sure we don't get more callbacks from the RTP module.
179 module_process_thread_->DeRegisterModule(
180 vie_receiver_.GetReceiveStatistics());
181 module_process_thread_->DeRegisterModule(vcm_);
182 module_process_thread_->DeRegisterModule(&vie_sync_);
183 send_payload_router_->SetSendingRtpModules(std::list<RtpRtcp*>());
184 for (size_t i = 0; i < num_active_rtp_rtcp_modules_; ++i)
185 packet_router_->RemoveRtpModule(rtp_rtcp_modules_[i]);
186 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) {
187 module_process_thread_->DeRegisterModule(rtp_rtcp);
188 delete rtp_rtcp;
189 }
190 if (!sender_)
191 StopDecodeThread();
192 // Release modules.
193 VideoCodingModule::Destroy(vcm_);
194 }
195
UpdateHistograms()196 void ViEChannel::UpdateHistograms() {
197 int64_t now = Clock::GetRealTimeClock()->TimeInMilliseconds();
198
199 {
200 CriticalSectionScoped cs(crit_.get());
201 int64_t elapsed_sec = (now - time_of_first_rtt_ms_) / 1000;
202 if (time_of_first_rtt_ms_ != -1 && num_rtts_ > 0 &&
203 elapsed_sec > metrics::kMinRunTimeInSeconds) {
204 int64_t avg_rtt_ms = (rtt_sum_ms_ + num_rtts_ / 2) / num_rtts_;
205 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
206 "WebRTC.Video.AverageRoundTripTimeInMilliseconds", avg_rtt_ms);
207 }
208 }
209
210 if (sender_) {
211 RtcpPacketTypeCounter rtcp_counter;
212 GetSendRtcpPacketTypeCounter(&rtcp_counter);
213 int64_t elapsed_sec = rtcp_counter.TimeSinceFirstPacketInMs(now) / 1000;
214 if (elapsed_sec > metrics::kMinRunTimeInSeconds) {
215 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
216 "WebRTC.Video.NackPacketsReceivedPerMinute",
217 rtcp_counter.nack_packets * 60 / elapsed_sec);
218 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
219 "WebRTC.Video.FirPacketsReceivedPerMinute",
220 rtcp_counter.fir_packets * 60 / elapsed_sec);
221 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
222 "WebRTC.Video.PliPacketsReceivedPerMinute",
223 rtcp_counter.pli_packets * 60 / elapsed_sec);
224 if (rtcp_counter.nack_requests > 0) {
225 RTC_HISTOGRAM_PERCENTAGE_SPARSE(
226 "WebRTC.Video.UniqueNackRequestsReceivedInPercent",
227 rtcp_counter.UniqueNackRequestsInPercent());
228 }
229 int fraction_lost = report_block_stats_sender_->FractionLostInPercent();
230 if (fraction_lost != -1) {
231 RTC_HISTOGRAM_PERCENTAGE_SPARSE("WebRTC.Video.SentPacketsLostInPercent",
232 fraction_lost);
233 }
234 }
235
236 StreamDataCounters rtp;
237 StreamDataCounters rtx;
238 GetSendStreamDataCounters(&rtp, &rtx);
239 StreamDataCounters rtp_rtx = rtp;
240 rtp_rtx.Add(rtx);
241 elapsed_sec = rtp_rtx.TimeSinceFirstPacketInMs(
242 Clock::GetRealTimeClock()->TimeInMilliseconds()) /
243 1000;
244 if (elapsed_sec > metrics::kMinRunTimeInSeconds) {
245 RTC_HISTOGRAM_COUNTS_SPARSE_100000(
246 "WebRTC.Video.BitrateSentInKbps",
247 static_cast<int>(rtp_rtx.transmitted.TotalBytes() * 8 / elapsed_sec /
248 1000));
249 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
250 "WebRTC.Video.MediaBitrateSentInKbps",
251 static_cast<int>(rtp.MediaPayloadBytes() * 8 / elapsed_sec / 1000));
252 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
253 "WebRTC.Video.PaddingBitrateSentInKbps",
254 static_cast<int>(rtp_rtx.transmitted.padding_bytes * 8 / elapsed_sec /
255 1000));
256 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
257 "WebRTC.Video.RetransmittedBitrateSentInKbps",
258 static_cast<int>(rtp_rtx.retransmitted.TotalBytes() * 8 /
259 elapsed_sec / 1000));
260 if (rtp_rtcp_modules_[0]->RtxSendStatus() != kRtxOff) {
261 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
262 "WebRTC.Video.RtxBitrateSentInKbps",
263 static_cast<int>(rtx.transmitted.TotalBytes() * 8 / elapsed_sec /
264 1000));
265 }
266 bool fec_enabled = false;
267 uint8_t pltype_red;
268 uint8_t pltype_fec;
269 rtp_rtcp_modules_[0]->GenericFECStatus(&fec_enabled, &pltype_red,
270 &pltype_fec);
271 if (fec_enabled) {
272 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
273 "WebRTC.Video.FecBitrateSentInKbps",
274 static_cast<int>(rtp_rtx.fec.TotalBytes() * 8 / elapsed_sec /
275 1000));
276 }
277 }
278 } else if (vie_receiver_.GetRemoteSsrc() > 0) {
279 // Get receive stats if we are receiving packets, i.e. there is a remote
280 // ssrc.
281 RtcpPacketTypeCounter rtcp_counter;
282 GetReceiveRtcpPacketTypeCounter(&rtcp_counter);
283 int64_t elapsed_sec = rtcp_counter.TimeSinceFirstPacketInMs(now) / 1000;
284 if (elapsed_sec > metrics::kMinRunTimeInSeconds) {
285 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
286 "WebRTC.Video.NackPacketsSentPerMinute",
287 rtcp_counter.nack_packets * 60 / elapsed_sec);
288 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
289 "WebRTC.Video.FirPacketsSentPerMinute",
290 rtcp_counter.fir_packets * 60 / elapsed_sec);
291 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
292 "WebRTC.Video.PliPacketsSentPerMinute",
293 rtcp_counter.pli_packets * 60 / elapsed_sec);
294 if (rtcp_counter.nack_requests > 0) {
295 RTC_HISTOGRAM_PERCENTAGE_SPARSE(
296 "WebRTC.Video.UniqueNackRequestsSentInPercent",
297 rtcp_counter.UniqueNackRequestsInPercent());
298 }
299 }
300
301 StreamDataCounters rtp;
302 StreamDataCounters rtx;
303 GetReceiveStreamDataCounters(&rtp, &rtx);
304 StreamDataCounters rtp_rtx = rtp;
305 rtp_rtx.Add(rtx);
306 elapsed_sec = rtp_rtx.TimeSinceFirstPacketInMs(now) / 1000;
307 if (elapsed_sec > metrics::kMinRunTimeInSeconds) {
308 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
309 "WebRTC.Video.BitrateReceivedInKbps",
310 static_cast<int>(rtp_rtx.transmitted.TotalBytes() * 8 / elapsed_sec /
311 1000));
312 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
313 "WebRTC.Video.MediaBitrateReceivedInKbps",
314 static_cast<int>(rtp.MediaPayloadBytes() * 8 / elapsed_sec / 1000));
315 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
316 "WebRTC.Video.PaddingBitrateReceivedInKbps",
317 static_cast<int>(rtp_rtx.transmitted.padding_bytes * 8 / elapsed_sec /
318 1000));
319 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
320 "WebRTC.Video.RetransmittedBitrateReceivedInKbps",
321 static_cast<int>(rtp_rtx.retransmitted.TotalBytes() * 8 /
322 elapsed_sec / 1000));
323 uint32_t ssrc = 0;
324 if (vie_receiver_.GetRtxSsrc(&ssrc)) {
325 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
326 "WebRTC.Video.RtxBitrateReceivedInKbps",
327 static_cast<int>(rtx.transmitted.TotalBytes() * 8 / elapsed_sec /
328 1000));
329 }
330 if (vie_receiver_.IsFecEnabled()) {
331 RTC_HISTOGRAM_COUNTS_SPARSE_10000(
332 "WebRTC.Video.FecBitrateReceivedInKbps",
333 static_cast<int>(rtp_rtx.fec.TotalBytes() * 8 / elapsed_sec /
334 1000));
335 }
336 }
337 }
338 }
339
SetSendCodec(const VideoCodec & video_codec,bool new_stream)340 int32_t ViEChannel::SetSendCodec(const VideoCodec& video_codec,
341 bool new_stream) {
342 RTC_DCHECK(sender_);
343 if (video_codec.codecType == kVideoCodecRED ||
344 video_codec.codecType == kVideoCodecULPFEC) {
345 LOG_F(LS_ERROR) << "Not a valid send codec " << video_codec.codecType;
346 return -1;
347 }
348 if (kMaxSimulcastStreams < video_codec.numberOfSimulcastStreams) {
349 LOG_F(LS_ERROR) << "Incorrect config "
350 << video_codec.numberOfSimulcastStreams;
351 return -1;
352 }
353 // Update the RTP module with the settings.
354 // Stop and Start the RTP module -> trigger new SSRC, if an SSRC hasn't been
355 // set explicitly.
356 // The first layer is always active, so the first module can be checked for
357 // sending status.
358 bool is_sending = rtp_rtcp_modules_[0]->Sending();
359 bool router_was_active = send_payload_router_->active();
360 send_payload_router_->set_active(false);
361 send_payload_router_->SetSendingRtpModules(std::list<RtpRtcp*>());
362
363 std::vector<RtpRtcp*> registered_modules;
364 std::vector<RtpRtcp*> deregistered_modules;
365 size_t num_active_modules = video_codec.numberOfSimulcastStreams > 0
366 ? video_codec.numberOfSimulcastStreams
367 : 1;
368 size_t num_prev_active_modules;
369 {
370 // Cache which modules are active so StartSend can know which ones to start.
371 CriticalSectionScoped cs(crit_.get());
372 num_prev_active_modules = num_active_rtp_rtcp_modules_;
373 num_active_rtp_rtcp_modules_ = num_active_modules;
374 }
375 for (size_t i = 0; i < num_active_modules; ++i)
376 registered_modules.push_back(rtp_rtcp_modules_[i]);
377
378 for (size_t i = num_active_modules; i < rtp_rtcp_modules_.size(); ++i)
379 deregistered_modules.push_back(rtp_rtcp_modules_[i]);
380
381 // Disable inactive modules.
382 for (RtpRtcp* rtp_rtcp : deregistered_modules) {
383 rtp_rtcp->SetSendingStatus(false);
384 rtp_rtcp->SetSendingMediaStatus(false);
385 }
386
387 // Configure active modules.
388 for (RtpRtcp* rtp_rtcp : registered_modules) {
389 rtp_rtcp->DeRegisterSendPayload(video_codec.plType);
390 if (rtp_rtcp->RegisterSendPayload(video_codec) != 0) {
391 return -1;
392 }
393 rtp_rtcp->SetSendingStatus(is_sending);
394 rtp_rtcp->SetSendingMediaStatus(is_sending);
395 }
396
397 // |RegisterSimulcastRtpRtcpModules| resets all old weak pointers and old
398 // modules can be deleted after this step.
399 vie_receiver_.RegisterRtpRtcpModules(registered_modules);
400
401 // Update the packet and payload routers with the sending RtpRtcp modules.
402 if (sender_) {
403 std::list<RtpRtcp*> active_send_modules;
404 for (RtpRtcp* rtp_rtcp : registered_modules)
405 active_send_modules.push_back(rtp_rtcp);
406 send_payload_router_->SetSendingRtpModules(active_send_modules);
407 }
408
409 if (router_was_active)
410 send_payload_router_->set_active(true);
411
412 // Deregister previously registered modules.
413 for (size_t i = num_active_modules; i < num_prev_active_modules; ++i) {
414 module_process_thread_->DeRegisterModule(rtp_rtcp_modules_[i]);
415 packet_router_->RemoveRtpModule(rtp_rtcp_modules_[i]);
416 }
417 // Register new active modules.
418 for (size_t i = num_prev_active_modules; i < num_active_modules; ++i) {
419 module_process_thread_->RegisterModule(rtp_rtcp_modules_[i]);
420 packet_router_->AddRtpModule(rtp_rtcp_modules_[i]);
421 }
422 return 0;
423 }
424
SetReceiveCodec(const VideoCodec & video_codec)425 int32_t ViEChannel::SetReceiveCodec(const VideoCodec& video_codec) {
426 RTC_DCHECK(!sender_);
427 if (!vie_receiver_.SetReceiveCodec(video_codec)) {
428 return -1;
429 }
430
431 if (video_codec.codecType != kVideoCodecRED &&
432 video_codec.codecType != kVideoCodecULPFEC) {
433 // Register codec type with VCM, but do not register RED or ULPFEC.
434 if (vcm_->RegisterReceiveCodec(&video_codec, number_of_cores_, false) !=
435 VCM_OK) {
436 return -1;
437 }
438 }
439 return 0;
440 }
441
RegisterExternalDecoder(const uint8_t pl_type,VideoDecoder * decoder)442 void ViEChannel::RegisterExternalDecoder(const uint8_t pl_type,
443 VideoDecoder* decoder) {
444 RTC_DCHECK(!sender_);
445 vcm_->RegisterExternalDecoder(decoder, pl_type);
446 }
447
ReceiveCodecStatistics(uint32_t * num_key_frames,uint32_t * num_delta_frames)448 int32_t ViEChannel::ReceiveCodecStatistics(uint32_t* num_key_frames,
449 uint32_t* num_delta_frames) {
450 CriticalSectionScoped cs(crit_.get());
451 *num_key_frames = receive_frame_counts_.key_frames;
452 *num_delta_frames = receive_frame_counts_.delta_frames;
453 return 0;
454 }
455
DiscardedPackets() const456 uint32_t ViEChannel::DiscardedPackets() const {
457 return vcm_->DiscardedPackets();
458 }
459
ReceiveDelay() const460 int ViEChannel::ReceiveDelay() const {
461 return vcm_->Delay();
462 }
463
SetExpectedRenderDelay(int delay_ms)464 void ViEChannel::SetExpectedRenderDelay(int delay_ms) {
465 vcm_->SetRenderDelay(delay_ms);
466 }
467
SetRTCPMode(const RtcpMode rtcp_mode)468 void ViEChannel::SetRTCPMode(const RtcpMode rtcp_mode) {
469 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_)
470 rtp_rtcp->SetRTCPStatus(rtcp_mode);
471 }
472
SetProtectionMode(bool enable_nack,bool enable_fec,int payload_type_red,int payload_type_fec)473 void ViEChannel::SetProtectionMode(bool enable_nack,
474 bool enable_fec,
475 int payload_type_red,
476 int payload_type_fec) {
477 // Validate payload types.
478 if (enable_fec) {
479 RTC_DCHECK_GE(payload_type_red, 0);
480 RTC_DCHECK_GE(payload_type_fec, 0);
481 RTC_DCHECK_LE(payload_type_red, 127);
482 RTC_DCHECK_LE(payload_type_fec, 127);
483 } else {
484 RTC_DCHECK_EQ(payload_type_red, -1);
485 RTC_DCHECK_EQ(payload_type_fec, -1);
486 // Set to valid uint8_ts to be castable later without signed overflows.
487 payload_type_red = 0;
488 payload_type_fec = 0;
489 }
490
491 VCMVideoProtection protection_method;
492 if (enable_nack) {
493 protection_method = enable_fec ? kProtectionNackFEC : kProtectionNack;
494 } else {
495 protection_method = kProtectionNone;
496 }
497
498 vcm_->SetVideoProtection(protection_method, true);
499
500 // Set NACK.
501 ProcessNACKRequest(enable_nack);
502
503 // Set FEC.
504 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) {
505 rtp_rtcp->SetGenericFECStatus(enable_fec,
506 static_cast<uint8_t>(payload_type_red),
507 static_cast<uint8_t>(payload_type_fec));
508 }
509 }
510
ProcessNACKRequest(const bool enable)511 void ViEChannel::ProcessNACKRequest(const bool enable) {
512 if (enable) {
513 // Turn on NACK.
514 if (rtp_rtcp_modules_[0]->RTCP() == RtcpMode::kOff)
515 return;
516 vie_receiver_.SetNackStatus(true, max_nack_reordering_threshold_);
517
518 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_)
519 rtp_rtcp->SetStorePacketsStatus(true, nack_history_size_sender_);
520
521 vcm_->RegisterPacketRequestCallback(this);
522 // Don't introduce errors when NACK is enabled.
523 vcm_->SetDecodeErrorMode(kNoErrors);
524 } else {
525 vcm_->RegisterPacketRequestCallback(NULL);
526 if (paced_sender_ == nullptr) {
527 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_)
528 rtp_rtcp->SetStorePacketsStatus(false, 0);
529 }
530 vie_receiver_.SetNackStatus(false, max_nack_reordering_threshold_);
531 // When NACK is off, allow decoding with errors. Otherwise, the video
532 // will freeze, and will only recover with a complete key frame.
533 vcm_->SetDecodeErrorMode(kWithErrors);
534 }
535 }
536
IsSendingFecEnabled()537 bool ViEChannel::IsSendingFecEnabled() {
538 bool fec_enabled = false;
539 uint8_t pltype_red = 0;
540 uint8_t pltype_fec = 0;
541
542 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) {
543 rtp_rtcp->GenericFECStatus(&fec_enabled, &pltype_red, &pltype_fec);
544 if (fec_enabled)
545 return true;
546 }
547 return false;
548 }
549
SetSenderBufferingMode(int target_delay_ms)550 int ViEChannel::SetSenderBufferingMode(int target_delay_ms) {
551 if ((target_delay_ms < 0) || (target_delay_ms > kMaxTargetDelayMs)) {
552 LOG(LS_ERROR) << "Invalid send buffer value.";
553 return -1;
554 }
555 if (target_delay_ms == 0) {
556 // Real-time mode.
557 nack_history_size_sender_ = kMinSendSidePacketHistorySize;
558 } else {
559 nack_history_size_sender_ = GetRequiredNackListSize(target_delay_ms);
560 // Don't allow a number lower than the default value.
561 if (nack_history_size_sender_ < kMinSendSidePacketHistorySize) {
562 nack_history_size_sender_ = kMinSendSidePacketHistorySize;
563 }
564 }
565 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_)
566 rtp_rtcp->SetStorePacketsStatus(true, nack_history_size_sender_);
567 return 0;
568 }
569
GetRequiredNackListSize(int target_delay_ms)570 int ViEChannel::GetRequiredNackListSize(int target_delay_ms) {
571 // The max size of the nack list should be large enough to accommodate the
572 // the number of packets (frames) resulting from the increased delay.
573 // Roughly estimating for ~40 packets per frame @ 30fps.
574 return target_delay_ms * 40 * 30 / 1000;
575 }
576
SetSendTimestampOffsetStatus(bool enable,int id)577 int ViEChannel::SetSendTimestampOffsetStatus(bool enable, int id) {
578 // Disable any previous registrations of this extension to avoid errors.
579 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) {
580 rtp_rtcp->DeregisterSendRtpHeaderExtension(
581 kRtpExtensionTransmissionTimeOffset);
582 }
583 if (!enable)
584 return 0;
585 // Enable the extension.
586 int error = 0;
587 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) {
588 error |= rtp_rtcp->RegisterSendRtpHeaderExtension(
589 kRtpExtensionTransmissionTimeOffset, id);
590 }
591 return error;
592 }
593
SetReceiveTimestampOffsetStatus(bool enable,int id)594 int ViEChannel::SetReceiveTimestampOffsetStatus(bool enable, int id) {
595 return vie_receiver_.SetReceiveTimestampOffsetStatus(enable, id) ? 0 : -1;
596 }
597
SetSendAbsoluteSendTimeStatus(bool enable,int id)598 int ViEChannel::SetSendAbsoluteSendTimeStatus(bool enable, int id) {
599 // Disable any previous registrations of this extension to avoid errors.
600 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_)
601 rtp_rtcp->DeregisterSendRtpHeaderExtension(kRtpExtensionAbsoluteSendTime);
602 if (!enable)
603 return 0;
604 // Enable the extension.
605 int error = 0;
606 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) {
607 error |= rtp_rtcp->RegisterSendRtpHeaderExtension(
608 kRtpExtensionAbsoluteSendTime, id);
609 }
610 return error;
611 }
612
SetReceiveAbsoluteSendTimeStatus(bool enable,int id)613 int ViEChannel::SetReceiveAbsoluteSendTimeStatus(bool enable, int id) {
614 return vie_receiver_.SetReceiveAbsoluteSendTimeStatus(enable, id) ? 0 : -1;
615 }
616
SetSendVideoRotationStatus(bool enable,int id)617 int ViEChannel::SetSendVideoRotationStatus(bool enable, int id) {
618 // Disable any previous registrations of this extension to avoid errors.
619 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_)
620 rtp_rtcp->DeregisterSendRtpHeaderExtension(kRtpExtensionVideoRotation);
621 if (!enable)
622 return 0;
623 // Enable the extension.
624 int error = 0;
625 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) {
626 error |= rtp_rtcp->RegisterSendRtpHeaderExtension(
627 kRtpExtensionVideoRotation, id);
628 }
629 return error;
630 }
631
SetReceiveVideoRotationStatus(bool enable,int id)632 int ViEChannel::SetReceiveVideoRotationStatus(bool enable, int id) {
633 return vie_receiver_.SetReceiveVideoRotationStatus(enable, id) ? 0 : -1;
634 }
635
SetSendTransportSequenceNumber(bool enable,int id)636 int ViEChannel::SetSendTransportSequenceNumber(bool enable, int id) {
637 // Disable any previous registrations of this extension to avoid errors.
638 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) {
639 rtp_rtcp->DeregisterSendRtpHeaderExtension(
640 kRtpExtensionTransportSequenceNumber);
641 }
642 if (!enable)
643 return 0;
644 // Enable the extension.
645 int error = 0;
646 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) {
647 error |= rtp_rtcp->RegisterSendRtpHeaderExtension(
648 kRtpExtensionTransportSequenceNumber, id);
649 }
650 return error;
651 }
652
SetReceiveTransportSequenceNumber(bool enable,int id)653 int ViEChannel::SetReceiveTransportSequenceNumber(bool enable, int id) {
654 return vie_receiver_.SetReceiveTransportSequenceNumber(enable, id) ? 0 : -1;
655 }
656
SetRtcpXrRrtrStatus(bool enable)657 void ViEChannel::SetRtcpXrRrtrStatus(bool enable) {
658 rtp_rtcp_modules_[0]->SetRtcpXrRrtrStatus(enable);
659 }
660
EnableTMMBR(bool enable)661 void ViEChannel::EnableTMMBR(bool enable) {
662 rtp_rtcp_modules_[0]->SetTMMBRStatus(enable);
663 }
664
SetSSRC(const uint32_t SSRC,const StreamType usage,const uint8_t simulcast_idx)665 int32_t ViEChannel::SetSSRC(const uint32_t SSRC,
666 const StreamType usage,
667 const uint8_t simulcast_idx) {
668 RtpRtcp* rtp_rtcp = rtp_rtcp_modules_[simulcast_idx];
669 if (usage == kViEStreamTypeRtx) {
670 rtp_rtcp->SetRtxSsrc(SSRC);
671 } else {
672 rtp_rtcp->SetSSRC(SSRC);
673 }
674 return 0;
675 }
676
SetRemoteSSRCType(const StreamType usage,const uint32_t SSRC)677 int32_t ViEChannel::SetRemoteSSRCType(const StreamType usage,
678 const uint32_t SSRC) {
679 vie_receiver_.SetRtxSsrc(SSRC);
680 return 0;
681 }
682
GetLocalSSRC(uint8_t idx,unsigned int * ssrc)683 int32_t ViEChannel::GetLocalSSRC(uint8_t idx, unsigned int* ssrc) {
684 RTC_DCHECK_LE(idx, rtp_rtcp_modules_.size());
685 *ssrc = rtp_rtcp_modules_[idx]->SSRC();
686 return 0;
687 }
688
GetRemoteSSRC()689 uint32_t ViEChannel::GetRemoteSSRC() {
690 return vie_receiver_.GetRemoteSsrc();
691 }
692
SetRtxSendPayloadType(int payload_type,int associated_payload_type)693 int ViEChannel::SetRtxSendPayloadType(int payload_type,
694 int associated_payload_type) {
695 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_)
696 rtp_rtcp->SetRtxSendPayloadType(payload_type, associated_payload_type);
697 SetRtxSendStatus(true);
698 return 0;
699 }
700
SetRtxSendStatus(bool enable)701 void ViEChannel::SetRtxSendStatus(bool enable) {
702 int rtx_settings =
703 enable ? kRtxRetransmitted | kRtxRedundantPayloads : kRtxOff;
704 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_)
705 rtp_rtcp->SetRtxSendStatus(rtx_settings);
706 }
707
SetRtxReceivePayloadType(int payload_type,int associated_payload_type)708 void ViEChannel::SetRtxReceivePayloadType(int payload_type,
709 int associated_payload_type) {
710 vie_receiver_.SetRtxPayloadType(payload_type, associated_payload_type);
711 }
712
SetUseRtxPayloadMappingOnRestore(bool val)713 void ViEChannel::SetUseRtxPayloadMappingOnRestore(bool val) {
714 vie_receiver_.SetUseRtxPayloadMappingOnRestore(val);
715 }
716
SetRtpStateForSsrc(uint32_t ssrc,const RtpState & rtp_state)717 void ViEChannel::SetRtpStateForSsrc(uint32_t ssrc, const RtpState& rtp_state) {
718 RTC_DCHECK(!rtp_rtcp_modules_[0]->Sending());
719 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) {
720 if (rtp_rtcp->SetRtpStateForSsrc(ssrc, rtp_state))
721 return;
722 }
723 }
724
GetRtpStateForSsrc(uint32_t ssrc)725 RtpState ViEChannel::GetRtpStateForSsrc(uint32_t ssrc) {
726 RTC_DCHECK(!rtp_rtcp_modules_[0]->Sending());
727 RtpState rtp_state;
728 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) {
729 if (rtp_rtcp->GetRtpStateForSsrc(ssrc, &rtp_state))
730 return rtp_state;
731 }
732 LOG(LS_ERROR) << "Couldn't get RTP state for ssrc: " << ssrc;
733 return rtp_state;
734 }
735
736 // TODO(pbos): Set CNAME on all modules.
SetRTCPCName(const char * rtcp_cname)737 int32_t ViEChannel::SetRTCPCName(const char* rtcp_cname) {
738 RTC_DCHECK(!rtp_rtcp_modules_[0]->Sending());
739 return rtp_rtcp_modules_[0]->SetCNAME(rtcp_cname);
740 }
741
GetRemoteRTCPCName(char rtcp_cname[])742 int32_t ViEChannel::GetRemoteRTCPCName(char rtcp_cname[]) {
743 uint32_t remoteSSRC = vie_receiver_.GetRemoteSsrc();
744 return rtp_rtcp_modules_[0]->RemoteCNAME(remoteSSRC, rtcp_cname);
745 }
746
GetSendRtcpStatistics(uint16_t * fraction_lost,uint32_t * cumulative_lost,uint32_t * extended_max,uint32_t * jitter_samples,int64_t * rtt_ms)747 int32_t ViEChannel::GetSendRtcpStatistics(uint16_t* fraction_lost,
748 uint32_t* cumulative_lost,
749 uint32_t* extended_max,
750 uint32_t* jitter_samples,
751 int64_t* rtt_ms) {
752 // Aggregate the report blocks associated with streams sent on this channel.
753 std::vector<RTCPReportBlock> report_blocks;
754 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_)
755 rtp_rtcp->RemoteRTCPStat(&report_blocks);
756
757 if (report_blocks.empty())
758 return -1;
759
760 uint32_t remote_ssrc = vie_receiver_.GetRemoteSsrc();
761 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
762 for (; it != report_blocks.end(); ++it) {
763 if (it->remoteSSRC == remote_ssrc)
764 break;
765 }
766 if (it == report_blocks.end()) {
767 // We have not received packets with an SSRC matching the report blocks. To
768 // have a chance of calculating an RTT we will try with the SSRC of the
769 // first report block received.
770 // This is very important for send-only channels where we don't know the
771 // SSRC of the other end.
772 remote_ssrc = report_blocks[0].remoteSSRC;
773 }
774
775 // TODO(asapersson): Change report_block_stats to not rely on
776 // GetSendRtcpStatistics to be called.
777 RTCPReportBlock report =
778 report_block_stats_sender_->AggregateAndStore(report_blocks);
779 *fraction_lost = report.fractionLost;
780 *cumulative_lost = report.cumulativeLost;
781 *extended_max = report.extendedHighSeqNum;
782 *jitter_samples = report.jitter;
783
784 int64_t dummy;
785 int64_t rtt = 0;
786 if (rtp_rtcp_modules_[0]->RTT(remote_ssrc, &rtt, &dummy, &dummy, &dummy) !=
787 0) {
788 return -1;
789 }
790 *rtt_ms = rtt;
791 return 0;
792 }
793
RegisterSendChannelRtcpStatisticsCallback(RtcpStatisticsCallback * callback)794 void ViEChannel::RegisterSendChannelRtcpStatisticsCallback(
795 RtcpStatisticsCallback* callback) {
796 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_)
797 rtp_rtcp->RegisterRtcpStatisticsCallback(callback);
798 }
799
RegisterReceiveChannelRtcpStatisticsCallback(RtcpStatisticsCallback * callback)800 void ViEChannel::RegisterReceiveChannelRtcpStatisticsCallback(
801 RtcpStatisticsCallback* callback) {
802 vie_receiver_.GetReceiveStatistics()->RegisterRtcpStatisticsCallback(
803 callback);
804 rtp_rtcp_modules_[0]->RegisterRtcpStatisticsCallback(callback);
805 }
806
RegisterRtcpPacketTypeCounterObserver(RtcpPacketTypeCounterObserver * observer)807 void ViEChannel::RegisterRtcpPacketTypeCounterObserver(
808 RtcpPacketTypeCounterObserver* observer) {
809 rtcp_packet_type_counter_observer_.Set(observer);
810 }
811
GetSendStreamDataCounters(StreamDataCounters * rtp_counters,StreamDataCounters * rtx_counters) const812 void ViEChannel::GetSendStreamDataCounters(
813 StreamDataCounters* rtp_counters,
814 StreamDataCounters* rtx_counters) const {
815 *rtp_counters = StreamDataCounters();
816 *rtx_counters = StreamDataCounters();
817 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) {
818 StreamDataCounters rtp_data;
819 StreamDataCounters rtx_data;
820 rtp_rtcp->GetSendStreamDataCounters(&rtp_data, &rtx_data);
821 rtp_counters->Add(rtp_data);
822 rtx_counters->Add(rtx_data);
823 }
824 }
825
GetReceiveStreamDataCounters(StreamDataCounters * rtp_counters,StreamDataCounters * rtx_counters) const826 void ViEChannel::GetReceiveStreamDataCounters(
827 StreamDataCounters* rtp_counters,
828 StreamDataCounters* rtx_counters) const {
829 StreamStatistician* statistician = vie_receiver_.GetReceiveStatistics()->
830 GetStatistician(vie_receiver_.GetRemoteSsrc());
831 if (statistician) {
832 statistician->GetReceiveStreamDataCounters(rtp_counters);
833 }
834 uint32_t rtx_ssrc = 0;
835 if (vie_receiver_.GetRtxSsrc(&rtx_ssrc)) {
836 StreamStatistician* statistician =
837 vie_receiver_.GetReceiveStatistics()->GetStatistician(rtx_ssrc);
838 if (statistician) {
839 statistician->GetReceiveStreamDataCounters(rtx_counters);
840 }
841 }
842 }
843
RegisterSendChannelRtpStatisticsCallback(StreamDataCountersCallback * callback)844 void ViEChannel::RegisterSendChannelRtpStatisticsCallback(
845 StreamDataCountersCallback* callback) {
846 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_)
847 rtp_rtcp->RegisterSendChannelRtpStatisticsCallback(callback);
848 }
849
RegisterReceiveChannelRtpStatisticsCallback(StreamDataCountersCallback * callback)850 void ViEChannel::RegisterReceiveChannelRtpStatisticsCallback(
851 StreamDataCountersCallback* callback) {
852 vie_receiver_.GetReceiveStatistics()->RegisterRtpStatisticsCallback(callback);
853 }
854
GetSendRtcpPacketTypeCounter(RtcpPacketTypeCounter * packet_counter) const855 void ViEChannel::GetSendRtcpPacketTypeCounter(
856 RtcpPacketTypeCounter* packet_counter) const {
857 std::map<uint32_t, RtcpPacketTypeCounter> counter_map =
858 rtcp_packet_type_counter_observer_.GetPacketTypeCounterMap();
859
860 RtcpPacketTypeCounter counter;
861 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_)
862 counter.Add(counter_map[rtp_rtcp->SSRC()]);
863 *packet_counter = counter;
864 }
865
GetReceiveRtcpPacketTypeCounter(RtcpPacketTypeCounter * packet_counter) const866 void ViEChannel::GetReceiveRtcpPacketTypeCounter(
867 RtcpPacketTypeCounter* packet_counter) const {
868 std::map<uint32_t, RtcpPacketTypeCounter> counter_map =
869 rtcp_packet_type_counter_observer_.GetPacketTypeCounterMap();
870
871 RtcpPacketTypeCounter counter;
872 counter.Add(counter_map[vie_receiver_.GetRemoteSsrc()]);
873
874 *packet_counter = counter;
875 }
876
RegisterSendSideDelayObserver(SendSideDelayObserver * observer)877 void ViEChannel::RegisterSendSideDelayObserver(
878 SendSideDelayObserver* observer) {
879 send_side_delay_observer_.Set(observer);
880 }
881
RegisterSendBitrateObserver(BitrateStatisticsObserver * observer)882 void ViEChannel::RegisterSendBitrateObserver(
883 BitrateStatisticsObserver* observer) {
884 send_bitrate_observer_.Set(observer);
885 }
886
StartSend()887 int32_t ViEChannel::StartSend() {
888 CriticalSectionScoped cs(crit_.get());
889
890 if (rtp_rtcp_modules_[0]->Sending())
891 return -1;
892
893 for (size_t i = 0; i < num_active_rtp_rtcp_modules_; ++i) {
894 RtpRtcp* rtp_rtcp = rtp_rtcp_modules_[i];
895 rtp_rtcp->SetSendingMediaStatus(true);
896 rtp_rtcp->SetSendingStatus(true);
897 }
898 send_payload_router_->set_active(true);
899 return 0;
900 }
901
StopSend()902 int32_t ViEChannel::StopSend() {
903 send_payload_router_->set_active(false);
904 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_)
905 rtp_rtcp->SetSendingMediaStatus(false);
906
907 if (!rtp_rtcp_modules_[0]->Sending()) {
908 return -1;
909 }
910
911 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) {
912 rtp_rtcp->SetSendingStatus(false);
913 }
914 return 0;
915 }
916
Sending()917 bool ViEChannel::Sending() {
918 return rtp_rtcp_modules_[0]->Sending();
919 }
920
StartReceive()921 void ViEChannel::StartReceive() {
922 if (!sender_)
923 StartDecodeThread();
924 vie_receiver_.StartReceive();
925 }
926
StopReceive()927 void ViEChannel::StopReceive() {
928 vie_receiver_.StopReceive();
929 if (!sender_) {
930 StopDecodeThread();
931 vcm_->ResetDecoder();
932 }
933 }
934
ReceivedRTPPacket(const void * rtp_packet,size_t rtp_packet_length,const PacketTime & packet_time)935 int32_t ViEChannel::ReceivedRTPPacket(const void* rtp_packet,
936 size_t rtp_packet_length,
937 const PacketTime& packet_time) {
938 return vie_receiver_.ReceivedRTPPacket(
939 rtp_packet, rtp_packet_length, packet_time);
940 }
941
ReceivedRTCPPacket(const void * rtcp_packet,size_t rtcp_packet_length)942 int32_t ViEChannel::ReceivedRTCPPacket(const void* rtcp_packet,
943 size_t rtcp_packet_length) {
944 return vie_receiver_.ReceivedRTCPPacket(rtcp_packet, rtcp_packet_length);
945 }
946
SetMTU(uint16_t mtu)947 int32_t ViEChannel::SetMTU(uint16_t mtu) {
948 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_)
949 rtp_rtcp->SetMaxTransferUnit(mtu);
950 return 0;
951 }
952
rtp_rtcp()953 RtpRtcp* ViEChannel::rtp_rtcp() {
954 return rtp_rtcp_modules_[0];
955 }
956
send_payload_router()957 rtc::scoped_refptr<PayloadRouter> ViEChannel::send_payload_router() {
958 return send_payload_router_;
959 }
960
vcm_protection_callback()961 VCMProtectionCallback* ViEChannel::vcm_protection_callback() {
962 return vcm_protection_callback_.get();
963 }
964
GetStatsObserver()965 CallStatsObserver* ViEChannel::GetStatsObserver() {
966 return stats_observer_.get();
967 }
968
969 // Do not acquire the lock of |vcm_| in this function. Decode callback won't
970 // necessarily be called from the decoding thread. The decoding thread may have
971 // held the lock when calling VideoDecoder::Decode, Reset, or Release. Acquiring
972 // the same lock in the path of decode callback can deadlock.
FrameToRender(VideoFrame & video_frame)973 int32_t ViEChannel::FrameToRender(VideoFrame& video_frame) { // NOLINT
974 CriticalSectionScoped cs(crit_.get());
975
976 if (pre_render_callback_ != NULL)
977 pre_render_callback_->FrameCallback(&video_frame);
978
979 // TODO(pbos): Remove stream id argument.
980 incoming_video_stream_->RenderFrame(0xFFFFFFFF, video_frame);
981 return 0;
982 }
983
ReceivedDecodedReferenceFrame(const uint64_t picture_id)984 int32_t ViEChannel::ReceivedDecodedReferenceFrame(
985 const uint64_t picture_id) {
986 return rtp_rtcp_modules_[0]->SendRTCPReferencePictureSelection(picture_id);
987 }
988
OnIncomingPayloadType(int payload_type)989 void ViEChannel::OnIncomingPayloadType(int payload_type) {
990 CriticalSectionScoped cs(crit_.get());
991 if (receive_stats_callback_)
992 receive_stats_callback_->OnIncomingPayloadType(payload_type);
993 }
994
OnDecoderImplementationName(const char * implementation_name)995 void ViEChannel::OnDecoderImplementationName(const char* implementation_name) {
996 CriticalSectionScoped cs(crit_.get());
997 if (receive_stats_callback_)
998 receive_stats_callback_->OnDecoderImplementationName(implementation_name);
999 }
1000
OnReceiveRatesUpdated(uint32_t bit_rate,uint32_t frame_rate)1001 void ViEChannel::OnReceiveRatesUpdated(uint32_t bit_rate, uint32_t frame_rate) {
1002 CriticalSectionScoped cs(crit_.get());
1003 if (receive_stats_callback_)
1004 receive_stats_callback_->OnIncomingRate(frame_rate, bit_rate);
1005 }
1006
OnDiscardedPacketsUpdated(int discarded_packets)1007 void ViEChannel::OnDiscardedPacketsUpdated(int discarded_packets) {
1008 CriticalSectionScoped cs(crit_.get());
1009 if (receive_stats_callback_)
1010 receive_stats_callback_->OnDiscardedPacketsUpdated(discarded_packets);
1011 }
1012
OnFrameCountsUpdated(const FrameCounts & frame_counts)1013 void ViEChannel::OnFrameCountsUpdated(const FrameCounts& frame_counts) {
1014 CriticalSectionScoped cs(crit_.get());
1015 receive_frame_counts_ = frame_counts;
1016 if (receive_stats_callback_)
1017 receive_stats_callback_->OnFrameCountsUpdated(frame_counts);
1018 }
1019
OnDecoderTiming(int decode_ms,int max_decode_ms,int current_delay_ms,int target_delay_ms,int jitter_buffer_ms,int min_playout_delay_ms,int render_delay_ms)1020 void ViEChannel::OnDecoderTiming(int decode_ms,
1021 int max_decode_ms,
1022 int current_delay_ms,
1023 int target_delay_ms,
1024 int jitter_buffer_ms,
1025 int min_playout_delay_ms,
1026 int render_delay_ms) {
1027 CriticalSectionScoped cs(crit_.get());
1028 if (!receive_stats_callback_)
1029 return;
1030 receive_stats_callback_->OnDecoderTiming(
1031 decode_ms, max_decode_ms, current_delay_ms, target_delay_ms,
1032 jitter_buffer_ms, min_playout_delay_ms, render_delay_ms, last_rtt_ms_);
1033 }
1034
RequestKeyFrame()1035 int32_t ViEChannel::RequestKeyFrame() {
1036 return rtp_rtcp_modules_[0]->RequestKeyFrame();
1037 }
1038
SliceLossIndicationRequest(const uint64_t picture_id)1039 int32_t ViEChannel::SliceLossIndicationRequest(
1040 const uint64_t picture_id) {
1041 return rtp_rtcp_modules_[0]->SendRTCPSliceLossIndication(
1042 static_cast<uint8_t>(picture_id));
1043 }
1044
ResendPackets(const uint16_t * sequence_numbers,uint16_t length)1045 int32_t ViEChannel::ResendPackets(const uint16_t* sequence_numbers,
1046 uint16_t length) {
1047 return rtp_rtcp_modules_[0]->SendNACK(sequence_numbers, length);
1048 }
1049
ChannelDecodeThreadFunction(void * obj)1050 bool ViEChannel::ChannelDecodeThreadFunction(void* obj) {
1051 return static_cast<ViEChannel*>(obj)->ChannelDecodeProcess();
1052 }
1053
ChannelDecodeProcess()1054 bool ViEChannel::ChannelDecodeProcess() {
1055 vcm_->Decode(kMaxDecodeWaitTimeMs);
1056 return true;
1057 }
1058
OnRttUpdate(int64_t avg_rtt_ms,int64_t max_rtt_ms)1059 void ViEChannel::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) {
1060 vcm_->SetReceiveChannelParameters(max_rtt_ms);
1061
1062 CriticalSectionScoped cs(crit_.get());
1063 if (time_of_first_rtt_ms_ == -1)
1064 time_of_first_rtt_ms_ = Clock::GetRealTimeClock()->TimeInMilliseconds();
1065 rtt_sum_ms_ += avg_rtt_ms;
1066 last_rtt_ms_ = avg_rtt_ms;
1067 ++num_rtts_;
1068 }
1069
ProtectionRequest(const FecProtectionParams * delta_fec_params,const FecProtectionParams * key_fec_params,uint32_t * video_rate_bps,uint32_t * nack_rate_bps,uint32_t * fec_rate_bps)1070 int ViEChannel::ProtectionRequest(const FecProtectionParams* delta_fec_params,
1071 const FecProtectionParams* key_fec_params,
1072 uint32_t* video_rate_bps,
1073 uint32_t* nack_rate_bps,
1074 uint32_t* fec_rate_bps) {
1075 *video_rate_bps = 0;
1076 *nack_rate_bps = 0;
1077 *fec_rate_bps = 0;
1078 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) {
1079 uint32_t not_used = 0;
1080 uint32_t module_video_rate = 0;
1081 uint32_t module_fec_rate = 0;
1082 uint32_t module_nack_rate = 0;
1083 rtp_rtcp->SetFecParameters(delta_fec_params, key_fec_params);
1084 rtp_rtcp->BitrateSent(¬_used, &module_video_rate, &module_fec_rate,
1085 &module_nack_rate);
1086 *video_rate_bps += module_video_rate;
1087 *nack_rate_bps += module_nack_rate;
1088 *fec_rate_bps += module_fec_rate;
1089 }
1090 return 0;
1091 }
1092
CreateRtpRtcpModules(bool receiver_only,ReceiveStatistics * receive_statistics,Transport * outgoing_transport,RtcpIntraFrameObserver * intra_frame_callback,RtcpBandwidthObserver * bandwidth_callback,TransportFeedbackObserver * transport_feedback_callback,RtcpRttStats * rtt_stats,RtcpPacketTypeCounterObserver * rtcp_packet_type_counter_observer,RemoteBitrateEstimator * remote_bitrate_estimator,RtpPacketSender * paced_sender,TransportSequenceNumberAllocator * transport_sequence_number_allocator,BitrateStatisticsObserver * send_bitrate_observer,FrameCountObserver * send_frame_count_observer,SendSideDelayObserver * send_side_delay_observer,size_t num_modules)1093 std::vector<RtpRtcp*> ViEChannel::CreateRtpRtcpModules(
1094 bool receiver_only,
1095 ReceiveStatistics* receive_statistics,
1096 Transport* outgoing_transport,
1097 RtcpIntraFrameObserver* intra_frame_callback,
1098 RtcpBandwidthObserver* bandwidth_callback,
1099 TransportFeedbackObserver* transport_feedback_callback,
1100 RtcpRttStats* rtt_stats,
1101 RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer,
1102 RemoteBitrateEstimator* remote_bitrate_estimator,
1103 RtpPacketSender* paced_sender,
1104 TransportSequenceNumberAllocator* transport_sequence_number_allocator,
1105 BitrateStatisticsObserver* send_bitrate_observer,
1106 FrameCountObserver* send_frame_count_observer,
1107 SendSideDelayObserver* send_side_delay_observer,
1108 size_t num_modules) {
1109 RTC_DCHECK_GT(num_modules, 0u);
1110 RtpRtcp::Configuration configuration;
1111 ReceiveStatistics* null_receive_statistics = configuration.receive_statistics;
1112 configuration.audio = false;
1113 configuration.receiver_only = receiver_only;
1114 configuration.receive_statistics = receive_statistics;
1115 configuration.outgoing_transport = outgoing_transport;
1116 configuration.intra_frame_callback = intra_frame_callback;
1117 configuration.rtt_stats = rtt_stats;
1118 configuration.rtcp_packet_type_counter_observer =
1119 rtcp_packet_type_counter_observer;
1120 configuration.paced_sender = paced_sender;
1121 configuration.transport_sequence_number_allocator =
1122 transport_sequence_number_allocator;
1123 configuration.send_bitrate_observer = send_bitrate_observer;
1124 configuration.send_frame_count_observer = send_frame_count_observer;
1125 configuration.send_side_delay_observer = send_side_delay_observer;
1126 configuration.bandwidth_callback = bandwidth_callback;
1127 configuration.transport_feedback_callback = transport_feedback_callback;
1128
1129 std::vector<RtpRtcp*> modules;
1130 for (size_t i = 0; i < num_modules; ++i) {
1131 RtpRtcp* rtp_rtcp = RtpRtcp::CreateRtpRtcp(configuration);
1132 rtp_rtcp->SetSendingStatus(false);
1133 rtp_rtcp->SetSendingMediaStatus(false);
1134 rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound);
1135 modules.push_back(rtp_rtcp);
1136 // Receive statistics and remote bitrate estimator should only be set for
1137 // the primary (first) module.
1138 configuration.receive_statistics = null_receive_statistics;
1139 configuration.remote_bitrate_estimator = nullptr;
1140 }
1141 return modules;
1142 }
1143
StartDecodeThread()1144 void ViEChannel::StartDecodeThread() {
1145 RTC_DCHECK(!sender_);
1146 if (decode_thread_.IsRunning())
1147 return;
1148 // Start the decode thread
1149 decode_thread_.Start();
1150 decode_thread_.SetPriority(rtc::kHighestPriority);
1151 }
1152
StopDecodeThread()1153 void ViEChannel::StopDecodeThread() {
1154 vcm_->TriggerDecoderShutdown();
1155
1156 decode_thread_.Stop();
1157 }
1158
SetVoiceChannel(int32_t ve_channel_id,VoEVideoSync * ve_sync_interface)1159 int32_t ViEChannel::SetVoiceChannel(int32_t ve_channel_id,
1160 VoEVideoSync* ve_sync_interface) {
1161 return vie_sync_.ConfigureSync(ve_channel_id, ve_sync_interface,
1162 rtp_rtcp_modules_[0],
1163 vie_receiver_.GetRtpReceiver());
1164 }
1165
VoiceChannel()1166 int32_t ViEChannel::VoiceChannel() {
1167 return vie_sync_.VoiceChannel();
1168 }
1169
RegisterPreRenderCallback(I420FrameCallback * pre_render_callback)1170 void ViEChannel::RegisterPreRenderCallback(
1171 I420FrameCallback* pre_render_callback) {
1172 CriticalSectionScoped cs(crit_.get());
1173 pre_render_callback_ = pre_render_callback;
1174 }
1175
RegisterPreDecodeImageCallback(EncodedImageCallback * pre_decode_callback)1176 void ViEChannel::RegisterPreDecodeImageCallback(
1177 EncodedImageCallback* pre_decode_callback) {
1178 vcm_->RegisterPreDecodeImageCallback(pre_decode_callback);
1179 }
1180
1181 // TODO(pbos): Remove OnInitializeDecoder which is called from the RTP module,
1182 // any decoder resetting should be handled internally within the VCM.
OnInitializeDecoder(const int8_t payload_type,const char payload_name[RTP_PAYLOAD_NAME_SIZE],const int frequency,const size_t channels,const uint32_t rate)1183 int32_t ViEChannel::OnInitializeDecoder(
1184 const int8_t payload_type,
1185 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
1186 const int frequency,
1187 const size_t channels,
1188 const uint32_t rate) {
1189 LOG(LS_INFO) << "OnInitializeDecoder " << static_cast<int>(payload_type)
1190 << " " << payload_name;
1191 vcm_->ResetDecoder();
1192
1193 return 0;
1194 }
1195
OnIncomingSSRCChanged(const uint32_t ssrc)1196 void ViEChannel::OnIncomingSSRCChanged(const uint32_t ssrc) {
1197 rtp_rtcp_modules_[0]->SetRemoteSSRC(ssrc);
1198 }
1199
OnIncomingCSRCChanged(const uint32_t CSRC,const bool added)1200 void ViEChannel::OnIncomingCSRCChanged(const uint32_t CSRC, const bool added) {}
1201
RegisterSendFrameCountObserver(FrameCountObserver * observer)1202 void ViEChannel::RegisterSendFrameCountObserver(
1203 FrameCountObserver* observer) {
1204 send_frame_count_observer_.Set(observer);
1205 }
1206
RegisterReceiveStatisticsProxy(ReceiveStatisticsProxy * receive_statistics_proxy)1207 void ViEChannel::RegisterReceiveStatisticsProxy(
1208 ReceiveStatisticsProxy* receive_statistics_proxy) {
1209 CriticalSectionScoped cs(crit_.get());
1210 receive_stats_callback_ = receive_statistics_proxy;
1211 }
1212
SetIncomingVideoStream(IncomingVideoStream * incoming_video_stream)1213 void ViEChannel::SetIncomingVideoStream(
1214 IncomingVideoStream* incoming_video_stream) {
1215 CriticalSectionScoped cs(crit_.get());
1216 incoming_video_stream_ = incoming_video_stream;
1217 }
1218 } // namespace webrtc
1219