Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / video_engine / vie_channel.cc
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_engine/vie_channel.h"
12
13 #include <algorithm>
14 #include <vector>
15
16 #include "webrtc/common.h"
17 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
18 #include "webrtc/experiments.h"
19 #include "webrtc/modules/pacing/include/paced_sender.h"
20 #include "webrtc/modules/rtp_rtcp/interface/rtp_receiver.h"
21 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
22 #include "webrtc/modules/utility/interface/process_thread.h"
23 #include "webrtc/modules/video_coding/main/interface/video_coding.h"
24 #include "webrtc/modules/video_processing/main/interface/video_processing.h"
25 #include "webrtc/modules/video_render/include/video_render_defines.h"
26 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
27 #include "webrtc/system_wrappers/interface/logging.h"
28 #include "webrtc/system_wrappers/interface/thread_wrapper.h"
29 #include "webrtc/video_engine/call_stats.h"
30 #include "webrtc/video_engine/include/vie_codec.h"
31 #include "webrtc/video_engine/include/vie_errors.h"
32 #include "webrtc/video_engine/include/vie_image_process.h"
33 #include "webrtc/video_engine/include/vie_rtp_rtcp.h"
34 #include "webrtc/frame_callback.h"
35 #include "webrtc/video_engine/vie_defines.h"
36
37 namespace webrtc {
38
39 const int kMaxDecodeWaitTimeMs = 50;
40 const int kInvalidRtpExtensionId = 0;
41 static const int kMaxTargetDelayMs = 10000;
42 static const float kMaxIncompleteTimeMultiplier = 3.5f;
43
44 // Helper class receiving statistics callbacks.
45 class ChannelStatsObserver : public CallStatsObserver {
46  public:
47   explicit ChannelStatsObserver(ViEChannel* owner) : owner_(owner) {}
48   virtual ~ChannelStatsObserver() {}
49
50   // Implements StatsObserver.
51   virtual void OnRttUpdate(uint32_t rtt) {
52     owner_->OnRttUpdate(rtt);
53   }
54
55  private:
56   ViEChannel* owner_;
57 };
58
59 ViEChannel::ViEChannel(int32_t channel_id,
60                        int32_t engine_id,
61                        uint32_t number_of_cores,
62                        const Config& config,
63                        ProcessThread& module_process_thread,
64                        RtcpIntraFrameObserver* intra_frame_observer,
65                        RtcpBandwidthObserver* bandwidth_observer,
66                        RemoteBitrateEstimator* remote_bitrate_estimator,
67                        RtcpRttStats* rtt_stats,
68                        PacedSender* paced_sender,
69                        RtpRtcp* default_rtp_rtcp,
70                        bool sender)
71     : ViEFrameProviderBase(channel_id, engine_id),
72       channel_id_(channel_id),
73       engine_id_(engine_id),
74       number_of_cores_(number_of_cores),
75       num_socket_threads_(kViESocketThreads),
76       callback_cs_(CriticalSectionWrapper::CreateCriticalSection()),
77       rtp_rtcp_cs_(CriticalSectionWrapper::CreateCriticalSection()),
78       default_rtp_rtcp_(default_rtp_rtcp),
79       vcm_(*VideoCodingModule::Create()),
80       vie_receiver_(channel_id, &vcm_, remote_bitrate_estimator, this),
81       vie_sender_(channel_id),
82       vie_sync_(&vcm_, this),
83       stats_observer_(new ChannelStatsObserver(this)),
84       module_process_thread_(module_process_thread),
85       codec_observer_(NULL),
86       do_key_frame_callbackRequest_(false),
87       rtp_observer_(NULL),
88       rtcp_observer_(NULL),
89       intra_frame_observer_(intra_frame_observer),
90       rtt_stats_(rtt_stats),
91       paced_sender_(paced_sender),
92       bandwidth_observer_(bandwidth_observer),
93       send_timestamp_extension_id_(kInvalidRtpExtensionId),
94       absolute_send_time_extension_id_(kInvalidRtpExtensionId),
95       external_transport_(NULL),
96       decoder_reset_(true),
97       wait_for_key_frame_(false),
98       decode_thread_(NULL),
99       effect_filter_(NULL),
100       color_enhancement_(false),
101       mtu_(0),
102       sender_(sender),
103       nack_history_size_sender_(kSendSidePacketHistorySize),
104       max_nack_reordering_threshold_(kMaxPacketAgeToNack),
105       pre_render_callback_(NULL),
106       config_(config) {
107   RtpRtcp::Configuration configuration;
108   configuration.id = ViEModuleId(engine_id, channel_id);
109   configuration.audio = false;
110   configuration.default_module = default_rtp_rtcp;
111   configuration.outgoing_transport = &vie_sender_;
112   configuration.rtcp_feedback = this;
113   configuration.intra_frame_callback = intra_frame_observer;
114   configuration.bandwidth_callback = bandwidth_observer;
115   configuration.rtt_stats = rtt_stats;
116   configuration.remote_bitrate_estimator = remote_bitrate_estimator;
117   configuration.paced_sender = paced_sender;
118   configuration.receive_statistics = vie_receiver_.GetReceiveStatistics();
119
120   rtp_rtcp_.reset(RtpRtcp::CreateRtpRtcp(configuration));
121   vie_receiver_.SetRtpRtcpModule(rtp_rtcp_.get());
122   vcm_.SetNackSettings(kMaxNackListSize, max_nack_reordering_threshold_, 0);
123 }
124
125 int32_t ViEChannel::Init() {
126   if (module_process_thread_.RegisterModule(
127       vie_receiver_.GetReceiveStatistics()) != 0) {
128     return -1;
129   }
130   // RTP/RTCP initialization.
131   if (rtp_rtcp_->SetSendingMediaStatus(false) != 0) {
132     return -1;
133   }
134   if (module_process_thread_.RegisterModule(rtp_rtcp_.get()) != 0) {
135     return -1;
136   }
137   rtp_rtcp_->SetKeyFrameRequestMethod(kKeyFrameReqFirRtp);
138   rtp_rtcp_->SetRTCPStatus(kRtcpCompound);
139   if (paced_sender_) {
140     rtp_rtcp_->SetStorePacketsStatus(true, nack_history_size_sender_);
141   }
142   if (vcm_.InitializeReceiver() != 0) {
143     return -1;
144   }
145   if (vcm_.SetVideoProtection(kProtectionKeyOnLoss, true)) {
146     return -1;
147   }
148   if (vcm_.RegisterReceiveCallback(this) != 0) {
149     return -1;
150   }
151   vcm_.RegisterFrameTypeCallback(this);
152   vcm_.RegisterReceiveStatisticsCallback(this);
153   vcm_.RegisterDecoderTimingCallback(this);
154   vcm_.SetRenderDelay(kViEDefaultRenderDelayMs);
155   if (module_process_thread_.RegisterModule(&vcm_) != 0) {
156     return -1;
157   }
158 #ifdef VIDEOCODEC_VP8
159   VideoCodec video_codec;
160   if (vcm_.Codec(kVideoCodecVP8, &video_codec) == VCM_OK) {
161     rtp_rtcp_->RegisterSendPayload(video_codec);
162     // TODO(holmer): Can we call SetReceiveCodec() here instead?
163     if (!vie_receiver_.RegisterPayload(video_codec)) {
164       return -1;
165     }
166     vcm_.RegisterReceiveCodec(&video_codec, number_of_cores_);
167     vcm_.RegisterSendCodec(&video_codec, number_of_cores_,
168                            rtp_rtcp_->MaxDataPayloadLength());
169   } else {
170     assert(false);
171   }
172 #endif
173
174   return 0;
175 }
176
177 ViEChannel::~ViEChannel() {
178   // Make sure we don't get more callbacks from the RTP module.
179   module_process_thread_.DeRegisterModule(vie_receiver_.GetReceiveStatistics());
180   module_process_thread_.DeRegisterModule(rtp_rtcp_.get());
181   module_process_thread_.DeRegisterModule(&vcm_);
182   module_process_thread_.DeRegisterModule(&vie_sync_);
183   while (simulcast_rtp_rtcp_.size() > 0) {
184     std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
185     RtpRtcp* rtp_rtcp = *it;
186     module_process_thread_.DeRegisterModule(rtp_rtcp);
187     delete rtp_rtcp;
188     simulcast_rtp_rtcp_.erase(it);
189   }
190   while (removed_rtp_rtcp_.size() > 0) {
191     std::list<RtpRtcp*>::iterator it = removed_rtp_rtcp_.begin();
192     delete *it;
193     removed_rtp_rtcp_.erase(it);
194   }
195   if (decode_thread_) {
196     StopDecodeThread();
197   }
198   // Release modules.
199   VideoCodingModule::Destroy(&vcm_);
200 }
201
202 int32_t ViEChannel::SetSendCodec(const VideoCodec& video_codec,
203                                  bool new_stream) {
204   if (!sender_) {
205     return 0;
206   }
207   if (video_codec.codecType == kVideoCodecRED ||
208       video_codec.codecType == kVideoCodecULPFEC) {
209     LOG_F(LS_ERROR) << "Not a valid send codec " << video_codec.codecType;
210     return -1;
211   }
212   if (kMaxSimulcastStreams < video_codec.numberOfSimulcastStreams) {
213     LOG_F(LS_ERROR) << "Incorrect config "
214                     << video_codec.numberOfSimulcastStreams;
215     return -1;
216   }
217   // Update the RTP module with the settings.
218   // Stop and Start the RTP module -> trigger new SSRC, if an SSRC hasn't been
219   // set explicitly.
220   bool restart_rtp = false;
221   if (rtp_rtcp_->Sending() && new_stream) {
222     restart_rtp = true;
223     rtp_rtcp_->SetSendingStatus(false);
224     for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
225          it != simulcast_rtp_rtcp_.end(); ++it) {
226       (*it)->SetSendingStatus(false);
227       (*it)->SetSendingMediaStatus(false);
228     }
229   }
230
231   bool fec_enabled = false;
232   uint8_t payload_type_red;
233   uint8_t payload_type_fec;
234   rtp_rtcp_->GenericFECStatus(fec_enabled, payload_type_red, payload_type_fec);
235
236   CriticalSectionScoped cs(rtp_rtcp_cs_.get());
237
238   if (video_codec.numberOfSimulcastStreams > 0) {
239     // Set correct bitrate to base layer.
240     // Create our simulcast RTP modules.
241     int num_modules_to_add = video_codec.numberOfSimulcastStreams -
242         simulcast_rtp_rtcp_.size() - 1;
243     if (num_modules_to_add < 0) {
244       num_modules_to_add = 0;
245     }
246
247     while (removed_rtp_rtcp_.size() > 0 && num_modules_to_add > 0) {
248       RtpRtcp* rtp_rtcp = removed_rtp_rtcp_.front();
249       removed_rtp_rtcp_.pop_front();
250       simulcast_rtp_rtcp_.push_back(rtp_rtcp);
251       rtp_rtcp->SetSendingStatus(rtp_rtcp_->Sending());
252       rtp_rtcp->SetSendingMediaStatus(rtp_rtcp_->SendingMedia());
253       module_process_thread_.RegisterModule(rtp_rtcp);
254       --num_modules_to_add;
255     }
256
257     for (int i = 0; i < num_modules_to_add; ++i) {
258       RtpRtcp::Configuration configuration;
259       configuration.id = ViEModuleId(engine_id_, channel_id_);
260       configuration.audio = false;  // Video.
261       configuration.default_module = default_rtp_rtcp_;
262       configuration.outgoing_transport = &vie_sender_;
263       configuration.intra_frame_callback = intra_frame_observer_;
264       configuration.bandwidth_callback = bandwidth_observer_.get();
265       configuration.rtt_stats = rtt_stats_;
266       configuration.paced_sender = paced_sender_;
267
268       RtpRtcp* rtp_rtcp = RtpRtcp::CreateRtpRtcp(configuration);
269
270       // Silently ignore error.
271       module_process_thread_.RegisterModule(rtp_rtcp);
272       rtp_rtcp->SetRTCPStatus(rtp_rtcp_->RTCP());
273
274       if (rtp_rtcp_->StorePackets()) {
275         rtp_rtcp->SetStorePacketsStatus(true, nack_history_size_sender_);
276       } else if (paced_sender_) {
277         rtp_rtcp->SetStorePacketsStatus(true, nack_history_size_sender_);
278       }
279
280       if (fec_enabled) {
281         rtp_rtcp->SetGenericFECStatus(fec_enabled, payload_type_red,
282             payload_type_fec);
283       }
284       rtp_rtcp->SetSendingStatus(rtp_rtcp_->Sending());
285       rtp_rtcp->SetSendingMediaStatus(rtp_rtcp_->SendingMedia());
286       simulcast_rtp_rtcp_.push_back(rtp_rtcp);
287     }
288     // Remove last in list if we have too many.
289     for (int j = simulcast_rtp_rtcp_.size();
290          j > (video_codec.numberOfSimulcastStreams - 1);
291          j--) {
292       RtpRtcp* rtp_rtcp = simulcast_rtp_rtcp_.back();
293       module_process_thread_.DeRegisterModule(rtp_rtcp);
294       rtp_rtcp->SetSendingStatus(false);
295       rtp_rtcp->SetSendingMediaStatus(false);
296       rtp_rtcp->RegisterSendFrameCountObserver(NULL);
297       rtp_rtcp->RegisterSendChannelRtcpStatisticsCallback(NULL);
298       rtp_rtcp->RegisterSendChannelRtpStatisticsCallback(NULL);
299       rtp_rtcp->RegisterVideoBitrateObserver(NULL);
300       simulcast_rtp_rtcp_.pop_back();
301       removed_rtp_rtcp_.push_front(rtp_rtcp);
302     }
303     uint8_t idx = 0;
304     // Configure all simulcast modules.
305     for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
306          it != simulcast_rtp_rtcp_.end();
307          it++) {
308       idx++;
309       RtpRtcp* rtp_rtcp = *it;
310       rtp_rtcp->DeRegisterSendPayload(video_codec.plType);
311       if (rtp_rtcp->RegisterSendPayload(video_codec) != 0) {
312         return -1;
313       }
314       if (mtu_ != 0) {
315         rtp_rtcp->SetMaxTransferUnit(mtu_);
316       }
317       if (restart_rtp) {
318         rtp_rtcp->SetSendingStatus(true);
319         rtp_rtcp->SetSendingMediaStatus(true);
320       }
321       if (send_timestamp_extension_id_ != kInvalidRtpExtensionId) {
322         // Deregister in case the extension was previously enabled.
323         rtp_rtcp->DeregisterSendRtpHeaderExtension(
324             kRtpExtensionTransmissionTimeOffset);
325         if (rtp_rtcp->RegisterSendRtpHeaderExtension(
326             kRtpExtensionTransmissionTimeOffset,
327             send_timestamp_extension_id_) != 0) {
328         }
329       } else {
330         rtp_rtcp->DeregisterSendRtpHeaderExtension(
331             kRtpExtensionTransmissionTimeOffset);
332       }
333       if (absolute_send_time_extension_id_ != kInvalidRtpExtensionId) {
334         // Deregister in case the extension was previously enabled.
335         rtp_rtcp->DeregisterSendRtpHeaderExtension(
336             kRtpExtensionAbsoluteSendTime);
337         if (rtp_rtcp->RegisterSendRtpHeaderExtension(
338             kRtpExtensionAbsoluteSendTime,
339             absolute_send_time_extension_id_) != 0) {
340         }
341       } else {
342         rtp_rtcp->DeregisterSendRtpHeaderExtension(
343             kRtpExtensionAbsoluteSendTime);
344       }
345       rtp_rtcp->RegisterSendFrameCountObserver(
346           rtp_rtcp_->GetSendFrameCountObserver());
347       rtp_rtcp->RegisterSendChannelRtcpStatisticsCallback(
348           rtp_rtcp_->GetSendChannelRtcpStatisticsCallback());
349       rtp_rtcp->RegisterSendChannelRtpStatisticsCallback(
350           rtp_rtcp_->GetSendChannelRtpStatisticsCallback());
351       rtp_rtcp->RegisterVideoBitrateObserver(
352           rtp_rtcp_->GetVideoBitrateObserver());
353     }
354     // |RegisterSimulcastRtpRtcpModules| resets all old weak pointers and old
355     // modules can be deleted after this step.
356     vie_receiver_.RegisterSimulcastRtpRtcpModules(simulcast_rtp_rtcp_);
357   } else {
358     while (!simulcast_rtp_rtcp_.empty()) {
359       RtpRtcp* rtp_rtcp = simulcast_rtp_rtcp_.back();
360       module_process_thread_.DeRegisterModule(rtp_rtcp);
361       rtp_rtcp->SetSendingStatus(false);
362       rtp_rtcp->SetSendingMediaStatus(false);
363       rtp_rtcp->RegisterSendFrameCountObserver(NULL);
364       rtp_rtcp->RegisterSendChannelRtcpStatisticsCallback(NULL);
365       rtp_rtcp->RegisterSendChannelRtpStatisticsCallback(NULL);
366       rtp_rtcp->RegisterVideoBitrateObserver(NULL);
367       simulcast_rtp_rtcp_.pop_back();
368       removed_rtp_rtcp_.push_front(rtp_rtcp);
369     }
370     // Clear any previous modules.
371     vie_receiver_.RegisterSimulcastRtpRtcpModules(simulcast_rtp_rtcp_);
372   }
373   // Enable this if H264 is available.
374   // This sets the wanted packetization mode.
375   // if (video_codec.plType == kVideoCodecH264) {
376   //   if (video_codec.codecSpecific.H264.packetization ==  kH264SingleMode) {
377   //     rtp_rtcp_->SetH264PacketizationMode(H264_SINGLE_NAL_MODE);
378   //   } else {
379   //     rtp_rtcp_->SetH264PacketizationMode(H264_NON_INTERLEAVED_MODE);
380   //   }
381   //   if (video_codec.codecSpecific.H264.configParametersSize > 0) {
382   //     rtp_rtcp_->SetH264SendModeNALU_PPS_SPS(true);
383   //   }
384   // }
385
386   // Don't log this error, no way to check in advance if this pl_type is
387   // registered or not...
388   rtp_rtcp_->DeRegisterSendPayload(video_codec.plType);
389   if (rtp_rtcp_->RegisterSendPayload(video_codec) != 0) {
390     return -1;
391   }
392   if (restart_rtp) {
393     rtp_rtcp_->SetSendingStatus(true);
394     for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
395          it != simulcast_rtp_rtcp_.end(); ++it) {
396       (*it)->SetSendingStatus(true);
397       (*it)->SetSendingMediaStatus(true);
398     }
399   }
400   return 0;
401 }
402
403 int32_t ViEChannel::SetReceiveCodec(const VideoCodec& video_codec) {
404   if (!vie_receiver_.SetReceiveCodec(video_codec)) {
405     return -1;
406   }
407
408   if (video_codec.codecType != kVideoCodecRED &&
409       video_codec.codecType != kVideoCodecULPFEC) {
410     // Register codec type with VCM, but do not register RED or ULPFEC.
411     if (vcm_.RegisterReceiveCodec(&video_codec, number_of_cores_,
412                                   wait_for_key_frame_) != VCM_OK) {
413       return -1;
414     }
415   }
416   return 0;
417 }
418
419 int32_t ViEChannel::GetReceiveCodec(VideoCodec* video_codec) {
420   if (vcm_.ReceiveCodec(video_codec) != 0) {
421     return -1;
422   }
423   return 0;
424 }
425
426 int32_t ViEChannel::RegisterCodecObserver(ViEDecoderObserver* observer) {
427   CriticalSectionScoped cs(callback_cs_.get());
428   if (observer) {
429     if (codec_observer_) {
430       LOG_F(LS_ERROR) << "Observer already registered.";
431       return -1;
432     }
433     codec_observer_ = observer;
434   } else {
435     codec_observer_ = NULL;
436   }
437   return 0;
438 }
439
440 int32_t ViEChannel::RegisterExternalDecoder(const uint8_t pl_type,
441                                             VideoDecoder* decoder,
442                                             bool buffered_rendering,
443                                             int32_t render_delay) {
444   int32_t result;
445   result = vcm_.RegisterExternalDecoder(decoder, pl_type, buffered_rendering);
446   if (result != VCM_OK) {
447     return result;
448   }
449   return vcm_.SetRenderDelay(render_delay);
450 }
451
452 int32_t ViEChannel::DeRegisterExternalDecoder(const uint8_t pl_type) {
453   VideoCodec current_receive_codec;
454   int32_t result = 0;
455   result = vcm_.ReceiveCodec(&current_receive_codec);
456   if (vcm_.RegisterExternalDecoder(NULL, pl_type, false) != VCM_OK) {
457     return -1;
458   }
459
460   if (result == 0 && current_receive_codec.plType == pl_type) {
461     result = vcm_.RegisterReceiveCodec(&current_receive_codec, number_of_cores_,
462                                        wait_for_key_frame_);
463   }
464   return result;
465 }
466
467 int32_t ViEChannel::ReceiveCodecStatistics(uint32_t* num_key_frames,
468                                            uint32_t* num_delta_frames) {
469   VCMFrameCount received_frames;
470   if (vcm_.ReceivedFrameCount(received_frames) != VCM_OK) {
471     return -1;
472   }
473   *num_key_frames = received_frames.numKeyFrames;
474   *num_delta_frames = received_frames.numDeltaFrames;
475   return 0;
476 }
477
478 uint32_t ViEChannel::DiscardedPackets() const {
479   return vcm_.DiscardedPackets();
480 }
481
482 int ViEChannel::ReceiveDelay() const {
483   return vcm_.Delay();
484 }
485
486 int32_t ViEChannel::WaitForKeyFrame(bool wait) {
487   wait_for_key_frame_ = wait;
488   return 0;
489 }
490
491 int32_t ViEChannel::SetSignalPacketLossStatus(bool enable,
492                                               bool only_key_frames) {
493   if (enable) {
494     if (only_key_frames) {
495       vcm_.SetVideoProtection(kProtectionKeyOnLoss, false);
496       if (vcm_.SetVideoProtection(kProtectionKeyOnKeyLoss, true) != VCM_OK) {
497         return -1;
498       }
499     } else {
500       vcm_.SetVideoProtection(kProtectionKeyOnKeyLoss, false);
501       if (vcm_.SetVideoProtection(kProtectionKeyOnLoss, true) != VCM_OK) {
502         return -1;
503       }
504     }
505   } else {
506     vcm_.SetVideoProtection(kProtectionKeyOnLoss, false);
507     vcm_.SetVideoProtection(kProtectionKeyOnKeyLoss, false);
508   }
509   return 0;
510 }
511
512 int32_t ViEChannel::SetRTCPMode(const RTCPMethod rtcp_mode) {
513   CriticalSectionScoped cs(rtp_rtcp_cs_.get());
514   for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
515        it != simulcast_rtp_rtcp_.end();
516        it++) {
517     RtpRtcp* rtp_rtcp = *it;
518     rtp_rtcp->SetRTCPStatus(rtcp_mode);
519   }
520   return rtp_rtcp_->SetRTCPStatus(rtcp_mode);
521 }
522
523 int32_t ViEChannel::GetRTCPMode(RTCPMethod* rtcp_mode) {
524   *rtcp_mode = rtp_rtcp_->RTCP();
525   return 0;
526 }
527
528 int32_t ViEChannel::SetNACKStatus(const bool enable) {
529   // Update the decoding VCM.
530   if (vcm_.SetVideoProtection(kProtectionNack, enable) != VCM_OK) {
531     return -1;
532   }
533   if (enable) {
534     // Disable possible FEC.
535     SetFECStatus(false, 0, 0);
536   }
537   // Update the decoding VCM.
538   if (vcm_.SetVideoProtection(kProtectionNack, enable) != VCM_OK) {
539     return -1;
540   }
541   return ProcessNACKRequest(enable);
542 }
543
544 int32_t ViEChannel::ProcessNACKRequest(const bool enable) {
545   if (enable) {
546     // Turn on NACK.
547     if (rtp_rtcp_->RTCP() == kRtcpOff) {
548       return -1;
549     }
550     vie_receiver_.SetNackStatus(true, max_nack_reordering_threshold_);
551     rtp_rtcp_->SetStorePacketsStatus(true, nack_history_size_sender_);
552     vcm_.RegisterPacketRequestCallback(this);
553
554     CriticalSectionScoped cs(rtp_rtcp_cs_.get());
555
556     for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
557          it != simulcast_rtp_rtcp_.end();
558          it++) {
559       RtpRtcp* rtp_rtcp = *it;
560       rtp_rtcp->SetStorePacketsStatus(true, nack_history_size_sender_);
561     }
562     // Don't introduce errors when NACK is enabled.
563     vcm_.SetDecodeErrorMode(kNoErrors);
564   } else {
565     CriticalSectionScoped cs(rtp_rtcp_cs_.get());
566     for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
567          it != simulcast_rtp_rtcp_.end();
568          it++) {
569       RtpRtcp* rtp_rtcp = *it;
570       if (paced_sender_ == NULL) {
571         rtp_rtcp->SetStorePacketsStatus(false, 0);
572       }
573     }
574     vcm_.RegisterPacketRequestCallback(NULL);
575     if (paced_sender_ == NULL) {
576       rtp_rtcp_->SetStorePacketsStatus(false, 0);
577     }
578     vie_receiver_.SetNackStatus(false, max_nack_reordering_threshold_);
579     // When NACK is off, allow decoding with errors. Otherwise, the video
580     // will freeze, and will only recover with a complete key frame.
581     vcm_.SetDecodeErrorMode(kWithErrors);
582   }
583   return 0;
584 }
585
586 int32_t ViEChannel::SetFECStatus(const bool enable,
587                                        const unsigned char payload_typeRED,
588                                        const unsigned char payload_typeFEC) {
589   // Disable possible NACK.
590   if (enable) {
591     SetNACKStatus(false);
592   }
593
594   return ProcessFECRequest(enable, payload_typeRED, payload_typeFEC);
595 }
596
597 int32_t ViEChannel::ProcessFECRequest(
598     const bool enable,
599     const unsigned char payload_typeRED,
600     const unsigned char payload_typeFEC) {
601   if (rtp_rtcp_->SetGenericFECStatus(enable, payload_typeRED,
602                                     payload_typeFEC) != 0) {
603     return -1;
604   }
605   CriticalSectionScoped cs(rtp_rtcp_cs_.get());
606   for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
607        it != simulcast_rtp_rtcp_.end();
608        it++) {
609     RtpRtcp* rtp_rtcp = *it;
610     rtp_rtcp->SetGenericFECStatus(enable, payload_typeRED, payload_typeFEC);
611   }
612   return 0;
613 }
614
615 int32_t ViEChannel::SetHybridNACKFECStatus(
616     const bool enable,
617     const unsigned char payload_typeRED,
618     const unsigned char payload_typeFEC) {
619   if (vcm_.SetVideoProtection(kProtectionNackFEC, enable) != VCM_OK) {
620     return -1;
621   }
622
623   int32_t ret_val = 0;
624   ret_val = ProcessNACKRequest(enable);
625   if (ret_val < 0) {
626     return ret_val;
627   }
628   return ProcessFECRequest(enable, payload_typeRED, payload_typeFEC);
629 }
630
631 int ViEChannel::SetSenderBufferingMode(int target_delay_ms) {
632   if ((target_delay_ms < 0) || (target_delay_ms > kMaxTargetDelayMs)) {
633     LOG(LS_ERROR) << "Invalid send buffer value.";
634     return -1;
635   }
636   if (target_delay_ms == 0) {
637     // Real-time mode.
638     nack_history_size_sender_ = kSendSidePacketHistorySize;
639   } else {
640     nack_history_size_sender_ = GetRequiredNackListSize(target_delay_ms);
641     // Don't allow a number lower than the default value.
642     if (nack_history_size_sender_ < kSendSidePacketHistorySize) {
643       nack_history_size_sender_ = kSendSidePacketHistorySize;
644     }
645   }
646   if (rtp_rtcp_->SetStorePacketsStatus(true, nack_history_size_sender_) != 0) {
647     return -1;
648   }
649   return 0;
650 }
651
652 int ViEChannel::SetReceiverBufferingMode(int target_delay_ms) {
653   if ((target_delay_ms < 0) || (target_delay_ms > kMaxTargetDelayMs)) {
654     LOG(LS_ERROR) << "Invalid receive buffer delay value.";
655     return -1;
656   }
657   int max_nack_list_size;
658   int max_incomplete_time_ms;
659   if (target_delay_ms == 0) {
660     // Real-time mode - restore default settings.
661     max_nack_reordering_threshold_ = kMaxPacketAgeToNack;
662     max_nack_list_size = kMaxNackListSize;
663     max_incomplete_time_ms = 0;
664   } else {
665     max_nack_list_size =  3 * GetRequiredNackListSize(target_delay_ms) / 4;
666     max_nack_reordering_threshold_ = max_nack_list_size;
667     // Calculate the max incomplete time and round to int.
668     max_incomplete_time_ms = static_cast<int>(kMaxIncompleteTimeMultiplier *
669         target_delay_ms + 0.5f);
670   }
671   vcm_.SetNackSettings(max_nack_list_size, max_nack_reordering_threshold_,
672                        max_incomplete_time_ms);
673   vcm_.SetMinReceiverDelay(target_delay_ms);
674   if (vie_sync_.SetTargetBufferingDelay(target_delay_ms) < 0)
675     return -1;
676   return 0;
677 }
678
679 int ViEChannel::GetRequiredNackListSize(int target_delay_ms) {
680   // The max size of the nack list should be large enough to accommodate the
681   // the number of packets (frames) resulting from the increased delay.
682   // Roughly estimating for ~40 packets per frame @ 30fps.
683   return target_delay_ms * 40 * 30 / 1000;
684 }
685
686 int32_t ViEChannel::SetKeyFrameRequestMethod(
687     const KeyFrameRequestMethod method) {
688   return rtp_rtcp_->SetKeyFrameRequestMethod(method);
689 }
690
691 bool ViEChannel::EnableRemb(bool enable) {
692   if (rtp_rtcp_->SetREMBStatus(enable) != 0)
693     return false;
694   return true;
695 }
696
697 int ViEChannel::SetSendTimestampOffsetStatus(bool enable, int id) {
698   CriticalSectionScoped cs(rtp_rtcp_cs_.get());
699   int error = 0;
700   if (enable) {
701     // Enable the extension, but disable possible old id to avoid errors.
702     send_timestamp_extension_id_ = id;
703     rtp_rtcp_->DeregisterSendRtpHeaderExtension(
704         kRtpExtensionTransmissionTimeOffset);
705     error = rtp_rtcp_->RegisterSendRtpHeaderExtension(
706         kRtpExtensionTransmissionTimeOffset, id);
707     for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
708          it != simulcast_rtp_rtcp_.end(); it++) {
709       (*it)->DeregisterSendRtpHeaderExtension(
710           kRtpExtensionTransmissionTimeOffset);
711       error |= (*it)->RegisterSendRtpHeaderExtension(
712           kRtpExtensionTransmissionTimeOffset, id);
713     }
714   } else {
715     // Disable the extension.
716     send_timestamp_extension_id_ = kInvalidRtpExtensionId;
717     rtp_rtcp_->DeregisterSendRtpHeaderExtension(
718         kRtpExtensionTransmissionTimeOffset);
719     for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
720          it != simulcast_rtp_rtcp_.end(); it++) {
721       (*it)->DeregisterSendRtpHeaderExtension(
722           kRtpExtensionTransmissionTimeOffset);
723     }
724   }
725   return error;
726 }
727
728 int ViEChannel::SetReceiveTimestampOffsetStatus(bool enable, int id) {
729   return vie_receiver_.SetReceiveTimestampOffsetStatus(enable, id) ? 0 : -1;
730 }
731
732 int ViEChannel::SetSendAbsoluteSendTimeStatus(bool enable, int id) {
733   CriticalSectionScoped cs(rtp_rtcp_cs_.get());
734   int error = 0;
735   if (enable) {
736     // Enable the extension, but disable possible old id to avoid errors.
737     absolute_send_time_extension_id_ = id;
738     rtp_rtcp_->DeregisterSendRtpHeaderExtension(
739         kRtpExtensionAbsoluteSendTime);
740     error = rtp_rtcp_->RegisterSendRtpHeaderExtension(
741         kRtpExtensionAbsoluteSendTime, id);
742     for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
743          it != simulcast_rtp_rtcp_.end(); it++) {
744       (*it)->DeregisterSendRtpHeaderExtension(
745           kRtpExtensionAbsoluteSendTime);
746       error |= (*it)->RegisterSendRtpHeaderExtension(
747           kRtpExtensionAbsoluteSendTime, id);
748     }
749   } else {
750     // Disable the extension.
751     absolute_send_time_extension_id_ = kInvalidRtpExtensionId;
752     rtp_rtcp_->DeregisterSendRtpHeaderExtension(
753         kRtpExtensionAbsoluteSendTime);
754     for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
755          it != simulcast_rtp_rtcp_.end(); it++) {
756       (*it)->DeregisterSendRtpHeaderExtension(
757           kRtpExtensionAbsoluteSendTime);
758     }
759   }
760   return error;
761 }
762
763 int ViEChannel::SetReceiveAbsoluteSendTimeStatus(bool enable, int id) {
764   return vie_receiver_.SetReceiveAbsoluteSendTimeStatus(enable, id) ? 0 : -1;
765 }
766
767 void ViEChannel::SetRtcpXrRrtrStatus(bool enable) {
768   CriticalSectionScoped cs(rtp_rtcp_cs_.get());
769   rtp_rtcp_->SetRtcpXrRrtrStatus(enable);
770 }
771
772 void ViEChannel::SetTransmissionSmoothingStatus(bool enable) {
773   assert(paced_sender_ && "No paced sender registered.");
774   paced_sender_->SetStatus(enable);
775 }
776
777 int32_t ViEChannel::EnableTMMBR(const bool enable) {
778   return rtp_rtcp_->SetTMMBRStatus(enable);
779 }
780
781 int32_t ViEChannel::EnableKeyFrameRequestCallback(const bool enable) {
782
783   CriticalSectionScoped cs(callback_cs_.get());
784   if (enable && !codec_observer_) {
785     LOG(LS_ERROR) << "No ViECodecObserver set.";
786     return -1;
787   }
788   do_key_frame_callbackRequest_ = enable;
789   return 0;
790 }
791
792 int32_t ViEChannel::SetSSRC(const uint32_t SSRC,
793                             const StreamType usage,
794                             const uint8_t simulcast_idx) {
795   int rtx_settings = kRtxRetransmitted;
796   if (config_.Get<PaddingStrategy>().redundant_payloads)
797     rtx_settings |= kRtxRedundantPayloads;
798   if (simulcast_idx == 0) {
799     if (usage == kViEStreamTypeRtx) {
800       return rtp_rtcp_->SetRTXSendStatus(rtx_settings, true, SSRC);
801     }
802     return rtp_rtcp_->SetSSRC(SSRC);
803   }
804   CriticalSectionScoped cs(rtp_rtcp_cs_.get());
805   if (simulcast_idx > simulcast_rtp_rtcp_.size()) {
806       return -1;
807   }
808   std::list<RtpRtcp*>::const_iterator it = simulcast_rtp_rtcp_.begin();
809   for (int i = 1; i < simulcast_idx; ++i, ++it) {
810     if (it ==  simulcast_rtp_rtcp_.end()) {
811       return -1;
812     }
813   }
814   RtpRtcp* rtp_rtcp_module = *it;
815   if (usage == kViEStreamTypeRtx) {
816     return rtp_rtcp_module->SetRTXSendStatus(rtx_settings, true, SSRC);
817   }
818   return rtp_rtcp_module->SetSSRC(SSRC);
819 }
820
821 int32_t ViEChannel::SetRemoteSSRCType(const StreamType usage,
822                                       const uint32_t SSRC) {
823   vie_receiver_.SetRtxStatus(true, SSRC);
824   return 0;
825 }
826
827 int32_t ViEChannel::GetLocalSSRC(uint8_t idx, unsigned int* ssrc) {
828   if (idx == 0) {
829     *ssrc = rtp_rtcp_->SSRC();
830     return 0;
831   }
832   CriticalSectionScoped cs(rtp_rtcp_cs_.get());
833   if (idx > simulcast_rtp_rtcp_.size()) {
834     return -1;
835   }
836   std::list<RtpRtcp*>::const_iterator it = simulcast_rtp_rtcp_.begin();
837   for (int i = 1; i < idx; ++i, ++it) {
838     if (it ==  simulcast_rtp_rtcp_.end()) {
839       return -1;
840     }
841   }
842   *ssrc = (*it)->SSRC();
843   return 0;
844 }
845
846 int32_t ViEChannel::GetRemoteSSRC(uint32_t* ssrc) {
847   *ssrc = vie_receiver_.GetRemoteSsrc();
848   return 0;
849 }
850
851 int32_t ViEChannel::GetRemoteCSRC(uint32_t CSRCs[kRtpCsrcSize]) {
852   uint32_t arrayCSRC[kRtpCsrcSize];
853   memset(arrayCSRC, 0, sizeof(arrayCSRC));
854
855   int num_csrcs = vie_receiver_.GetCsrcs(arrayCSRC);
856   if (num_csrcs > 0) {
857     memcpy(CSRCs, arrayCSRC, num_csrcs * sizeof(uint32_t));
858   }
859   return 0;
860 }
861
862 int ViEChannel::SetRtxSendPayloadType(int payload_type) {
863   if (rtp_rtcp_->Sending()) {
864     return -1;
865   }
866   rtp_rtcp_->SetRtxSendPayloadType(payload_type);
867   CriticalSectionScoped cs(rtp_rtcp_cs_.get());
868   for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
869        it != simulcast_rtp_rtcp_.end(); it++) {
870     (*it)->SetRtxSendPayloadType(payload_type);
871   }
872   return 0;
873 }
874
875 void ViEChannel::SetRtxReceivePayloadType(int payload_type) {
876   vie_receiver_.SetRtxPayloadType(payload_type);
877 }
878
879 int32_t ViEChannel::SetStartSequenceNumber(uint16_t sequence_number) {
880   if (rtp_rtcp_->Sending()) {
881     return -1;
882   }
883   return rtp_rtcp_->SetSequenceNumber(sequence_number);
884 }
885
886 int32_t ViEChannel::SetRTCPCName(const char rtcp_cname[]) {
887   if (rtp_rtcp_->Sending()) {
888     return -1;
889   }
890   return rtp_rtcp_->SetCNAME(rtcp_cname);
891 }
892
893 int32_t ViEChannel::GetRTCPCName(char rtcp_cname[]) {
894   return rtp_rtcp_->CNAME(rtcp_cname);
895 }
896
897 int32_t ViEChannel::GetRemoteRTCPCName(char rtcp_cname[]) {
898   uint32_t remoteSSRC = vie_receiver_.GetRemoteSsrc();
899   return rtp_rtcp_->RemoteCNAME(remoteSSRC, rtcp_cname);
900 }
901
902 int32_t ViEChannel::RegisterRtpObserver(ViERTPObserver* observer) {
903   CriticalSectionScoped cs(callback_cs_.get());
904   if (observer) {
905     if (rtp_observer_) {
906       LOG_F(LS_ERROR) << "Observer already registered.";
907       return -1;
908     }
909     rtp_observer_ = observer;
910   } else {
911     rtp_observer_ = NULL;
912   }
913   return 0;
914 }
915
916 int32_t ViEChannel::RegisterRtcpObserver(ViERTCPObserver* observer) {
917   CriticalSectionScoped cs(callback_cs_.get());
918   if (observer) {
919     if (rtcp_observer_) {
920       LOG_F(LS_ERROR) << "Observer already registered.";
921       return -1;
922     }
923     rtcp_observer_ = observer;
924   } else {
925     rtcp_observer_ = NULL;
926   }
927   return 0;
928 }
929
930 int32_t ViEChannel::SendApplicationDefinedRTCPPacket(
931     const uint8_t sub_type,
932     uint32_t name,
933     const uint8_t* data,
934     uint16_t data_length_in_bytes) {
935   if (!rtp_rtcp_->Sending()) {
936     return -1;
937   }
938   if (!data) {
939     LOG_F(LS_ERROR) << "Invalid input.";
940     return -1;
941   }
942   if (data_length_in_bytes % 4 != 0) {
943     LOG(LS_ERROR) << "Invalid input length.";
944     return -1;
945   }
946   RTCPMethod rtcp_method = rtp_rtcp_->RTCP();
947   if (rtcp_method == kRtcpOff) {
948     LOG_F(LS_ERROR) << "RTCP not enable.";
949     return -1;
950   }
951   // Create and send packet.
952   if (rtp_rtcp_->SetRTCPApplicationSpecificData(sub_type, name, data,
953                                                data_length_in_bytes) != 0) {
954     return -1;
955   }
956   return 0;
957 }
958
959 int32_t ViEChannel::GetSendRtcpStatistics(uint16_t* fraction_lost,
960                                           uint32_t* cumulative_lost,
961                                           uint32_t* extended_max,
962                                           uint32_t* jitter_samples,
963                                           int32_t* rtt_ms) {
964   // TODO(pwestin) how do we do this for simulcast ? average for all
965   // except cumulative_lost that is the sum ?
966   // CriticalSectionScoped cs(rtp_rtcp_cs_.get());
967
968   // for (std::list<RtpRtcp*>::const_iterator it = simulcast_rtp_rtcp_.begin();
969   //      it != simulcast_rtp_rtcp_.end();
970   //      it++) {
971   //   RtpRtcp* rtp_rtcp = *it;
972   // }
973   uint32_t remote_ssrc = vie_receiver_.GetRemoteSsrc();
974
975   // Get all RTCP receiver report blocks that have been received on this
976   // channel. If we receive RTP packets from a remote source we know the
977   // remote SSRC and use the report block from him.
978   // Otherwise use the first report block.
979   std::vector<RTCPReportBlock> remote_stats;
980   if (rtp_rtcp_->RemoteRTCPStat(&remote_stats) != 0 || remote_stats.empty()) {
981     return -1;
982   }
983   std::vector<RTCPReportBlock>::const_iterator statistics =
984       remote_stats.begin();
985   for (; statistics != remote_stats.end(); ++statistics) {
986     if (statistics->remoteSSRC == remote_ssrc)
987       break;
988   }
989
990   if (statistics == remote_stats.end()) {
991     // If we have not received any RTCP packets from this SSRC it probably means
992     // we have not received any RTP packets.
993     // Use the first received report block instead.
994     statistics = remote_stats.begin();
995     remote_ssrc = statistics->remoteSSRC;
996   }
997
998   *fraction_lost = statistics->fractionLost;
999   *cumulative_lost = statistics->cumulativeLost;
1000   *extended_max = statistics->extendedHighSeqNum;
1001   *jitter_samples = statistics->jitter;
1002
1003   uint16_t dummy;
1004   uint16_t rtt = 0;
1005   if (rtp_rtcp_->RTT(remote_ssrc, &rtt, &dummy, &dummy, &dummy) != 0) {
1006     return -1;
1007   }
1008   *rtt_ms = rtt;
1009   return 0;
1010 }
1011
1012 void ViEChannel::RegisterSendChannelRtcpStatisticsCallback(
1013     RtcpStatisticsCallback* callback) {
1014   rtp_rtcp_->RegisterSendChannelRtcpStatisticsCallback(callback);
1015   CriticalSectionScoped cs(rtp_rtcp_cs_.get());
1016   for (std::list<RtpRtcp*>::const_iterator it = simulcast_rtp_rtcp_.begin();
1017        it != simulcast_rtp_rtcp_.end();
1018        ++it) {
1019     (*it)->RegisterSendChannelRtcpStatisticsCallback(callback);
1020   }
1021 }
1022
1023 // TODO(holmer): This is a bad function name as it implies that it returns the
1024 // received RTCP, while it actually returns the statistics which will be sent
1025 // in the RTCP.
1026 int32_t ViEChannel::GetReceivedRtcpStatistics(uint16_t* fraction_lost,
1027                                               uint32_t* cumulative_lost,
1028                                               uint32_t* extended_max,
1029                                               uint32_t* jitter_samples,
1030                                               int32_t* rtt_ms) {
1031   uint32_t remote_ssrc = vie_receiver_.GetRemoteSsrc();
1032   StreamStatistician* statistician =
1033       vie_receiver_.GetReceiveStatistics()->GetStatistician(remote_ssrc);
1034   RtcpStatistics receive_stats;
1035   if (!statistician || !statistician->GetStatistics(
1036       &receive_stats, rtp_rtcp_->RTCP() == kRtcpOff)) {
1037     return -1;
1038   }
1039   *fraction_lost = receive_stats.fraction_lost;
1040   *cumulative_lost = receive_stats.cumulative_lost;
1041   *extended_max = receive_stats.extended_max_sequence_number;
1042   *jitter_samples = receive_stats.jitter;
1043
1044   uint16_t dummy = 0;
1045   uint16_t rtt = 0;
1046   rtp_rtcp_->RTT(remote_ssrc, &rtt, &dummy, &dummy, &dummy);
1047   *rtt_ms = rtt;
1048   return 0;
1049 }
1050
1051 void ViEChannel::RegisterReceiveChannelRtcpStatisticsCallback(
1052     RtcpStatisticsCallback* callback) {
1053   vie_receiver_.GetReceiveStatistics()->RegisterRtcpStatisticsCallback(
1054       callback);
1055 }
1056
1057 int32_t ViEChannel::GetRtpStatistics(uint32_t* bytes_sent,
1058                                      uint32_t* packets_sent,
1059                                      uint32_t* bytes_received,
1060                                      uint32_t* packets_received) const {
1061   StreamStatistician* statistician = vie_receiver_.GetReceiveStatistics()->
1062       GetStatistician(vie_receiver_.GetRemoteSsrc());
1063   *bytes_received = 0;
1064   *packets_received = 0;
1065   if (statistician)
1066     statistician->GetDataCounters(bytes_received, packets_received);
1067   if (rtp_rtcp_->DataCountersRTP(bytes_sent, packets_sent) != 0) {
1068     return -1;
1069   }
1070   CriticalSectionScoped cs(rtp_rtcp_cs_.get());
1071   for (std::list<RtpRtcp*>::const_iterator it = simulcast_rtp_rtcp_.begin();
1072        it != simulcast_rtp_rtcp_.end();
1073        it++) {
1074     uint32_t bytes_sent_temp = 0;
1075     uint32_t packets_sent_temp = 0;
1076     RtpRtcp* rtp_rtcp = *it;
1077     rtp_rtcp->DataCountersRTP(&bytes_sent_temp, &packets_sent_temp);
1078     *bytes_sent += bytes_sent_temp;
1079     *packets_sent += packets_sent_temp;
1080   }
1081   for (std::list<RtpRtcp*>::const_iterator it = removed_rtp_rtcp_.begin();
1082        it != removed_rtp_rtcp_.end(); ++it) {
1083     uint32_t bytes_sent_temp = 0;
1084     uint32_t packets_sent_temp = 0;
1085     RtpRtcp* rtp_rtcp = *it;
1086     rtp_rtcp->DataCountersRTP(&bytes_sent_temp, &packets_sent_temp);
1087     *bytes_sent += bytes_sent_temp;
1088     *packets_sent += packets_sent_temp;
1089   }
1090   return 0;
1091 }
1092
1093 void ViEChannel::RegisterSendChannelRtpStatisticsCallback(
1094       StreamDataCountersCallback* callback) {
1095   rtp_rtcp_->RegisterSendChannelRtpStatisticsCallback(callback);
1096   {
1097     CriticalSectionScoped cs(rtp_rtcp_cs_.get());
1098     for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
1099          it != simulcast_rtp_rtcp_.end();
1100          it++) {
1101       (*it)->RegisterSendChannelRtpStatisticsCallback(callback);
1102     }
1103   }
1104 }
1105
1106 void ViEChannel::RegisterReceiveChannelRtpStatisticsCallback(
1107     StreamDataCountersCallback* callback) {
1108   vie_receiver_.GetReceiveStatistics()->RegisterRtpStatisticsCallback(callback);
1109 }
1110
1111 void ViEChannel::GetRtcpPacketTypeCounters(
1112     RtcpPacketTypeCounter* packets_sent,
1113     RtcpPacketTypeCounter* packets_received) const {
1114   rtp_rtcp_->GetRtcpPacketTypeCounters(packets_sent, packets_received);
1115
1116   CriticalSectionScoped cs(rtp_rtcp_cs_.get());
1117   for (std::list<RtpRtcp*>::const_iterator it = simulcast_rtp_rtcp_.begin();
1118        it != simulcast_rtp_rtcp_.end(); ++it) {
1119     RtcpPacketTypeCounter sent;
1120     RtcpPacketTypeCounter received;
1121     (*it)->GetRtcpPacketTypeCounters(&sent, &received);
1122     packets_sent->Add(sent);
1123     packets_received->Add(received);
1124   }
1125   for (std::list<RtpRtcp*>::const_iterator it = removed_rtp_rtcp_.begin();
1126        it != removed_rtp_rtcp_.end(); ++it) {
1127     RtcpPacketTypeCounter sent;
1128     RtcpPacketTypeCounter received;
1129     (*it)->GetRtcpPacketTypeCounters(&sent, &received);
1130     packets_sent->Add(sent);
1131     packets_received->Add(received);
1132   }
1133 }
1134
1135 void ViEChannel::GetBandwidthUsage(uint32_t* total_bitrate_sent,
1136                                    uint32_t* video_bitrate_sent,
1137                                    uint32_t* fec_bitrate_sent,
1138                                    uint32_t* nackBitrateSent) const {
1139   rtp_rtcp_->BitrateSent(total_bitrate_sent, video_bitrate_sent,
1140                          fec_bitrate_sent, nackBitrateSent);
1141   CriticalSectionScoped cs(rtp_rtcp_cs_.get());
1142   for (std::list<RtpRtcp*>::const_iterator it = simulcast_rtp_rtcp_.begin();
1143        it != simulcast_rtp_rtcp_.end(); it++) {
1144     uint32_t stream_rate = 0;
1145     uint32_t video_rate = 0;
1146     uint32_t fec_rate = 0;
1147     uint32_t nackRate = 0;
1148     RtpRtcp* rtp_rtcp = *it;
1149     rtp_rtcp->BitrateSent(&stream_rate, &video_rate, &fec_rate, &nackRate);
1150     *total_bitrate_sent += stream_rate;
1151     *video_bitrate_sent += video_rate;
1152     *fec_bitrate_sent += fec_rate;
1153     *nackBitrateSent += nackRate;
1154   }
1155 }
1156
1157 bool ViEChannel::GetSendSideDelay(int* avg_send_delay,
1158                                   int* max_send_delay) const {
1159   *avg_send_delay = 0;
1160   *max_send_delay = 0;
1161   bool valid_estimate = false;
1162   int num_send_delays = 0;
1163   if (rtp_rtcp_->GetSendSideDelay(avg_send_delay, max_send_delay)) {
1164     ++num_send_delays;
1165     valid_estimate = true;
1166   }
1167   CriticalSectionScoped cs(rtp_rtcp_cs_.get());
1168   for (std::list<RtpRtcp*>::const_iterator it = simulcast_rtp_rtcp_.begin();
1169        it != simulcast_rtp_rtcp_.end(); it++) {
1170     RtpRtcp* rtp_rtcp = *it;
1171     int sub_stream_avg_delay = 0;
1172     int sub_stream_max_delay = 0;
1173     if (rtp_rtcp->GetSendSideDelay(&sub_stream_avg_delay,
1174                                    &sub_stream_max_delay)) {
1175       *avg_send_delay += sub_stream_avg_delay;
1176       *max_send_delay = std::max(*max_send_delay, sub_stream_max_delay);
1177       ++num_send_delays;
1178     }
1179   }
1180   if (num_send_delays > 0) {
1181     valid_estimate = true;
1182     *avg_send_delay = *avg_send_delay / num_send_delays;
1183     *avg_send_delay = (*avg_send_delay + num_send_delays / 2) / num_send_delays;
1184   }
1185   return valid_estimate;
1186 }
1187
1188 void ViEChannel::RegisterSendBitrateObserver(
1189     BitrateStatisticsObserver* observer) {
1190   rtp_rtcp_->RegisterVideoBitrateObserver(observer);
1191   CriticalSectionScoped cs(rtp_rtcp_cs_.get());
1192   for (std::list<RtpRtcp*>::const_iterator it = simulcast_rtp_rtcp_.begin();
1193        it != simulcast_rtp_rtcp_.end();
1194        it++) {
1195     (*it)->RegisterVideoBitrateObserver(observer);
1196   }
1197 }
1198
1199 void ViEChannel::GetReceiveBandwidthEstimatorStats(
1200     ReceiveBandwidthEstimatorStats* output) const {
1201   vie_receiver_.GetReceiveBandwidthEstimatorStats(output);
1202 }
1203
1204 int32_t ViEChannel::StartRTPDump(const char file_nameUTF8[1024],
1205                                  RTPDirections direction) {
1206   if (direction == kRtpIncoming) {
1207     return vie_receiver_.StartRTPDump(file_nameUTF8);
1208   } else {
1209     return vie_sender_.StartRTPDump(file_nameUTF8);
1210   }
1211 }
1212
1213 int32_t ViEChannel::StopRTPDump(RTPDirections direction) {
1214   if (direction == kRtpIncoming) {
1215     return vie_receiver_.StopRTPDump();
1216   } else {
1217     return vie_sender_.StopRTPDump();
1218   }
1219 }
1220
1221 int32_t ViEChannel::StartSend() {
1222   CriticalSectionScoped cs(callback_cs_.get());
1223   if (!external_transport_) {
1224     LOG(LS_ERROR) << "No transport set.";
1225     return -1;
1226   }
1227   rtp_rtcp_->SetSendingMediaStatus(true);
1228
1229   if (rtp_rtcp_->Sending()) {
1230     return kViEBaseAlreadySending;
1231   }
1232   if (rtp_rtcp_->SetSendingStatus(true) != 0) {
1233     return -1;
1234   }
1235   CriticalSectionScoped cs_rtp(rtp_rtcp_cs_.get());
1236   for (std::list<RtpRtcp*>::const_iterator it = simulcast_rtp_rtcp_.begin();
1237        it != simulcast_rtp_rtcp_.end();
1238        it++) {
1239     RtpRtcp* rtp_rtcp = *it;
1240     rtp_rtcp->SetSendingMediaStatus(true);
1241     rtp_rtcp->SetSendingStatus(true);
1242   }
1243   return 0;
1244 }
1245
1246 int32_t ViEChannel::StopSend() {
1247   CriticalSectionScoped cs(rtp_rtcp_cs_.get());
1248   rtp_rtcp_->SetSendingMediaStatus(false);
1249   for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
1250        it != simulcast_rtp_rtcp_.end();
1251        it++) {
1252     RtpRtcp* rtp_rtcp = *it;
1253     rtp_rtcp->SetSendingMediaStatus(false);
1254   }
1255   if (!rtp_rtcp_->Sending()) {
1256     return kViEBaseNotSending;
1257   }
1258
1259   // Reset.
1260   rtp_rtcp_->ResetSendDataCountersRTP();
1261   if (rtp_rtcp_->SetSendingStatus(false) != 0) {
1262     return -1;
1263   }
1264   for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
1265        it != simulcast_rtp_rtcp_.end();
1266        it++) {
1267     RtpRtcp* rtp_rtcp = *it;
1268     rtp_rtcp->ResetSendDataCountersRTP();
1269     rtp_rtcp->SetSendingStatus(false);
1270   }
1271   return 0;
1272 }
1273
1274 bool ViEChannel::Sending() {
1275   return rtp_rtcp_->Sending();
1276 }
1277
1278 int32_t ViEChannel::StartReceive() {
1279   CriticalSectionScoped cs(callback_cs_.get());
1280   if (StartDecodeThread() != 0) {
1281     vie_receiver_.StopReceive();
1282     return -1;
1283   }
1284   vie_receiver_.StartReceive();
1285   return 0;
1286 }
1287
1288 int32_t ViEChannel::StopReceive() {
1289   vie_receiver_.StopReceive();
1290   StopDecodeThread();
1291   vcm_.ResetDecoder();
1292   return 0;
1293 }
1294
1295 int32_t ViEChannel::RegisterSendTransport(Transport* transport) {
1296   if (rtp_rtcp_->Sending()) {
1297     return -1;
1298   }
1299
1300   CriticalSectionScoped cs(callback_cs_.get());
1301   if (external_transport_) {
1302     LOG_F(LS_ERROR) << "Transport already registered.";
1303     return -1;
1304   }
1305   external_transport_ = transport;
1306   vie_sender_.RegisterSendTransport(transport);
1307   return 0;
1308 }
1309
1310 int32_t ViEChannel::DeregisterSendTransport() {
1311   CriticalSectionScoped cs(callback_cs_.get());
1312   if (!external_transport_) {
1313     return 0;
1314   }
1315   if (rtp_rtcp_->Sending()) {
1316     LOG_F(LS_ERROR) << "Can't deregister transport when sending.";
1317     return -1;
1318   }
1319   external_transport_ = NULL;
1320   vie_sender_.DeregisterSendTransport();
1321   return 0;
1322 }
1323
1324 int32_t ViEChannel::ReceivedRTPPacket(
1325     const void* rtp_packet, const int32_t rtp_packet_length,
1326     const PacketTime& packet_time) {
1327   {
1328     CriticalSectionScoped cs(callback_cs_.get());
1329     if (!external_transport_) {
1330       return -1;
1331     }
1332   }
1333   return vie_receiver_.ReceivedRTPPacket(
1334       rtp_packet, rtp_packet_length, packet_time);
1335 }
1336
1337 int32_t ViEChannel::ReceivedRTCPPacket(
1338   const void* rtcp_packet, const int32_t rtcp_packet_length) {
1339   {
1340     CriticalSectionScoped cs(callback_cs_.get());
1341     if (!external_transport_) {
1342       return -1;
1343     }
1344   }
1345   return vie_receiver_.ReceivedRTCPPacket(rtcp_packet, rtcp_packet_length);
1346 }
1347
1348 int32_t ViEChannel::SetMTU(uint16_t mtu) {
1349   if (rtp_rtcp_->SetMaxTransferUnit(mtu) != 0) {
1350     return -1;
1351   }
1352   CriticalSectionScoped cs(rtp_rtcp_cs_.get());
1353   for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
1354        it != simulcast_rtp_rtcp_.end();
1355        it++) {
1356     RtpRtcp* rtp_rtcp = *it;
1357     rtp_rtcp->SetMaxTransferUnit(mtu);
1358   }
1359   mtu_ = mtu;
1360   return 0;
1361 }
1362
1363 uint16_t ViEChannel::MaxDataPayloadLength() const {
1364   return rtp_rtcp_->MaxDataPayloadLength();
1365 }
1366
1367 int32_t ViEChannel::EnableColorEnhancement(bool enable) {
1368   CriticalSectionScoped cs(callback_cs_.get());
1369   color_enhancement_ = enable;
1370   return 0;
1371 }
1372
1373 RtpRtcp* ViEChannel::rtp_rtcp() {
1374   return rtp_rtcp_.get();
1375 }
1376
1377 CallStatsObserver* ViEChannel::GetStatsObserver() {
1378   return stats_observer_.get();
1379 }
1380
1381 // Do not acquire the lock of |vcm_| in this function. Decode callback won't
1382 // necessarily be called from the decoding thread. The decoding thread may have
1383 // held the lock when calling VideoDecoder::Decode, Reset, or Release. Acquiring
1384 // the same lock in the path of decode callback can deadlock.
1385 int32_t ViEChannel::FrameToRender(
1386     I420VideoFrame& video_frame) {  // NOLINT
1387   CriticalSectionScoped cs(callback_cs_.get());
1388
1389   if (decoder_reset_) {
1390     // Trigger a callback to the user if the incoming codec has changed.
1391     if (codec_observer_) {
1392       // The codec set by RegisterReceiveCodec might not be the size we're
1393       // actually decoding.
1394       receive_codec_.width = static_cast<uint16_t>(video_frame.width());
1395       receive_codec_.height = static_cast<uint16_t>(video_frame.height());
1396       codec_observer_->IncomingCodecChanged(channel_id_, receive_codec_);
1397     }
1398     decoder_reset_ = false;
1399   }
1400   // Post processing is not supported if the frame is backed by a texture.
1401   if (video_frame.native_handle() == NULL) {
1402     if (pre_render_callback_ != NULL)
1403       pre_render_callback_->FrameCallback(&video_frame);
1404     if (effect_filter_) {
1405       unsigned int length = CalcBufferSize(kI420,
1406                                            video_frame.width(),
1407                                            video_frame.height());
1408       scoped_ptr<uint8_t[]> video_buffer(new uint8_t[length]);
1409       ExtractBuffer(video_frame, length, video_buffer.get());
1410       effect_filter_->Transform(length,
1411                                 video_buffer.get(),
1412                                 video_frame.ntp_time_ms(),
1413                                 video_frame.timestamp(),
1414                                 video_frame.width(),
1415                                 video_frame.height());
1416     }
1417     if (color_enhancement_) {
1418       VideoProcessingModule::ColorEnhancement(&video_frame);
1419     }
1420   }
1421
1422   uint32_t arr_ofCSRC[kRtpCsrcSize];
1423   int32_t no_of_csrcs = vie_receiver_.GetCsrcs(arr_ofCSRC);
1424   if (no_of_csrcs <= 0) {
1425     arr_ofCSRC[0] = vie_receiver_.GetRemoteSsrc();
1426     no_of_csrcs = 1;
1427   }
1428   DeliverFrame(&video_frame, no_of_csrcs, arr_ofCSRC);
1429   return 0;
1430 }
1431
1432 int32_t ViEChannel::ReceivedDecodedReferenceFrame(
1433   const uint64_t picture_id) {
1434   return rtp_rtcp_->SendRTCPReferencePictureSelection(picture_id);
1435 }
1436
1437 void ViEChannel::IncomingCodecChanged(const VideoCodec& codec) {
1438   CriticalSectionScoped cs(callback_cs_.get());
1439   receive_codec_ = codec;
1440 }
1441
1442 int32_t ViEChannel::OnReceiveStatisticsUpdate(const uint32_t bit_rate,
1443                                               const uint32_t frame_rate) {
1444   CriticalSectionScoped cs(callback_cs_.get());
1445   if (codec_observer_) {
1446     codec_observer_->IncomingRate(channel_id_, frame_rate, bit_rate);
1447   }
1448   return 0;
1449 }
1450
1451 void ViEChannel::OnDecoderTiming(int decode_ms,
1452                                  int max_decode_ms,
1453                                  int current_delay_ms,
1454                                  int target_delay_ms,
1455                                  int jitter_buffer_ms,
1456                                  int min_playout_delay_ms,
1457                                  int render_delay_ms) {
1458   CriticalSectionScoped cs(callback_cs_.get());
1459   if (!codec_observer_)
1460     return;
1461   codec_observer_->DecoderTiming(decode_ms,
1462                                  max_decode_ms,
1463                                  current_delay_ms,
1464                                  target_delay_ms,
1465                                  jitter_buffer_ms,
1466                                  min_playout_delay_ms,
1467                                  render_delay_ms);
1468 }
1469
1470 int32_t ViEChannel::RequestKeyFrame() {
1471   {
1472     CriticalSectionScoped cs(callback_cs_.get());
1473     if (codec_observer_ && do_key_frame_callbackRequest_) {
1474       codec_observer_->RequestNewKeyFrame(channel_id_);
1475     }
1476   }
1477   return rtp_rtcp_->RequestKeyFrame();
1478 }
1479
1480 int32_t ViEChannel::SliceLossIndicationRequest(
1481   const uint64_t picture_id) {
1482   return rtp_rtcp_->SendRTCPSliceLossIndication((uint8_t) picture_id);
1483 }
1484
1485 int32_t ViEChannel::ResendPackets(const uint16_t* sequence_numbers,
1486                                         uint16_t length) {
1487   return rtp_rtcp_->SendNACK(sequence_numbers, length);
1488 }
1489
1490 bool ViEChannel::ChannelDecodeThreadFunction(void* obj) {
1491   return static_cast<ViEChannel*>(obj)->ChannelDecodeProcess();
1492 }
1493
1494 bool ViEChannel::ChannelDecodeProcess() {
1495   vcm_.Decode(kMaxDecodeWaitTimeMs);
1496   return true;
1497 }
1498
1499 void ViEChannel::OnRttUpdate(uint32_t rtt) {
1500   vcm_.SetReceiveChannelParameters(rtt);
1501 }
1502
1503 int32_t ViEChannel::StartDecodeThread() {
1504   // Start the decode thread
1505   if (decode_thread_) {
1506     // Already started.
1507     return 0;
1508   }
1509   decode_thread_ = ThreadWrapper::CreateThread(ChannelDecodeThreadFunction,
1510                                                    this, kHighestPriority,
1511                                                    "DecodingThread");
1512   if (!decode_thread_) {
1513     return -1;
1514   }
1515
1516   unsigned int thread_id;
1517   if (decode_thread_->Start(thread_id) == false) {
1518     delete decode_thread_;
1519     decode_thread_ = NULL;
1520     LOG(LS_ERROR) << "Could not start decode thread.";
1521     return -1;
1522   }
1523   return 0;
1524 }
1525
1526 int32_t ViEChannel::StopDecodeThread() {
1527   if (!decode_thread_) {
1528     return 0;
1529   }
1530
1531   decode_thread_->SetNotAlive();
1532   if (decode_thread_->Stop()) {
1533     delete decode_thread_;
1534   } else {
1535     assert(false && "could not stop decode thread");
1536   }
1537   decode_thread_ = NULL;
1538   return 0;
1539 }
1540
1541 int32_t ViEChannel::SetVoiceChannel(int32_t ve_channel_id,
1542                                           VoEVideoSync* ve_sync_interface) {
1543   if (ve_sync_interface) {
1544     // Register lip sync
1545     module_process_thread_.RegisterModule(&vie_sync_);
1546   } else {
1547     module_process_thread_.DeRegisterModule(&vie_sync_);
1548   }
1549   return vie_sync_.ConfigureSync(ve_channel_id,
1550                                  ve_sync_interface,
1551                                  rtp_rtcp_.get(),
1552                                  vie_receiver_.GetRtpReceiver());
1553 }
1554
1555 int32_t ViEChannel::VoiceChannel() {
1556   return vie_sync_.VoiceChannel();
1557 }
1558
1559 int32_t ViEChannel::RegisterEffectFilter(ViEEffectFilter* effect_filter) {
1560   CriticalSectionScoped cs(callback_cs_.get());
1561   if (effect_filter && effect_filter_) {
1562     LOG(LS_ERROR) << "Effect filter already registered.";
1563     return -1;
1564   }
1565   effect_filter_ = effect_filter;
1566   return 0;
1567 }
1568
1569 void ViEChannel::RegisterPreRenderCallback(
1570     I420FrameCallback* pre_render_callback) {
1571   CriticalSectionScoped cs(callback_cs_.get());
1572   pre_render_callback_ = pre_render_callback;
1573 }
1574
1575 void ViEChannel::RegisterPreDecodeImageCallback(
1576     EncodedImageCallback* pre_decode_callback) {
1577   CriticalSectionScoped cs(callback_cs_.get());
1578   vcm_.RegisterPreDecodeImageCallback(pre_decode_callback);
1579 }
1580
1581 void ViEChannel::OnApplicationDataReceived(const int32_t id,
1582                                            const uint8_t sub_type,
1583                                            const uint32_t name,
1584                                            const uint16_t length,
1585                                            const uint8_t* data) {
1586   if (channel_id_ != ChannelId(id)) {
1587     return;
1588   }
1589   CriticalSectionScoped cs(callback_cs_.get());
1590   {
1591     if (rtcp_observer_) {
1592       rtcp_observer_->OnApplicationDataReceived(
1593           channel_id_, sub_type, name, reinterpret_cast<const char*>(data),
1594           length);
1595     }
1596   }
1597 }
1598
1599 int32_t ViEChannel::OnInitializeDecoder(
1600     const int32_t id,
1601     const int8_t payload_type,
1602     const char payload_name[RTP_PAYLOAD_NAME_SIZE],
1603     const int frequency,
1604     const uint8_t channels,
1605     const uint32_t rate) {
1606   LOG(LS_INFO) << "OnInitializeDecoder " << payload_type << " "
1607                << payload_name;
1608   vcm_.ResetDecoder();
1609
1610   CriticalSectionScoped cs(callback_cs_.get());
1611   decoder_reset_ = true;
1612   return 0;
1613 }
1614
1615 void ViEChannel::OnIncomingSSRCChanged(const int32_t id, const uint32_t ssrc) {
1616   assert(channel_id_ == ChannelId(id));
1617   rtp_rtcp_->SetRemoteSSRC(ssrc);
1618
1619   CriticalSectionScoped cs(callback_cs_.get());
1620   {
1621     if (rtp_observer_) {
1622       rtp_observer_->IncomingSSRCChanged(channel_id_, ssrc);
1623     }
1624   }
1625 }
1626
1627 void ViEChannel::OnIncomingCSRCChanged(const int32_t id,
1628                                        const uint32_t CSRC,
1629                                        const bool added) {
1630   assert(channel_id_ == ChannelId(id));
1631   CriticalSectionScoped cs(callback_cs_.get());
1632   {
1633     if (rtp_observer_) {
1634       rtp_observer_->IncomingCSRCChanged(channel_id_, CSRC, added);
1635     }
1636   }
1637 }
1638
1639 void ViEChannel::ResetStatistics(uint32_t ssrc) {
1640   StreamStatistician* statistician =
1641       vie_receiver_.GetReceiveStatistics()->GetStatistician(ssrc);
1642   if (statistician)
1643     statistician->ResetStatistics();
1644 }
1645
1646 void ViEChannel::RegisterSendFrameCountObserver(
1647     FrameCountObserver* observer) {
1648   rtp_rtcp_->RegisterSendFrameCountObserver(observer);
1649   CriticalSectionScoped cs(rtp_rtcp_cs_.get());
1650   for (std::list<RtpRtcp*>::iterator it = simulcast_rtp_rtcp_.begin();
1651        it != simulcast_rtp_rtcp_.end();
1652        it++) {
1653     (*it)->RegisterSendFrameCountObserver(observer);
1654   }
1655 }
1656
1657 void ViEChannel::ReceivedBWEPacket(int64_t arrival_time_ms,
1658     int payload_size, const RTPHeader& header) {
1659   vie_receiver_.ReceivedBWEPacket(arrival_time_ms, payload_size, header);
1660 }
1661 }  // namespace webrtc